# Sum of Structured Tensors

When certain operations are performed on a tensor which is formed as a sum of tensors, it can be beneficial to avoid explicitly forming the sum. For example, if a tensor is formed as a sum of a low rank tensor and a sparse tensor, the structure of the summands can make storage, decomposition and operations with other tensors significantly more efficient. The tensor toolbox supports a `sumtensor` object designed to exploit this structure. Here we explain the basics of defining and using sumtensors.

## Contents

- Creating sumtensors
- An Large-Scale Example
- Further examples of the sumtensor constructer
- Use ndims and size for the dimensions of a sumtensor
- Use full to convert a sumtensor to a "generic" tensor
- Use double to convert a sumtensor to a multidimensional array
- Matricized Khatri-Rao product of a sumtensor
- Use innerprod to compute the inner product of a sumtensor
- Use norm for compatibility with the other types of tensors.
- Use CP-ALS to find a CP decomposition of a sumtensor
- Basic operations (plus) for sumtensors
- Subscripted reference for sumtensors

## Creating sumtensors

A sumtensor T can only be delared as a sum of same-sized tensors `T1, T2,...,TN`. The summand tensors are stored in a cell array, which define the "parts" of the sumtensor. The parts of a sumtensor can be (generic) tensors (as `tensor`), sparse tensors (as `sptensor`), Kruskal tensors (as `ktensor`), or Tucker tensors (as `ttensor`). An example of the use of the `sumtensor` constructor follows.

T1 = tensor(ones(3,3,3)); %<--A tensor T2 = sptensor([1 1 1; 2 2 2; 3 3 2; 2 1 1], 1, [3,3,3]); %<--A sparse tensor T = sumtensor(T1,T2)

T is a sumtensor of size 3 x 3 x 3 with 2 parts T.part{1} is a tensor of size 3 x 3 x 3 T.part{1}(:,:,1) = 1 1 1 1 1 1 1 1 1 T.part{1}(:,:,2) = 1 1 1 1 1 1 1 1 1 T.part{1}(:,:,3) = 1 1 1 1 1 1 1 1 1 T.part{2} is a sparse tensor of size 3 x 3 x 3 with 4 nonzeros (1,1,1) 1 (2,1,1) 1 (2,2,2) 1 (3,3,2) 1

## An Large-Scale Example

For large-scale problems, the `sumtensor` class may make the difference as to whether or not a tensor can be stored in memory. Consider the following example, where is of size , formed from the sum of a `ktensor` and an `sptensor`.

X1 = rand(500, 3); %Generating some factor matrices X2 = rand(500, 3); X3 = rand(500, 3); K = ktensor([1; 1; 1], X1, X2, X3); S = sptenrand([500, 500, 500], 1e-100); ST = sumtensor(K,S); %<-- Declare the sumtensor TT = full(ST); %<-- Form the sum of the tensors explicitly whos ST TT %<--Output the storage information for these variables

Name Size Bytes Class Attributes ST 500x500x500 37696 sumtensor TT 500x500x500 1000000376 tensor

The difference in memory between the full and sumtensor is a factor of 10^5! Hence we prefer to use the sumtensor object whenever possible.

## Further examples of the sumtensor constructer

The sumtensor constructor can declare an empty sumtensor object, having no parts, as follows

P = sumtensor()

P is an empty sumtensor

`sumtensor` also supports use as a copy constructor.

S = sumtensor(P)

S is an empty sumtensor

## Use ndims and size for the dimensions of a sumtensor

For a given sumtensor, `ndims` returns the number of modes of a sumtensor. Similarly, `size` returns a size array of the sumtensor.

ndims(T) size(T)

ans = 3 ans = 3 3 3

## Use full to convert a sumtensor to a "generic" tensor

The `full` function can be used to convert a sumtensor to a generic tensor. Note that for large-scale tensors, this can a large amount of memory because each part of the sumtensor will be expanded and then summed.

full(T)

