pyttb.tenmat
- class pyttb.tenmat(data: ndarray | None = None, rdims: ndarray | None = None, cdims: ndarray | None = None, tshape: int | Iterable[int] | None = None, copy: bool = True)[source]
Bases:
object
Store tensor as a matrix.
Construct a
pyttb.tenmat
from explicit components.If you already have a tensor see
pyttb.tensor.to_tenmat()
.- Parameters:
data – Flattened tensor data.
rdims – Which dimensions of original tensor map to rows.
cdims – Which dimensions of original tensor map to columns.
tshape – Original tensor shape.
copy – Whether to make a copy of provided data or just reference it.
Examples
Create an empty
pyttb.tenmat
.>>> ttb.tenmat() matrix corresponding to a tensor of shape () with order F rindices = [ ] (modes of tensor corresponding to rows) cindices = [ ] (modes of tensor corresponding to columns) data = []
Create tensor shaped data.
>>> tshape = (2, 2, 2) >>> data = np.reshape(np.arange(prod(tshape), dtype=np.double), tshape) >>> data array([[[0., 1.], [2., 3.]], [[4., 5.], [6., 7.]]])
Manually matrize the tensor.
>>> flat_data = np.reshape(data, (2, 4), order="F") >>> flat_data array([[0., 2., 1., 3.], [4., 6., 5., 7.]])
Encode matrication into
pyttb.tenmat
.>>> tm = ttb.tenmat(flat_data, rdims=np.array([0]), tshape=tshape)
Extract original tensor shaped data.
>>> tm.to_tensor().double() array([[[0., 1.], [2., 3.]], [[4., 5.], [6., 7.]]])
- property order: Literal['F']
Return the data layout of the underlying storage.
- copy() tenmat [source]
Return a deep copy of the
pyttb.tenmat
.Examples
Create a
pyttb.tenmat
(TM1) and make a deep copy. Verify the deep copy (TM3) is not just a reference (like TM2) to the original.>>> T1 = ttb.tensor(np.ones((3, 2))) >>> TM1 = T1.to_tenmat(np.array([0])) >>> TM2 = TM1 >>> TM3 = TM1.copy() >>> TM1[0, 0] = 3
# Item to convert numpy boolean to python boolena for nicer printing
>>> (TM1[0, 0] == TM2[0, 0]).item() True >>> (TM1[0, 0] == TM3[0, 0]).item() False
- to_tensor(copy: bool = True) tensor [source]
Return
pyttb.tenmat
data as apyttb.tensor
.- Parameters:
copy – Whether to make a copy of provided data or just reference it.
Examples
Create tensor shaped data.
>>> tshape = (2, 2, 2) >>> data = np.reshape(np.arange(np.prod(tshape), dtype=np.double), tshape) >>> data array([[[0., 1.], [2., 3.]], [[4., 5.], [6., 7.]]])
Manually matrize the tensor.
>>> flat_data = np.reshape(data, (2, 4), order="F") >>> flat_data array([[0., 2., 1., 3.], [4., 6., 5., 7.]])
Encode matrication into
pyttb.tenmat
.>>> tm = ttb.tenmat(flat_data, rdims=np.array([0]), tshape=tshape)
Extract original tensor shaped data.
>>> tm.to_tensor() tensor of shape (2, 2, 2) with order F data[:, :, 0] = [[0. 2.] [4. 6.]] data[:, :, 1] = [[1. 3.] [5. 7.]]
- ctranspose() tenmat [source]
Complex conjugate transpose for
pyttb.tenmat
.Examples
Create
pyttb.tensor
then convert topyttb.tenmat
.>>> T = ttb.tenones((2, 2, 2)) >>> TM = T.to_tenmat(rdims=np.array([0])) >>> TM matrix corresponding to a tensor of shape (2, 2, 2) with order F rindices = [ 0 ] (modes of tensor corresponding to rows) cindices = [ 1, 2 ] (modes of tensor corresponding to columns) data[:, :] = [[1. 1. 1. 1.] [1. 1. 1. 1.]] >>> TM.ctranspose() matrix corresponding to a tensor of shape (2, 2, 2) with order F rindices = [ 1, 2 ] (modes of tensor corresponding to rows) cindices = [ 0 ] (modes of tensor corresponding to columns) data[:, :] = [[1. 1.] [1. 1.] [1. 1.] [1. 1.]]
- double() ndarray [source]
Convert a
pyttb.tenmat
to an array of doubles.Examples
>>> T = ttb.tenones((2, 2, 2)) >>> TM = T.to_tenmat(rdims=np.array([0])) >>> TM matrix corresponding to a tensor of shape (2, 2, 2) with order F rindices = [ 0 ] (modes of tensor corresponding to rows) cindices = [ 1, 2 ] (modes of tensor corresponding to columns) data[:, :] = [[1. 1. 1. 1.] [1. 1. 1. 1.]] >>> TM.double() array([[1., 1., 1., 1.], [1., 1., 1., 1.]])
- Returns:
Copy of tenmat data.
- property ndims: int
Return the number of dimensions of a
pyttb.tenmat
.Examples
>>> TM = ttb.tenmat() # empty tenmat >>> TM.ndims 0
>>> TM = ttb.tenones((2, 2, 2)).to_tenmat(np.array([0])) >>> TM.ndims 2
- norm() float [source]
Frobenius norm of a
pyttb.tenmat
.Examples
>>> T = ttb.tenones((2, 2, 2)) >>> TM = T.to_tenmat(rdims=np.array([0])) >>> TM matrix corresponding to a tensor of shape (2, 2, 2) with order F rindices = [ 0 ] (modes of tensor corresponding to rows) cindices = [ 1, 2 ] (modes of tensor corresponding to columns) data[:, :] = [[1. 1. 1. 1.] [1. 1. 1. 1.]] >>> TM.norm() 2.82...
- property shape: Tuple[int, ...]
Return the shape of a
pyttb.tenmat
.Examples
>>> TM = ttb.tenmat() # empty tenmat >>> TM.shape ()
>>> TM = ttb.tenones((2, 2, 2)).to_tenmat(np.array([0])) >>> TM.shape (2, 4)
- isequal(other: tenmat) bool [source]
Exact equality for
pyttb.tenmat
.Examples
>>> TM1 = ttb.tenmat() # empty tenmat >>> TM2 = ttb.tenones((2, 2, 2)).to_tenmat(np.array([0])) >>> TM1.isequal(TM2) False >>> TM1.isequal(TM1) True
- __setitem__(key, value)[source]
Subscripted assignment for a
pyttb.tenmat
.Examples
>>> TM = ttb.tenones((2, 2, 2)).to_tenmat(np.array([0])) >>> TM matrix corresponding to a tensor of shape (2, 2, 2) with order F rindices = [ 0 ] (modes of tensor corresponding to rows) cindices = [ 1, 2 ] (modes of tensor corresponding to columns) data[:, :] = [[1. 1. 1. 1.] [1. 1. 1. 1.]] >>> TM[0, 0] = 2.0 >>> TM matrix corresponding to a tensor of shape (2, 2, 2) with order F rindices = [ 0 ] (modes of tensor corresponding to rows) cindices = [ 1, 2 ] (modes of tensor corresponding to columns) data[:, :] = [[2. 1. 1. 1.] [1. 1. 1. 1.]]
- __getitem__(item)[source]
Subscripted reference for
pyttb.tenmat
.Examples
>>> TM = ttb.tenones((2, 2, 2)).to_tenmat(np.array([0])) >>> print(TM[0, 0]) 1.0
- Returns:
numpy.ndarray
, float, int
- __mul__(other)[source]
Multiplies two
pyttb.tenmat
objects.- Parameters:
other (
pyttb.tenmat
)
Examples
>>> TM = ttb.tenones((2, 2)).to_tenmat(np.array([0])) >>> TM * TM matrix corresponding to a tensor of shape (2, 2) with order F rindices = [ 0 ] (modes of tensor corresponding to rows) cindices = [ 1 ] (modes of tensor corresponding to columns) data[:, :] = [[2. 2.] [2. 2.]]
- Returns:
- __rmul__(other)[source]
Multiplies two
pyttb.tenmat
objects.- Parameters:
other (
pyttb.tenmat
)
Examples
>>> TM = ttb.tenones((2, 2)).to_tenmat(np.array([0])) >>> TM * TM matrix corresponding to a tensor of shape (2, 2) with order F rindices = [ 0 ] (modes of tensor corresponding to rows) cindices = [ 1 ] (modes of tensor corresponding to columns) data[:, :] = [[2. 2.] [2. 2.]]
- Returns:
- __add__(other)[source]
Binary addition (+) for
pyttb.tenmat
.- Parameters:
other (
pyttb.tenmat
, float, int)
Examples
>>> TM = ttb.tenones((2, 2)).to_tenmat(np.array([0])) >>> TM + TM matrix corresponding to a tensor of shape (2, 2) with order F rindices = [ 0 ] (modes of tensor corresponding to rows) cindices = [ 1 ] (modes of tensor corresponding to columns) data[:, :] = [[2. 2.] [2. 2.]] >>> TM + 1.0 matrix corresponding to a tensor of shape (2, 2) with order F rindices = [ 0 ] (modes of tensor corresponding to rows) cindices = [ 1 ] (modes of tensor corresponding to columns) data[:, :] = [[2. 2.] [2. 2.]]
- Returns:
- __radd__(other)[source]
Right binary addition (+) for
pyttb.tenmat
.- Parameters:
other (
pyttb.tenmat
, float, int)
Examples
>>> TM = ttb.tenones((2, 2)).to_tenmat(np.array([0])) >>> 1.0 + TM matrix corresponding to a tensor of shape (2, 2) with order F rindices = [ 0 ] (modes of tensor corresponding to rows) cindices = [ 1 ] (modes of tensor corresponding to columns) data[:, :] = [[2. 2.] [2. 2.]]
- Returns:
- __sub__(other)[source]
Binary subtraction (-) for
pyttb.tenmat
.- Parameters:
other (
pyttb.tenmat
, float, int)
Examples
>>> TM = ttb.tenones((2, 2)).to_tenmat(np.array([0])) >>> TM - TM matrix corresponding to a tensor of shape (2, 2) with order F rindices = [ 0 ] (modes of tensor corresponding to rows) cindices = [ 1 ] (modes of tensor corresponding to columns) data[:, :] = [[0. 0.] [0. 0.]] >>> TM - 1.0 matrix corresponding to a tensor of shape (2, 2) with order F rindices = [ 0 ] (modes of tensor corresponding to rows) cindices = [ 1 ] (modes of tensor corresponding to columns) data[:, :] = [[0. 0.] [0. 0.]]
- Returns:
- __rsub__(other)[source]
Right binary subtraction (-) for
pyttb.tenmat
.- Parameters:
other (
pyttb.tenmat
, float, int)
Examples
>>> TM = ttb.tenones((2, 2)).to_tenmat(np.array([0])) >>> 1.0 - TM matrix corresponding to a tensor of shape (2, 2) with order F rindices = [ 0 ] (modes of tensor corresponding to rows) cindices = [ 1 ] (modes of tensor corresponding to columns) data[:, :] = [[0. 0.] [0. 0.]]
- Returns:
- __pos__()[source]
Unary plus (+) for
pyttb.tenmat
.Examples
>>> TM = ttb.tenones((2, 2)).to_tenmat(np.array([0])) >>> +TM matrix corresponding to a tensor of shape (2, 2) with order F rindices = [ 0 ] (modes of tensor corresponding to rows) cindices = [ 1 ] (modes of tensor corresponding to columns) data[:, :] = [[1. 1.] [1. 1.]]
- Returns:
pyttb.tenmat
– copy of tenmat
- __neg__()[source]
Unary minus (-) for
pyttb.tenmat
.Examples
>>> TM = ttb.tenones((2, 2)).to_tenmat(np.array([0])) >>> -TM matrix corresponding to a tensor of shape (2, 2) with order F rindices = [ 0 ] (modes of tensor corresponding to rows) cindices = [ 1 ] (modes of tensor corresponding to columns) data[:, :] = [[-1. -1.] [-1. -1.]]
- Returns:
pyttb.tenmat
– Copy of original tenmat with negated data.
- __repr__()[source]
Return string representation of a
pyttb.tenmat
.Examples
Print an empty
pyttb.tenmat
.>>> ttb.tenmat() matrix corresponding to a tensor of shape () with order F rindices = [ ] (modes of tensor corresponding to rows) cindices = [ ] (modes of tensor corresponding to columns) data = []
Print a non-empty
pyttb.tenmat
.>>> TM = ttb.tenones((2, 2)).to_tenmat(np.array([0])) >>> TM matrix corresponding to a tensor of shape (2, 2) with order F rindices = [ 0 ] (modes of tensor corresponding to rows) cindices = [ 1 ] (modes of tensor corresponding to columns) data[:, :] = [[1. 1.] [1. 1.]]
- Returns:
str – Contains the shape, row indices (rindices), column indices (cindices) and data as strings on different lines.
- __str__()
Return string representation of a
pyttb.tenmat
.Examples
Print an empty
pyttb.tenmat
.>>> ttb.tenmat() matrix corresponding to a tensor of shape () with order F rindices = [ ] (modes of tensor corresponding to rows) cindices = [ ] (modes of tensor corresponding to columns) data = []
Print a non-empty
pyttb.tenmat
.>>> TM = ttb.tenones((2, 2)).to_tenmat(np.array([0])) >>> TM matrix corresponding to a tensor of shape (2, 2) with order F rindices = [ 0 ] (modes of tensor corresponding to rows) cindices = [ 1 ] (modes of tensor corresponding to columns) data[:, :] = [[1. 1.] [1. 1.]]
- Returns:
str – Contains the shape, row indices (rindices), column indices (cindices) and data as strings on different lines.