Code covered by the BSD License  

Highlights from
Kronecker product

5.0

5.0 | 2 ratings Rate this file 17 Downloads (last 30 days) File Size: 1.46 KB File ID: #28889

Kronecker product

by Laurent S

 

29 Sep 2010 (Updated 06 Feb 2011)

An efficient implementation of the Kronecker product for dense, sparse and logical matrices.

| Watch this File

File Information
Description

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.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
kronecker

MATLAB release MATLAB 7.11 (2010b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (12)
30 Sep 2010 Christophe

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

30 Sep 2010 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);

30 Sep 2010 Laurent S

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

30 Sep 2010 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);

30 Sep 2010 Laurent S

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

01 Oct 2010 Matt J  
02 Oct 2010 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?

04 Oct 2010 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.

05 Feb 2011 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);
 

06 Feb 2011 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(:);

06 Feb 2011 Laurent S

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

07 Feb 2011 Paulo

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

Please login to add a comment or rating.
Updates
30 Sep 2010

Included support for logical matrices.

30 Sep 2010

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

03 Oct 2010

Improved support for combinations of different data types.

06 Feb 2011

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

Tag Activity for this File
Tag Applied By Date/Time
kronecker Laurent S 29 Sep 2010 13:13:34
kron Laurent S 29 Sep 2010 13:13:34
kronecker omar cherrak 14 May 2012 19:05:26

Contact us at files@mathworks.com