-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontracttensors.m
executable file
·44 lines (38 loc) · 1.22 KB
/
contracttensors.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function [X, numindX] = contracttensors(X, numindX, indX, Y, numindY, indY)
%
Xsize = ones(1, numindX); Xsize(1:ndims(X)) = size(X);
Ysize = ones(1, numindY); Ysize(1:ndims(Y)) = size(Y);
indXl = 1:numindX; indXl(indX) = [];
indYr = 1:numindY; indYr(indY) = [];
sizeXl = Xsize(indXl);
sizeX = Xsize(indX);
sizeYr = Ysize(indYr);
sizeY = Ysize(indY);
if prod(sizeX)~= prod(sizeY)
error('indX and indY are not of same dimension.');
end
if isempty(indYr)
if isempty(indXl)
X = permute(X, indX);
X = reshape(X, [1, prod(sizeX)]);
Y = permute(Y, indY);
Y = reshape(Y, [prod(sizeY), 1]);
X = X * Y; Xsize = 1;
return;
else
X = permute(X, [indXl, indX]);
X = reshape(X, [prod(sizeXl), prod(sizeX)]);
Y = permute(Y, [indY]);
Y = reshape(Y, [prod(sizeY), 1]);
X = X * Y;
Xsize = Xsize(indXl);
X = reshape(X, [Xsize, 1]);
return
end
end
X = permute(X, [indXl, indX]); X = reshape(X, [prod(sizeXl), prod(sizeX)]);
Y = permute(Y, [indY, indYr]); Y = reshape(Y, [prod(sizeY), prod(sizeYr)]);
X = X * Y;
Xsize = [Xsize(indXl), Ysize(indYr)];
numindX = length(Xsize);
X = reshape(X, [Xsize, 1]);