# Tucker Tensors

Tucker format is a decomposition of a tensor X as the product of a core tensor G and matrices (e.g., A,B,C) in each dimension. In other words, a tensor X is expressed as:

In MATLAB notation, X=ttm(G,{A,B,C}). The ttensor class stores the components of the tensor X and can perform many operations, e.g., ttm, without explicitly forming the tensor X.

## Creating a ttensor with a tensor core

core = tensor(rand(3,2,1),[3 2 1]); %<-- The core tensor.
U = {rand(5,3), rand(4,2), rand(3,1)}; %<-- The matrices.
X = ttensor(core,U) %<-- Create the ttensor.

X is a ttensor of size 5 x 4 x 3
X.core is a tensor of size 3 x 2 x 1
X.core(:,:,1) =
0.6808    0.7942
0.4611    0.0592
0.5678    0.6029
X.U{1} =
0.0503    0.7680    0.4983
0.4154    0.9708    0.2140
0.3050    0.9901    0.6435
0.8744    0.7889    0.3200
0.0150    0.4387    0.9601
X.U{2} =
0.7266    0.4399
0.4120    0.9334
0.7446    0.6833
0.2679    0.2126
X.U{3} =
0.8392
0.6288
0.1338


## Alternate core formats: sptensor, ktensor, or ttensor

core1 = sptenrand([3 2 1],3); %<-- Create a 3 x 2 x 1 sptensor.
Y = ttensor(core1,U) %<-- Core is a sptensor.

Y is a ttensor of size 5 x 4 x 3
Y.core is a sparse tensor of size 3 x 2 x 1 with 3 nonzeros
(1,1,1)    0.0129
(2,1,1)    0.3840
(2,2,1)    0.6831
Y.U{1} =
0.0503    0.7680    0.4983
0.4154    0.9708    0.2140
0.3050    0.9901    0.6435
0.8744    0.7889    0.3200
0.0150    0.4387    0.9601
Y.U{2} =
0.7266    0.4399
0.4120    0.9334
0.7446    0.6833
0.2679    0.2126
Y.U{3} =
0.8392
0.6288
0.1338

V = {rand(3,2),rand(2,2),rand(1,2)}; %<-- Create some random matrices.
core2 = ktensor(V); %<-- Create a 3 x 2 x 1 ktensor.
Y = ttensor(core2,U) %<-- Core is a ktensor.

Y is a ttensor of size 5 x 4 x 3
Y.core is a ktensor of size 3 x 2 x 1
Y.core.lambda =
1     1
Y.core.U{1} =
0.0928    0.6085
0.0353    0.0158
0.6124    0.0164
Y.core.U{2} =
0.1901    0.0576
0.5869    0.3676
Y.core.U{3} =
0.6315    0.7176
Y.U{1} =
0.0503    0.7680    0.4983
0.4154    0.9708    0.2140
0.3050    0.9901    0.6435
0.8744    0.7889    0.3200
0.0150    0.4387    0.9601
Y.U{2} =
0.7266    0.4399
0.4120    0.9334
0.7446    0.6833
0.2679    0.2126
Y.U{3} =
0.8392
0.6288
0.1338

core3 = ttensor(tensor(1:8,[2 2 2]),V); %<-- Create a 3 x 2 x 1 ttensor.
Y = ttensor(core3,U) %<-- Core is a ttensor.

Y is a ttensor of size 5 x 4 x 3
Y.core is a ttensor of size 3 x 2 x 1
Y.core.core is a tensor of size 2 x 2 x 2
Y.core.core(:,:,1) =
1     3
2     4
Y.core.core(:,:,2) =
5     7
6     8
Y.core.U{1} =
0.0928    0.6085
0.0353    0.0158
0.6124    0.0164
Y.core.U{2} =
0.1901    0.0576
0.5869    0.3676
Y.core.U{3} =
0.6315    0.7176
Y.U{1} =
0.0503    0.7680    0.4983
0.4154    0.9708    0.2140
0.3050    0.9901    0.6435
0.8744    0.7889    0.3200
0.0150    0.4387    0.9601
Y.U{2} =
0.7266    0.4399
0.4120    0.9334
0.7446    0.6833
0.2679    0.2126
Y.U{3} =
0.8392
0.6288
0.1338


