pyttb.sptenmat

class pyttb.sptenmat(subs: ndarray | None = None, vals: ndarray | None = None, rdims: ndarray | None = None, cdims: ndarray | None = None, tshape: Tuple[int, ...] = (), copy: bool = True)[source]

Bases: object

SPTENMAT Store sparse tensor as a sparse matrix.

Construct a pyttb.sptenmat from a set of 2D subscripts (subs) and values (vals) along with the mappings of the row (rdims) and column indices (cdims) and the shape of the original tensor (tshape).

If you already have an sparse tensor see :method:`pyttb.sptensor.to_sptenmat`.

Parameters:
  • subs – Location of non-zero entries, in sptenmat.

  • vals – Values for non-zero entries, in sptenmat.

  • rdims – Mapping of row indices.

  • cdims – Mapping of column indices.

  • tshape – Shape of the original tensor.

  • copy – Whether to make a copy of provided data or just reference it. Skips error checking when just setting reference.

Examples

Create an empty pyttb.sptenmat:

>>> S = ttb.sptenmat()
>>> S 
sptenmat corresponding to a sptensor of shape () with 0 nonzeros
rdims = [  ] (modes of sptensor corresponding to rows)
cdims = [  ] (modes of sptensor corresponding to columns)
Create a pyttb.sptenmat from subscripts, values, and unwrapping

dimensions:

>>> subs = np.array([[1, 6], [1, 7]])
>>> vals = np.array([[6], [7]])
>>> tshape = (4, 4, 4)
>>> S = ttb.sptenmat(            subs,            vals,            rdims=np.array([0]),            cdims=np.array([1,2]),            tshape=tshape        )
>>> S 
sptenmat corresponding to a sptensor of shape (4, 4, 4) with 2 nonzeros
rdims = [ 0 ] (modes of sptensor corresponding to rows)
cdims = [ 1, 2 ] (modes of sptensor corresponding to columns)
    [1, 6] = 6
    [1, 7] = 7
classmethod from_array(array: coo_matrix | ndarray, rdims: ndarray | None = None, cdims: ndarray | None = None, tshape: Tuple[int, ...] = ())[source]

Construct a pyttb.sptenmat from a coo_matrix along with the mappings of the row (rdims) and column indices (cdims) and the shape of the original tensor (tshape).

Parameters:
  • array – Representation of sparse tensor data (sparse or dense).

  • rdims – Mapping of row indices.

  • cdims – Mapping of column indices.

  • tshape – Shape of the original tensor.

Examples

Create a pyttb.sptenmat from a sparse matrix and unwrapping

dimensions. Infer column dimensions from row dimensions specification.

>>> data = np.array([6, 7])
>>> rows = np.array([1, 1])
>>> cols = np.array([6, 7])
>>> sparse_matrix = sparse.coo_matrix((data, (rows, cols)))
>>> tshape = (4, 4, 4)
>>> S = ttb.sptenmat.from_array(            sparse_matrix,            rdims=np.array([0]),            tshape=tshape        )
>>> S 
sptenmat corresponding to a sptensor of shape (4, 4, 4) with 2 nonzeros
rdims = [ 0 ] (modes of sptensor corresponding to rows)
cdims = [ 1, 2 ] (modes of sptensor corresponding to columns)
    [1, 6] = 6
    [1, 7] = 7
copy() sptenmat[source]

Return a deep copy of the pyttb.sptenmat.

Examples

Create a pyttb.sptenmat (ST1) and make a deep copy. Verify the deep copy (ST3) is not just a reference (like ST2) to the original.

>>> S1 = ttb.sptensor(shape=(2, 2))
>>> S1[0, 0] = 1
>>> ST1 = S1.to_sptenmat(np.array([0]))
>>> ST2 = ST1
>>> ST3 = ST1.copy()
>>> ST1[0, 0] = 3
>>> ST1.to_sptensor().isequal(ST2.to_sptensor())
True
>>> ST1.to_sptensor().isequal(ST3.to_sptensor())
False
to_sptensor() sptensor[source]

