TuckerTensorTrain.__sub__#

t3toolbox.tucker_tensor_train.TuckerTensorTrain.__sub__(other)#
def __sub__(
        self: 'TuckerTensorTrain',
        other: 'TuckerTensorTrain',
) -> 'TuckerTensorTrain':

Subtract Tucker tensor trains, result = self - other, yielding a Tucker tensor train with summed ranks.

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

For corewise subtraction, see t3toolbox.corewise.corewise_sub()

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

Parameters:

other (TuckerTensorTrain or NDArray or scalar) – Other tensor or scalar to be subtracted from 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 – Difference, result = self - 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
>>> 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.linalg.norm(x.to_dense() - y.to_dense() - z.to_dense()))
0.0
>>> print(z.structure)
((14, 15, 16), (7, 12, 8), (2, 8, 8, 2), ())

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'>

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), ())