File Exchange

image thumbnail

Efficient Object-Oriented Kronecker Product Manipulation

version 1.10 (55.3 KB) by

A class for efficient manipulation of N-fold Kronecker products in terms of their operands only.



View License

This is a class for efficiently representing and manipulating N-fold Kronecker products of matrices (or of objects that behave like matrices) in terms of their operands only.
Given matrices {A,B,C,D,...} and a scalar s, an object M of this class can be used to represent
    Matrix = s * A kron B kron C kron D kron ... (Eq. 1)
where "A kron B" denotes kron(A,B), the Kronecker product of A and B. Internally, however, M stores the operands {s,A,B,C,D,...} separately, which is typically far more byte-compact than numerically expanding out the RHS of Eq. 1.
Furthermore, many mathematical manipulations of Kronecker products are more efficient when done in terms of {s,A,B,C,D,...} separately than when done with the explicit numerical form of M as a matrix. The class overloads a number of methods and math operators in a way that exploits the Kronecker product structure accordingly.
Among these methods/operators are: mtimes (*), times (.*) , tranpose (.') , ctranpose (') , rdivide (./), ldivide (.\), mldivide (\), mrdivide (/), inv, pinv, power, mpower, norm, sum, cond, eig, svd, abs, nnz, orth, chol, lu, qr, full, sparse, ...
Some restrictions apply to these overloads. In particular, bi-operand math operations involving two KronProd objects, e.g. M1*M2, typically require the operands of each KronProd to be of compatible sizes. However, I find these restrictions to be satisfied often in applications.

Consult "help KronProd/methodname" for more info on each method. Optionally also, read krontest.m for demonstrations of their use.
 A primary application of this class is to efficiently perform separable tensorial operations, i.e., where a linear transform is applied to all columns of an array, then all rows, and so on.
The following example is of a separable transformation of a 3D array X that transforms all of its columns via multiplication with a non-square matrix A, then transforms all rows by multiplication with B, then finally transforms all 3rd-dimensional axes by multiplication with C. Two approaches to this are compared. The first approach uses kron(). The second uses the KronProd class. Other operations are also shown for illustration purposes.
Notice the orders of magnitude reduction both in CPU time and in memory consumption, using the KronProd object.
      m=25; n=15; p=40;
      mm=16; nn=n; pp=10;
      A=rand(mm,m); B=pi*eye(n); C=rand(pp,p);
      s=4; % a scalar
      %METHOD I: based on kron()
         Matrix = s*kron(C,kron(B,A));
         y1 = Matrix*X(:); %The tensorial transformation
         z1 = Matrix.'*y1(:);
         w1 = Matrix.'\z1;
      %Elapsed time is 78.729007 seconds.
      %METHOD II: based on KronProd object
         Object = KronProd({A,pi,C},[1 2 3],[m,n,p],s); %equivalent to Matrix above
           y2 = Object*X;
                  % This operation could also have been implemented
                  % as y2=reshape( Object*X(:) , [mm,nn,pp]);
           z2 = Object.'*y1;
           w2 = Object.'\z1;
      % Elapsed time is 0.003958 seconds.
      PercentError=@(x,y) norm(x(:)-y(:),2)/norm(x(:),'inf')*100;
          PercentError(y1,y2), % = 3.0393e-012
          PercentError(size(y1),size(y2)), % = 0
          PercentError(z1,z2), % = 1.3017e-012
          PercentError(w1,w2), % = 4.3409e-011
     >> whos Matrix Object
             Name Size Bytes Class Attributes
             Matrix 2400x15000 288000000 double
             Object 2400x15000 8102 KronProd

EXAMPLE #2: As a more practical example, the KronProd class is very useful in conjunction with the following tool for signal interpolation/reconstruction:

An example involving 2D signal reconstruction using cubic B-splines is provided in the file Example2D.m at the above link.

Comments and Ratings (17)

Matt J

Matt J (view profile)

Hi Elad,

Sorry I didn't see this sooner. As you mentioned, full(Object) is the way to obtain the full array form of the Kronecker product. It might also make sense to do sparse(Object), if your input operands are themselves sparse matrices. You are correct, however, that doing so would defeat the purpose and benefits of the object representation. I would point out, however, that KronProd objects are capable of doing other things besides multiplication, see or example,

 >> methods KronProd

So, if you elaborate on what you're trying to do, I might b able to make some recommendations.

Elad Harel

Is it possible to extract the full matrix or is the speedup only occur when multiplying the output? For instance, I can use full(Object), but this really slows down the code. I suppose the speedup arises because this attempts to take advantage of the sparsity of the data and if the entire tensor is extracted those savings are lost, but I want to make sure I'm not doing something wrong. To be explicit, I want the full array kron(C,kron(B,A)) and not just the product of this array with some other matrix (e.g. X in your example).

Matt J

Matt J (view profile)


Presumably you would do something like

   ifft( conv(fft(x),fft(y)))

However, I don't see the computational motivation of doing so. x.*y is surely the fastest way to implement x.*y. I also don't see the connection between this question and KronProd.

Hi Matt

How does it work for this case?

Suppose we have the following matrices:

x = [3 5 4; 7 6 1; -1 2 0];

y = [2 7 1; 2 -3 2; 5 6 9];

How can I get the same results as x.*y using convolution in frequency domain?