## Creating a one-dimensional ttensor

Z = ttensor(tensor(rand(2,1),2), rand(4,2)) %<-- One-dimensional ttensor.

Z is a ttensor of size 4
Z.core is a tensor of size 2
Z.core(:) =
0.6927
0.0841
Z.U{1} =
0.4544    0.6756
0.4418    0.6992
0.3533    0.7275
0.1536    0.4784


## Constituent parts of a ttensor

X.core %<-- Core tensor.

ans is a tensor of size 3 x 2 x 1
ans(:,:,1) =
0.6808    0.7942
0.4611    0.0592
0.5678    0.6029

X.U %<-- Cell array of matrices.

ans =
1×3 cell array
{5×3 double}    {4×2 double}    {3×1 double}


## Creating a ttensor from its constituent parts

Y = ttensor(X.core,X.U) %<-- Recreate a tensor from its parts.

Y is a ttensor of size 5 x 4 x 3
Y.core is a tensor of size 3 x 2 x 1
Y.core(:,:,1) =
0.6808    0.7942
0.4611    0.0592
0.5678    0.6029
Y.U{1} =
0.0503    0.7680    0.4983
0.4154    0.9708    0.2140
0.3050    0.9901    0.6435
0.8744    0.7889    0.3200
0.0150    0.4387    0.9601
Y.U{2} =
0.7266    0.4399
0.4120    0.9334
0.7446    0.6833
0.2679    0.2126
Y.U{3} =
0.8392
0.6288
0.1338


## Creating an empty ttensor.

X = ttensor %<-- empty ttensor

X is a ttensor of size [empty tensor]
X.core is a tensor of size [empty tensor]
X.core = []


## Use full or tensor to convert a ttensor to a tensor

X = ttensor(core,U) %<-- Create a tensor

X is a ttensor of size 5 x 4 x 3
X.core is a tensor of size 3 x 2 x 1
X.core(:,:,1) =
0.6808    0.7942
0.4611    0.0592
0.5678    0.6029
X.U{1} =
0.0503    0.7680    0.4983
0.4154    0.9708    0.2140
0.3050    0.9901    0.6435
0.8744    0.7889    0.3200
0.0150    0.4387    0.9601
X.U{2} =
0.7266    0.4399
0.4120    0.9334
0.7446    0.6833
0.2679    0.2126
X.U{3} =
0.8392
0.6288
0.1338

full(X) %<-- Converts to a tensor.

ans is a tensor of size 5 x 4 x 3
ans(:,:,1) =
0.5518    0.5343    0.6407    0.2198
0.7102    0.6990    0.8285    0.2837
0.8821    0.8955    1.0383    0.3544
1.0405    1.1261    1.2485    0.4232
0.6897    0.7450    0.8271    0.2804
ans(:,:,2) =
0.4134    0.4003    0.4800    0.1647
0.5321    0.5237    0.6207    0.2125
0.6609    0.6709    0.7780    0.2655
0.7796    0.8437    0.9354    0.3170
0.5168    0.5582    0.6197    0.2101
ans(:,:,3) =
0.0880    0.0852    0.1021    0.0350
0.1132    0.1114    0.1321    0.0452
0.1406    0.1427    0.1655    0.0565
0.1659    0.1795    0.1990    0.0674
0.1099    0.1188    0.1318    0.0447

tensor(X) %<-- Also converts to a tensor.

