function K=kronSum(A,B)
% kronSum <Find Kronecker tensor sum.>
% Usage:: K=kronSum(A,B}
%

% revision history:
% 05/10/16 Mark D. Shattuck <mds> kronSum.m
% 05/10/16 mds stolen from matlab KRON see below

%KRON   Kronecker tensor product.
%   KRON(X,Y) is the Kronecker tensor product of X and Y.
%   The result is a large matrix formed by taking all possible
%   products between the elements of X and those of Y. For
%   example, if X is 2 by 3, then KRON(X,Y) is
%
%      [ X(1,1)*Y  X(1,2)*Y  X(1,3)*Y
%        X(2,1)*Y  X(2,2)*Y  X(2,3)*Y ]
%
%   If either X or Y is sparse, only nonzero elements are multiplied
%   in the computation, and the result is sparse.
%
%   Class support for inputs X,Y:
%      float: double, single

%   Thanks Paul Fackler and Jordan Rosenthal for previous versions.
%   Copyright 1984-2009 The MathWorks, Inc. 
%   $Revision: 5.17.4.4 $ $Date: 2010/08/23 23:11:32 $

if ~ismatrix(A) || ~ismatrix(B)
    error(message('MATLAB:kron:TwoDInput'));
end

[ma,na] = size(A);
[mb,nb] = size(B);

if ~issparse(A) && ~issparse(B)

   % Both inputs full, result is full.

   [ia,ib] = meshgrid(1:ma,1:mb);
   [ja,jb] = meshgrid(1:na,1:nb);
   K = A(ia,ja)+B(ib,jb);

else

   % At least one input is sparse, result is sparse.
   % have not converted for kronSum

   disp('Not working for sparse yet');

%    [ia,ja,sa] = find(A);
%    [ib,jb,sb] = find(B);
%    ia = ia(:); ja = ja(:); sa = sa(:);
%    ib = ib(:); jb = jb(:); sb = sb(:);
%    ka = ones(size(sa));
%    kb = ones(size(sb));
%    t = mb*(ia-1)';
%    ik = t(kb,:)+ib(:,ka);
%    t = nb*(ja-1)';
%    jk = t(kb,:)+jb(:,ka);
%    K = sparse(ik,jk,sb*sa.',ma*mb,na*nb);

end