Thanks in advance

Matt J

Matt J (view profile)

Well, if all else fails, you can always use pcg(). The KronProd representation will facilitate the matrix multiplication operations.


Manny (view profile)

Thanks for your answer, Matt.

The matrix F with which I am working is the transition matrix of state-space model:

x_{t+1} = F*x_t + e_t,

so I am not sure it is always diagonalizable. The problem with which I am working is indeed a system of equations:

(I-kron(F,F))*vec(P) = vec(Q),

where F has dimension close to 350x350.

Matt J

Matt J (view profile)


There may be options, but it depends on the specifics of F and what exactly you're trying to do with the inverse.

For example, if F is diagonalizable (e.g., symmetric or possessing distinct eigenvalues) and if you are really trying to solve the equations


for x, then I might do the following,



 x = V*((V\y)./d);


Manny (view profile)

I need to obtain inv(I-kron(F,F)), where F is a large matrix. Is there any script in the codes that could allow an efficient treatment of that (as an object)?


Manny (view profile)


WALEEM (view profile)

Thanks for sharing this code, Matt. With the help of your code, I have been able to resolve an estimation issue I have had (for the past 13 months) with a model. In the process of estimating the model, I had to deal with a 4^10-by-4^10 (8796Gb) matrix in a several thousands loop. This almost meant the end of the project.

But I stumbled upon your code, and was able to complete the construction of the model.

I can't thank you enough for this. I will be sharing my code when it is all done, and I will duely reference your code.

Thank you.

Matt J

Matt J (view profile)

Hi Roemer,

Thanks for you feedback. Regarding concatenating KronProd objects: The concatenation operation that you've described isn't possible precisely as you've requested it, because the concatenation of 2 Kronecker products is not, in general, mathematically equivalent to a 3rd Kronecker product. Because it's not possible mathematically, it is equally not possible in software.

However, I did create the following FEX package to allow one to combine simpler operators into more complicated operators, which might be equivalent to what you're asking for

Here's a simplified example where I concatenate kron(eye(3),eye(3)) with itself

>> A=eye(3); B=eye(3); K=KronProd({A,B});
>> Kcat=MatrixObj; Kcat.Ops.mtimes=@(A,x)[K*x;K*x];
>> Kcat*(1:9)'

ans =


The mixture of the MatrixObj class with other classes has had some problems in past MATLAB versions, unfortunately, so how well it works for you could depends on what version you have.

This is a fantastic piece of work. The amount of speed-up and memory efficiency has been a tremendous help already. There are calculations I have to do on often enormous arrays resulting from Kronecker products, easily needing up to 400GB of RAM/memory-mapped space. Using your OOP implementation is of huge help to me and my (PhD) project, and it's a nice way to learn how to do some OOP myself. Thanks!

I have one question/request, I assume you might not have the time, but maybe you could give me some pointers on how to do it. Maybe it's very easy, and I'm just missing it.
I would like to be able to do:

KronProdC = cat(1,KronProdA,KronProdB)

I.e., concatenating KronProd objects along dimension 1 (or 2). Concatenation would occur over 1st/2nd dimension of the kronecker product. Maybe it's not necessary to expand the class, but use a trick to modify existing KronProd objects, but my intuition for this is not very trained.
Being able to do this, I could extend your work into a Kathri-Rao product, which would speed up a large set of problems and algorithms using the PARAFAC/TuckerX models. These multi-dimensional decomposition models are used a lot in chemometrics, and are gaining a lot of popularity in neuroscience the past years (my field). Both usually use Matlab for these calculations, and this would speedup results for a lot of people, many could benefit. So, I got very excited when I saw and tested your work :).
Any help is greatly appreciated,


Wow! No more out of memory errors and so fast!


Omar (view profile)

This is what I need :-)

Matt J

Matt J (view profile)

Oleg, I fixed some of the ragged text justification and uncommented some of the text. Not sure if those were the unreadabilities that you were talking about, though. The Description editor is a little unwieldy and gives me very limited control of spacing.

Oleg Komarov

Oleg Komarov (view profile)

You may consider to edit the description since it's really unreadable. No way I'm gonna get past all those blocks of comments and those funny spaced examples.


Sean (view profile)



In constructor KronProd(opset,opinds, ...) introduced default value for opinds of 1:length(opset).

This allows simple 1-argument contructor calls such as
KronProd({C,B,A}), which is equivalent to kron(A,kron(B,C))


At the suggestion of a user, neatened up the Description section


*Overloaded kron() for the KronProd class.
*Reimplemented the class definition using classdef file.
*loadobj now always does a construct-on-load.
*See UpdateNotes for more details.


*Overloaded cellfun() for the KronProd class

*Added simpler options for the domainsize parameter in the constructor syntax KronProd(opset,opdims,domainsizes,...)

*See UpdateNotes for details


Added additional example to Description section with link to a related FEX submission.


Bug fix in qr() method (see UpdateNotes.txt). Also added relevant test of the fix to krontest.m


*New class methods: times, rdivide ldivide, rank, orth, svd, pinv, qr, lu, chol, loadobj.

*Expansions of old methods: eig, mldivide, mtimes, mrdivide

*See UpdateNotes.txt in package for more


Minor typo fixes in description of package

MATLAB Release
MATLAB 7.9 (R2009b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video