TuckerTensorTrain.sum ===================== .. py:method:: t3toolbox.tucker_tensor_train.TuckerTensorTrain.sum(axis=None) .. code-block:: python 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 :func:`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. :param axis: If ``int``, sum over index specified by ``axis`. If ``Sequence[int]``, sum over all indices in ``axis``. If None (default), sum over all axes. :type axis: int or Sequence[int], optional :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. :rtype: scalar or NDArray or TuckerTensorTrain .. seealso:: :py:meth:`.TuckerTensorTrain.__add__`, :py:meth:`.TuckerTensorTrain.__sub__`, :py:meth:`.TuckerTensorTrain.__neg__`, :py:meth:`.TuckerTensorTrain.__mul__`, :py:meth:`.TuckerTensorTrain.inner`, :py:meth:`.TuckerTensorTrain.norm` .. rubric:: 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)) >>> 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)) >>> 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)) >>> print(S.shape) (10, 12, 13) >>> print(S.stack_shape) (2, 3)