ans is a tensor of size 5 x 4 x 3
ans(:,:,1) =
0.5518    0.5343    0.6407    0.2198
0.7102    0.6990    0.8285    0.2837
0.8821    0.8955    1.0383    0.3544
1.0405    1.1261    1.2485    0.4232
0.6897    0.7450    0.8271    0.2804
ans(:,:,2) =
0.4134    0.4003    0.4800    0.1647
0.5321    0.5237    0.6207    0.2125
0.6609    0.6709    0.7780    0.2655
0.7796    0.8437    0.9354    0.3170
0.5168    0.5582    0.6197    0.2101
ans(:,:,3) =
0.0880    0.0852    0.1021    0.0350
0.1132    0.1114    0.1321    0.0452
0.1406    0.1427    0.1655    0.0565
0.1659    0.1795    0.1990    0.0674
0.1099    0.1188    0.1318    0.0447


## Use double to convert a ttensor to a (multidimensional) array

double(X) %<-- Converts to a MATLAB array

ans(:,:,1) =
0.5518    0.5343    0.6407    0.2198
0.7102    0.6990    0.8285    0.2837
0.8821    0.8955    1.0383    0.3544
1.0405    1.1261    1.2485    0.4232
0.6897    0.7450    0.8271    0.2804
ans(:,:,2) =
0.4134    0.4003    0.4800    0.1647
0.5321    0.5237    0.6207    0.2125
0.6609    0.6709    0.7780    0.2655
0.7796    0.8437    0.9354    0.3170
0.5168    0.5582    0.6197    0.2101
ans(:,:,3) =
0.0880    0.0852    0.1021    0.0350
0.1132    0.1114    0.1321    0.0452
0.1406    0.1427    0.1655    0.0565
0.1659    0.1795    0.1990    0.0674
0.1099    0.1188    0.1318    0.0447


## Use ndims and size to get the size of a ttensor

ndims(X) %<-- Number of dimensions.

ans =
3

size(X) %<-- Row vector of the sizes.

ans =
5     4     3

size(X,2) %<-- Size of the 2nd mode.

ans =
4


## Subscripted reference to a ttensor

X.core(1,1,1) %<-- Access an element of the core.

ans =
0.6808

X.U{2} %<-- Extract a matrix.

ans =
0.7266    0.4399
0.4120    0.9334
0.7446    0.6833
0.2679    0.2126

X{2} %<-- Same as above.

ans =
0.7266    0.4399
0.4120    0.9334
0.7446    0.6833
0.2679    0.2126


## Subscripted assignment for a ttensor

X.core = tenones(size(X.core)) %<-- Insert a new core.

X is a ttensor of size 5 x 4 x 3
X.core is a tensor of size 3 x 2 x 1
X.core(:,:,1) =
1     1
1     1
1     1
X.U{1} =
0.0503    0.7680    0.4983
0.4154    0.9708    0.2140
0.3050    0.9901    0.6435
0.8744    0.7889    0.3200
0.0150    0.4387    0.9601
X.U{2} =
0.7266    0.4399
0.4120    0.9334
0.7446    0.6833
0.2679    0.2126
X.U{3} =
0.8392
0.6288
0.1338

X.core(2,2,1) = 7 %<-- Change a single element.

X is a ttensor of size 5 x 4 x 3
X.core is a tensor of size 3 x 2 x 1
X.core(:,:,1) =
1     1
1     7
1     1
X.U{1} =
0.0503    0.7680    0.4983
0.4154    0.9708    0.2140
0.3050    0.9901    0.6435
0.8744    0.7889    0.3200
0.0150    0.4387    0.9601
X.U{2} =
0.7266    0.4399
0.4120    0.9334
0.7446    0.6833
0.2679    0.2126
X.U{3} =
0.8392
0.6288
0.1338

X{3}(1:2,1) = [1;1] %<-- Change the matrix for mode 3.

