TuckerTensorTrain.__add__#

t3toolbox.tucker_tensor_train.TuckerTensorTrain.__add__(other)#
def __add__(
        self,
        other,
):

Add this TuckerTensorTrains self to other tensor, yielding a tensor result = self + other with summed ranks.

Addition is defined with respect to the dense N0 x ... x N(d-1) tensor that is represented by the TuckerTensorTrain.

For corewise addition, see t3toolbox.corewise.corewise_add()

Allowed types are as follows:

  • TuckerTensorTrain + TuckerTensorTrain -> TuckerTensorTrain

    (self + other).to_dense() = self.to_dense() + other.to_dense()

  • TuckerTensorTrain + NDArray -> NDArray

    self + other = self.to_dense() + other

  • TuckerTensorTrain + scalar -> TuckerTensorTrain

    (self + other).to_dense() = self.to_dense() + other * np.ones(self.stack_shape + self.shape)

Parameters:

other (TuckerTensorTrain or NDArray or scalar) – Other tensor or scalar to add to this TuckerTensorTrain. If other is TuckerTensorTrain, requires other.shape=self.shape and other.stack_shape=self.stack_shape. If other is NDArray, requires other.shape=self.stack_shape+self.shape.

Returns:

result – Sum of tensors self and other. If other is TuckerTensorTrain or scalar, result.shape=self.shape, result.stack_shape=self.stack_shape. If other is NDArray, result.shape=self.stack_shape+self.shape.

Return type:

TuckerTensorTrain or NDArray

Raises:

ValueError – If shapes and/or stack shapes of self and other are inconsistent.

Examples

>>> import numpy as np
>>> import t3toolbox.tucker_tensor_train as t3
>>> np.random.seed(0)
>>> x = t3.TuckerTensorTrain.randn((14,15,16), (4,5,6), (1,3,2,1))
>>> y = t3.TuckerTensorTrain.randn((14,15,16), (3,7,2), (1,5,6,1))
>>> z = x + y
>>> print(np.allclose(x.to_dense() + y.to_dense(), z.to_dense()))
True
>>> print(z.structure)                  # adding T3s ADDS their ranks: Tucker 4+3,5+7,6+2; TT 1+1,3+5,2+6,1+1
((14, 15, 16), (7, 12, 8), (2, 8, 8, 2), ())

Adding T3 + dense

>>> import numpy as np
>>> import t3toolbox.tucker_tensor_train as t3
>>> x = t3.TuckerTensorTrain.randn((14,15,16), (4,5,6), (1,3,2,1))
>>> y = np.random.randn(14,15,16)
>>> z = x + y
>>> print(np.linalg.norm(x.to_dense() + y - z))
0.0
>>> print(type(z))
<class 'numpy.ndarray'>

Adding T3 + scalar

>>> import numpy as np
>>> import t3toolbox.tucker_tensor_train as t3
>>> x = t3.TuckerTensorTrain.randn((14,15,16), (4,5,6), (1,3,2,1))
>>> s = 3.5
>>> z = x + s
>>> print(np.linalg.norm(x.to_dense() + s - z.to_dense()))
0.0
>>> print(z.structure)
((14, 15, 16), (5, 6, 7), (2, 4, 3, 2), ())