File Exchange

image thumbnail

Kronecker product

version 1.6.0.0 (1.46 KB) by Laurent S
An efficient implementation of the Kronecker product for dense, sparse and logical matrices.

2 Downloads

Updated 06 Feb 2011

View License

This file serves as a replacement for Matlab's kron.m. It contains a more efficient implementation of the Kronecker product for dense, sparse and logical matrices.

Cite As

Laurent S (2019). Kronecker product (https://www.mathworks.com/matlabcentral/fileexchange/28889-kronecker-product), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (13)

This function was not efficient for my run test. According to the profiler the matlab function kron took 34s while this one took 64s. Still looking for a better option than the current matlab function though.

Paulo

Works great now! Thanks a lot for the fix and the update.

Laurent S

@Paulo,Matt J: Thanks for letting me know, I submitted an updated version.

Matt J

These two modified lines of code are needed:

[ia,ja,sa] = find(A); ia=ia(:); ja=ja(:); sa=sa(:);
[ib,jb,sb] = find(B); ib=ib(:); jb=jb(:); sb=sb(:);

Paulo

kron(1:3, speye(3)) does not work (Matlab 2010b 64bit Linux).

Here's the error message:

??? Error using ==> mtimes
Inner matrix dimensions must agree.

Error in ==> kron at 24
X = sparse(ix,jx,double(sb)*double(sa.'),I*K,J*L);

Laurent S

@Matt J: I was under the impression converting sa and sb to doubles would be slower than converting their outer product to double, unfortunately Matlab isn't too good about handling operations between different types of data. I updated the code again.

Matt J

I'm going to stop bothering you, but my recommended solution also handled integer types, whereas, your current one does not.

kron(sparse(rand(6)),uint16(rand(5)*5));
??? Error using ==> mtimes
Integers can only be combined with integers of the same class, or scalar doubles.

Is there a reason you're trying so hard to economize on if-statements and lines of code?

Matt J

Laurent S

@Matt J: I updated the code again to include support for other class types, thanks for catching that.

Matt J

Nice 1-line trick, but it prevents you from having full class support when you're already so close!

Notice that mixed sparse/single ops still don't work

>> kron(sparse(rand(6)),rand(6,'single'));
??? Undefined function or method 'sparse' for input arguments of type 'single'.

This is my revised recommendation

if islogical(sb) && islogical(sa)
X = sparse(ix,jx,bsxfun(@and,sb,sa.'),I*K,J*L);
return;
end

if ~(isdouble(sa)||islogical(sa)); sa=double(sa); end
if ~(isdouble(sb)||islogical(sb)); sb=double(sb); end

X = sparse(ix,jx,sb*sa.',I*K,J*L);

Laurent S

@Matt J: The updated version supports logical matrices (adding only one line of code), thanks for your comment!

Matt J

Very good, but like the original kron, this version cannot handle logical inputs, e.g.

kron(sparse(true(3)),sparse(true(6)));
??? Error using ==> mtimes
Logical inputs must be scalar.

Error in ==> kron at 25
X = sparse(ix,jx,sb*sa.',I*K,J*L);

Consider making the following modification

if islogical(sb) && islogical(sa)
X = sparse(ix,jx,bsxfun(@and,sb,sa.'),I*K,J*L);
return;
elseif islogical(sb)
sb=double(sb);
elseif islogical(sa)
sa=double(sa);
end
X = sparse(ix,jx,sb*sa.',I*K,J*L);

Christophe

As promised, the program does the job faster than the original implementation.
Very good!

Updates

1.6.0.0

Fixed a bug where an error would occur if one of the arguments is a row vector (thanks Paulo and Matt J).

1.5.0.0

Improved support for combinations of different data types.

1.3.0.0

Included support for more class types, such as single precision.

1.2.0.0

Included support for logical matrices.

MATLAB Release Compatibility
Created with R2010b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags
Acknowledgements

Inspired by: kronecker