X is a ttensor of size 5 x 4 x 3
X.core is a tensor of size 3 x 2 x 1
X.core(:,:,1) =
1     1
1     7
1     1
X.U{1} =
0.0503    0.7680    0.4983
0.4154    0.9708    0.2140
0.3050    0.9901    0.6435
0.8744    0.7889    0.3200
0.0150    0.4387    0.9601
X.U{2} =
0.7266    0.4399
0.4120    0.9334
0.7446    0.6833
0.2679    0.2126
X.U{3} =
1.0000
1.0000
0.1338


## Using end for last index

X{end}  %<-- The same as X{3}.

ans =
1.0000
1.0000
0.1338


## Basic operations (uplus, uminus, mtimes) for a ttensor.

X = ttensor(tenrand([2 2 2]),{rand(3,2),rand(1,2),rand(2,2)}) %<-- Data.
+X %<-- Calls uplus.

X is a ttensor of size 3 x 1 x 2
X.core is a tensor of size 2 x 2 x 2
X.core(:,:,1) =
0.5548    0.4508
0.1210    0.7159
X.core(:,:,2) =
0.8928    0.2548
0.2731    0.8656
X.U{1} =
0.2324    0.2319
0.8049    0.2393
0.9084    0.0498
X.U{2} =
0.0784    0.6408
X.U{3} =
0.1909    0.1739
0.8439    0.1708
ans is a ttensor of size 3 x 1 x 2
ans.core is a tensor of size 2 x 2 x 2
ans.core(:,:,1) =
0.5548    0.4508
0.1210    0.7159
ans.core(:,:,2) =
0.8928    0.2548
0.2731    0.8656
ans.U{1} =
0.2324    0.2319
0.8049    0.2393
0.9084    0.0498
ans.U{2} =
0.0784    0.6408
ans.U{3} =
0.1909    0.1739
0.8439    0.1708

-X %<-- Calls uminus.

ans is a ttensor of size 3 x 1 x 2
ans.core is a tensor of size 2 x 2 x 2
ans.core(:,:,1) =
-0.5548   -0.4508
-0.1210   -0.7159
ans.core(:,:,2) =
-0.8928   -0.2548
-0.2731   -0.8656
ans.U{1} =
0.2324    0.2319
0.8049    0.2393
0.9084    0.0498
ans.U{2} =
0.0784    0.6408
ans.U{3} =
0.1909    0.1739
0.8439    0.1708

5*X %<-- Calls mtimes.

ans is a ttensor of size 3 x 1 x 2
ans.core is a tensor of size 2 x 2 x 2
ans.core(:,:,1) =
2.7742    2.2538
0.6052    3.5794
ans.core(:,:,2) =
4.4642    1.2738
1.3655    4.3280
ans.U{1} =
0.2324    0.2319
0.8049    0.2393
0.9084    0.0498
ans.U{2} =
0.0784    0.6408
ans.U{3} =
0.1909    0.1739
0.8439    0.1708


## Use permute to reorder the modes of a ttensor

permute(X,[3 2 1]) %<-- Reverses the modes of X

ans is a ttensor of size 2 x 1 x 3
ans.core is a tensor of size 2 x 2 x 2
ans.core(:,:,1) =
0.5548    0.4508
0.8928    0.2548
ans.core(:,:,2) =
0.1210    0.7159
0.2731    0.8656
ans.U{1} =
0.1909    0.1739
0.8439    0.1708
ans.U{2} =
0.0784    0.6408
ans.U{3} =
0.2324    0.2319
0.8049    0.2393
0.9084    0.0498


## Displaying a ttensor

The tensor displays by displaying the core and each of the component matrices.

disp(X) %<-- Prints out the ttensor.

ans is a ttensor of size 3 x 1 x 2
ans.core is a tensor of size 2 x 2 x 2
ans.core(:,:,1) =
0.5548    0.4508
0.1210    0.7159
ans.core(:,:,2) =
0.8928    0.2548
0.2731    0.8656
ans.U{1} =
0.2324    0.2319
0.8049    0.2393
0.9084    0.0498
ans.U{2} =
0.0784    0.6408
ans.U{3} =
0.1909    0.1739
0.8439    0.1708