ans is a tensor of size 3 x 3 x 3 ans(:,:,1) = 2 1 1 2 1 1 1 1 1 ans(:,:,2) = 1 1 1 1 2 1 1 1 2 ans(:,:,3) = 1 1 1 1 1 1 1 1 1

## Use double to convert a sumtensor to a multidimensional array

The `double` function can be used to convert a sumtensor to a multidimensional array. Similarly to the `full` expansion, this can use a prohibitive amount of memory for large-scale problems.

double(T)

ans(:,:,1) = 2 1 1 2 1 1 1 1 1 ans(:,:,2) = 1 1 1 1 2 1 1 1 2 ans(:,:,3) = 1 1 1 1 1 1 1 1 1

## Matricized Khatri-Rao product of a sumtensor

The `mttkrp` function computes the Khatri-Rao product of a matricized tensor and a sumtensor. The required arguments are: a sumtensor X, a cell array of matrices U={U1,...,Um}, and a mode n. The cell array must consist of m matrices, where m is the number of modes in X. The number of columns of these matrices should be constant, and number of rows of matrix Ui should match the dimension of the tensor X in mode i. The matricized Khatri-Rao product operation on sumtensor distributes the operation to the summands of the sumtensor. For details of this specific computation, see the mttkrp documentation for a generic tensor. An example of the use of `mttkrp` follows.

```
U={eye(3), ones(3,3), randn(3,3)}; %<--The cell array of matrices
mttkrp(T,U,2)
```

ans = 4.4399 -2.5814 -2.4033 3.1497 -1.3988 -2.4033 3.1497 -1.3790 -2.1460

## Use innerprod to compute the inner product of a sumtensor

The `innerprod` function computes the inner product of a sumtensor T and any type of tensor. The operation is performed by distributing across each of the sumtensor's parts.

S = sptensor([1 1 1; 2 1 3; 3 2 2; 2 1 1], 1, [3,3,3]); innerprod(T,S)

ans = 6

## Use norm for compatibility with the other types of tensors.

The `norm` function returns 0 and a warning when called on a sumtensor. The procedure of computing the Frobenius norm of a sumtensor does not distribute across its parts, and hence is not supported for sumtensors. This default behavior is provided in order to ensure compatibility of the sumtensor class with existing decomposition routines.

norm(T)

Warning: The NORM function is not supported by SUMTENSOR. Returning zero. ans = 0

In order avoid this default behavior and return the Frobenius norm of a sumtensor, it can be converted to a tensor using `full`.

norm(full(T))

ans = 6.2450

## Use CP-ALS to find a CP decomposition of a sumtensor

One of the primary motivations for defining the `sumtensor` class is for efficient decomposition. In particular, when trying to find a CP decomposition of a tensor using alternating least squares, the subproblems can be efficiently created and solved using `mttkrp` and `innerprod`. Both of these operations can be performed more efficiently by exploiting extra structure in the tensors which form the sum, so the performance of `cp_als` is also improved. Consider the following example, where a `cp_als` is run on a sumtensor.

cp_als(T, 2)

