Home > tt2 > @tt_tensor > dot.m

dot

PURPOSE ^

Dot product of two TT tensors

SYNOPSIS ^

function [p] = dot(tt1,tt2,do_qr)

DESCRIPTION ^

Dot  product of two TT tensors
   [PR]=DOT(TT1,TT2) -- dot product of two TT-tensors

   [PR]=DOT(TT1,TT2, DO_QR) if DO_QR==true is specified, perform the 
   left-to-right QRs of TT1,TT2
   before the scalar product. It increases the  accuracy in some cases.

 In general, returns a 4D tensor of sizes 
 r0(tt1), r0(tt2), rd(tt1), rd(tt2)
 If r0(tt1) = r0(tt2) = 1 it returns a matrix of size rd(tt1) x rd(tt2)


 TT-Toolbox 2.2, 2009-2012

This is TT Toolbox, written by Ivan Oseledets et al.
Institute of Numerical Mathematics, Moscow, Russia
webpage: http://spring.inm.ras.ru/osel

For all questions, bugs and suggestions please mail
ivan.oseledets@gmail.com
---------------------------

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [p] = dot(tt1,tt2,do_qr)
0002 %Dot  product of two TT tensors
0003 %   [PR]=DOT(TT1,TT2) -- dot product of two TT-tensors
0004 %
0005 %   [PR]=DOT(TT1,TT2, DO_QR) if DO_QR==true is specified, perform the
0006 %   left-to-right QRs of TT1,TT2
0007 %   before the scalar product. It increases the  accuracy in some cases.
0008 %
0009 % In general, returns a 4D tensor of sizes
0010 % r0(tt1), r0(tt2), rd(tt1), rd(tt2)
0011 % If r0(tt1) = r0(tt2) = 1 it returns a matrix of size rd(tt1) x rd(tt2)
0012 %
0013 %
0014 % TT-Toolbox 2.2, 2009-2012
0015 %
0016 %This is TT Toolbox, written by Ivan Oseledets et al.
0017 %Institute of Numerical Mathematics, Moscow, Russia
0018 %webpage: http://spring.inm.ras.ru/osel
0019 %
0020 %For all questions, bugs and suggestions please mail
0021 %ivan.oseledets@gmail.com
0022 %---------------------------
0023 
0024 
0025 if (nargin<3)||(isempty(do_qr))
0026     do_qr = false;
0027 end;
0028 
0029 if (do_qr)
0030     [tt1,rv1]=qr(tt1, 'lr');
0031     [tt2,rv2]=qr(tt2, 'lr');
0032 end;
0033 
0034 d=tt1.d; 
0035 r1=tt1.r; r2=tt2.r; ps1=tt1.ps; ps2=tt2.ps;
0036 n=tt1.n;
0037 core1=tt1.core; core2=tt2.core;
0038 
0039 %ps is always r1(i-1)xr; but if there is a hanging thing? what to do?
0040 %That means, I define a dot product of two "hanging" tensors as a matrix...
0041 %brr.... And if it is hanging on the right?
0042 %
0043 % p=ones(r1(1),r2(1)); % Over r1(1) and r2(1) there is not summation blin.
0044 % %So, if we just sum over n(1) separatedly and leave a strange QxR1(I)xR2(I)
0045 % %matrix...
0046 % for i=1:d
0047 %   cr1=core1(ps1(i):ps1(i+1)-1);
0048 %   cr2=core2(ps2(i):ps2(i+1)-1);
0049 %   p=reshape(p,[r1(i),r2(i)]);
0050 %   cr2=reshape(cr2,[r2(i),numel(cr2)/r2(i)]);
0051 %   p=p*cr2; %p is Q*r1(i)xn(i)xr2(i+1);
0052 %   cr1=reshape(cr1,[r1(i)*n(i),numel(cr1)/(r1(i)*n(i))]);
0053 %   p=reshape(p,[r1(i)*n(i),numel(p)/(r1(i)*n(i))]);
0054 %   p=cr1'*p;
0055 % end
0056 
0057 % If the first indices are not ones
0058 p=eye(r1(1)*r2(1));
0059 p = reshape(p, r1(1)*r2(1)*r1(1), r2(1));
0060 
0061 for i=1:d
0062     cr1=core1(ps1(i):ps1(i+1)-1);
0063     cr2=core2(ps2(i):ps2(i+1)-1);
0064     cr2=reshape(cr2,[r2(i), n(i)*r2(i+1)]);
0065     
0066     p = p*cr2; % size r11*r21*r1-, n*r2+
0067     p = reshape(p,r1(1)*r2(1), r1(i)*n(i), r2(i+1));
0068     p = permute(p, [1, 3, 2]);
0069     p = reshape(p, r1(1)*r2(1)*r2(i+1), r1(i)*n(i));
0070     
0071     cr1=reshape(cr1,[r1(i)*n(i), r1(i+1)]);
0072     
0073     p = p*conj(cr1); % size r11*r12*r2+, r1+
0074     p = reshape(p, r1(1)*r2(1), r2(i+1), r1(i+1));
0075     p = permute(p, [1, 3, 2]);
0076     p = reshape(p, r1(1)*r2(1)*r1(i+1), r2(i+1));
0077 end;
0078 
0079 if (do_qr)
0080     r2old = size(rv2, 2);
0081     r1old = size(rv1,2);
0082     p = p*rv2;
0083     p = reshape(p, r1(1)*r2(1), r1(d+1), r2old);
0084     p = permute(p, [1, 3, 2]);
0085     p = reshape(p, r1(1)*r2(1)*r2old, r1(d+1));
0086     p = p*conj(rv1);
0087     p = reshape(p, r1(1), r2(1), r2old, r1old);
0088     p = permute(p, [1,2,4,3]);
0089 else
0090     p = reshape(p, r1(1), r2(1), r1(d+1), r2(d+1));
0091 end;
0092 if ( r1(1)*r2(1) == 1 ) %Save the rabbits
0093    p=reshape(p,r1(d+1),r2(d+1));
0094 end
0095 end

Generated on Wed 08-Feb-2012 18:20:24 by m2html © 2005