pyttb.tenmat
- class pyttb.tenmat(data: Optional[ndarray] = None, rdims: Optional[ndarray] = None, cdims: Optional[ndarray] = None, tshape: Optional[Tuple[int, ...]] = None)[source]
Bases:
objectTENMAT Store tensor as a matrix.
Construct a
pyttb.tenmatfrom explicit components. If you already have a tensor seepyttb.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.
Examples
Create an empty
pyttb.tenmat.>>> ttb.tenmat() matrix corresponding to a tensor of shape () 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(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().double() array([[[0., 1.], [2., 3.]], [[4., 5.], [6., 7.]]])
- 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() tensor[source]
Return copy of tenmat data as a tensor.
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) data[0, :, :] = [[0. 1.] [2. 3.]] data[1, :, :] = [[4. 5.] [6. 7.]]
- ctranspose() tenmat[source]
Complex conjugate transpose for tenmat.
Examples
Create
pyttb.tensorthen 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) 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) 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 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) 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.
- norm() float[source]
Frobenius norm of a 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) 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...
- isequal(other: tenmat) bool[source]
Exact equality for
pyttb.tenmat
- __getitem__(item)[source]
SUBSREF Subscripted reference for tenmat.
- Parameters:
item
- Returns:
numpy.ndarray, float, int
- __mul__(other)[source]
Multiplies two tenmat objects.
- Parameters:
other (
pyttb.tenmat)- Returns:
- __rmul__(other)[source]
Multiplies two tenmat objects.
- Parameters:
other (
pyttb.tenmat)- Returns:
- __add__(other)[source]
Binary addition (+) for tenmats
- Parameters:
other (
pyttb.tenmat, float, int)- Returns:
- __radd__(other)[source]
Reverse binary addition (+) for tenmats
- Parameters:
other (
pyttb.tenmat, float, int)- Returns:
- __sub__(other)[source]
Binary subtraction (-) for tenmats
- Parameters:
other (
pyttb.tenmat, float, int)- Returns:
- __rsub__(other)[source]
Reverse binary subtraction (-) for tenmats
- Parameters:
other (
pyttb.tenmat, float, int)- Returns:
- __pos__()[source]
Unary plus (+) for tenmats
- Returns:
pyttb.tenmat– copy of tenmat
- __neg__()[source]
Unary minus (-) for tenmats
- Returns:
pyttb.tenmat– copy of tenmat
- __repr__()[source]
String representation of a tenmat.
- Returns:
str – Contains the shape, row indices (rindices), column indices (cindices) and data as strings on different lines.
- __str__()
String representation of a tenmat.
- Returns:
str – Contains the shape, row indices (rindices), column indices (cindices) and data as strings on different lines.