function K = kron(A,B)
%MATLAB Code Generation Library Function

%   Limitations:
%   1) Does not support sparse matrices.

%   Previous versions by Paul Fackler, North Carolina State,
%   and Jordan Rosenthal, Georgia Tech.
%   Copyright 2002-2016 The MathWorks, Inc.
%#codegen

eml_invariant(nargin > 0, ...
    'Coder:toolbox:kron_1', ...
    'IfNotConst','Fail');
eml_invariant(nargin > 1, ...
    'Coder:toolbox:kron_2', ...
    'IfNotConst','Fail');
eml_invariant(coder.internal.isBuiltInNumeric(A) || ischar(A) || islogical(A), ...
    'Coder:toolbox:unsupportedClass','kron',class(A));
eml_invariant(coder.internal.isBuiltInNumeric(B) || ischar(B) || islogical(B), ...
    'Coder:toolbox:unsupportedClass','kron',class(B));
eml_invariant(ismatrix(A) && ismatrix(B), ...
    'MATLAB:kron:TwoDInput');
ma = cast(size(A,1),coder.internal.indexIntClass);
na = cast(size(A,2),coder.internal.indexIntClass);
mb = cast(size(B,1),coder.internal.indexIntClass);
nb = cast(size(B,2),coder.internal.indexIntClass);
mk = coder.internal.indexTimes(ma,mb);
nk = coder.internal.indexTimes(na,nb);
K = coder.nullcopy(eml_expand(coder.internal.scalarEg(A,B),[mk,nk]));
kidx = zeros(coder.internal.indexIntClass);
for j1 = 1:na
    for j2 = 1:nb
        for i1 = 1:ma
            for i2 = 1:mb
                kidx = coder.internal.indexPlus(kidx,1);
                K(kidx) = A(i1,j1)*B(i2,j2);
            end
        end
    end
end        
