Home > manopt > manifolds > ttfixedrank > TTeMPS_1.1 > @TTeMPS > from_array.m

from_array

PURPOSE ^

FROM_ARRAY Approximate full array by TTeMPS tensor of prescribed rank

SYNOPSIS ^

function x = from_array(A,opt)

DESCRIPTION ^

FROM_ARRAY Approximate full array by TTeMPS tensor of prescribed rank 
   or within a prescribed tolerance.

   X = TTeMPS.from_array( A, tol ) approximates the given array A by a
       TTeMPS tensor such that the the error is in the order of tol.

   X = TTeMPS.from_array( A, r ), with r a vector of length (ndims(A))+1),
       approximates the given array A by a rank-r TTeMPS tensor, such that 
       X.rank = r.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function x = from_array(A,opt)
0002     %FROM_ARRAY Approximate full array by TTeMPS tensor of prescribed rank
0003     %   or within a prescribed tolerance.
0004     %
0005     %   X = TTeMPS.from_array( A, tol ) approximates the given array A by a
0006     %       TTeMPS tensor such that the the error is in the order of tol.
0007     %
0008     %   X = TTeMPS.from_array( A, r ), with r a vector of length (ndims(A))+1),
0009     %       approximates the given array A by a rank-r TTeMPS tensor, such that
0010     %       X.rank = r.
0011     %
0012 
0013     %   TTeMPS Toolbox.
0014     %   Michael Steinlechner, 2013-2016
0015     %   Questions and contact: michael.steinlechner@epfl.ch
0016     %   BSD 2-clause license, see LICENSE.txt
0017 
0018     n = size(A);
0019     d = length(n);
0020 
0021     if length(opt) == 1
0022         useTol = true;
0023         tol = opt;
0024         r = ones(1,d+1);
0025     else
0026         useTol = false;
0027         r = opt;
0028         if r(1) ~= 1 || r(d+1) ~= 1
0029             error('Invalid rank specified')
0030         end
0031     end
0032 
0033     U = cell(1,d);
0034 
0035     % process from left to right
0036     % first core
0037     A = reshape( A, n(1), prod(n(2:end)));
0038     [u,s,v] = svd(A,'econ');
0039     if useTol
0040         r(2) = trunc_singular( diag(s), tol );
0041     end
0042     U{1} = reshape( u(:,1:r(2)), [1, n(1), r(2)] );
0043     A = s(1:r(2),1:r(2))*v(:,1:r(2))';
0044 
0045     % middle cores
0046     for i = 2:d-1
0047         A = reshape( A, n(i)*r(i), prod(n(i+1:end)));
0048         [u,s,v] = svd(A,'econ');
0049         if useTol
0050             r(i+1) = trunc_singular( diag(s), tol );
0051         end
0052         U{i} = reshape( u(:,1:r(i+1)), [r(i), n(i), r(i+1)] );
0053         A = s(1:r(i+1),1:r(i+1)) * v(:,1:r(i+1))';
0054     end
0055 
0056     %last core
0057     U{d} = reshape(A, [r(d), n(d), 1]);
0058 
0059     x = TTeMPS( U );
0060 end

Generated on Fri 30-Sep-2022 13:18:25 by m2html © 2005