Home > tt2 > @tt_tensor > subsref.m

subsref

PURPOSE ^

Evaluate elements, cores of TT-formats and fields of the TT-structure

SYNOPSIS ^

function [elem] = subsref(tt,s)

DESCRIPTION ^

 Evaluate elements, cores of TT-formats and fields of the TT-structure
   A=TT{I} computes I-th core of the TT-representation

   ELEM=TT(IND) computes element with index IND of the tensor TT
   

 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 [elem] = subsref(tt,s)
0002 % Evaluate elements, cores of TT-formats and fields of the TT-structure
0003 %   A=TT{I} computes I-th core of the TT-representation
0004 %
0005 %   ELEM=TT(IND) computes element with index IND of the tensor TT
0006 %
0007 %
0008 % TT-Toolbox 2.2, 2009-2012
0009 %
0010 %This is TT Toolbox, written by Ivan Oseledets et al.
0011 %Institute of Numerical Mathematics, Moscow, Russia
0012 %webpage: http://spring.inm.ras.ru/osel
0013 %
0014 %For all questions, bugs and suggestions please mail
0015 %ivan.oseledets@gmail.com
0016 %---------------------------
0017 
0018 switch s(1).type    
0019     case '()'
0020       %Evaluate element of a tensor in position s
0021       d=tt.d; n=tt.n; ps=tt.ps; cr=tt.core; r=tt.r;
0022       %ind=double(s);
0023       pp=s.subs;
0024       mn=numel(pp); 
0025       if ( mn < d && mn > 1 || (mn == 1 && numel(pp{1}) < d) ) 
0026         error('Invalid number of indices specified: given %d, need %d \n',mn,d);
0027       end
0028       if ( mn == 1 ) %Special case
0029          ind=pp{1};
0030          if ( isa(ind,'double') )
0031            for i=1:d
0032              pp{i}=ind(i);
0033            end
0034          else
0035             pp=ind; %Supposedly it was a cell array of index subsets
0036          end
0037       end
0038       elem=tt_tensor;
0039       elem.r=r; 
0040       elem.d=d;
0041       n0=zeros(d,1);
0042       for i=1:d
0043         ind=pp{i};
0044         if ( strcmp(ind, ':'))
0045             pp{i}=1:n(i);
0046             ind=pp{i};
0047         end
0048         n0(i)=numel(ind);
0049       end
0050       ps1=cumsum([1;n0.*r(1:d).*r(2:d+1)]);
0051       cr1=zeros(ps1(d+1)-1,1);
0052       for i=1:d
0053          ind=pp{i}; 
0054          crx=cr(ps(i):ps(i+1)-1);
0055          crx=reshape(crx,[r(i),n(i),r(i+1)]);
0056          crx=crx(:,ind,:);
0057          cr1(ps1(i):ps1(i+1)-1)=crx(:);          
0058       end
0059       if ( prod(n0) == 1 ) %single element
0060          v=1;
0061          for i=1:d
0062             crx=cr1(ps1(i):ps1(i+1)-1); crx=reshape(crx,[r(i),r(i+1)]);
0063             v=v*crx;
0064          end
0065          elem=v;
0066       else
0067          elem.n=n0;
0068          elem.core=cr1;
0069          elem.ps=ps1;
0070          
0071       end
0072     case '.'
0073         switch s(1).subs
0074             case 'r'
0075                 elem = tt.r;
0076                 if (numel(s)>1)
0077                     s = s(2:end);
0078                     elem = subsref(elem, s);
0079                 end;
0080 %                 if (numel(s)>1)&&(strcmp(s(2).type,'()'))
0081 %                     elem = tt.r(s(2).subs{1});
0082 %                 else
0083 %                     elem=tt.r;
0084 %                 end;
0085             case 'core'
0086                 if (numel(s)>1)&&(strcmp(s(2).type,'()'))
0087                     elem = tt.core(s(2).subs{1});
0088                 else
0089                     elem=tt.core;
0090                 end;
0091             case 'd'
0092                 elem=tt.d;
0093             case 'ps'
0094                 elem = tt.ps;
0095                 if (numel(s)>1)
0096                     s = s(2:end);
0097                     elem = subsref(elem, s);
0098                 end;
0099 
0100 %                 if (numel(s)>1)&&(strcmp(s(2).type,'()'))
0101 %                     elem = tt.ps(s(2).subs{1});
0102 %                 else
0103 %                     elem=tt.ps;
0104 %                 end;
0105             case 'n'
0106                 elem = tt.n;
0107                 if (numel(s)>1)
0108                     s = s(2:end);
0109                     elem = subsref(elem, s);
0110                 end;
0111 
0112 %                 if (numel(s)>1)&&(strcmp(s(2).type,'()'))
0113 %                     elem = tt.n(s(2).subs{1});
0114 %                 else
0115 %                     elem=tt.n;
0116 %                 end;
0117             otherwise
0118                 error(['No field ', s.subs, ' is here.']);
0119         end
0120     case '{}'
0121         %Return the core in the old (not exactly!!! r1-n-r2 here) format
0122         pp=s.subs;
0123         mn=numel(pp);
0124         if ( mn > 1 )
0125           error('Invalid number of cores asked');
0126         end
0127         elem=core(tt,pp{1});
0128         
0129         if (numel(s)>1)
0130             s = s(2:end);
0131             elem = subsref(elem, s);
0132         end;
0133 %         if (pp{1}~=1)
0134 %             elem=permute(elem,[2,1,3]);
0135 %         end;
0136         
0137     otherwise
0138         error('Invalid subsref.');
0139 end

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