TuckerTensorTrain.sum#

t3toolbox.tucker_tensor_train.TuckerTensorTrain.sum(axis=None)#
def sum(
        self,
        axis=None,
):

Sum over one or more axes of TuckerTensorTrain.

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

For corewise norm, see t3toolbox.corewise.corewise_norm()

If all axes are summed over, returns NDArray or scalar, depending on whether or not self is stacked. If at least one axis is not summed over, returns TuckerTensorTrain.

Parameters:

axis (int or Sequence[int], optional) – If int, sum over index specified by axis`. If ``Sequence[int], sum over all indices in axis. If None (default), sum over all axes.

Returns:

result – Sum of tensor over specified axes. Case 1a: axis is None or axis contains all indices 1,dots,d and self is not stacked: result is scalar. Case 1b: axis is None or axis contains all axes 1,...,d and self is stacked: result is NDArray and result.shape=self.stack_shape. Case 2: axis is int, or axis is Sequence[int], and axis is missing at least ine index from 1,...,d: result is TuckerTensorTrain.

Return type:

scalar or NDArray or TuckerTensorTrain

Examples

>>> import numpy as np
>>> import t3toolbox.tucker_tensor_train as t3
>>> np.random.seed(0)
>>> x = t3.TuckerTensorTrain.randn((10,11,12,13), (7,8,9,10), (2,3,4,3,1), (2,3))
>>> S = x.sum()                         # all free axes summed -> ndarray over the stack
>>> dense_x = x.to_dense()
>>> non_stack_axes = (2,3,4,5)
>>> print(np.allclose(S, dense_x.sum(axis=non_stack_axes)))
True
>>> print(type(S))
<class 'numpy.ndarray'>
>>> print(S.shape)
(2, 3)

Axis is a tuple of ints:

>>> import numpy as np
>>> import t3toolbox.tucker_tensor_train as t3
>>> np.random.seed(0)
>>> x = t3.TuckerTensorTrain.randn((10,11,12,13), (7,8,9,10), (2,3,4,3,1), (2,3))
>>> axis = (1,3)
>>> S = x.sum(axis=axis)                # some axes kept -> TuckerTensorTrain
>>> dense_x = x.to_dense()
>>> shifted_axis = tuple(ii + len(x.stack_shape) for ii in axis)
>>> print(np.allclose(S.to_dense(), dense_x.sum(axis=shifted_axis)))
True
>>> print(type(S))
<class 't3toolbox.tucker_tensor_train.TuckerTensorTrain'>
>>> print(S.shape)
(10, 12)
>>> print(S.stack_shape)
(2, 3)

Axis is int:

>>> import numpy as np
>>> import t3toolbox.tucker_tensor_train as t3
>>> np.random.seed(0)
>>> x = t3.TuckerTensorTrain.randn((10,11,12,13), (7,8,9,10), (2,3,4,3,1), (2,3))
>>> axis = 1
>>> S = x.sum(axis=axis)
>>> dense_x = x.to_dense()
>>> shifted_axis = axis + len(x.stack_shape)
>>> print(np.allclose(S.to_dense(), dense_x.sum(axis=shifted_axis)))
True
>>> print(type(S))
<class 't3toolbox.tucker_tensor_train.TuckerTensorTrain'>
>>> print(S.shape)
(10, 12, 13)
>>> print(S.stack_shape)
(2, 3)