Contruct a pyttb.sptensor from :class:pyttb.sptenmat

Examples

>>> S1 = ttb.sptensor(shape=(2, 2, 2))
>>> S1[0, 0, 0] = 1
>>> S1  
sparse tensor of shape (2, 2, 2) with 1 nonzeros
[0, 0, 0] = 1.0
>>> ST1 = S1.to_sptenmat(np.array([0]))
>>> ST1  
sptenmat corresponding to a sptensor of shape (2, 2, 2) with 1 nonzeros
rdims = [ 0 ] (modes of sptensor corresponding to rows)
cdims = [ 1, 2 ] (modes of sptensor corresponding to columns)
    [0, 0] = 1.0
>>> ST1.to_sptensor()  
sparse tensor of shape (2, 2, 2) with 1 nonzeros
[0, 0, 0] = 1.0
property shape: Tuple[int, ...]

Return the shape of a pyttb.sptenmat.

Examples

>>> ttb.sptenmat().shape  # empty sptenmat
()
>>> S1 = ttb.sptensor(shape=(2, 2, 2))
>>> S1[0, 0, 0] = 1
>>> ST1 = S1.to_sptenmat(np.array([0]))
>>> ST1.shape
(2, 4)
double() coo_matrix[source]

Convert a pyttb.sptenmat to a COO scipy.sparse.coo_matrix.

Examples

>>> S1 = ttb.sptensor(shape=(2, 2, 2))
>>> S1[0, 0, 0] = 1
>>> ST1 = S1.to_sptenmat(np.array([0]))
>>> COO = ST1.double()
>>> type(COO)  
<class 'scipy.sparse._coo.coo_matrix'>
>>> COO.nnz  
1
>>> COO.toarray()  
array([[1., 0., 0., 0.],
    [0., 0., 0., 0.]])
full() tenmat[source]

Convert a pyttb.sptenmat to a (dense) pyttb.tenmat.

Examples

>>> S1 = ttb.sptensor(shape=(2, 2, 2))
>>> S1[0, 0, 0] = 1
>>> ST1 = S1.to_sptenmat(np.array([0]))
>>> ST1.full()  
matrix corresponding to a tensor of shape (2, 2, 2)
rindices = [ 0 ] (modes of tensor corresponding to rows)
cindices = [ 1, 2 ] (modes of tensor corresponding to columns)
data[:, :] =
[[1. 0. 0. 0.]
 [0. 0. 0. 0.]]
property nnz: int

Number of nonzero values in the pyttb.sptenmat.

Examples

>>> S1 = ttb.sptensor(shape=(2, 2, 2))
>>> S1[0, 0, 0] = 1
>>> ST1 = S1.to_sptenmat(np.array([0]))
>>> ST1.nnz
1
norm() float[source]

Compute the norm (i.e., Frobenius norm, or square root of the sum of squares of entries) of the pyttb.sptenmat.

Examples

>>> S1 = ttb.sptensor(shape=(2, 2, 2))
>>> S1[0, 0, 0] = 1
>>> ST1 = S1.to_sptenmat(np.array([0]))
>>> ST1.norm()
1.0
isequal(other: sptenmat) bool[source]

Exact equality for pyttb.sptenmat.

Examples

>>> S1 = ttb.sptensor(shape=(2, 2, 2))
>>> S1[0, 0, 0] = 1
>>> ST1 = S1.to_sptenmat(np.array([0]))
>>> ST2 = ttb.sptenmat()
>>> ST1.isequal(ST2)
False
>>> ST1.isequal(ST1)
True
__pos__()[source]

Unary plus operator (+).

Examples

