Previous topic

Canonical Decomposition

Next topic

Indices and tables

This Page

Miscellaneous

TensorToolbox.core.idxunfold(dlist, idxs)

Find the index corresponding to the unfolded (flat) version of a tensor

Parameters:
  • dlist (list,int) – list of integers containing the dimensions of the tensor
  • idxs (list,int) – tensor index
Returns:

index for the flatten tensor

TensorToolbox.core.idxfold(dlist, idx)

Find the index corresponding to the folded version of a tensor from the flatten version

Parameters:
  • dlist (list,int) – list of integers containing the dimensions of the tensor
  • idx (int) – tensor flatten index
Returns:

list of int – the index for the folded version

Note:

this routine can be used to get the indexes of a TTmat from indices of a matkron (matrix obtained using np.kron): (i,j) in N^d x N^d -> ((i_1,..,i_d),(j_1,..,j_d)) in (N x .. x N) x (N x .. x N)

TensorToolbox.core.expand_idxs(idxs_in, shape, ghost_shape=None, fix_dims=None, fix_idxs=None)

From a tuple of indicies, apply all the unslicing transformations and restore the fixed indices in order to extract values from a tensor with a certain ghost_shape. If ghost_shape==None, fix_dims==None and fix_idxs==None, then this performs only an unslicing of the index and it is assumed ghost_shape=shape.

Parameters:
  • idxs_in (tuple) – indexing tuple. The admissible slicing format is the same used in np.ndarray.
  • shape (tuple) – shape of the tensor
  • ghost_shape (tuple) – shape of the tensor without fixed indices
  • fix_dims (list) – whether there are dimensions which had been fixed and need to be added.
  • fix_idxs (list) – fixed idxs for each fixed dimension.
Returns:

tuple (lidxs,out_shape,transpose_list_shape). lidxs is an iterator of the indices. out_shape is a tuple containing the shape of the output tensor. transpose_list_shape is a flag indicating whether the output format need to be transposed (behaving accordingly to np.ndarray).

TensorToolbox.core.matkron_to_mattensor(A, nrows, ncols)

This function reshapes a 2D-matrix obtained as kron product of len(nrows)==len(ncols) matrices, to a len(nrows)-tensor that can be used as input for the TTmat constructor. Applies the Van Loan-Pitsianis reordering of the matrix elements.

Parameters:
  • A (ndarray) – 2-dimensional matrix
  • nrows,ncols (list,int) – number of rows and number of columns of the original matrices used for the kron product
TensorToolbox.core.mat_to_tt_idxs(rowidxs, colidxs, nrows, ncols)

Mapping from the multidimensional matrix indexing to the tt matrix indexing

(rowidxs,colidxs) = ((i_1,...,i_d),(j_1,...,j_d)) -> (l_1,...,l_d)

Parameters:
  • rowidxs,colidxs (tuple,int) – list of row and column indicies. len(rowidxs) == len(colidxs)
  • nrows,ncols (tuple,int) – dimensions of matrices
Returns:

tuple,int indices in the tt format

TensorToolbox.core.tt_to_mat_idxs(idxs, nrows, ncols)

Mapping from the tt matrix indexing to the multidimensional matrix indexing

(l_1,...,l_d) -> (rowidxs,colidxs) = ((i_1,...,i_d),(j_1,...,j_d))
Parameters:
  • idxs (tuple,int) – list of tt indicies. len(idxs) == len(nrows) == len(ncols)
  • nrows,ncols (tuple,int) – dimensions of matrices
Returns:

(rowidxs,colidxs) = ((i_1,..,i_d),(j_1,..,j_d)) indices in the matrix indexing

TensorToolbox.core.maxvol(A, delta=0.01, maxit=100)

Find the rxr submatrix of maximal volume in A(nxr), n>=r

Parameters:
  • A (ndarray) – two dimensional array with (n,r)=shape(A) where r<=n
  • delta (float) – stopping cirterion [default=1e-2]
  • maxit (int) – maximum number of iterations [default=100]
Returns:

(I,AsqInv,it) where I is the list or rows of A forming the matrix with maximal volume, AsqInv is the inverse of the matrix with maximal volume and it is the number of iterations to convergence

Raises:

raise exception if the dimension of A is r>n or if A is singular

Raises:

ConvergenceError if convergence is not reached in maxit iterations

