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 byaxis`. If ``Sequence[int], sum over all indices inaxis. If None (default), sum over all axes.- Returns:
result – Sum of tensor over specified axes. Case 1a:
axisis None oraxiscontains all indices1,dots,dand self is not stacked:resultis scalar. Case 1b:axisis None oraxiscontains all axes1,...,dand self is stacked:resultis NDArray andresult.shape=self.stack_shape. Case 2:axisisint, oraxisisSequence[int], andaxisis missing at least ine index from1,...,d:resultis TuckerTensorTrain.- Return type:
scalar or NDArray or TuckerTensorTrain
See also
TuckerTensorTrain.__add__(),TuckerTensorTrain.__sub__(),TuckerTensorTrain.__neg__(),TuckerTensorTrain.__mul__(),TuckerTensorTrain.inner(),TuckerTensorTrain.norm()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)