Home > tt2 > @tt_tensor > plus.m

plus

PURPOSE ^

A=B+C

SYNOPSIS ^

function [a]=plus(b,c)

DESCRIPTION ^

A=B+C
   [A]=PLUS(B,C)  Adds two TT-tensors B and C


 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 [a]=plus(b,c)
0002 %A=B+C
0003 %   [A]=PLUS(B,C)  Adds two TT-tensors B and C
0004 %
0005 %
0006 % TT-Toolbox 2.2, 2009-2012
0007 %
0008 %This is TT Toolbox, written by Ivan Oseledets et al.
0009 %Institute of Numerical Mathematics, Moscow, Russia
0010 %webpage: http://spring.inm.ras.ru/osel
0011 %
0012 %For all questions, bugs and suggestions please mail
0013 %ivan.oseledets@gmail.com
0014 %---------------------------
0015 
0016 if ( isempty(b) )
0017  a=c;
0018  return
0019 elseif ( isempty(c) )
0020  a=b;
0021  return
0022 end
0023 
0024 if (b.d==1)&&(c.d==1)
0025     a = b;
0026     a.core = b.core + c.core;
0027     return;
0028 end;
0029 
0030 a=tt_tensor;
0031 n1=b.n;
0032 r1=b.r;
0033 r2=c.r;
0034 d=b.d;
0035 cr1=b.core;
0036 cr2=c.core;
0037 %Determine size of the result
0038 r=r1+r2;
0039 if ( r1(1) == r2(1) )
0040     r(1)=r1(1);
0041 else
0042   error('Inconsistent sizes in mode 1');    
0043 end
0044 if ( r1(d+1) == r2(d+1) )
0045   r(d+1)=r1(d+1);
0046 else
0047   error('Inconsistent sizes in the last mode');    
0048 end
0049 n=n1;
0050 a.d=d;
0051 a.n=n;
0052 a.r=r;
0053 sz=dot(n.*r(1:d),r(2:d+1));
0054 pos=(n.*r(1:d)).*r(2:d+1);
0055 pos=cumsum([1;pos]);
0056 a.ps=pos;
0057 cr=zeros(sz,1);
0058 pos1=b.ps; pos2=c.ps;
0059 for i=1:d
0060   pp=zeros(r(i),n(i),r(i+1));
0061   p1=cr1(pos1(i):pos1(i+1)-1);
0062   p2=cr2(pos2(i):pos2(i+1)-1);
0063   p1=reshape(p1,[r1(i),n(i),r1(i+1)]);
0064   p2=reshape(p2,[r2(i),n(i),r2(i+1)]);
0065   pp(1:r1(i),:,1:r1(i+1))=p1;
0066   pp(r(i)-r2(i)+1:r(i),:,r(i+1)-r2(i+1)+1:r(i+1))=p2;
0067   cr(pos(i):pos(i+1)-1)=pp(:);
0068 end
0069 a.core=cr;
0070 return
0071 end

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