0001 function [c]=kron2(a,b)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 if ( isempty(a) )
0019 c=b;
0020 return
0021 elseif ( isempty(b) )
0022 c=a;
0023 return
0024 end
0025 if ( size(a.core,1) == 1 )
0026 a.core=(a.core).';
0027 end
0028 if ( size(b.core,1) == 1 )
0029 b.core=(b.core).';
0030 end
0031
0032 d = a.d;
0033 if (b.d~=d)
0034 error('Could not compute kron2 from the tensors with not equal dimensions\n');
0035 end;
0036 cra = a.core;
0037 na = a.n;
0038 ra = a.r;
0039 psa = a.ps;
0040 crb = b.core;
0041 nb = b.n;
0042 rb = b.r;
0043 psb = b.ps;
0044
0045 c=tt_tensor;
0046 n = [(na).*(nb)];
0047 r = [(ra).*(rb)];
0048 ps = cumsum([1; n.*r(2:d+1).*r(1:d)]);
0049
0050
0051 c.d=d;
0052 c.n=n;
0053 c.r=r;
0054 c.ps = ps;
0055 cr = zeros(ps(d+1)-1, 1);
0056 for i=1:d
0057 a1 = cra(psa(i):psa(i+1)-1);
0058 b1 = crb(psb(i):psb(i+1)-1);
0059 c1 = a1*(b1.');
0060 c1 = reshape(c1, ra(i), na(i), ra(i+1), rb(i), nb(i), rb(i+1));
0061 c1 = permute(c1, [1, 4, 2, 5, 3, 6]);
0062 cr(ps(i):ps(i+1)-1) = c1(:);
0063 end;
0064
0065 c.core=cr;
0066
0067 end