pyttb.sumtensor

class pyttb.sumtensor(tensors: List[tensor | sptensor | ktensor | ttensor] | None = None, copy: bool = True)[source]

Bases: object

Class for implicit sum of other tensors.

Create a pyttb.sumtensor from a collection of tensors.

Each provided tensor is explicitly retained. All provided tensors must have the same shape but can be combinations of types.

Parameters:
  • tensors – Tensor source data.

  • copy – Whether to make a copy of provided data or just reference it.

Examples

Create an empty pyttb.tensor:

>>> T1 = ttb.tenones((3, 4, 5))
>>> T2 = ttb.sptensor(shape=(3, 4, 5))
>>> S = ttb.sumtensor([T1, T2])
property order: Literal['F']

Return the data layout of the underlying storage.

copy() sumtensor[source]

Make a deep copy of a pyttb.sumtensor.

Returns:

Copy of original sumtensor.

Examples

>>> T1 = ttb.tensor(np.ones((3, 2)))
>>> S1 = ttb.sumtensor([T1, T1])
>>> S2 = S1
>>> S3 = S2.copy()
>>> S1.parts[0][0, 0] = 3
>>> S1.parts[0][0, 0] == S2.parts[0][0, 0]
True
>>> S1.parts[0][0, 0] == S3.parts[0][0, 0]
False
__deepcopy__(memo)[source]

Return deepcopy of this sumtensor.

property shape: Tuple[int, ...]

Shape of a pyttb.sumtensor.

__repr__()[source]

Return string representation of the sumtensor.

Returns:

String displaying shape and constituent parts.

Examples

>>> T1 = ttb.tenones((2, 2))
>>> T2 = ttb.sptensor(shape=(2, 2))
>>> ttb.sumtensor([T1, T2])  
sumtensor of shape (2, 2) with 2 parts:
Part 0:
    tensor of shape (2, 2) with order F
    data[:, :] =
    [[1. 1.]
     [1. 1.]]
Part 1:
    empty sparse tensor of shape (2, 2) with order F
__str__()

Return string representation of the sumtensor.

Returns:

String displaying shape and constituent parts.

Examples

>>> T1 = ttb.tenones((2, 2))
>>> T2 = ttb.sptensor(shape=(2, 2))
>>> ttb.sumtensor([T1, T2])  
sumtensor of shape (2, 2) with 2 parts:
Part 0:
    tensor of shape (2, 2) with order F
    data[:, :] =
    [[1. 1.]
     [1. 1.]]
Part 1:
    empty sparse tensor of shape (2, 2) with order F
property ndims: int

Number of dimensions of the sumtensor.

Examples

>>> T1 = ttb.tenones((2, 2))
>>> S = ttb.sumtensor([T1, T1])
>>> S.ndims
2
__pos__()[source]

Unary plus (+) for tensors.

Returns:

Copy of sumtensor.

Examples

>>> T = ttb.tensor(np.array([[1, 2], [3, 4]]))
>>> S = ttb.sumtensor([T, T])
>>> S2 = +S
__neg__()[source]

Unary minus (-) for tensors.

Returns:

Copy of negated sumtensor.

Examples

>>> T = ttb.tensor(np.array([[1, 2], [3, 4]]))
>>> S = ttb.sumtensor([T, T])
>>> S2 = -S
>>> S2.parts[0].isequal(-1 * S.parts[0])
True
__add__(other)[source]

Binary addition (+) for sumtensors.

Parameters:

other (pyttb.tensor, pyttb.sptensor) – pyttb.ktensor, pyttb.ttensor, or list containing those classes

Returns:

pyttb.sumtensor

Examples

>>> T = ttb.tenones((2, 2))
>>> S = ttb.sumtensor([T])
>>> len(S.parts)
1
>>> S2 = S + T
>>> len(S2.parts)
2
>>> S3 = S2 + [T, T]
>>> len(S3.parts)
4
__radd__(other)[source]

Right Binary addition (+) for sumtensors.

Parameters:

other (pyttb.tensor, pyttb.sptensor) – pyttb.ktensor, pyttb.ttensor, or list containing those classes

Returns:

pyttb.sumtensor

Examples