Warning: The NORM function is not supported by SUMTENSOR. Returning zero. CP_ALS: Iter 1: f = -3.740453e+01 f-delta = 3.7e+01 Iter 2: f = -3.755900e+01 f-delta = 1.5e-01 Iter 3: f = -3.756781e+01 f-delta = 8.8e-03 Iter 4: f = -3.757607e+01 f-delta = 8.3e-03 Iter 5: f = -3.758587e+01 f-delta = 9.8e-03 Iter 6: f = -3.759762e+01 f-delta = 1.2e-02 Iter 7: f = -3.761184e+01 f-delta = 1.4e-02 Iter 8: f = -3.762919e+01 f-delta = 1.7e-02 Iter 9: f = -3.765037e+01 f-delta = 2.1e-02 Iter 10: f = -3.767600e+01 f-delta = 2.6e-02 Iter 11: f = -3.770612e+01 f-delta = 3.0e-02 Iter 12: f = -3.773928e+01 f-delta = 3.3e-02 Iter 13: f = -3.777165e+01 f-delta = 3.2e-02 Iter 14: f = -3.779786e+01 f-delta = 2.6e-02 Iter 15: f = -3.781462e+01 f-delta = 1.7e-02 Iter 16: f = -3.782312e+01 f-delta = 8.5e-03 Iter 17: f = -3.782675e+01 f-delta = 3.6e-03 Iter 18: f = -3.782817e+01 f-delta = 1.4e-03 Iter 19: f = -3.782871e+01 f-delta = 5.4e-04 Iter 20: f = -3.782892e+01 f-delta = 2.1e-04 Iter 21: f = -3.782901e+01 f-delta = 8.9e-05 Final f = -3.782901e+01 ans is a ktensor of size 3 x 3 x 3 ans.lambda = 4.6822 2.5591 ans.U{1} = 0.4686 0.7277 0.5901 0.6432 0.6574 0.2383 ans.U{2} = 0.4087 0.9486 0.6316 0.2483 0.6588 0.1961 ans.U{3} = 0.4120 0.9386 0.7467 0.1492 0.5222 0.3112

It follows that in cases where is too large for its full expansion to be stored in memory, we may still be able find a CP decomposition by exploiting the sumtensor structure.

Note that the fit returned by cp_als is not correct for sumtensors, because the norm operation is not supported.

## Basic operations (plus) for sumtensors

Sumtensors can be added to any other type of tensor. The result is a new sumtensor with the tensor appended to the parts of the original sumtensor. Note that the tensor is always appended, despite the order of the operation.

T+S %<--S is appended to the parts of T S+T %<--S is still the last part of T, despite order

ans is a sumtensor of size 3 x 3 x 3 with 3 parts ans.part{1} is a tensor of size 3 x 3 x 3 ans.part{1}(:,:,1) = 1 1 1 1 1 1 1 1 1 ans.part{1}(:,:,2) = 1 1 1 1 1 1 1 1 1 ans.part{1}(:,:,3) = 1 1 1 1 1 1 1 1 1 ans.part{2} is a sparse tensor of size 3 x 3 x 3 with 4 nonzeros (1,1,1) 1 (2,1,1) 1 (2,2,2) 1 (3,3,2) 1 ans.part{3} is a sparse tensor of size 3 x 3 x 3 with 4 nonzeros (1,1,1) 1 (2,1,1) 1 (2,1,3) 1 (3,2,2) 1 ans is a sumtensor of size 3 x 3 x 3 with 3 parts ans.part{1} is a tensor of size 3 x 3 x 3 ans.part{1}(:,:,1) = 1 1 1 1 1 1 1 1 1 ans.part{1}(:,:,2) = 1 1 1 1 1 1 1 1 1 ans.part{1}(:,:,3) = 1 1 1 1 1 1 1 1 1 ans.part{2} is a sparse tensor of size 3 x 3 x 3 with 4 nonzeros (1,1,1) 1 (2,1,1) 1 (2,2,2) 1 (3,3,2) 1 ans.part{3} is a sparse tensor of size 3 x 3 x 3 with 4 nonzeros (1,1,1) 1 (2,1,1) 1 (2,1,3) 1 (3,2,2) 1

## Subscripted reference for sumtensors

Subscripted reference can be used to return the individual parts of a sumtensor.

T.part{1} T.part{2}

ans is a tensor of size 3 x 3 x 3 ans(:,:,1) = 1 1 1 1 1 1 1 1 1 ans(:,:,2) = 1 1 1 1 1 1 1 1 1 ans(:,:,3) = 1 1 1 1 1 1 1 1 1 ans is a sparse tensor of size 3 x 3 x 3 with 4 nonzeros (1,1,1) 1 (2,1,1) 1 (2,2,2) 1 (3,3,2) 1