TensorToolbox.core.lowrankapprox(A, r, Jinit=None, delta=1e-05, maxit=100, maxvoleps=0.01, maxvolit=100)

Given a matrix A nxm, find the maximum volume submatrix with rank r<n,m.

Parameters:
  • A (ndarray) – two dimensional array with dimension nxm
  • r (int) – rank of the maxvol submatrix
  • Jinit (list) – list of integers containing the r starting columns. If None then pick them randomly.
  • delta (float) – accuracy parameter
  • maxit (int) – maximum number of iterations in the lowrankapprox routine
  • maxvoleps (float) – accuracy parameter for each usage of the maxvol algorithm
Parma int maxvolit:
 

maximum number of iterations in the maxvol routine

Returns:

(I,J,AsqInv,it) where I and J are the list of rows and columns of A that compose the submatrix of maximal volume, AsqInv is the inverse of such matrix and it is the number of iteration to convergence

TensorToolbox.core.reort(u, uadd)

Golub-Kahan reorthogonalization

TensorToolbox.core.load(filename, load_data=True)

Used to load TensorToolbox data.

Parameters:
  • filename (string) – path to the filename. This must be the main filename with no extension.
  • load_data (bool) – whether to load additional data from ”.h5” files.
class TensorToolbox.core.storable_object(store_location='', store_freq=None, store_overwrite=False, store_object=None)

Constructor of objects that can be stored

Parameters:
  • store_location (string) – path to the storage file
  • store_freq (int) – Number of seconds between each storage
  • store_overwrite (bool) – whether to overwrite existing files
  • store_object (object) – parent object to be stored in place of the current object
Attributes:
serialize_list: list of objects that must be serialized. subserialize_list: list of objects for which the serialization must be done separately.
h5load(h5file)

Used to load additional data in hdf5 format. To be redefined in subclasses.

h5store(h5file)

Used to store additional data in hdf5 format. To be redefined in subclasses.

load(h5_location=None)

Used to load additional data.

set_store_freq(store_freq)

Set a new store frequency for the object

Parameters:store_freq (int) – new store location
set_store_location(store_location)

Set a new store location for the object

Parameters:store_location (string) – new store location
store(force=False)

Used to store any object in the library.

Parameters:force (bool) – force storage before time
to_v_0_3_0(store_location)

To be implemented for objects that need to be upgraded to v0.3.0.

Parameters:filename (string) – path to the filename. This must be the main filename with no extension.
TensorToolbox.core.ttcross_store(path, TW, TTapp)

Used to store the computed values of a TTcross approximation. Usually needed when the single function evaluation is demanding or when we need to restart TTcross later on.

Parameters:
  • path (string) – path pointing to the location where to store the data
  • TW (TensorWrapper) – Tensor wrapper used to build the ttcross approximation. TW.get_data(), TW.get_X() and TW.get_params() will be stored.
  • TTapp (TTvec) – TTcross approximation. TTapp.ttcross.Jinit will be stored.

Deprecated since version 0.3.0: Use the objects’ methods store().

TensorToolbox.core.to_v_0_3_0(filename)

Used to upgrade the storage version from version <0.3.0 to version 0.3.0

Parameters:filename (string) – path to the filename. This must be the main filename with no extension.
TensorToolbox.core.randvec(d, N)

Returns the rank-1 multidimensional random vector in Tensor Train format

Args:
d (int): number of dimensions N (int or list): If int then uniform sizes are used for all the dimensions, if list of int then len(N) == d and each dimension will use different size
Returns:
TTvec The rank-1 multidim random vector in Tensor Train format
TensorToolbox.core.zerosvec(d, N)

Returns the rank-1 multidimensional vector of zeros in Tensor Train format

Args:
d (int): number of dimensions N (int or list): If int then uniform sizes are used for all the dimensions, if list of int then len(N) == d and each dimension will use different size
Returns:
TTvec The rank-1 multidim vector of zeros in Tensor Train format
TensorToolbox.core.eye(d, N)

Returns the multidimensional identity operator in Tensor Train format

Args:
d (int): number of dimensions N (int or list): If int then uniform sizes are used for all the dimensions, if list of int then len(N) == d and each dimension will use different size
Returns:
TTmat The multidim identity matrix in Tensor Train format
Note:
TODO: improve construction avoiding passage through Candecomp
TensorToolbox.core.randmat(d, nrows, ncols)

Returns the rank-1 multidimensional random matrix in Tensor Train format

