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

SYNOPSIS

function z = hadamard( x, y, idx )

DESCRIPTION

```HADAMARD Hadamard product of two TT/MPS tensors.
tensors X and Y and returns the new TT/MPS tensor Z.

Z = HADAMARD(X, U, idx) calculates the Hadamard product of the TT/MPS
tensors X and the 3D Matlab array Y, assumed to be in core position IDX.
Only core IDX of the resulting tensor is returned.

CROSS-REFERENCE INFORMATION

This function calls: This function is called by:

SOURCE CODE

```0001 function z = hadamard( x, y, idx )
0003     %   Z = HADAMARD(X, Y) calculates the Hadamard product of two TT/MPS
0004     %   tensors X and Y and returns the new TT/MPS tensor Z.
0005     %
0006     %   Z = HADAMARD(X, U, idx) calculates the Hadamard product of the TT/MPS
0007     %   tensors X and the 3D Matlab array Y, assumed to be in core position IDX.
0008     %   Only core IDX of the resulting tensor is returned.
0009     %
0011
0012     %   TTeMPS Toolbox.
0013     %   Michael Steinlechner, 2013-2016
0014     %   Questions and contact: michael.steinlechner@epfl.ch
0016
0017     rx = x.rank;
0018     n = x.size;
0019     d = x.order;
0020
0021     % first case: all cores.
0022     if ~exist( 'idx', 'var' )
0023         ry = y.rank;
0024
0025         C = cell(1,d);
0026         for i = 1:d
0027             % move outer dimension to the front
0028             xx1 = permute( x.U{i}, [2 1 3]);
0029             p = size( y.U{i}, 4 );
0030             C{i} = zeros( [rx(i)*ry(i), n(i), rx(i+1)*ry(i+1), p] );
0031             for j = 1:p
0032                 yy = permute( y.U{i}(:,:,:,j), [2 1 3]);
0033
0034                 % duplicate entries
0035                 xx = repmat( xx1(:), [1, ry(i)*ry(i+1)]);
0036                 yy = repmat( yy(:), [1, rx(i)*rx(i+1)]);
0037
0038                 % reshape and permute to same sizes
0039                 xx = reshape( xx, [n(i), rx(i), rx(i+1), ry(i), ry(i+1)]);
0040                 yy = reshape( yy, [n(i), ry(i), ry(i+1), rx(i), rx(i+1)]);
0041
0042                 xx = permute( xx, [1 2 4 3 5]);
0043                 yy = permute( yy, [1 4 2 5 3]);
0044
0045                 xx = reshape( xx, [n(i), rx(i)*ry(i), rx(i+1)*ry(i+1)]);
0046                 yy = reshape( yy, [n(i), rx(i)*ry(i), rx(i+1)*ry(i+1)]);
0047
0048                 % multiply elementwise.
0049                 zz = xx.*yy;
0050                 C{i}(:,:,:,j) = permute( zz, [2 1 3] );
0051             end
0052         end
0053         z = TTeMPS( C );
0054     else
0055         i = idx;
0056         ry = [size(y,1), size(y,3)];
0057
0058         % move outer dimension to the front
0059         xx1 = permute( x.U{i}, [2 1 3]);
0060         p = size( y, 4 );
0061         z = zeros( [rx(i)*ry(1), n(i), rx(i+1)*ry(2), p] );
0062         for j = 1:p
0063             yy = permute( y(:,:,:,j), [2 1 3]);
0064
0065             % duplicate entries
0066             xx = repmat( xx1(:), [1, ry(1)*ry(2)]);
0067             yy = repmat( yy(:), [1, rx(i)*rx(i+1)]);
0068
0069             % reshape and permute to same sizes
0070             xx = reshape( xx, [n(i), rx(i), rx(i+1), ry(1), ry(2)]);
0071             yy = reshape( yy, [n(i), ry(1), ry(2), rx(i), rx(i+1)]);
0072
0073             xx = permute( xx, [1 2 4 3 5]);
0074             yy = permute( yy, [1 4 2 5 3]);
0075
0076             xx = reshape( xx, [n(i), rx(i)*ry(1), rx(i+1)*ry(2)]);
0077             yy = reshape( yy, [n(i), rx(i)*ry(1), rx(i+1)*ry(2)]);
0078
0079             % multiply elementwise.
0080             zz = xx.*yy;
0081             z(:,:,:,j) = permute( zz, [2 1 3] );
0082         end
0083     end
0084
0085 end```

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