>>> S1 = ttb.sptensor(shape=(2, 2, 2))
>>> S1[0, 0, 0] = 1
>>> ST1 = S1.to_sptenmat(np.array([0]))
>>> +ST1  
sptenmat corresponding to a sptensor of shape (2, 2, 2) with 1 nonzeros
rdims = [ 0 ] (modes of sptensor corresponding to rows)
cdims = [ 1, 2 ] (modes of sptensor corresponding to columns)
    [0, 0] = 1.0
__neg__()[source]

Unary minus operator (-).

Examples

>>> S1 = ttb.sptensor(shape=(2, 2, 2))
>>> S1[0, 0, 0] = 1
>>> ST1 = S1.to_sptenmat(np.array([0]))
>>> -ST1  
sptenmat corresponding to a sptensor of shape (2, 2, 2) with 1 nonzeros
rdims = [ 0 ] (modes of sptensor corresponding to rows)
cdims = [ 1, 2 ] (modes of sptensor corresponding to columns)
    [0, 0] = -1.0
__setitem__(key, value)[source]

Subscripted assignment for the pyttb.sptenmat.

Examples

Create an empty pyttb.sptenmat.

>>> ST = ttb.sptenmat(rdims=np.array([0]), tshape=(2, 2, 2))
>>> ST  
sptenmat corresponding to a sptensor of shape (2, 4) with 0 nonzeros
rdims = [ 0 ] (modes of sptensor corresponding to rows)
cdims = [ 1, 2 ] (modes of sptensor corresponding to columns)

Insert a new value into it.

>>> ST[0, 0] = 1.0
>>> ST  
sptenmat corresponding to a sptensor of shape (2, 2, 2) with 1 nonzeros
rdims = [ 0 ] (modes of sptensor corresponding to rows)
cdims = [ 1, 2 ] (modes of sptensor corresponding to columns)
    [0, 0] = 1.0

Update an existing value in it.

>>> ST[0, 0] = 2.0
>>> ST  
sptenmat corresponding to a sptensor of shape (2, 2, 2) with 1 nonzeros
rdims = [ 0 ] (modes of sptensor corresponding to rows)
cdims = [ 1, 2 ] (modes of sptensor corresponding to columns)
    [0, 0] = 2.0
__repr__()[source]

String representation of a pyttb.sptenmat.

Examples

>>> ttb.sptenmat()  
sptenmat corresponding to a sptensor of shape () with 0 nonzeros
rdims = [  ] (modes of sptensor corresponding to rows)
cdims = [  ] (modes of sptensor corresponding to columns)
>>> S1 = ttb.sptensor(shape=(2, 2, 2))
>>> S1[0, 0, 0] = 1
>>> ST1 = S1.to_sptenmat(np.array([0]))
>>> ST1  
sptenmat corresponding to a sptensor of shape (2, 2, 2) with 1 nonzeros
rdims = [ 0 ] (modes of sptensor corresponding to rows)
cdims = [ 1, 2 ] (modes of sptensor corresponding to columns)
    [0, 0] = 1.0
Returns:

str

Contains the shape, row indices (rindices), column indices (cindices)

and data as strings on different lines.

__str__()

String representation of a pyttb.sptenmat.

Examples

>>> ttb.sptenmat()  
sptenmat corresponding to a sptensor of shape () with 0 nonzeros
rdims = [  ] (modes of sptensor corresponding to rows)
cdims = [  ] (modes of sptensor corresponding to columns)
>>> S1 = ttb.sptensor(shape=(2, 2, 2))
>>> S1[0, 0, 0] = 1
>>> ST1 = S1.to_sptenmat(np.array([0]))
>>> ST1  
sptenmat corresponding to a sptensor of shape (2, 2, 2) with 1 nonzeros
rdims = [ 0 ] (modes of sptensor corresponding to rows)
cdims = [ 1, 2 ] (modes of sptensor corresponding to columns)
    [0, 0] = 1.0
Returns:

str

Contains the shape, row indices (rindices), column indices (cindices)

and data as strings on different lines.