pyttb.sptenmat

class pyttb.sptenmat(subs: Optional[ndarray] = None, vals: Optional[ndarray] = None, rdims: Optional[ndarray] = None, cdims: Optional[ndarray] = None, tshape: Tuple[int, ...] = ())[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).

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.

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: Union[coo_matrix, ndarray], rdims: Optional[ndarray] = None, cdims: Optional[ndarray] = 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

property shape: Tuple[int, ...]

Return the shape of a sptenmat

double() coo_matrix[source]

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

full() tenmat[source]

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

property nnz: int

Number of nonzero values in the pyttb.sptenmat.

norm() floating[source]

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

isequal(other: sptenmat) bool[source]

Exact equality for pyttb.sptenmat

__pos__()[source]

Unary plus operator (+).

__neg__()[source]

Unary minus operator (-).

__setitem__(key, value)[source]

Subscripted assignment for the pyttb.sptenmat.

__repr__()[source]

String representation of a sptenmat.

Returns:

str

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

and data as strings on different lines.

__str__()

String representation of a sptenmat.

Returns:

str

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

and data as strings on different lines.