>>> T = ttb.tenones((2, 2))
>>> S = ttb.sumtensor([T])
>>> len(S.parts)
1
>>> S2 = T + S
>>> len(S2.parts)
2
>>> S3 = [T, T] + S2
>>> len(S3.parts)
4
to_tensor() tensor[source]

Return sumtensor converted to dense tensor.

Same as pyttb.sumtensor.full().

full() tensor[source]

Convert a pyttb.sumtensor to a pyttb.tensor.

Returns:

Re-assembled dense tensor.

Examples

>>> T = ttb.tenones((2, 2))
>>> S = ttb.sumtensor([T, T])
>>> print(S.full())  
tensor of shape (2, 2) with order F
data[:, :] =
[[2. 2.]
 [2. 2.]]
double() ndarray[source]

Convert :class:pyttb.tensor to an :class:numpy.ndarray of doubles.

Returns:

Copy of tensor data.

Examples

>>> T = ttb.tenones((2, 2))
>>> S = ttb.sumtensor([T, T])
>>> S.double()
array([[2., 2.],
       [2., 2.]])
innerprod(other: tensor | sptensor | ktensor | ttensor) float[source]

Efficient inner product between a sumtensor and other pyttb tensors.

Parameters:

other – Tensor to take an innerproduct with.

Examples

>>> T1 = ttb.tensor(np.array([[1.0, 0.0], [0.0, 4.0]]))
>>> T2 = T1.to_sptensor()
>>> S = ttb.sumtensor([T1, T2])
>>> T1.innerprod(T1)
17.0
>>> T1.innerprod(T2)
17.0
>>> S.innerprod(T1)
34.0
mttkrp(U: ktensor | List[ndarray], n: int | integer) ndarray[source]

Matricized tensor times Khatri-Rao product.

The matrices used in the Khatri-Rao product are passed as a pyttb.ktensor (where the factor matrices are used) or as a list of numpy.ndarray objects.

Parameters:
  • U – Matrices to create the Khatri-Rao product.

  • n – Mode used to matricize tensor.

Returns:

Array containing matrix product.

Examples

>>> T1 = ttb.tenones((2, 2, 2))
>>> T2 = T1.to_sptensor()
>>> S = ttb.sumtensor([T1, T2])
>>> U = [np.ones((2, 2))] * 3
>>> T1.mttkrp(U, 2)
array([[4., 4.],
       [4., 4.]])
>>> S.mttkrp(U, 2)
array([[8., 8.],
       [8., 8.]])
ttv(vector: ndarray | List[ndarray], dims: ndarray | int | None = None, exclude_dims: ndarray | int | None = None) float | sumtensor[source]

Tensor times vector.

Computes the n-mode product of parts with the vector vector; i.e., self x_n vector. The integer n specifies the dimension (or mode) along which the vector should be multiplied. If vector.shape = (I,), then the sumtensor must have self.shape[n] = I. The result will be the same order and shape as self except that the size of dimension n will be J. The resulting parts of the sum tensor have one less dimension, as dimension n is removed in the multiplication.

Multiplication with more than one vector is provided using a list of vectors and corresponding dimensions in the tensor to use.

The dimensions of the tensor with which to multiply can be provided as dims, or the dimensions to exclude from [0, …, self.ndims] can be specified using exclude_dims.

Parameters:
  • vector – Vector or vectors to multiple by.

  • dims – Dimensions to multiply against.

  • exclude_dims – Use all dimensions but these.

Returns:

Sumtensor containing individual products or a single sum if every – product is a single value.

Examples

>>> T = ttb.tensor(np.array([[1, 2], [3, 4]]))
>>> S = ttb.sumtensor([T, T])
>>> T.ttv(np.ones(2), 0)
tensor of shape (2,) with order F
data[:] =
[4. 6.]
>>> S.ttv(np.ones(2), 0)  
sumtensor of shape (2,) with 2 parts:
Part 0:
     tensor of shape (2,) with order F
     data[:] =
     [4. 6.]
Part 1:
     tensor of shape (2,) with order F
     data[:] =
     [4. 6.]
>>> T.ttv([np.ones(2), np.ones(2)])
10.0
>>> S.ttv([np.ones(2), np.ones(2)])
20.0
norm() float[source]

Compatibility Interface. Just returns 0.