Args:
d (int): number of dimensions nrows (int or list): If int then uniform sizes are used for all the dimensions, if list of int then len(nrows) == d and each dimension will use different size ncols (int or list): If int then uniform sizes are used for all the dimensions, if list of int then len(ncols) == d and each dimension will use different size
Returns:
TTmat The rank-1 multidim random matrix in Tensor Train format
TensorToolbox.core.QTTzerosvec(d, N, base)

Returns the rank-1 multidimensional vector of zeros in Quantics Tensor Train format

Args:
d (int): number of dimensions N (int or list): If int then uniform sizes are used for all the dimensions, if list of int then len(N) == d and each dimension will use different size base (int): QTT base
Returns:
QTTvec The rank-1 multidim vector of zeros in Tensor Train format
class TensorToolbox.core.SQTT(f, grids, params, range_dim=0, marshal_f=True, base=2, surrogateONOFF=False, surrogate_type=None, orders=None, orderAdapt=None, eps=0.0001, method='ttdmrg', rs=None, fix_rank=False, Jinit=None, delta=0.0001, maxit=100, mv_eps=1e-06, mv_maxit=100, kickrank=None, store_location='', store_overwrite=False, store_freq=0)

Constructor of the Spectral Quantics Tensor Train approximation. Given a function f(x,theta,params):(Is, It) -> R with dim(Is)=n and dim(It)=d, construct an approximation of g(theta,params): It -> h_t(Is). For example Is could be the discretization of a spatial dimension, and It some parameter space, so that f(x,theta,params) describes a scalar field depending some parameters that vary in It. The params in the definition of f can be constants used by the function or othere objects that must be passed to the function definition.

Parameters:
  • f (function) – multidimensional function to be approximated with format f(x,theta,params)
  • grids (list) – this is a list with len(grids)=dim(Is)+dim(It) which can contain: a) 1-dimensional numpy.array of points discretizing the i-th dimension, b) a tuple (PolyType,QuadType,PolyParams,span) where PolyType is one of the polynomials available in SpectralToolbox.Spectral1D and QuadType is one of the quadrature rules associated to the selected polynomial and PolyParams are the parameters for the selected polynomial. span is a tuple defining the left and right end for dimension i (Example: (-3,np.inf)) c) a tuple (QuadType,span) where QuadType is one of the quadrature rules available in SpectralToolbox.Spectral1D without the selection of a particular polynomial type, and span is defined as above.
  • params (object) – any list of parameters to be passed to the function f
  • range_dim (int) – define the dimension of the spatial dimension Is. For functionals f(theta,params), dim(Is)=0. For scalar fileds in 3D, dim(Is)=3.
  • marshal_f (bool) – whether to marshal the function f or not. For MPI support, the function f must be marshalable (does this adverb exists??).
  • base (int) – base parameter for Quantics Tensor Train
  • surrogateONOFF (bool) – whether to construct the surrogate or not
  • surrogate_type (str) – whether the surrogate will be an interpolating surrogate (TensorTrain.LINEAR_INTERPOLATION or TensorTrain.LAGRANGE_INTERPOLATION) or a projection surrogate (TensorTrain.PROJECTION)
  • orders (list) – polynomial orders for each dimension if TensorTrain.PROJECTION is used. If orderAdapt==True then the orders are starting orders that can be increased as needed by the construction algorithm. If this parameter is not provided but orderAdapt==True, then the starting order is 1 for all the dimensions.
  • orderAdapt (bool) – whether the order is fixed or not.
  • stt_store_location (str) – path to a file where function evaluations can be stored and used in order to restart the construction.
  • stt_store_overwrite (bool) – whether to overwrite pre-existing files
  • stt_store_freq (int) – storage frequency. Determines every how many seconds the state is stored. stt_store_freq==0 stores every time it is possible.
  • empty (bool) – Creates an instance without initializing it. All the content can be initialized using the setstate() function.

Note

For a description of the remaining parameters see TTvec.

__getitem__()
__call__(x_in, verbose=False)

Evaluate the surrogate on points x_in

Parameters:x_in (np.ndarray) – 1 or 2 dimensional array of points in the parameter space where to evaluate the function. In 2 dimensions, each row is an entry, i.e. x_in.shape[1] == self.param_dim
Returns:an array with dimension equal to the space dimension (range_dim) plus one. If A is the returned vector and range_dim=2, then A[i,:,:] is the value of the surrogate for x_in[i,:]