tensap.tensor_algebra.tensors package¶
Submodules¶
tensap.tensor_algebra.tensors.canonical_tensor module¶
Module canonical_tensor.
-
class
tensap.tensor_algebra.tensors.canonical_tensor.
CanonicalTensor
(space, core)¶ Bases:
object
Class CanonicalTensor.
- Attributes
- coretensap.DiagonalTensor
The core of the canonical tensor.
- spacenumpy.ndarray
The tensor space.
- orderint
The order of the tensor.
- shapenumpy.ndarray
The shape of the tensor.
- is_orthbool
Boolean indicating, if is_orth = True, the dimension mu for which the mu-matricization of the tensor is orthogonal.
Methods
create
(generator, rank, shape)Create a FullTensor of rank rank and shape shape using a given generator.
dot
(tensor_2)Return the inner product of two tensors.
eval_diag
([dims])Extract the diagonal of the tensor.
full
()Convert the object to a tensap.FullTensor.
norm
([matrix])Compute the canonical norm of the CanonicalTensor.
numpy
()Convert the CanonicalTensor to a numpy.ndarray.
ones
(rank, shape)Create a FullTensor of rank rank and shape shape with entries equal to 1.
orth
()Return an orthonormalized representation of the tensor.
parameter_gradient_eval_diag
(mu[, matrices])Compute the diagonal of the gradient of the tensor with respect to a given parameter.
rand
(rank, shape)Create a FullTensor of rank rank and shape shape with i.i.d.
randn
(rank, shape)Create a FullTensor of rank rank and shape shape with i.i.d.
Return the representation rank of the tensor.
Return the sparse storage complexity of the CanonicalTensor.
storage
()Return the storage complexity of the CanonicalTensor.
tensor_matrix_product
(matrices[, dims])Contract a tensor with matrices.
tensor_matrix_product_eval_diag
(matrices[, dims])Evaluate the diagonal of a tensor obtained by contraction with matrices.
tree_based_tensor
(tree[, is_active_node])Convert a CanonicalTensor to a tensap.TreeBasedTensor with given dimension tree and active nodes.
zeros
(rank, shape)Create a FullTensor of rank rank and shape shape with entries equal to 0.
-
static
create
(generator, rank, shape)¶ Create a FullTensor of rank rank and shape shape using a given generator.
- Parameters
- generatorfunction
Function generating a numpy.ndarray, given a shape.
- rankint
The rank of the tensor.
- shapenumpy.ndarray or list
The shape of the tensor.
- Returns
- CanonicalTensor
The created tensor.
-
dot
(tensor_2)¶ Return the inner product of two tensors.
- Parameters
- tensor2CanonicalTensor
The second tensor of the inner products.
- Returns
- numpy.float
The inner product of the two tensors.
-
eval_diag
(dims=None)¶ Extract the diagonal of the tensor.
The tensor must be such that self.shape[mu] = n for all mu (in dims if provided).
- Parameters
- dimslist of numpy.ndarray, optional
The dimensions associated with the indices of the diagonal. The default is None,indicating that the indices refer to all the dimensions.
- Returns
- dataCanonicalTensor or tensap.FullTensor
The evaluations of the diagonal of the tensor.
-
full
()¶ Convert the object to a tensap.FullTensor.
- Returns
- tensap.FullTensor
The canonical tensor as a tensap.FullTensor.
-
property
ndim
¶ Compute the order of the tensor. Equivalent to self.order.
- Returns
- int
The order of the tensor.
-
norm
(matrix=None)¶ Compute the canonical norm of the CanonicalTensor.
- Returns
- numpy.float
The norm of the tensor.
-
numpy
()¶ Convert the CanonicalTensor to a numpy.ndarray.
- Returns
- numpy.ndarray
The CanonicalTensor as a numpy.ndarray.
-
static
ones
(rank, shape)¶ Create a FullTensor of rank rank and shape shape with entries equal to 1.
- Parameters
- rankint
The rank of the tensor.
- shapenumpy.ndarray or list
The shape of the tensor.
- Returns
- CanonicalTensor
The created tensor.
-
orth
()¶ Return an orthonormalized representation of the tensor.
- Returns
- outCanonicalTensor
The orthonormalized representation of the tensor.
-
parameter_gradient_eval_diag
(mu, matrices=None)¶ Compute the diagonal of the gradient of the tensor with respect to a given parameter.
- Parameters
- muint
Index of the parameter.
- matriceslist or numpy.array, optional
Matrices with which to compute outer_product_eval_diag if alpha is associated with some dimensions. Useful for evaluation the gradient of a tensap.FunctionalTensor. The default is None, indicating identity matrices.
- Returns
- outtensap.FullTensor
The diagonal of the gradient of the tensor with respect to the parameter with index mu.
-
static
rand
(rank, shape)¶ Create a FullTensor of rank rank and shape shape with i.i.d. entries drawn according to the uniform distribution on [0, 1].
- Parameters
- rankint
The rank of the tensor.
- shapenumpy.ndarray or list
The shape of the tensor.
- Returns
- CanonicalTensor
The created tensor.
-
static
randn
(rank, shape)¶ Create a FullTensor of rank rank and shape shape with i.i.d. entries drawn according to the standard gaussian distribution.
- Parameters
- rankint
The rank of the tensor.
- shapenumpy.ndarray or list
The shape of the tensor.
- Returns
- CanonicalTensor
The created tensor.
-
representation_rank
()¶ Return the representation rank of the tensor.
- Returns
- int
The representation rank of the tensor.
-
sparse_storage
()¶ Return the sparse storage complexity of the CanonicalTensor.
- Returns
- int
The sparse storage complexity of the CanonicalTensor.
-
storage
()¶ Return the storage complexity of the CanonicalTensor.
- Returns
- int
The storage complexity of the CanonicalTensor.
-
tensor_matrix_product
(matrices, dims=None)¶ Contract a tensor with matrices.
The second dimension of the matrix matrices[k] is contracted with the k-th dimension of self, with the indices k given in dims (if provided).
- Parameters
- matricesnumpy.ndarray or list of numpy.ndarray
The matrices to use in the product.
- dimslist or numpy.ndarray, optional
Indices of the contractions. The default is None, indicating all the dimensions.
- Returns
- CanonicalTensor
The tensor after the contractions with the matrices.
-
tensor_matrix_product_eval_diag
(matrices, dims=None)¶ Evaluate the diagonal of a tensor obtained by contraction with matrices.
Provides the diagonal of the tensor obtained by contracting the tensor with matrices H[k] along dimensions dims(k)+1, for k = 0, …, dims.size-1.
- Parameters
- matriceslist
The matrices to use in the product.
- dimslist or numpy.ndarray, optional
Indices of the contractions. The default is None, indicating all the dimensions.
- Returns
- outCanonicalTensor or tensap.FullTensor
The diagonal of the contractions of the tensor with the matrices.
-
tree_based_tensor
(tree, is_active_node=None)¶ Convert a CanonicalTensor to a tensap.TreeBasedTensor with given dimension tree and active nodes.
- Parameters
- treetensap.DimensionTree
The dimension tree.
- is_active_nodelist or numpy.ndarray, optional
Booleans indicating if the nodes are active. The default is None, settings all the nodes active.
- Returns
- outtensap.TreeBasedTensor
The canonical tensor converted into a tree-based tensor.
-
static
zeros
(rank, shape)¶ Create a FullTensor of rank rank and shape shape with entries equal to 0.
- Parameters
- rankint
The rank of the tensor.
- shapenumpy.ndarray or list
The shape of the tensor.
- Returns
- CanonicalTensor
The created tensor.
tensap.tensor_algebra.tensors.diagonal_tensor module¶
Module diagonal_tensor.
-
class
tensap.tensor_algebra.tensors.diagonal_tensor.
DiagonalTensor
(data, order=None)¶ Bases:
object
Class DiagonalTensor.
- Attributes
- datanumpy.ndarray
The diagonal entries of the tensor.
- orderint
The order of the tensor.
- shapenumpy.ndarray
The shape of the tensor.
- is_orthbool
Boolean indicating if the representation of the tensor is orthogonal (i.e. one mu-matricization is orthogonal).
Methods
cat
(y)Concatenate the tensors.
create
(generator, rank, order)Create a DiagonalTensor of rank rank and order order using a given generator.
dot
(y)Return the inner product of two tensors.
dot_with_rank_one_metric
(y, matrix)Compute the weighted inner product of two tensors.
eval_at_indices
(ind[, dims])Evaluate the tensor at indices.
eval_diag
([dims])Extract the diagonal of the tensor.
full
()Convert the DiagonalTensor to a tensap.FullTensor.
itranspose
(dims)Return the inverse transpose (permutation) of the dimensions of the tensor.
kron
(y)Kronecker product of tensors.
norm
()Compute the canonical norm of the DiagonalTensor.
numpy
()Convert the DiagonalTensor to a numpy.ndarray.
ones
(rank, order)Create a DiagonalTensor of rank rank and order order with with entries equal to 1.
orth
()Placeholder method returning a copy of self.
rand
(rank, order)Create a DiagonalTensor of rank rank and order order with i.i.d.
randn
(rank, order)Create a DiagonalTensor of rank rank and order order with i.i.d.
reshape
(shape)Reshape the tensor.
sparse
()Convert the DiagonalTensor to a tensap.SparseTensor.
Return the sparse storage complexity of the DiagonalTensor.
storage
()Return the storage complexity of the DiagonalTensor.
sub_tensor
(*args)Extract a subtensor of the tensor.
tensor_diagonal_matrix_product
(matrices)Contract a FullTensor with matrices built from their diagonals.
tensor_matrix_product
(matrices[, dims])Contract a tensor with matrices.
tensor_matrix_product_eval_diag
(matrices[, dims])Evaluate the diagonal of a tensor obtained by contraction with matrices.
tensor_vector_product
(vectors)Compute the contraction of the tensor with vectors.
tensordot
(y, dims1[, dims2])Contract two tensors along specified dimensions.
Particular type of contraction.
transpose
(dims)Transpose (permute) the dimensions of the tensor.
tree_based_tensor
([tree, is_active_node])Convert the tensap.DiagonalTensor into a tensap.TreeBasedTensor.
Update the attribute shape of self if data or order have been modified.
zeros
(rank, order)Create a DiagonalTensor of rank rank and order order with with entries equal to 0.
-
cat
(y)¶ Concatenate the tensors.
Concatenates self and y in a tensor z such that: z(i_1 ,…, i_d) = x(i_1, …, i_d) if i_k <= sz[k]-1 for k in dims, z(i_1, …, i_d) = y(i_1-sz[0], …, i_d-sz[d-1]) if i_k >= sz[k] for k in dims, z(i_1, …, i_d) = 0 otherwise, with sz = self.shape and dims = range(self.order).
- Parameters
- yDiagonalTensor
The second tensor to be concatenaed.
- Returns
- ——-
- DiagonalTensor
The concatenated tensors.
-
static
create
(generator, rank, order)¶ Create a DiagonalTensor of rank rank and order order using a given generator.
- Parameters
- generatorfunction
Function generating an array, given a rank.
- rankint
The rank of the tensor.
- orderint
The order of the tensor.
- Returns
- DiagonalTensor
The created tensor.
-
dot
(y)¶ Return the inner product of two tensors.
- Parameters
- tensor2DiagonalTensor
The second tensor of the inner products.
- Returns
- numpy.float
The inner product of the two tensors.
-
dot_with_rank_one_metric
(y, matrix)¶ Compute the weighted inner product of two tensors.
Compute the weighted canonical inner product of self and y, where the inner product related to dimension k is weighted by matrix[k]. It is equivalent to self.dot(y.tensor_matrix_product(matrix)), but can be much faster.
- Parameters
- yDiagonalTensor
The second tensor of the inner product.
- matrixlist or numpy.ndarray or FullTensor
The weight matrix.
- Returns
- numpy.float
The weighted inner product.
-
eval_at_indices
(ind, dims=None)¶ Evaluate the tensor at indices.
If dims is None, return s(k) = x(indices(k, 1), indices(k, 2), …, indices(k, d)), 1 <= k <= self.shape[0].
If dims is not None, return a partial evaluation: up to a permutation (placing the dimensions dims on the left), return s(k, i_1, …, i_d’) = x(indices(k, 1), indices(k, 2), …, indices(k, M), i_1, …, i_d’), 1 <= k <= self.shape[0], with M = dims.size and d’ = self.order - M.
- Parameters
- indiceslist of numpy.ndarray
The indices of the tensor.
- dimslist of numpy.ndarray, optional
The dimensions associated with the indices. The default is None, indicating that indices refers to all the dimensions.
- Returns
- evaluationsnumpy.ndarray or DiagonalTensor
The evaluations of the tensor.
-
eval_diag
(dims=None)¶ Extract the diagonal of the tensor.
- Parameters
- dimslist of numpy.ndarray, optional
The dimensions associated with the indices of the diagonal. The default is None,indicating that the indices refer to all the dimensions.
- Returns
- dataDiagonalTensor or numpy.ndarray
The evaluations of the diagonal of the tensor.
-
full
()¶ Convert the DiagonalTensor to a tensap.FullTensor.
- Returns
- tensap.FullTensor
The DiagonalTensor as a tensap.FullTensor.
-
itranspose
(dims)¶ Return the inverse transpose (permutation) of the dimensions of the tensor.
- Parameters
- dimslist or numpy.ndarray
The original transpose (permutation) indices.
- Returns
- DiagonalTensor
The transposed (permuted) tensor.
-
kron
(y)¶ Kronecker product of tensors.
Similar to numpy.kron but for arbitrary tensors.
- Parameters
- tensor2DiagonalTensor
The second tensor of the Kronecker product.
- Returns
- DiagonalTensor
The tensor resulting from the Kronecker product.
-
property
ndim
¶ Compute the order of the tensor. Equivalent to self.order.
- Returns
- int
The order of the tensor.
-
norm
()¶ Compute the canonical norm of the DiagonalTensor.
- Returns
- numpy.float
The norm of the tensor.
-
numpy
()¶ Convert the DiagonalTensor to a numpy.ndarray.
- Returns
- numpy.ndarray
The DiagonalTensor as a numpy.ndarray.
-
static
ones
(rank, order)¶ Create a DiagonalTensor of rank rank and order order with with entries equal to 1.
- Parameters
- rankint
The rank of the tensor.
- orderint
The order of the tensor.
- Returns
- DiagonalTensor
The created tensor.
-
orth
()¶ Placeholder method returning a copy of self.
- Returns
- DiagonalTensor
A copy of self.
-
static
rand
(rank, order)¶ Create a DiagonalTensor of rank rank and order order with i.i.d. entries drawn according to the uniform distribution on [0, 1].
- Parameters
- rankint
The rank of the tensor.
- orderint
The order of the tensor.
- Returns
- DiagonalTensor
The created tensor.
-
static
randn
(rank, order)¶ Create a DiagonalTensor of rank rank and order order with i.i.d. entries drawn according to the standard gaussian distribution.
- Parameters
- rankint
The rank of the tensor.
- orderint
The order of the tensor.
- Returns
- DiagonalTensor
The created tensor.
-
reshape
(shape)¶ Reshape the tensor. The method has no effet.
- Parameters
- shapelist or numpy.ndarray
The new shape of the tensor.
- Returns
- tensorDiagonalTensor
The reshaped tensor.
-
sparse
()¶ Convert the DiagonalTensor to a tensap.SparseTensor.
- Returns
- tensap.SparseTensor
The DiagonalTensor as a tensap.SparseTensor.
-
sparse_storage
()¶ Return the sparse storage complexity of the DiagonalTensor.
- Returns
- int
The sparse storage complexity of the DiagonalTensor.
-
storage
()¶ Return the storage complexity of the DiagonalTensor.
- Returns
- int
The storage complexity of the DiagonalTensor.
-
sub_tensor
(*args)¶ Extract a subtensor of the tensor.
See also tensap.FullTensor.sub_tensor.
- Parameters
- *indiceslist
The indices to extract in each dimension. ‘:’ indicates all the indices.
- Returns
- FullTensor
The subtensor.
-
tensor_diagonal_matrix_product
(matrices)¶ Contract a FullTensor with matrices built from their diagonals.
The second dimension of the matrix matrices[k] is contracted with the k-th dimension of self.
- Parameters
- matricesnumpy.ndarray or list of numpy.ndarray
The diagonals of the matrices to use in the product.
- Returns
- DiagonalTensor
The tensor after the contractions with the matrices.
-
tensor_matrix_product
(matrices, dims=None)¶ Contract a tensor with matrices.
The second dimension of the matrix matrices[k] is contracted with the k-th dimension of self, with the indices k given in dims (if provided).
See also tensap.FullTensor.tensor_matrix_product.
- Parameters
- matricesnumpy.ndarray or list of numpy.ndarray
The matrices to use in the product.
- dimslist or numpy.ndarray, optional
Indices of the contractions. The default is None, indicating all the dimensions.
- Returns
- FullTensor
The tensor after the contractions with the matrices.
-
tensor_matrix_product_eval_diag
(matrices, dims=None)¶ Evaluate the diagonal of a tensor obtained by contraction with matrices.
Provides the diagonal of the tensor obtained by contracting the tensor with matrices H[k] along dimensions dims(k)+1, for k = 0, …, dims.size-1.
- Parameters
- matriceslist
The matrices to use in the product.
- dimslist or numpy.ndarray, optional
Indices of the contractions. The default is None, indicating all the dimensions.
- Returns
- tensap.DiagonalTensor or numpy.ndarray
The diagonal of the contractions of the tensor with the matrices.
-
tensor_vector_product
(vectors)¶ Compute the contraction of the tensor with vectors.
Compute the contraction of self with each vector contained in the list vectors along all the dimensions. The operation is such that V[k] is contracted with the k-th dimension of self.
- Parameters
- vectorsnumpy.ndarray or list of numpy.ndarray
The vectors to use in the product.
- Returns
- DiagonalTensor
The tensor after the contractions with the vectors.
-
tensordot
(y, dims1, dims2=None)¶ Contract two tensors along specified dimensions.
See also tensap.FullTensor.tensordot.
- Parameters
- tensor2Tensor
The second tensor of the contraction.
- dims1list or int
The dimensions of contractions for the first tensor.
- dims2list or int, optional
The dimensions of contractions for the second tensor. The default is None which indicates, if dims1 = 0, to perform the outer product of the two tensors, similarly to tensorflow.tensordot.
- Returns
- outFullTensor
The resulting tensor.
-
tensordot_matrix_product_except_dim
(y, matrices, dim)¶ Particular type of contraction.
Compute a special contraction of two tensors self, y, a list of matrices matrices and a particular dimension dim. Note that dim must be a scalar, while matrices must be a list array with self.order elements.
- Parameters
- yDiagonalTensor
The second tensor of the contraction.
- matriceslist
The list of matrices of the contraction.
- dimint
The excluded dimension.
- Returns
- numpy.ndarray
The result of the contraction.
-
transpose
(dims)¶ Transpose (permute) the dimensions of the tensor.
- Parameters
- dimslist or numpy.ndarray
The new ordering of the dimensions.
- Returns
- tensorDiagonalTensor
The transposed (permuted) tensor.
-
tree_based_tensor
(tree=None, is_active_node=None)¶ Convert the tensap.DiagonalTensor into a tensap.TreeBasedTensor.
- Parameters
- treetensap.DimensionTree, optional
The tree associated with the tree-based tensor representation. The default is a linear tree.
- is_active_nodelist or numpy.ndarray, optional
List or array of booleans indicating if each node of the tree is active. The default is True for all nodes except the leaves.
- Returns
- tensap.TreeBasedTensor
A tree-based tensor representation of the diagonal tensor.
- Raises
- ValueError
If the internal nodes are not all active.
-
update_attributes
()¶ Update the attribute shape of self if data or order have been modified.
- Returns
- None.
-
static
zeros
(rank, order)¶ Create a DiagonalTensor of rank rank and order order with with entries equal to 0.
- Parameters
- rankint
The rank of the tensor.
- orderint
The order of the tensor.
- Returns
- DiagonalTensor
The created tensor.
tensap.tensor_algebra.tensors.dimension_tree module¶
Module dimension_tree.
-
class
tensap.tensor_algebra.tensors.dimension_tree.
DimensionTree
(dim2ind, adjacency_matrix)¶ Bases:
object
Class DimensionTree.
- Attributes
- adjacency_matrixnumpy.ndarray
Adjacency matrix. The nth row indicates the sons of the (n+1)th node. The nth column indicates the parents of the (n+1)th node.
- arityint
Maximal number of children.
- _child_numbernumpy.ndarray
_child_number[n] = k means that (n+1)th node is the kth child of its parent.
- _childrennumpy.ndarray
_children[:,n] is the set of indices of the children of (n+1)th node.
- dim2indnumpy.ndarray
dim2ind[k] is the index of the node (leaf) corresponding to dimension k.
- dimslist
dims[n] is the set of dimensions associated with node n+1.
- internal_nodesnumpy.ndarray
Indices of internal (non leaf) nodes.
- is_leafnumpy.ndarray
is_leaf[n] = True if the (n+1)th node is a leaf and False otherwise.
- levelnumpy.ndarray
level[n] is the level of the (n+1)th node.
- nb_nodesint
Number of nodes.
- nodes_parent_of_leavesnumpy.ndarray
Indices of nodes which are parents of leaves.
- _parentnumpy.ndarray
_parent[n] is the index of the parent of the (n+1)th node.
- rootnumpy.int64
Index of the root node.
- siblingnumpy.ndarray
sibling[:,n] contains the indices of the children of the parent of the (n+1)th node.
- nodes_indicesnumpy.ndarray
Indices of the nodes of the tree.
- plot_optionsdict
Options for plotting the tree.
Methods
ascendants
(nod)Return the ascendants of a given node.
balanced
(order)Create a balanced dimension tree.
child_number
(nod)Return the child number of nod.
children
(nod)Return the children of a given node.
descendants
(nod)Return the descendants of a given node.
ipermute
(sigma)Inverse permutation of the dimensions of the DimensionTree.
linear
(order)Create a linear dimension tree.
node_with_dims
(dims)Return the index of the node with given set of dimensions.
nodes_with_level
(level)Return the indices of the nodes at a given level.
parent
(nod)Return the parent of a given node.
permute
(sigma)Permute the dimensions of the DimensionTree.
plot
(**args)Plot the tree with the nodes indices.
plot_dims
([nodes])Plot the dimensions associated with the nodes of the tree.
plot_with_labels_at_nodes
(labels[, …])Plot the tree with labels at nodes.
random
(order[, arity])Create a random dimension tree over {1,…,order}.
sub_dimension_tree
(root)Extract a sub dimension tree.
Return the layout of a tree.
trivial
(order)Create a dimension tree with one level.
Update the dimensions of all nodes from the dimensions of the leaves given in T.dim2ind.
-
ascendants
(nod)¶ Return the ascendants of a given node.
- Parameters
- nodint
Node for which to compute the ascendants.
- Returns
- anodlist
List of ascendants of nod.
-
static
balanced
(order)¶ Create a balanced dimension tree.
- Parameters
- orderint
Order of the tensor (dimension).
- Returns
- DimensionTree
Balanced dimension tree.
-
child_number
(nod)¶ Return the child number of nod.
- Parameters
- nodint
Node for which to compute the child number.
- Returns
- int
The child number.
-
children
(nod)¶ Return the children of a given node.
- Parameters
- nodint
Node for which to compute the children.
- Returns
- list
List of children of nod.
-
descendants
(nod)¶ Return the descendants of a given node.
- Parameters
- nodint
Node for which to compute the descendants.
- Returns
- dnodlist
List of descendants of nod.
-
ipermute
(sigma)¶ Inverse permutation of the dimensions of the DimensionTree.
- Parameters
- sigmalist
Indicates the permutation of the dimensions.
- Returns
- DimensionTree
A DimensionTree with permuted dimensions.
-
static
linear
(order)¶ Create a linear dimension tree.
- Parameters
- orderint
Order of the tensor (dimension).
- Returns
- DimensionTree
Linear dimension tree.
-
node_with_dims
(dims)¶ Return the index of the node with given set of dimensions.
Return the index of the node corresponding to dimensions dims or an empty array if no node contains these dimensions.
- Parameters
- dimslist or numpy.ndarray
List of dimensions.
- Returns
- numpy.ndarray
Index of the node with the given set of dimensions dims.
-
nodes_with_level
(level)¶ Return the indices of the nodes at a given level.
- Parameters
- levelint
Level.
- Returns
- numpy.ndarray
Nodes with given level.
-
parent
(nod)¶ Return the parent of a given node.
- Parameters
- nodint
Node for which to compute the parent.
- Returns
- int
The parent of nod.
-
permute
(sigma)¶ Permute the dimensions of the DimensionTree.
- Parameters
- sigmalist
Indicates the permutation of the dimensions.
- Returns
- DimensionTree
A DimensionTree with permuted dimensions.
-
plot
(**args)¶ Plot the tree with the nodes indices.
This method requires the package igraph.
- Parameters
- node_colorstr, optional
Color for the colored nodes. The default is ‘red’.
- colored_nodeslist or numpy.ndarray, optional
Colored nodes. The default is [].
- Returns
- None.
-
plot_dims
(nodes=None, **args)¶ Plot the dimensions associated with the nodes of the tree.
This method requires the package igraph.
- Parameters
- nodeslist or numpy.ndarray, optional
List of leaf nodes for which to display the dimensions. The default is None, the dimensions of all the leaf nodes are displayed.
- Returns
- ——-
- None.
-
plot_with_labels_at_nodes
(labels, node_color='red', colored_nodes=None, title=None)¶ Plot the tree with labels at nodes.
This method requires the package igraph.
- Parameters
- labelslist or numpy.ndarray
Nodes labels.
- node_colorstr, optional
Color for the colored nodes. The default is ‘red’.
- colored_nodeslist or numpy.ndarray, optional
Colored nodes. The default is [].
- titlestr, optional
The title of the plot. The default is None.
- Returns
- None.
-
static
random
(order, arity=2)¶ Create a random dimension tree over {1,…,order}.
If arity is an interval [amin,amin], then the number of children of a node is randomly drawn from the uniform distribution over {amin,…,amax}.
- Parameters
- orderint
Order of the tensor (dimension).
- arityint, list or numpy.ndarray, optional
Arity or interval for the arity. The default is 2.
- Returns
- DimensionTree
Random dimension tree.
-
sub_dimension_tree
(root)¶ Extract a sub dimension tree.
The attribute dim2ind of the sub dimension tree gives the nodes indices corresponding to the dimensions in T.dims[r] (not sorted).
- Parameters
- rootint
Index of the node which is the root of the sub dimension tree.
- Returns
- DimensionTree
Sub dimension tree.
- nodnp.array
Extracted nodes from T.
-
tree_layout
()¶ Return the layout of a tree.
- Returns
- dict
The layout of the tree, used for plotting.
-
static
trivial
(order)¶ Create a dimension tree with one level.
- Parameters
- orderint
Order of the tensor (dimension).
- Returns
- DimensionTree
Trivial dimension tree.
-
update_dims_from_leaves
()¶ Update the dimensions of all nodes from the dimensions of the leaves given in T.dim2ind.
- Returns
- DimensionTree
The DimensionTree object with updated attribute dims.
tensap.tensor_algebra.tensors.full_tensor module¶
Module full_tensor.
-
class
tensap.tensor_algebra.tensors.full_tensor.
FullTensor
(data, order=None, shape=None)¶ Bases:
object
Class FullTensor.
- Attributes
- datanumpy.ndarray
The content of the tensor.
order
intCompute the order of the tensor.
shape
numpy.ndarrayCompute the shape of the tensor
- is_orthbool
Boolean indicating if the representation of the tensor is orthogonal (i.e. one mu-matricization is orthogonal).
- orth_dimbool
Boolean indicating, if is_orth = True, the dimension mu for which the mu-matricization of the tensor is orthogonal.
Methods
alpha_principal_components
(alpha[, parameter])Compute the alpha-principal components of a tensor.
cat
(tensor2[, dims])Concatenate the tensors.
create
(generator, shape)Create a FullTensor of shape shape using a given generator.
diag
(diag, order)Create a diagonal tensor x of order order, such that x[i, …, i] = diag[i] for i = 0, …, diag.size - 1.
dot
(tensor2)Return the inner product of two tensors.
dot_with_rank_one_metric
(tensor2, matrix)Compute the weighted inner product of two tensors.
eval_at_indices
(indices[, dims])Evaluate the tensor at indices.
eval_diag
([dims])Extract the diagonal of the tensor.
full
()Return the tensor.
hadamard_product
(arg)Compute the Hadamard product of two tensors.
itranspose
(dims)Return the inverse transpose (permutation) of the dimensions of the tensor.
kron
(tensor2)Kronecker product of tensors.
matricize
(dims1[, dims2])Return the matricization of the tensor.
norm
()Compute the canonical norm of the FullTensor.
numpy
()Convert the FullTensor to a numpy.ndarray.
ones
(shape)Create a FullTensor of shape shape with entries equal to 1.
orth
([dim])Orthogonalize the tensor.
outer_product_eval_diag
(tensor2, dims1, dims2)Compute the diagonal of the outer product of two tensors.
principal_components
([parameter])Compute the principal components of an order-2 tensor.
rand
(shape)Create a FullTensor of shape shape with i.i.d.
randn
(shape)Create a FullTensor of shape shape with i.i.d.
reduce_mean
([dims])Compute the mean of elements across dimensions dims of a tensor.
reduce_sum
([dims])Compute the sum of elements across dimensions dims of a tensor.
reshape
(shape)Reshape the tensor.
Compute the higher-order singular values of a tensor (the collection of singular values of d different matricizations).
sparse
()Conversion of a FullTensor into a SparseTensor.
Return the sparse storage complexity of the FullTensor.
squeeze
([dims])Remove the singleton dimensions of the tensor.
storage
()Return the storage complexity of the FullTensor.
sub_tensor
(*indices)Extract a subtensor of the tensor.
tensor_diagonal_matrix_product
(matrices[, dims])Contract a FullTensor with matrices built from their diagonals.
tensor_matrix_product
(matrices[, dims])Contract a tensor with matrices.
tensor_matrix_product_eval_diag
(matrices[, dims])Evaluate the diagonal of a tensor obtained by contraction with matrices.
tensor_vector_product
(vectors[, dims])Compute the contraction of the tensor with vectors.
tensordot
(tensor2, dims1[, dims2])Contract two tensors along specified dimensions.
tensordot_eval_diag
(tensor2, dims1, dims2, …)Evaluate of the diagonal of a tensor obtained by contraction of two tensors.
tensordot_matrix_product_except_dim
(tensor2, …)Particular type of contraction.
transpose
(dims)Transpose (permute) the dimensions of the tensor.
Convert a FullTensor into a TreeBasedTensor.
zeros
(shape)Create a FullTensor of shape shape with entries equal to 0.
-
alpha_principal_components
(alpha, parameter=None)¶ Compute the alpha-principal components of a tensor.
Return the principal components of the alpha-matricization M_alpha(self) of the tensor self of order d.
See also the method principal_components.
- Parameters
- alphaint
The index of the alpha-matricization.
- parameterfloat or int, optional
A parameter controlling the number of principal components. The default is M_alpha(self).shape[0].
- Returns
- principal_componentsnumpy.ndarray
The principal components of the tensor.
- singular_valuesnumpy.ndarray
The diagonal matrix of the associated singular values.
-
cat
(tensor2, dims=None)¶ Concatenate the tensors.
Concatenates self and tensor2 in a tensor z such that: z(i_1 ,…, i_d) = x(i_1, …, i_d) if i_k <= sz[k]-1 for k in dims, z(i_1, …, i_d) = y(i_1-sz[0], …, i_d-sz[d-1]) if i_k >= sz[k] for k in dims, z(i_1, …, i_d) = 0 otherwise, with sz = self.shape and dims = range(self.order) if not provided.
- Parameters
- tensor2FullTensor
The second tensor to be concatenaed.
- dimslist or numpy.ndarray, optional
The dimensions of the concatenation. The default is None, indicating all the dimensions.
- Returns
- ——-
- dataFullTensor
The concatenated tensors.
-
static
create
(generator, shape)¶ Create a FullTensor of shape shape using a given generator.
- Parameters
- generatorfunction
Function generating a numpy.ndarray, given a shape.
- shapenumpy.ndarray or list
The shape of the tensor.
- Returns
- FullTensor
The created tensor.
-
static
diag
(diag, order)¶ Create a diagonal tensor x of order order, such that x[i, …, i] = diag[i] for i = 0, …, diag.size - 1.
- Parameters
- diaglist or numpy.ndarray
The diagonal of the tensor.
- orderint
The order of the tensor.
- Returns
- FullTensor
The created tensor.
-
dot
(tensor2)¶ Return the inner product of two tensors.
- Parameters
- tensor2FullTensor
The second tensor of the inner products.
- Returns
- numpy.float
The inner product of the two tensors.
-
dot_with_rank_one_metric
(tensor2, matrix)¶ Compute the weighted inner product of two tensors.
Compute the weighted canonical inner product of self and tensor2, where the inner product related to dimension k is weighted by matrix[k]. It is equivalent to self.dot(tensor2.tensor_matrix_product(matrix)), but can be much faster.
- Parameters
- tensor2FullTensor
The second tensor of the inner product.
- matrixlist or numpy.ndarray or FullTensor
The weight matrix.
- Returns
- numpy.float
The weighted inner product.
-
eval_at_indices
(indices, dims=None)¶ Evaluate the tensor at indices.
If dims is None, return s(k) = x(indices(k, 1), indices(k, 2), …, indices(k, d)), 1 <= k <= self.shape[0].
If dims is not None, return a partial evaluation: up to a permutation (placing the dimensions dims on the left), return s(k, i_1, …, i_d’) = x(indices(k, 1), indices(k, 2), …, indices(k, M), i_1, …, i_d’), 1 <= k <= self.shape[0], with M = dims.size and d’ = self.order - M.
- Parameters
- indiceslist of numpy.ndarray
The indices of the tensor.
- dimslist of numpy.ndarray, optional
The dimensions associated with the indices. The default is None, indicating that indices refers to all the dimensions.
- Returns
- evaluationsnumpy.ndarray or FullTensor
The evaluations of the tensor.
-
eval_diag
(dims=None)¶ Extract the diagonal of the tensor.
The tensor must be such that self.shape[mu] = n for all mu (in dims if provided).
- Parameters
- dimslist of numpy.ndarray, optional
The dimensions associated with the indices of the diagonal. The default is None,indicating that the indices refer to all the dimensions.
- Returns
- datanumpy.ndarray
The evaluations of the diagonal of the tensor.
-
full
()¶ Return the tensor.
- Returns
- FullTensor
The tensor.
-
hadamard_product
(arg)¶ Compute the Hadamard product of two tensors.
Equivalent to self * arg.
- Parameters
- argtensap.FullTensor or numpy.ndarray
The second tensor of the Hadamard product.
- Returns
- FullTensor
The tensor resulting from the Hadamard product.
-
itranspose
(dims)¶ Return the inverse transpose (permutation) of the dimensions of the tensor.
- Parameters
- dimslist or numpy.ndarray
The original transpose (permutation) indices.
- Returns
- FullTensor
The transposed (permuted) tensor.
-
kron
(tensor2)¶ Kronecker product of tensors.
Similar to numpy.kron but for arbitrary tensors.
- Parameters
- tensor2FullTensor
The second tensor of the Kronecker product.
- Returns
- FullTensor
The tensor resulting from the Kronecker product.
-
matricize
(dims1, dims2=None)¶ Return the matricization of the tensor.
- Parameters
- dims1list or numpy.ndarray
The dimensions of the tensor corresponding to the first dimension of the matricization.
- dims2list or numpy.ndarray, optional
The dimensions of the tensor corresponding to the first dimension of the matricization. The default is None, for which they are deduced from dims1.
- Returns
- FullTensor
The matricization of the tensor.
-
property
ndim
¶ Compute the order of the tensor. Equivalent to self.order.
- Returns
- int
The order of the tensor.
-
norm
()¶ Compute the canonical norm of the FullTensor.
- Returns
- numpy.float
The norm of the tensor.
-
numpy
()¶ Convert the FullTensor to a numpy.ndarray.
- Returns
- numpy.ndarray
The FullTensor as a numpy.ndarray.
-
static
ones
(shape)¶ Create a FullTensor of shape shape with entries equal to 1.
- Parameters
- shapenumpy.ndarray or list
The shape of the tensor.
- Returns
- FullTensor
The created tensor.
-
property
order
¶ Compute the order of the tensor.
- Returns
- int
The order of the tensor.
-
orth
(dim=None)¶ Orthogonalize the tensor.
- Parameters
- dimint, optional
The dimension of the orthogonal dim-matricization of self. The default is None, returning a copy of the original tensor.
- Returns
- tensorFullTensor
A tensor whose dim-matricization is an orthogonal matrix corresponding to the Q factor of a QR factorization of the dim-matricization of self.
- r_matrixnumpy.ndarray
The R factor.
-
outer_product_eval_diag
(tensor2, dims1, dims2, diag=False)¶ Compute the diagonal of the outer product of two tensors.
Equivalent to self.tensordot_eval_diag(tensor2, None, None, dims1, dims2, diag)
- Parameters
- tensor2FullTensor
The second tensor of the product.
- dims1list or numpy.ndarray
Indices of the first tensor for the evaluation of the diagonal.
- dims2list or numpy.ndarray,
Indices of the second tensor for the evaluation of the diagonal.
- diagbool, optional
Boolean enabling the evaluation of multiple diagonals. The default is False.
- Returns
- FullTensor
The evaluated tensor.
-
principal_components
(parameter=None)¶ Compute the principal components of an order-2 tensor.
- Parameters
- parameterfloat or int, optional
A parameter controlling the number of principal components. - If it is an integer, the number of principal components is the minimum between parameter and self.shape[0]. - If it is a float smaller than 1, the number of principal components is determined such that ||x - VV’x||_F < t ||x||_F, with x the tensor, V the matrix of principal components, t the parameter, V’ the transpose of the matrix V and ||.||_F the Frobenius norm. The default is self.shape[0].
- Returns
- principal_componentsnumpy.ndarray
The principal components of the tensor.
- singular_valuesnumpy.ndarray
The diagonal matrix of the associated singular values.
-
static
rand
(shape)¶ Create a FullTensor of shape shape with i.i.d. entries drawn according to the uniform distribution on [0, 1].
- Parameters
- shapenumpy.ndarray or list
The shape of the tensor.
- Returns
- FullTensor
The created tensor.
-
static
randn
(shape)¶ Create a FullTensor of shape shape with i.i.d. entries drawn according to the standard gaussian distribution.
- Parameters
- shapenumpy.ndarray or list
The shape of the tensor.
- Returns
- FullTensor
The created tensor.
-
reduce_mean
(dims=None)¶ Compute the mean of elements across dimensions dims of a tensor.
Similar to tensorflow.mean.
- Parameters
- dimslist or numpy.ndarray, optional
The dimensions to be reduced. The default is None, indicating all the dimensions.
- Returns
- FullTensor
The reduced tensor.
-
reduce_sum
(dims=None)¶ Compute the sum of elements across dimensions dims of a tensor.
Similar to tensorflow.reduce_sum.
- Parameters
- dimslist or numpy.ndarray, optional
The dimensions to be reduced. The default is None, indicating all the dimensions.
- Returns
- FullTensor
The reduced tensor.
-
reshape
(shape)¶ Reshape the tensor.
- Parameters
- shapelist or numpy.ndarray
The new shape of the tensor.
- Returns
- tensorFullTensor
The reshaped tensor.
-
property
shape
¶ Compute the shape of the tensor
- Returns
- numpy.ndarray
The shape of the tensor.
-
singular_values
()¶ Compute the higher-order singular values of a tensor (the collection of singular values of d different matricizations).
- Returns
- sin_valnumpy.ndarray or list of numpy.ndarray.
The higher-order singular values.
-
property
size
¶ Compute the number of elements of the tensor.
- Returns
- numpy.ndarray
The number of elements of the tensor.
-
sparse
()¶ Conversion of a FullTensor into a SparseTensor.
- Returns
- tensap.SparseTensor
A SparseTensor representation of the FullTensor.
-
sparse_storage
()¶ Return the sparse storage complexity of the FullTensor.
- Returns
- int
The sparse storage complexity of the FullTensor.
-
squeeze
(dims=None)¶ Remove the singleton dimensions of the tensor.
- Parameters
- dimslist or numpy.ndarray, optional
Dimensions to squeeze. The default is None, indicating all the singleton dimensions.
- Returns
- outfloat or FullTensor
The squeezed tensor.
-
storage
()¶ Return the storage complexity of the FullTensor.
- Returns
- int
The storage complexity of the FullTensor.
-
sub_tensor
(*indices)¶ Extract a subtensor of the tensor.
The result is a tensor s of shape len(indices[0]), …, len(indices[self.order-1]), such that s(k1,…,kd) = x(indices[0][k1], …, indices[self.order-1][kd]).
Example: x.subTensor([1, 2], ‘:’, [2, 5, 6]) returns a tensor with shape [2, self.shape[1], 3].
- Parameters
- *indiceslist
The indices to extract in each dimension. ‘:’ indicates all the indices.
- Returns
- FullTensor
The subtensor.
-
tensor_diagonal_matrix_product
(matrices, dims=None)¶ Contract a FullTensor with matrices built from their diagonals.
The second dimension of the matrix matrices[k] is contracted with the k-th dimension of self, with the indices k given in dims (if provided).
- Parameters
- matricesnumpy.ndarray or list of numpy.ndarray
The diagonals of the matrices to use in the product.
- dimslist or numpy.ndarray, optional
Indices of the contractions. The default is None, indicating all the dimensions.
- Returns
- FullTensor
The tensor after the contractions with the matrices.
-
tensor_matrix_product
(matrices, dims=None)¶ Contract a tensor with matrices.
The second dimension of the matrix matrices[k] is contracted with the k-th dimension of self, with the indices k given in dims (if provided).
- Parameters
- matricesnumpy.ndarray or list of numpy.ndarray
The matrices to use in the product.
- dimslist or numpy.ndarray, optional
Indices of the contractions. The default is None, indicating all the dimensions.
- Returns
- FullTensor
The tensor after the contractions with the matrices.
-
tensor_matrix_product_eval_diag
(matrices, dims=None)¶ Evaluate the diagonal of a tensor obtained by contraction with matrices.
Provides the diagonal of the tensor obtained by contracting the tensor with matrices H[k] along dimensions dims(k)+1, for k = 0, …, dims.size-1.
- Parameters
- matriceslist
The matrices to use in the product.
- dimslist or numpy.ndarray, optional
Indices of the contractions. The default is None, indicating all the dimensions.
- Returns
- outFullTensor
The diagonal of the contractions of the tensor with the matrices.
-
tensor_vector_product
(vectors, dims=None)¶ Compute the contraction of the tensor with vectors.
Compute the contraction of self with each vector contained in the list vectors along dimensions specified by dims. The operation is such that V[k] is contracted with the dims[k]-th dimension of self.
- Parameters
- vectorsnumpy.ndarray or list of numpy.ndarray
The vectors to use in the product.
- dimslist or numpy.ndarray, optional
Indices of the contractions. The default is None, indicating all the dimensions.
- Returns
- FullTensor
The tensor after the contractions with the vectors.
-
tensordot
(tensor2, dims1, dims2=None)¶ Contract two tensors along specified dimensions.
Similar to tensorflow.tensordot.
- Parameters
- tensor2FullTensor
The second tensor of the contraction.
- dims1list or int
The dimensions of contractions for the first tensor.
- dims2list or int, optional
The dimensions of contractions for the second tensor. The default is None which indicates, if dims1 = 0, to perform the outer product of the two tensors, similarly to tensorflow.tensordot.
- Returns
- outFullTensor
The resulting tensor.
-
tensordot_eval_diag
(tensor2, dims1, dims2, diag_dims1, diag_dims2, diag=False)¶ Evaluate of the diagonal of a tensor obtained by contraction of two tensors.
The contraction is performed along the dimensions dims1 for self and dims2 for tensor2, and the diagonal is evaluated according to the dimensions diag_dims1 for self and diag_dims2 for tensor2.
The boolean diag indicates if the several diagonals are evaluated, for instance: - if diag is False, for order-4 tensors x and y, z = x.tensordot_eval_diag(y,[1,3],[2,3],2,0) returns an order-3 tensor z(i1,k,j2) = sum_{l1,l2} x(i1,l1,k,l2) y(k,j2,l1,l2) - if diag is True, for order-5 tensors x and y, z = x.tensordot_eval_diag(y,[1,3],[2,3],[0,2],[1,4]) returns an order-4 tensor z(k,l,i5,j1) = sum_{l1,l2} x(k,l1,l,l2,i5) y(j1,k,l1,l2,l)
- Parameters
- tensor2FullTensor
The second tensor of the product.
- dims1list or numpy.ndarray
Dimensions of the first tensor for the contraction.
- dims2list or numpy.ndarray
Dimensions of the second tensor for the contraction.
- diag_dims1list or numpy.ndarray
Indices of the first tensor for the evaluation of the diagonal.
- diag_dims2list or numpy.ndarray
Indices of the second tensor for the evaluation of the diagonal.
- diagbool, optional
Boolean enabling the evaluation of multiple diagonals. The default is False.
- Returns
- FullTensor
The evaluated tensor.
-
tensordot_matrix_product_except_dim
(tensor2, matrices, dim)¶ Particular type of contraction.
Compute a special contraction of two tensors self, tensor2, a list of matrices matrices and a particular dimension dim. Note that dim must be a scalar, while matrices must be a list array with self.order elements.
- Parameters
- tensor2FullTensor
The second tensor of the contraction.
- matriceslist
The list of matrices of the contraction.
- dimint
The excluded dimension.
- Returns
- numpy.ndarray
The result of the contraction.
-
transpose
(dims)¶ Transpose (permute) the dimensions of the tensor.
- Parameters
- dimslist or numpy.ndarray
The new ordering of the dimensions.
- Returns
- tensorFullTensor
The transposed (permuted) tensor.
-
tree_based_tensor
()¶ Convert a FullTensor into a TreeBasedTensor.
- Returns
- TreeBasedTensor
The FullTensor in tree-based tensor format.
-
static
zeros
(shape)¶ Create a FullTensor of shape shape with entries equal to 0.
- Parameters
- shapenumpy.ndarray or list
The shape of the tensor.
- Returns
- FullTensor
The created tensor.
tensap.tensor_algebra.tensors.sparse_tensor module¶
Module sparse_tensor.
-
class
tensap.tensor_algebra.tensors.sparse_tensor.
SparseTensor
(data=None, indices=None, shape=None)¶ Bases:
object
Class SparseTensor.
- Attributes
- datanumpy.ndarray
The values of the tensor at the entries in indices.
- indicestensap.MultiIndices
The set of multi-indices corresponding to the non-zero coefficients of the tensor.
- orderint
The order of the tensor.
- shapenumpy.ndarray
The shape of the tensor.
Methods
cat
(y, dims)Concatenate the tensors.
Return the number of non-zero coefficients of the SparseTensor.
dot
(y)Return the inner product of two tensors.
dot_with_rank_one_metric
(y, M)Compute the weighted inner product of two tensors.
eval_at_indices
(ind[, dims])Evaluate the tensor at indices.
eval_diag
([dims])Extract the diagonal of the tensor.
full
()Convert the SparseTensor to a tensap.FullTensor.
itranspose
(dims)Return the inverse transpose (permutation) of the dimensions of the tensor.
kron
(y)Kronecker product of tensors.
norm
()Compute the canonical norm of the SparseTensor.
numpy
()Convert the SparseTensor to a scipy.sparse.lil.lil_matrix, which can be converted to a numpy.matrix using the command todense().
orth
(dim)Orthogonalize the tensor.
reshape
(shape)Reshape the tensor.
Return the sparse storage complexity of the SparseTensor.
squeeze
(dims)Remove the singleton dimensions of the tensor.
storage
()Return the storage complexity of the SparseTensor.
sub_tensor
(*indices)Extract a subtensor of the tensor.
tensor_diagonal_matrix_product
(matrices[, dims])Contract a SparseTensor with matrices built from their diagonals.
tensor_matrix_product
(matrices[, dims])Contract a tensor with matrices.
tensor_matrix_product_eval_diag
(matrices)Evaluate the diagonal of a tensor obtained by contraction with matrices.
tensor_vector_product
(vectors[, dims])Compute the contraction of the tensor with vectors.
tensordot_matrix_product_except_dim
(y, M, dim)Particular type of contraction.
transpose
(dims)Transpose (permute) the dimensions of the tensor.
-
cat
(y, dims)¶ Concatenate the tensors.
Concatenates self and y in a tensor z such that: z(i_1 ,…, i_d) = x(i_1, …, i_d) if i_k <= sz[k]-1 for k in dims, z(i_1, …, i_d) = y(i_1-sz[0], …, i_d-sz[d-1]) if i_k >= sz[k] for k in dims, z(i_1, …, i_d) = 0 otherwise, with sz = self.shape and dims = range(self.order) if not provided.
- Parameters
- yTensor
The second tensor to be concatenaed.
- dimslist or numpy.ndarray, optional
The dimensions of the concatenation. The default is None, indicating all the dimensions.
- Returns
- ——-
- SparseTensor
The concatenated tensors.
-
count_non_zero
()¶ Return the number of non-zero coefficients of the SparseTensor. Equivalent to self.sparse_storage().
- Returns
- int
The number of non-zero coefficients of the SparseTensor.
-
dot
(y)¶ Return the inner product of two tensors.
- Parameters
- ytensap.Tensor
The second tensor of the inner products. Must be convertible to a SparseTensor.
- Returns
- numpy.float
The inner product of the two tensors.
-
dot_with_rank_one_metric
(y, M)¶ Compute the weighted inner product of two tensors.
Compute the weighted canonical inner product of self and y, where the inner product related to dimension k is weighted by M[k]. It is equivalent to self.dot(y.tensor_matrix_product(M)), but can be much faster.
- Parameters
- yTensor
The second tensor of the inner product.
- Mlist or numpy.ndarray or FullTensor
The weight matrix.
- Returns
- numpy.float
The weighted inner product.
-
eval_at_indices
(ind, dims=None)¶ Evaluate the tensor at indices.
If dims is None, return s(k) = x(indices(k, 1), indices(k, 2), …, indices(k, d)), 1 <= k <= self.shape[0].
If dims is not None, return a partial evaluation: up to a permutation (placing the dimensions dims on the left), return s(k, i_1, …, i_d’) = x(indices(k, 1), indices(k, 2), …, indices(k, M), i_1, …, i_d’), 1 <= k <= self.shape[0], with M = dims.size and d’ = self.order - M.
- Parameters
- indlist of numpy.ndarray
The indices of the tensor.
- dimslist of numpy.ndarray, optional
The dimensions associated with the indices. The default is None, indicating that indices refers to all the dimensions.
- Returns
- evaluationsscipy.sparse.lil.lil_matrix
The evaluations of the tensor.
-
eval_diag
(dims=None)¶ Extract the diagonal of the tensor.
The tensor must be such that self.shape[mu] = n for all mu (in dims if provided).
- Parameters
- dimslist of numpy.ndarray, optional
The dimensions associated with the indices of the diagonal. The default is None,indicating that indices refers to all the dimensions.
- Returns
- datanumpy.ndarray
The evaluations of the diagonal of the tensor.
-
full
()¶ Convert the SparseTensor to a tensap.FullTensor.
- Returns
- ytensap.FullTensor
The SparseTensor as a tensap.FullTensor.
-
itranspose
(dims)¶ Return the inverse transpose (permutation) of the dimensions of the tensor.
- Parameters
- dimslist or numpy.ndarray
The original transpose (permutation) indices.
- Returns
- SparseTensor
The transposed (permuted) tensor.
-
kron
(y)¶ Kronecker product of tensors.
Similar to numpy.kron but for sparse tensors.
- Parameters
- yTensor
The second tensor of the Kronecker product.
- Returns
- SparseTensor
The tensor resulting from the Kronecker product.
-
property
ndim
¶ Compute the order of the tensor. Equivalent to self.order.
- Returns
- int
The order of the tensor.
-
norm
()¶ Compute the canonical norm of the SparseTensor.
- Returns
- numpy.float
The norm of the tensor.
-
numpy
()¶ Convert the SparseTensor to a scipy.sparse.lil.lil_matrix, which can be converted to a numpy.matrix using the command todense().
- Returns
- yscipy.sparse.lil.lil_matrix
The SparseTensor as a scipy.sparse.lil.lil_matrix.
-
orth
(dim)¶ Orthogonalize the tensor.
- Parameters
- dimint
The dimension of the orthogonal dim-matricization of self.
- Returns
- SparseTensor
A tensor whose dim-matricization is an orthogonal matrix corresponding to the Q factor of a QR factorization of the dim-matricization of self.
- r_matrixnumpy.ndarray
The R factor.
-
reshape
(shape)¶ Reshape the tensor.
- Parameters
- shapelist or numpy.ndarray
The new shape of the tensor.
- Returns
- tensorSparseTensor
The reshaped tensor.
-
property
size
¶ Compute the size of the tensor. Equivalent to self.storage().
- Returns
- numpy.ndarray
The size of the tensor.
-
sparse_storage
()¶ Return the sparse storage complexity of the SparseTensor.
- Returns
- int
The sparse storage complexity of the SparseTensor.
-
squeeze
(dims)¶ Remove the singleton dimensions of the tensor.
- Parameters
- dimslist or numpy.ndarray, optional
Dimensions to squeeze. The default is None, indicating all the singleton dimensions.
- Returns
- SparseTensor
The squeezed tensor.
-
storage
()¶ Return the storage complexity of the SparseTensor.
- Returns
- int
The storage complexity of the SparseTensor.
-
sub_tensor
(*indices)¶ Extract a subtensor of the tensor.
The result is a tensor s of shape len(indices[0]), …, len(indices[self.order-1]), such that s(k1,…,kd) = x(indices[0][k1], …, indices[self.order-1][kd]).
Example: x.subTensor([1, 2], ‘:’, [2, 5, 6]) returns a tensor with shape [2, self.shape[1], 3].
- Parameters
- *indiceslist
The indices to extract in each dimension. ‘:’ indicates all the indices.
- Returns
- SparseTensor
The subtensor.
-
tensor_diagonal_matrix_product
(matrices, dims=None)¶ Contract a SparseTensor with matrices built from their diagonals.
The second dimension of the matrix matrices[k] is contracted with the k-th dimension of self, with the indices k given in dims (if provided).
FIXME: not optimal, does not exploit sparsity.
- Parameters
- matricesnumpy.ndarray or list of numpy.ndarray
The diagonals of the matrices to use in the product.
- dimslist or numpy.ndarray, optional
Indices of the contractions. The default is None, indicating all the dimensions.
- Returns
- SparseTensor
The tensor after the contractions with the matrices.
-
tensor_matrix_product
(matrices, dims=None)¶ Contract a tensor with matrices.
The second dimension of the matrix matrices[k] is contracted with the k-th dimension of self, with the indices k given in dims (if provided).
- Parameters
- matricesnumpy.ndarray or list of numpy.ndarray
The matrices to use in the product.
- dimslist or numpy.ndarray, optional
Indices of the contractions. The default is None, indicating all the dimensions.
- Returns
- outSparseTensor
The tensor after the contractions with the matrices.
-
tensor_matrix_product_eval_diag
(matrices)¶ Evaluate the diagonal of a tensor obtained by contraction with matrices.
- Parameters
- matriceslist
The matrices to use in the product.
- Returns
- SparseTensor
The diagonal of the contractions of the tensor with the matrices.
-
tensor_vector_product
(vectors, dims=None)¶ Compute the contraction of the tensor with vectors.
Compute the contraction of self with each vector contained in the list vectors along dimensions specified by dims. The operation is such that V[k] is contracted with the dims[k]-th dimension of self.
- Parameters
- vectorsnumpy.ndarray or list of numpy.ndarray
The vectors to use in the product.
- dimslist or numpy.ndarray, optional
Indices of the contractions. The default is None, indicating all the dimensions.
- Returns
- outSparseTensor
The tensor after the contractions with the vectors.
-
tensordot_matrix_product_except_dim
(y, M, dim)¶ Particular type of contraction.
Compute a special contraction of two tensors self, y, a list of matrices M and a particular dimension dim. Note that dim must be a scalar, while M must be a list array with x.self.order elements.
- Parameters
- yTensor
The second tensor of the contraction.
- Mlist
The list of matrices of the contraction.
- dimint
The excluded dimension.
- Returns
- numpy.ndarray
The result of the contraction.
-
transpose
(dims)¶ Transpose (permute) the dimensions of the tensor.
- Parameters
- dimslist or numpy.ndarray
The new ordering of the dimensions.
- Returns
- outSparseTensor
The transposed (permuted) tensor.
tensap.tensor_algebra.tensors.tree_based_tensor module¶
Module tree_based_tensor.
-
class
tensap.tensor_algebra.tensors.tree_based_tensor.
TreeBasedTensor
(cores, tree=None)¶ Bases:
object
Class TreeBasedTensor: algebraic tensors in tree-based tensor format.
References: - Nouy, A. (2017). Low-rank methods for high-dimensional approximation and model order reduction. Model reduction and approximation, P. Benner, A. Cohen, M. Ohlberger, and K. Willcox, eds., SIAM, Philadelphia, PA, 171-226. - Falco, A., Hackbusch, W., & Nouy, A. (2018). Tree-based tensor formats. SeMA Journal, 1-15 - Grelier, E., Nouy, A., & Chevreuil, M. (2018). Learning with tree-based tensor formats. arXiv preprint arXiv:1811.04455 - Nouy, A. (2019). Higher-order principal component analysis for the approximation of tensors in tree-based low-rank formats. Numerische Mathematik, 141(3), 743-789
- Attributes
- tensorsnumpy.ndarray
Parameters of the representation.
- ranksnumpy.ndarray
Tree-based rank.
- orderint
Order of the tensor.
- shapenumpy.ndarray
Shape of the tensor.
- treetensap.DimensionTree
Dimension tree.
- is_orthbool
True if the representation of the tensor is orthogonal.
- is_active_nodenumpy.ndarray
Logical array indicating if the nodes are active.
- orth_nodeint
Node with respect to which the representation is orthogonalized (0 is the root node)
Methods
cat
(tensor2)Concatenate the tensors.
create
(generator, tree[, ranks, shape, …])Create a tree-based tensor from a generator.
dot
(tensor2)Return the inner product of two tensors.
dot_with_rank_one_metric
(tensor2, matrices)Compute the weighted inner product of two tree-based tensors.
eval_at_indices
(indices[, dims])Evaluate the tensor at indices.
eval_diag
([dims, nargout])Extract the diagonal of the tensor.
eval_diag_above
([diag_below, alpha])Evaluate the diagonal of the tensor of the function w^lpha of the representation f = sum_{k=1}^{beta} v^beta_k w^beta_k (optionally for the node alpha and its ascendants).
eval_diag_below
([alpha, except_nodes])Evaluate the diagonal of the tensor of the function v^beta of the representation f = sum_{k=1}^{beta} v^beta_k w^beta_k (optionally for all the nodes except the ascendants of a node alpha).
full
()Convert a TreeBasedTensor to a tensap.FullTensor
gramians
([alpha])Compute the Gram matrices of the bases of minimal subspaces associated with nodes of the tree.
hadamard_product
(arg)Compute the Hadamard product of two tensors.
inactivate_nodes
(nodes)Inactivate a list of nodes.
is_active_dim
(dim)Return an array containing true if the given dimensions are active, false otherwise.
is_admissible_rank
([ranks, nargout])Check if a given tuple is an admissible tree-based rank.
kron
(tensor2)Kronecker product of tensors.
nodes_permutation_cost
(alpha)Cost of the permutation of a given node alpha with the other nodes of the dimension tree.
norm
()Compute the canonical norm of the tensor.
numpy
()Convert the TreeBasedTensor to a numpy.ndarray.
ones
(tree[, ranks, shape, is_active_node])Create a tensor of shape shape and tree-based rank ranks with node tensors generated with the method ones of numpy.
optimize_dimension_tree
(tolerance, max_iter)Optimization over the set of trees to obtain a representation of the tensor with lower complexity.
optimize_leaves_permutations
(tolerance, max_iter)Optimization over the ordering of the leaves of the tree to obtain a representation of the tensor with lower complexity.
orth
()Orthogonalize the representation of the tensor.
orth_at_node
(nod)Orthogonalize the representation with respect to a given node.
parameter_gradient_eval_diag
(alpha[, matrices])Compute the diagonal of the gradient of the tensor with respect to a given parameter.
parameter_gradient_eval_diag_dmrg
(alpha[, …])Return the diagonal of the gradient of the tensor with respect to a given parameter, obtained by contraction of two node tensors along their common edge; used in a DMRG algorithm.
permute_leaves
(perm[, tolerance])Permutation of leaf nodes given a permutation of the dimensions.
permute_nodes
(nodes[, tolerance])Permutation of two nodes of the tree.
plot
([nodes_labels, title])Plot the tree with the nodes indices and the active nodes.
rand
(tree[, ranks, shape, is_active_node])Create a tensor of shape shape and tree-based rank ranks with node tensors generated with the method random.rand of numpy.
randn
(tree[, ranks, shape, is_active_node])Create a tensor of shape shape and tree-based rank ranks with node tensors generated with the method random.randn of numpy.
Remove the unique children of a tree-based tensor (nodes with no siblings in the tree).
Compute the tree-based singular values of a tensor, which are the singular values associated with alpha-matricizations of the tensor, for all alpha in the dimension tree.
Return the storage complexity of the TreeBasedTensor taking into account the sparsity in the leaves.
Return the sparse storage complexity of the TreeBasedTensor.
squeeze
([dims])Remove the singleton dimensions of the tensor.
storage
()Return the storage complexity of the TreeBasedTensor.
sub_tensor
(*indices)Extract a subtensor of the tensor.
tensor_diagonal_matrix_product
(matrices[, dims])Contract a TreeBasedTensor with matrices built from their diagonals.
tensor_matrix_product
(matrices[, dims])Contract a tensor with matrices.
tensor_matrix_product_eval_diag
(matrices[, dims])Evaluate the diagonal of a tensor obtained by contraction with matrices.
tensor_train
(cores[, dims])Create a tree-based tensor with a tensor-train structure.
tensor_vector_product
(vectors[, dims])Compute the contraction of the tensor with vectors.
tensordot_matrix_product_except_dim
(tensor2, …)Particular type of contraction.
Update the attributes of the TreeBasedTensor.
zeros
(tree[, ranks, shape, is_active_node])Create a tensor of shape shape and tree-based rank ranks with node tensors generated with the method zeros of numpy.
-
property
active_dims
¶ Return the list of active dimensions.
- Returns
- numpy.ndarray
The list of active dimensions.
-
property
active_nodes
¶ Return the list of active nodes.
- Returns
- numpy.ndarray
The list of active nodes.
-
cat
(tensor2)¶ Concatenate the tensors.
- Parameters
- tensor2TreeBasedTensor
The second tensor to be concatenated.
- Returns
- tensorTreeBasedTensor
The concatenated tensors.
-
static
create
(generator, tree, ranks=None, shape=None, is_active_node=None)¶ Create a tree-based tensor from a generator.
- Parameters
- generatorfunction
Function generating a tensap.FullTensor, given a shape.
- treetensap.FullTensor
The tensap.FullTensor of the TreeBasedTensor.
- ranksnumpy.ndarray, list or ‘random’, optional
Ranks of the TreeBasedTensorFormat. The default is None, generating random ranks between 1 and 5.
- shapenumpy.ndarray, list or ‘random’, optional
The size of the spaces of the leaves of the tree. The default is None, assigning ranks to shape.
- is_active_nodenumpy.ndarray, list or ‘random’, optional
Booleans indicating if the node is active. The default is None, for which all the nodes are active.
- Returns
- TreeBasedTensor
A TreeBasedTensor with the input characteristics.
- Raises
- ValueError
If some internal nodes are inactive.
-
dot
(tensor2)¶ Return the inner product of two tensors.
- Parameters
- tensor2TreeBasedTensor
The second tensor of the inner products.
- Returns
- numpy.float
The inner product of the two tensors.
-
dot_with_rank_one_metric
(tensor2, matrices)¶ Compute the weighted inner product of two tree-based tensors.
Compute the weighted canonical inner product of self and tensor2, where the inner product related to dimension k is weighted by matrix[k]. It is equivalent to self.dot(tensor2.tensor_matrix_product(matrix)), but can be much faster.
- Parameters
- tensor2TreeBasedTensor
The second tensor of the inner product.
- matrixlist or numpy.ndarray
The weight matrices.
- Returns
- numpy.float
The weighted inner product.
-
eval_at_indices
(indices, dims=None)¶ Evaluate the tensor at indices.
If dims is None, return s(k) = x(indices(k, 1), indices(k, 2), …, indices(k, d)), 1 <= k <= self.shape[0].
If dims is not None, return a partial evaluation: up to a permutation (placing the dimensions dims on the left), return s(k, i_1, …, i_d’) = x(indices(k, 1), indices(k, 2), …, indices(k, M), i_1, …, i_d’), 1 <= k <= self.shape[0], with M = dims.size and d’ = self.order - M.
- Parameters
- indiceslist of numpy.ndarray
The indices of the tensor.
- dimslist of numpy.ndarray, optional
The dimensions associated with the indices. The default is None, indicating that indices refers to all the dimensions.
- Returns
- evaluationsnumpy.ndarray or TreeBasedTensor
The evaluations of the tensor.
-
eval_diag
(dims=None, nargout=1)¶ Extract the diagonal of the tensor.
- Parameters
- nargoutint, optional
The number of outputs. The default is 1, returning the diagonal. If set to 2, return the node tensors with their diagonal evaluated as well.
- Returns
- numpy.array or (numpy.array and list)
The diagonal and, if nargout == 2, the node tensors with their diagonal evaluated.
-
eval_diag_above
(diag_below=None, alpha=None)¶ Evaluate the diagonal of the tensor of the function w^lpha of the representation f = sum_{k=1}^{beta} v^beta_k w^beta_k (optionally for the node alpha and its ascendants).
- Parameters
- diag_belownumpy.ndarray, optional
The result of the method eval_diag_below. The default is None, calling the method eval_diag_below.
- alphaint, optional
A node of the tree, including it and its ascendants in the computation of the functions w^beta. The default is None, indicating that all the nodes are included.
- Returns
- diag_abovenumpy.ndarray or tensap.FullTensor
The diagonals of the tensor of the functions w^beta for the included nodes, and None for the excluded nodes.
-
eval_diag_below
(alpha=None, except_nodes=[])¶ Evaluate the diagonal of the tensor of the function v^beta of the representation f = sum_{k=1}^{beta} v^beta_k w^beta_k (optionally for all the nodes except the ascendants of a node alpha).
- Parameters
- alphaint, optional
A node of the tree, exluding its ascendants in the computation of the functions v^beta. The default is None, indicating that no node is excluded.
- except_nodesint or list or numpy.array, optional
Nodes for which the computation is not performed. The default is None.
- Returns
- diag_belownumpy.ndarray
An array containing the diagonals of the tensor of the functions v^beta for the included nodes, and None for the excluded nodes.
-
full
()¶ Convert a TreeBasedTensor to a tensap.FullTensor
- Returns
- tensortensap.FullTensor
A representation of the TreeBasedTensor as a tensap.FullTensor.
-
gramians
(alpha=None)¶ Compute the Gram matrices of the bases of minimal subspaces associated with nodes of the tree.
- Parameters
- alphalist or numpy.ndarray, optional
The nodes associated to the Gram matrices to be computed. The default is None, for all the nodes of the tree.
- Returns
- gramlist
The Gram matrices.
- TreeBasedTensor
The orthogonalized TreeBasedTensor.
-
hadamard_product
(arg)¶ Compute the Hadamard product of two tensors.
Equivalent to self * arg.
- Parameters
- argTreeBasedTensor
The second tensor of the Hadamard product.
- Returns
- tensap.FullTensor
The tensor resulting from the Hadamard product.
-
inactivate_nodes
(nodes)¶ Inactivate a list of nodes.
- Parameters
- nodeslist or numpy.ndarray
The list of nodes to inactivate.
- Returns
- TreeBasedTensor
The tensor with inactivated nodes.
-
is_active_dim
(dim)¶ Return an array containing true if the given dimensions are active, false otherwise.
- Parameters
- diminteger
The tested dimension.
- Returns
- resultnumpy.ndarray or boolean
Array containing true if the given dimensions are active, false otherwise.
-
is_admissible_rank
(ranks=None, nargout=1)¶ Check if a given tuple is an admissible tree-based rank.
If no tree-based rank is provided, the tree-based rank of self is checked.
- Parameters
- rankstyple of numpy.ndarray, optional
The tree-based rank to be checked. The default is None, indicating the tree-based rank of self.
- nargoutint, optional
Indicates the number of expected outputs. The default is 1, indicating to return only the boolean characterizing if the tree-based rank is admissible.
- Returns
- is_admissbool
True if the tree-based rank is admissible.
- ch_admissnumpy.ndarray
Array detailing the admissibility of the children ranks.
-
kron
(tensor2)¶ Kronecker product of tensors.
Similar to numpy.kron but for tree-based tensors.
- Parameters
- tensor2TreeBasedTensor
The second tensor of the Kronecker product.
- Returns
- tensorTreeBasedTensor
The tensor resulting from the Kronecker product.
-
property
ndim
¶ Compute the order of the tensor. Equivalent to self.order.
- Returns
- int
The order of the tensor.
-
nodes_permutation_cost
(alpha)¶ Cost of the permutation of a given node alpha with the other nodes of the dimension tree.
- Parameters
- alphaint
The node from which all the permutation costs are computed.
- Returns
- costnumpy.ndarray
The cost of permuting alpha with the other nodes of the tree.
-
property
non_active_dims
¶ Return the list of non active dimensions.
- Returns
- numpy.ndarray
The list of active dimensions.
-
property
non_active_nodes
¶ Return the list of non active nodes.
- Returns
- numpy.ndarray
The list of non active nodes.
-
norm
()¶ Compute the canonical norm of the tensor.
- Returns
- numpy.float
The norm of the tensor.
-
numpy
()¶ Convert the TreeBasedTensor to a numpy.ndarray.
- Returns
- numpy.ndarray
The TreeBasedTensor as a numpy.ndarray.
-
static
ones
(tree, ranks=None, shape=None, is_active_node=None)¶ Create a tensor of shape shape and tree-based rank ranks with node tensors generated with the method ones of numpy.
- Parameters
- treetensap.FullTensor
The tensap.FullTensor of the TreeBasedTensor.
- ranksnumpy.ndarray, list or ‘random’, optional
Ranks of the TreeBasedTensorFormat. The default is None, generating random ranks between 1 and 5.
- shapenumpy.ndarray, list or ‘random’, optional
The size of the spaces of the leaves of the tree. The default is None, assigning ranks to shape.
- is_active_nodenumpy.ndarray, list or ‘random’, optional
Booleans indicating if the node is active. The default is None, for which all the nodes are active.
- Returns
- TreeBasedTensor
A TreeBasedTensor with the input characteristics.
-
optimize_dimension_tree
(tolerance, max_iter)¶ Optimization over the set of trees to obtain a representation of the tensor with lower complexity.
- Parameters
- tolerancefloat
The relative tolerance for the tree changes.
- max_iterint
The maximum number of tree changes.
- Returns
- tensor_startensap.TreeBasedTensor
The tree-based tensor with optimized tree.
-
optimize_leaves_permutations
(tolerance, max_iter)¶ Optimization over the ordering of the leaves of the tree to obtain a representation of the tensor with lower complexity.
- Parameters
- tolerancefloat
The relative tolerance for the tree changes.
- max_iterint
The maximum number of tree changes.
- Returns
- tensor_startensap.TreeBasedTensor
The tree-based tensor with optimized leaves ordering.
-
orth
()¶ Orthogonalize the representation of the tensor.
All core tensors except the root core represents orthonormal bases of principal subspaces.
- Returns
- tensorTreeBasedTensor
The TreeBasedTensor with an orthogonal representation.
-
orth_at_node
(nod)¶ Orthogonalize the representation with respect to a given node.
All core tensors except the one of node nod represents orthonormal bases of principal subspaces. The core tensor of node nod is such that the tensor x(i_alpha,i_alpha^c) = sum_k u_k(i_alpha) w_k(i_alpha^c), where w_k is a set of orthonormal vectors.
- Parameters
- nodint
The node with respect to which the representation is orthogonal.
- Returns
- tensorTreeBasedTensor
The TreeBasedTensor with an orthogonal representation.
- Raises
- ValueError
If nod is non active.
-
parameter_gradient_eval_diag
(alpha, matrices=None)¶ Compute the diagonal of the gradient of the tensor with respect to a given parameter.
- Parameters
- alphaint
Index of node of the dimension tree.
- matriceslist or numpy.array, optional
Matrices with which to compute outer_product_eval_diag if alpha is associated with some dimensions. Useful for evaluating the gradient of a tensap.FunctionalTensor. The default is None, indicating identity matrices.
- Returns
- outtensap.FullTensor
The diagonal of the gradient of the tensor with respect to self.tensors[alpha-1].
-
parameter_gradient_eval_diag_dmrg
(alpha, matrices=None)¶ Return the diagonal of the gradient of the tensor with respect to a given parameter, obtained by contraction of two node tensors along their common edge; used in a DMRG algorithm.
- Parameters
- alphaint
Index of node of the dimension tree.
- matriceslist or numpy.array, optional
Matrices with which to compute outer_product_eval_diag if alpha is associated with some dimensions. Useful for evaluating the gradient of a tensap.FunctionalTensor. The default is None, indicating identity matrices.
- Returns
- outtensap.FullTensor
The diagonal of the gradient of the tensor with respect to the parameter.
-
permute_leaves
(perm, tolerance=1e-15)¶ Permutation of leaf nodes given a permutation of the dimensions.
Permutations of the leaf nodes given a permutation perm of the dimensions and a tolerance (for SVD-based truncations).
- Parameters
- permlist or numpy.ndarray
Permutation of (1,…,self.order).
- tolerancefloat, optional
Relative precision for SVD truncations. The default is 1e-15.
- Returns
- tensortensap.TreeBasedTensor
The tree-based tensor with permuted leaf nodes.
-
permute_nodes
(nodes, tolerance=1e-15)¶ Permutation of two nodes of the tree.
Permutations of the two nodes in nodes given a tolerance tol (for SVD-based truncations).
- Parameters
- nodeslist or numpy.ndarray
The two nodes to permute.
- tolerancefloat, optional
Relative precision for SVD truncations. The default is 1e-15.
- Returns
- tensortensap.TreeBasedTensor
The tree-based tensor with permuted nodes.
- Raises
- ValueError
If the first node to permute is an ascendant or a descendant of the second node to permute.
-
plot
(nodes_labels=None, title=None)¶ Plot the tree with the nodes indices and the active nodes.
This method requires the package igraph.
- Parameters
- nodes_labelslist or numpy.ndarray, optional
The labels of the nodes. The default is None, displaying the nodes numbers.
- titlestr, optional
The title of the graph. The default is None.
- Returns
- None.
-
static
rand
(tree, ranks=None, shape=None, is_active_node=None)¶ Create a tensor of shape shape and tree-based rank ranks with node tensors generated with the method random.rand of numpy.
- Parameters
- treetensap.FullTensor
The tensap.FullTensor of the TreeBasedTensor.
- ranksnumpy.ndarray, list or ‘random’, optional
Ranks of the TreeBasedTensorFormat. The default is None, generating random ranks between 1 and 5.
- shapenumpy.ndarray, list or ‘random’, optional
The size of the spaces of the leaves of the tree. The default is None, assigning ranks to shape.
- is_active_nodenumpy.ndarray, list or ‘random’, optional
Booleans indicating if the node is active. The default is None, for which all the nodes are active.
- Returns
- TreeBasedTensor
A TreeBasedTensor with the input characteristics.
-
static
randn
(tree, ranks=None, shape=None, is_active_node=None)¶ Create a tensor of shape shape and tree-based rank ranks with node tensors generated with the method random.randn of numpy.
- Parameters
- treetensap.FullTensor
The tensap.FullTensor of the TreeBasedTensor.
- ranksnumpy.ndarray, list or ‘random’, optional
Ranks of the TreeBasedTensorFormat. The default is None, generating random ranks between 1 and 5.
- shapenumpy.ndarray, list or ‘random’, optional
The size of the spaces of the leaves of the tree. The default is None, assigning ranks to shape.
- is_active_nodenumpy.ndarray, list or ‘random’, optional
Booleans indicating if the node is active. The default is None, for which all the nodes are active.
- Returns
- TreeBasedTensor
A TreeBasedTensor with the input characteristics.
-
property
rank
¶ Return the tree-based rank of the tensor (computed by SVD).
- Returns
- numpy.ndarray
The tree-based rank of the tensor.
-
remove_unique_children
()¶ Remove the unique children of a tree-based tensor (nodes with no siblings in the tree).
- Returns
- TreeBasedTensor
The tensor with no unique children.
-
property
representation_rank
¶ Return the representation tree-based rank of the tensor.
Corresponds to self.ranks.
- Returns
- numpy.ndarray
The representation tree-based rank of the tensor.
-
singular_values
()¶ Compute the tree-based singular values of a tensor, which are the singular values associated with alpha-matricizations of the tensor, for all alpha in the dimension tree.
- Returns
- numpy.ndarray
The tree-based singular values of the tensor.
-
sparse_leaves_storage
()¶ Return the storage complexity of the TreeBasedTensor taking into account the sparsity in the leaves.
- Returns
- int
The storage complexity of the TreeBasedTensor taking into account the sparsity in the leaves.
-
sparse_storage
()¶ Return the sparse storage complexity of the TreeBasedTensor.
- Returns
- int
The sparse storage complexity of the TreeBasedTensor.
-
squeeze
(dims=None)¶ Remove the singleton dimensions of the tensor.
- Parameters
- dimslist or numpy.ndarray, optional
Dimensions to squeeze. The default is None, indicating all the singleton dimensions.
- Returns
- TreeBasedTensor
The squeezed tensor.
-
storage
()¶ Return the storage complexity of the TreeBasedTensor.
- Returns
- int
The storage complexity of the TreeBasedTensor.
-
sub_tensor
(*indices)¶ Extract a subtensor of the tensor.
The result is a tensor s of shape len(indices[0]), …, len(indices[self.order-1]), such that s(k1,…,kd) = x(indices[0][k1], …, indices[self.order-1][kd]).
Example: x.subTensor([1, 2], ‘:’, [2, 5, 6]) returns a tensor with shape [2, self.shape[1], 3].
- Parameters
- *indiceslist
The indices to extract in each dimension. ‘:’ indicates all the indices.
- Returns
- TreeaBasedTensor
The subtensor.
-
tensor_diagonal_matrix_product
(matrices, dims=None)¶ Contract a TreeBasedTensor with matrices built from their diagonals.
The second dimension of the matrix matrices[k] is contracted with the k-th dimension of self, with the indices k given in dims (if provided).
- Parameters
- matricesnumpy.ndarray or list of numpy.ndarray
The diagonals of the matrices to use in the product.
- dimslist or numpy.ndarray, optional
Indices of the contractions. The default is None, indicating all the dimensions.
- Returns
- TreeBasedTensor
The tensor after the contractions with the matrices.
-
tensor_matrix_product
(matrices, dims=None)¶ Contract a tensor with matrices.
The second dimension of the matrix matrices[k] is contracted with the k-th dimension of self, with the indices k given in dims (if provided).
- Parameters
- matricesnumpy.ndarray or list of numpy.ndarray
The matrices to use in the product.
- dimslist or numpy.ndarray, optional
Indices of the contractions. The default is None, indicating all the dimensions.
- Returns
- TreeBasedTensor
The tensor after the contractions with the matrices.
-
tensor_matrix_product_eval_diag
(matrices, dims=None)¶ Evaluate the diagonal of a tensor obtained by contraction with matrices.
Provides the diagonal of the tensor obtained by contracting the tensor with matrices H[k] along dimensions dims(k)+1, for k = 0, …, dims.size-1.
- Parameters
- matriceslist
The matrices to use in the product.
- dimslist or numpy.ndarray, optional
Indices of the contractions. The default is None, indicating all the dimensions.
- Returns
- outtensap.FullTensor
The result of the contractions of the tensor with the matrices.
-
static
tensor_train
(cores, dims=None)¶ Create a tree-based tensor with a tensor-train structure.
- Parameters
- coreslist or numpy.ndarray
List of tensap.FullTensor, each associated with one dimension.
- dimslist or numpy.ndarray, optional
Dimension associated with each core. The default is None, indicating that the i-th core is associated with the dimension (i-1).
- Returns
- tensap.TreeBasedTensor
The tree-based tensor with a tensor-train structure.
-
tensor_vector_product
(vectors, dims=None)¶ Compute the contraction of the tensor with vectors.
Compute the contraction of self with each vector contained in the list vectors along dimensions specified by dims. The operation is such that V[k] is contracted with the dims[k]-th dimension of self.
- Parameters
- vectorsnumpy.ndarray or list of numpy.ndarray
The vectors to use in the product.
- dimslist or numpy.ndarray, optional
Indices of the contractions. The default is None, indicating all the dimensions.
- Returns
- TreeBasedTensor
The tensor after the contractions with the vectors.
-
tensordot_matrix_product_except_dim
(tensor2, matrices, dim)¶ Particular type of contraction.
Compute a special contraction of two tensors self, tensor2, a list of matrices matrices and a particular dimension dim. Note that dim must be a scalar, while matrices must be a list array with x.self.order elements.
- Parameters
- tensor2TreeBasedTensor
The second tensor of the contraction.
- matriceslist
The list of matrices of the contraction.
- dimint
The excluded dimension.
- Returns
- numpy.ndarray
The result of the contraction.
-
update_attributes
()¶ Update the attributes of the TreeBasedTensor.
- Returns
- TreeBasedTensor
A TreeBasedTensor with updated attributes.
-
static
zeros
(tree, ranks=None, shape=None, is_active_node=None)¶ Create a tensor of shape shape and tree-based rank ranks with node tensors generated with the method zeros of numpy.
- Parameters
- treetensap.FullTensor
The tensap.FullTensor of the TreeBasedTensor.
- ranksnumpy.ndarray, list or ‘random’, optional
Ranks of the TreeBasedTensorFormat. The default is None, generating random ranks between 1 and 5.
- shapenumpy.ndarray, list or ‘random’, optional
The size of the spaces of the leaves of the tree. The default is None, assigning ranks to shape.
- is_active_nodenumpy.ndarray, list or ‘random’, optional
Booleans indicating if the node is active. The default is None, for which all the nodes are active.
- Returns
- TreeBasedTensor
A TreeBasedTensor with the input characteristics.