Home > tt2 > core > tt_max_abs.m

tt_max_abs

PURPOSE ^

Compute the (supposedly) maximal in modulus element in a TT-tensor

SYNOPSIS ^

function [mx,ind_full]=tt_max_abs(tt)

DESCRIPTION ^

Compute the (supposedly) maximal in modulus element in a TT-tensor
   [MX,IND_FULL]=TT_MAX_ABS(TT) Compute element quasi-maximal in modulus in 
   a TT tensor TT by computing. It return the value MX and the multiindex
   IND_FULL where this element is located.


 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 [mx,ind_full]=tt_max_abs(tt)
0002 %Compute the (supposedly) maximal in modulus element in a TT-tensor
0003 %   [MX,IND_FULL]=TT_MAX_ABS(TT) Compute element quasi-maximal in modulus in
0004 %   a TT tensor TT by computing. It return the value MX and the multiindex
0005 %   IND_FULL where this element is located.
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 %Algorithm: Compute left index set, compute right index set
0019 %compute submatrices of a tensors, compute maximal element in them
0020 tt=core(tt); %From the TT2 format
0021 d=size(tt,1);
0022 
0023 %NEW version
0024 tt1=tt;
0025 mat=tt1{1};
0026 [q,r]=qr(mat,0);
0027 tt1{1}=q; 
0028 for i=2:d-1
0029     core1=tt1{i};
0030     core1=ten_conv(core1,2,r');
0031     ncur=size(core1,1);
0032     r2=size(core1,2);
0033     r3=size(core1,3);
0034     core1=reshape(core1,[ncur*r2,r3]);
0035     [tt1{i},r]=qr(core1,0);
0036     rnew=min(ncur*r2,r3);
0037     tt1{i}=reshape(tt1{i},[ncur,r2,rnew]);
0038 end
0039 mat=tt1{1};
0040 ind_left=cell(d,1);
0041 [ind]=maxvol2(mat);
0042 ind_left{1}=ind;
0043 ind_l=cell(d,1);
0044 r1=mat(ind,:);
0045 tt1{1}=mat/r1;
0046 ind_l{1}=ind;
0047 for i=2:d-1
0048     core1=tt1{i};
0049     ncur=size(core1,1);
0050     r2=size(core1,2);
0051     r3=size(core1,3);
0052     core1=ten_conv(core1,2,r1');
0053     core1=permute(core1,[2,1,3]);
0054     core1=reshape(core1,[r2*ncur,r3]);
0055     ind=maxvol2(core1);
0056     %ind(k) varies from 1 to ncur*r2 and we need to
0057     %convert it to two-dimensional index
0058     rnew=min(ncur*r2,r3);
0059     ind_new=zeros(i,r3);
0060     ind_old=ind_left{i-1};
0061      for s=1:rnew
0062         f_in=ind(s);
0063         w1=tt_ind2sub([r2,ncur],f_in);
0064         rs=w1(1); js=w1(2);
0065         ind_new(:,s)=[ind_old(:,rs)',js];
0066      end
0067     ind_left{i}=ind_new;
0068     r1=core1(ind,:);
0069     ind_l{i}=ind;
0070     tt1{i}=core1/r1;
0071     rnew=min(r2*ncur,rnew);
0072     tt1{i}=reshape(tt1{i},[r2,ncur,rnew]);
0073     tt1{i}=permute(tt1{i},[2,1,3]);
0074 end
0075 %Compute right-to-left qr & maxvol
0076 tt2=tt;
0077 mat=tt2{d};
0078 [q,rv]=qr(mat,0);
0079 tt2{d}=q;
0080 for i=(d-1):-1:2
0081     core1=tt2{i};
0082     core1=ten_conv(core1,3,rv');
0083     ncur=size(core1,1);
0084     r2=size(core1,2);
0085     r3=size(core1,3);
0086     core1=permute(core1,[1,3,2]);
0087     core1=reshape(core1,[ncur*r3,r2]);
0088     [tt2{i},rv]=qr(core1,0);
0089     rnew=min(r2,ncur*r3);
0090     tt2{i}=reshape(tt2{i},[ncur,r3,rnew]);
0091     tt2{i}=permute(tt2{i},[1,3,2]);
0092 end
0093 tt2{1}=tt2{1}*rv';
0094 %keyboard;
0095 %Now compute right-to-left maxvol
0096 %sbm_tmp=cell(i,1);
0097 mat=tt2{d};
0098 ind_right=cell(d,1);
0099 [ind]=maxvol2(mat);
0100 ind_right{d-1}=ind;
0101 r1=mat(ind,:);
0102 %sbm_r=cell(d,1);
0103 ind_r=cell(d,1);
0104 tt2{d}=mat/r1;
0105 ind_r{d-1}=ind;
0106 %sbm_tmp{d}=r1;
0107 for i=(d-1):-1:2
0108     core1=tt2{i};
0109     ncur=size(core1,1);
0110     r2=size(core1,2);
0111     r3=size(core1,3);
0112     core1=ten_conv(core1,3,r1');
0113     core1=permute(core1,[3,1,2]);
0114     core1=reshape(core1,[r3*ncur,r2]);
0115     [ind]=maxvol2(core1); 
0116     ind_old=ind_right{i};
0117     rnew=min(ncur*r3,r2);
0118     ind_new=zeros(d-i+1,rnew);
0119     %ncur=sz(k);
0120      for s=1:rnew
0121         f_in=ind(s);
0122         w1=tt_ind2sub([r3,ncur],f_in);
0123         rs=w1(1); js=w1(2);
0124         ind_new(:,s)=[js,ind_old(:,rs)'];
0125      end
0126      ind_right{i-1}=ind_new;
0127     r1=core1(ind,:);
0128     %sbm_r{i-1}=r1;
0129     ind_r{i-1}=ind;
0130     %sbm_tmp{i}=r1;
0131     tt2{i}=core1/r1;
0132     rnew=min(ncur*r3,r2);
0133     tt2{i}=reshape(tt2{i},[r3,ncur,rnew]);
0134     tt2{i}=permute(tt2{i},[2,3,1]);
0135 %    keyboard;
0136 end
0137 %keyboard;
0138 %Compute subtensors
0139 %A(ind_left{k},ind_right{k})
0140 % sbm=cell(d-1,1);
0141 % %ind=zeros(d,1);
0142 % mx=-1.0;
0143 % pos=0;
0144 % for k=1:d
0145 %    r=size(ind_left{k},2);
0146 %    sbm{k}=zeros(r);
0147 %    for i=1:r
0148 %      for j=1:r
0149 %         ind=[ind_left{k}(:,i)',ind_right{k}(:,j)'];
0150 %         sbm{k}(i,j)=tt_elem(tt,ind);
0151 %         el=tt_elem(tt,ind);
0152 %         if ( abs(el) > mx )
0153 %            mx=abs(el);
0154 %            pos=ind;
0155 %         end
0156 %      end
0157 %    end
0158 %    fprintf('k=%d \n',k);
0159 % %   keyboard;
0160 % end
0161 %Compute left & right submatrices in U and V
0162 sbm_l=cell(d-1,1);
0163 sbm_r=cell(d-1,1);
0164 sbm_l{1}=tt{1}(ind_l{1},:);
0165 for k=2:d-1
0166   core1=tt{k};
0167   core1=ten_conv(core1,2,sbm_l{k-1}');
0168   ncur=size(core1,1); r2=size(core1,2); r3=size(core1,3);
0169   core1=reshape(permute(core1,[2,1,3]),[r2*ncur,r3]);
0170   sbm_l{k}=core1(ind_l{k},:);
0171 end
0172 sbm_r{d-1}=tt{d}(ind_r{d-1},:);
0173 for k=d-1:-1:2
0174   core1 = tt{k};
0175   core1=ten_conv(core1,3,sbm_r{k}');
0176   ncur=size(core1,1); r2=size(core1,2); r3=size(core1,3);
0177   core1=reshape(permute(core1,[3,1,2]),[r3*ncur,r2]);
0178   sbm_r{k-1}=core1(ind_r{k-1},:);
0179 end
0180 mx=-1.0;
0181 ind_full=zeros(1,d-1);
0182 for k=1:d-1
0183     sbm_new=sbm_l{k}*sbm_r{k}';
0184     r1=size(sbm_new,1);
0185     %Find position
0186     sbm_new=abs(sbm_new(:));
0187     [mx1,is]=max(sbm_new);
0188     w=tt_ind2sub([r1,r1],is);
0189     if ( mx1 > mx )
0190      ind_full=[ind_left{k}(:,w(1))',ind_right{k}(:,w(2))'];
0191       mx=mx1;
0192     end 
0193     %mx=max(mx,mx1);
0194 end
0195 %keyboard;
0196 
0197 return
0198 end

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