tensap.functions package

Submodules

tensap.functions.compositional_model_function module

Module compositional_model_function

class tensap.functions.compositional_model_function.CompositionalModelFunction(tree, fun, measure)

Bases: tensap.functions.function.Function

Class CompositionalModelFunction.

Attributes
treetensap.DimensionTree

The dimension tree associated with the function.

funlist or numpy.ndarray or function or tensap.Function

A list or array of functions, one for each node of the tree, or one function, identical for all the internal nodes of the tree.

measuretensap.Measure

The measure associated with the function.

Methods

__call__(x[, return_f])

Call self as a function.

eval(x)

Evaluate the CompositionalModelFunction at points x.

eval_on_tensor_grid(x)

Evaluate the Function on a grid x.

fplot([support, n_points])

Plot the function on a support using a given number of points.

partial_evaluation(not_alpha, x_not_alpha)

Return the partial evaluation of a function f(x) = f(x_alpha,x_not_alpha), a function f_alpha(.) = f(., x_not_alpha) for fixed values x_not_alpha of the variables with indices not_alpha.

random([n, measure])

Evaluates the function at n points x drawn randomly according to the ProbabilityMeasure in measure if provided, or in self.measure.

store_eval(x)

Evaluate the function, reuising previous evaluations if possible, and storing the new evaluations in self.

surf([n])

Surface plot of the bivariate function.

test_error(g[, n, measure])

Compute the test error associated with the function, using a function g or some of its evaluations as a reference.

eval(x)

Evaluate the CompositionalModelFunction at points x.

Parameters
xlist or numpy.ndarray

The points at which the function is to be evaluated.

Returns
numpy.ndarray

The evaluations of the function at points x.

tensap.functions.function module

Module function.

class tensap.functions.function.Function

Bases: object

Class Function.

Attributes
dimint

The dimension of the input of the function.

measuretensap.Measure

The measure associated with the function.

output_shapeint or list or numpy.ndarray

The shape of the output of the function.

evaluation_at_multiple_pointsbool

Indicates if the function can be evaluated at multiple points at once.

storebool

Indicates if the Function object should store the evaluations of the function.

Methods

__call__(x[, return_f])

Call self as a function.

eval(x)

Evaluate the function at the points x.

eval_on_tensor_grid(x)

Evaluate the Function on a grid x.

fplot([support, n_points])

Plot the function on a support using a given number of points.

partial_evaluation(not_alpha, x_not_alpha)

Return the partial evaluation of a function f(x) = f(x_alpha,x_not_alpha), a function f_alpha(.) = f(., x_not_alpha) for fixed values x_not_alpha of the variables with indices not_alpha.

random([n, measure])

Evaluates the function at n points x drawn randomly according to the ProbabilityMeasure in measure if provided, or in self.measure.

store_eval(x)

Evaluate the function, reuising previous evaluations if possible, and storing the new evaluations in self.

surf([n])

Surface plot of the bivariate function.

test_error(g[, n, measure])

Compute the test error associated with the function, using a function g or some of its evaluations as a reference.

abstract eval(x)

Evaluate the function at the points x.

Parameters
xlist or numpy.ndarray

The points at which the function is to be evaluated.

Returns
numpy.ndarray

The evaluations of the function at the points x.

eval_on_tensor_grid(x)

Evaluate the Function on a grid x.

Parameters
xtensap.TensorGrid

The tensap.ensorGrid used for the evaluation..

Returns
fxnumpy.ndarray

The evaluation of the Function on the grid.

Raises
NotImplementedError

If the method is not implemented.

ValueError

If x is not a tensap.TensorGrid object.

fplot(support=None, n_points=100, *args, **kwargs)

Plot the function on a support using a given number of points.

Parameters
supportlist or numpy.ndarray, optional

The support of the plot. The default is None, indicating to use the truncated_support of self.measure.

n_pointsint, optional

The number of points used for the plot. The default is 100.

*argstuple

Additional parameters used by the function matplotlib.pyplot.plot.

Returns
None.
partial_evaluation(not_alpha, x_not_alpha)

Return the partial evaluation of a function f(x) = f(x_alpha,x_not_alpha), a function f_alpha(.) = f(., x_not_alpha) for fixed values x_not_alpha of the variables with indices not_alpha.

Parameters
not_alphalist or numpy.ndarray

The indices of the fixed variables.

x_not_alphanumpy.ndarray

The points at which the function is evaluated in the dimensions not_alpha.

Returns
f_alphatensap.UserDefinedFunction

The partial evaluation of the Function.

Raises
ValueError

If the Function has an empty attribute dim.

random(n=1, measure=None)

Evaluates the function at n points x drawn randomly according to the ProbabilityMeasure in measure if provided, or in self.measure.

Parameters
nint, optional

The number of random evaluations. The default is 1.

measuretensap.ProbabilityMeasure, optional

The probability measure according to which the points x are drawn. The default is None, indicating to use self.measure.

Returns
numpy.ndarray

The evaluations of the function at the points x.

xnumpy.ndarray

The points at which the function is to be evaluated.

Raises
ValueError

If the provided measure is not a tensap.ProbabilityMeasure.

store_eval(x)

Evaluate the function, reuising previous evaluations if possible, and storing the new evaluations in self.

Parameters
xnumpy.ndarray

The input points.

Returns
ynumpy.ndarray

The evaluations of the Function.

tensap.Function

The Function with stored evaluations, for future reuse.

surf(n=None, *args)

Surface plot of the bivariate function.

Parameters
nlist or numpy.ndarray, optional

The number of points used for the surface plot in each dimension. The default is [1000, 1000].

*argstuple

Additional parameters used by matplotlib.pyplot’s plot_surface function.

Returns
axmatplotlib.axes._subplots.AxesSubplot

The surface plot as a matplotlib.axes._subplots.AxesSubplot object.

test_error(g, n=1000, measure=None)

Compute the test error associated with the function, using a function g or some of its evaluations as a reference. A measure can be provided to randomly draw the test input data.

Parameters
gtensap.Function or numpy.ndarray

The reference function or evaluations of it.

nint or numpy.ndarray, optional

The test sample size, or the test input data. The default is 1000 test input points.

measuretap.ProbabilityMeasure, optional

A probability measure used to draw the test input data. The default is None, indicating to either use self.measure, g.measure or the provided input points.

Returns
err_l2float

The L2 error.

err_linffloat

The L-infinity error.

tensap.functions.functional_tensor module

Module functional_tensor.

class tensap.functions.functional_tensor.FunctionalTensor(tensor=None, bases=None, fdims=None)

Bases: tensap.functions.function.Function

Class FunctionalTensor.

Attributes
tensorTensor or tensap.FunctionalTensor, optional

The tensor of the FunctionalTensor. The default is None.

baseslist or tensap.FunctionalBases, optional

The functional bases of the FunctionalTensor. The default is None.

fdimslist or numpy.ndarray, optional

The dimensions corresponding to the bases. The default is None.

Methods

__call__(x[, return_f])

Call self as a function.

conditional_expectation(dims, *args)

Compute the conditional expectation of self with respect to the random variables dims (a subset of [1, …, d]).

derivative(n)

Compute the n-th order derivative of self.

dot_product_expectation(f_2[, fdims])

Computes the expectation of self(X)f_2(X) where X is the random vector associated with self.bases if measure is not provided, and measure otherwise.

eval(x, *dims)

Evaluate self at the points x.

eval_derivative(n, x, *dims)

Evaluate the n-th order derivative of self at the points x.

eval_on_grid(x[, dims])

Compute evaluations of self at points x.

eval_on_tensor_grid(x)

Evaluate the Function on a grid x.

eval_with_bases_evals(bases_eval[, dims])

Evaluate the function self, given evaluations of self.bases.

expectation(*measure)

Compute the expectation of the random variable self(measure) if measure is provided, and of self(self.bases.measure) otherwise.

fplot([support, n_points])

Plot the function on a support using a given number of points.

get_random_vector()

Return the RandomVector associated with self.bases.

is_random()

Determine if self is random.

mean(*measure)

Compute the expectation of the random variable self(measure) if measure is provided, and of self(self.bases.measure) otherwise.

norm(*measure)

Return the L^2 norm of self(X), with X = measure if provided, and X = self.bases.measure otherwise.

parameter_gradient_eval(alpha[, x])

Compute the gradient of the function with respect to its alpha-th parameter, evaluated at some points.

partial_evaluation(not_alpha, x_not_alpha)

Return the partial evaluation of a function f(x) = f(x_alpha,x_not_alpha), a function f_alpha(.) = f(., x_not_alpha) for fixed values x_not_alpha of the variables with indices not_alpha.

random(*args, **kwargs)

Evaluates the function at n points x drawn randomly according to the ProbabilityMeasure in measure if provided, or in self.measure.

random_dims(dims, *args[, nargout])

Evaluate the function in dimensions dims using n points drawn randomly according to measure if provided, or to self.bases.measure.marginal(dims) otherwise.

std(*measure)

storage()

Return the storage requirement of the FunctionalTensor.

store_eval(x)

Evaluate the function, reuising previous evaluations if possible, and storing the new evaluations in self.

surf([n])

Surface plot of the bivariate function.

test_error(g[, n, measure])

Compute the test error associated with the function, using a function g or some of its evaluations as a reference.

variance(*measure)

Compute the variance of the random variable self(measure) if measure is provided, and of self(self.bases.measure) otherwise.

variance_conditional_expectation(alpha)

Compute the variance of the conditional expectation of self in dimensions in alpha.

parameter_gradient_eval_dmrg

conditional_expectation(dims, *args)

Compute the conditional expectation of self with respect to the random variables dims (a subset of [1, …, d]).

The expectation with respect to other variables (in the complementary set of dims) is taken with respect to the probability measure given by a tensap.RandomVector if provided as an additional argument, or with respect to the probability measure associated with the corresponding bases of self.

Parameters
dimslist or numpy.ndarray

The dimensions of the random variables with respect to which the conditional expectation is to be computed.

*argstuple

Tuple containing a tensap.randomVector giving the probability measure of the variables other than the ones in dims. If not provided, the measure is infered from self.bases.measure.

Returns
tensap.FunctionalTensor

The conditional expectation of self with respect to the random variables dims, as a len(dims)-order tensor.

derivative(n)

Compute the n-th order derivative of self.

Parameters
nint

The order of derivation.

Returns
dftensap.FunctionalTensor

The n-th order derivative of self.

dot_product_expectation(f_2, fdims=None, *measure)

Computes the expectation of self(X)f_2(X) where X is the random vector associated with self.bases if measure is not provided, and measure otherwise.

For tensor-valued functions of X (len(X)<self.order), fdims specifies the dimensions of self and f_2 corresponding to theRandomVector X.

Parameters
f_2tensap.FunctionalTensor

The second functional tensor of the product.

fdimslist of numpy.ndarray, optional

Specifies the dimensions of self and f_2 corresponding to theRandomVector X. The default is None, indicating all the dimensions

*measuretensap.RandomVector, optional

The measure used for the computation of the product. If not provided, indicates to use self.bases.measure.

Returns
float or Tensor

The expectation of self(X)f_2(X).

Raises
ValueError

If the two tensors do not have the same order and fdims is not specified.

NotImplementedError

If the bases of self and f_2 are not equal.

eval(x, *dims)

Evaluate self at the points x.

If dims is provided, compute the partial evaluations of self at points x in dimensions in dims.

Parameters
xlist or numpy.ndarray or None

The points at which the function is to be evaluated. If x is None and self.evaluated_bases, evaluates the function using the evaluations of the bases.

*dimslist or numpy.ndarray, optional

The dimensions of the partial evaluation. If not provided, evaluate the function in all dimensions.

Returns
numpu.ndarray or Tensor

The evaluations of self at the points x.

eval_derivative(n, x, *dims)

Evaluate the n-th order derivative of self at the points x.

If dims is provided, compute the partial evaluations of the n-th order derivative of self at points x n dimensions in dims.

Parameters
nint

The order of derivation.

xlist or numpy.ndarray

The points at which the function is to be evaluated.

*dimslist or numpy.ndarray, optional

The dimensions of the partial evaluation. If not provided, evaluate the function in all dimensions.

Returns
numpu.ndarray or Tensor

The evaluations of the n-th derivative of self at the points x.

eval_on_grid(x, dims=None)

Compute evaluations of self at points x.

Parameters
xlist

List such that x[k] contains the grid associated with the (k+1)-th variable.

dimslist or numpy.ndarray, optional

Array indicating the dimensions associated with x. The default is None, indicating all the dimensions.

Returns
outFunctionalTensor or tensap.Tensor

The evaluations of self at points x.

eval_with_bases_evals(bases_eval, dims=None)

Evaluate the function self, given evaluations of self.bases.

Parameters
bases_evallist or numpy.ndarray

The evaluations of self.bases.

dimslist or numpy.ndarray, optional

The dimensions of the evaluation. The default is None, indicating all the dimensions.

Returns
outnumpy.ndarray or Tensor

The (partially) evaluated function.

expectation(*measure)

Compute the expectation of the random variable self(measure) if measure is provided, and of self(self.bases.measure) otherwise.

Parameters
*measuretensap.RandomVector, optional

The measure used for the computation of the expectation. If not provided, indicates to use self.bases.measure.

Returns
float or Tensor

The expectation of the function.

get_random_vector()

Return the RandomVector associated with self.bases.

Returns
tensap.RandomVector

The RandomVector associated with self.bases.

is_random()

Determine if self is random.

Returns
bool

Boolean indicating if if self is random.

mean(*measure)

Compute the expectation of the random variable self(measure) if measure is provided, and of self(self.bases.measure) otherwise.

Parameters
*measuretensap.RandomVector, optional

The measure used for the computation of the mean. If not provided, indicates to use self.bases.measure.

Returns
float or Tensor

The mean of the function.

norm(*measure)

Return the L^2 norm of self(X), with X = measure if provided, and X = self.bases.measure otherwise.

If self.evaluatedBases is true, without additional information, return the canonical norm of self.tensor.

Parameters
*measuretensap.RandomVector, optional

The measure used for the computation of the norm. If not provided, indicates to use self.bases.measure.

Returns
float or Tensor

The L^2 norm of self(X).

parameter_gradient_eval(alpha, x=None, *args)

Compute the gradient of the function with respect to its alpha-th parameter, evaluated at some points.

Parameters
alphaint

The number of the parameter with respect to which compute the gradient of self.

xlist or numpy.ndarray, optional

The points at which the gradient is to be evaluated. The default is None, indicating to use self.bases if self.evaluated_bases is True.

Returns
gradTensor

The gradient of the function with respect to its alpha-th parameter, evaluated at some points.

Raises
ValueError

If no input points are provided.

parameter_gradient_eval_dmrg(alpha, x=None, dmrg_type='dmrg', *args)
random(*args, **kwargs)

Evaluates the function at n points x drawn randomly according to the ProbabilityMeasure in measure if provided, or in self.measure.

Parameters
nint, optional

The number of random evaluations. The default is 1.

measuretensap.ProbabilityMeasure, optional

The probability measure according to which the points x are drawn. The default is None, indicating to use self.measure.

Returns
numpy.ndarray

The evaluations of the function at the points x.

xnumpy.ndarray

The points at which the function is to be evaluated.

Raises
ValueError

If the provided measure is not a tensap.ProbabilityMeasure.

random_dims(dims, *args, nargout=1)

Evaluate the function in dimensions dims using n points drawn randomly according to measure if provided, or to self.bases.measure.marginal(dims) otherwise.

Parameters
dimslist or numpy.ndarray

The dimensions of the bases to be evaluated.

nint, optional

The number of random evaluations. The default is 1.

measuretensap.ProbabilityMeasure, optional

The probability measure used for the generation of the input points. The default is None, indicating to use self.measure.marginal(dims).

Returns
bases_evallist or numpy.ndarray

Random evaluations of the function.

xnumpy.ndarray

The input points, grouped by basis.

std(*measure)

‘ Compute the standard deviation of the random variable self(measure) if measure is provided, and of self(self.bases.measure) otherwise.

Parameters
*measuretensap.RandomVector, optional

The measure used for the computation of the standard deviation. If not provided, indicates to use self.bases.measure.

Returns
vfloat or Tensor

The standard deviation of the function.

storage()

Return the storage requirement of the FunctionalTensor.

Returns
int

The storage requirement of the FunctionalTensor.

variance(*measure)

Compute the variance of the random variable self(measure) if measure is provided, and of self(self.bases.measure) otherwise.

Parameters
*measuretensap.RandomVector, optional

The measure used for the computation of the variance. If not provided, indicates to use self.bases.measure.

Returns
varfloat or Tensor

The variance of the function.

variance_conditional_expectation(alpha)

Compute the variance of the conditional expectation of self in dimensions in alpha.

Parameters
alphalist or numpy.ndarray

Array containing the dimensions (either explicitely or using booleans) in which the variance of the conditional expectation is computed.

Returns
vnumpy.ndarray

The variance of the conditional expectation of self in dimensions in alpha.

tensap.functions.multivariate_functions_benchmark module

Module multivariate_functions_benchmark

tensap.functions.multivariate_functions_benchmark.multivariate_functions_benchmark(case, *args)

Generate different functions used to benchmark the package.

Parameters
casestr

The name of the function. Can be ‘borehole’, ‘ishigami’, ‘sin_of_a_sum’, ‘linear_additive’, ‘linear_rank_one’, ‘quadratic_rank_one’, ‘canonical_rank_2’, ‘mixture’, ‘field’, ‘oscillatory’, ‘product_peak’, ‘corner_peak’, ‘gaussian’, ‘continuous’, ‘discontinuous’, ‘henon_heiles’, ‘sobol’, ‘anisotropic’, ‘polynomial’.

*argstuple

Parameters of the function.

Returns
function

The asked function.

tensap.RandomVector

Input random variables.

Raises
NotImplementedError

If the function is not implemented.

tensap.functions.tensorized_function module

Module tensorized_function.

class tensap.functions.tensorized_function.TensorizedFunction(fun, tens=None)

Bases: tensap.functions.function.Function

Class TensorizedFunction.

Attributes
funfunction or tensap.Function

A function of (d+1)*dim variables.

tenstensap.Tensorizer

A tensap.Tensorizer object.

Methods

__call__(x[, return_f])

Call self as a function.

domain()

Return the domain of the function.

eval(z)

Evaluate the function at the points x.

eval_on_tensor_grid(x)

Evaluate the Function on a grid x.

fplot([support, n_points])

Plot the function on a support using a given number of points.

partial_evaluation(not_alpha, x_not_alpha)

Return the partial evaluation of a function f(x) = f(x_alpha,x_not_alpha), a function f_alpha(.) = f(., x_not_alpha) for fixed values x_not_alpha of the variables with indices not_alpha.

random([n, measure])

Evaluates the function at n points x drawn randomly according to the ProbabilityMeasure in measure if provided, or in self.measure.

store_eval(x)

Evaluate the function, reuising previous evaluations if possible, and storing the new evaluations in self.

surf([n])

Surface plot of the bivariate function.

test_error(g[, n, measure])

Compute the test error associated with the function, using a function g or some of its evaluations as a reference.

domain()

Return the domain of the function.

Returns
supnumpy.ndarray

The domain of the function.

eval(z)

Evaluate the function at the points x.

Parameters
xlist or numpy.ndarray

The points at which the function is to be evaluated.

Returns
numpy.ndarray

The evaluations of the function at the points x.

tensap.functions.tensorizer module

Module tensorizer.

class tensap.functions.tensorizer.Tensorizer(b, d, dim=1, X=None, Y=None)

Bases: object

Class Tensorizer.

Attributes
bint

The base of the map.

dint

The resolution of the map.

dimint

The input dimension of the function to be tensorized.

Xtensap.RandomVector

Random vector used for the map.

Ytensap.RandomVector

Random vector used for the map.

Methods

inverse_map(z)

Evaluate the map at points z = (i_1, …, i_d, y), returning x such that self.map(x) = z.

map(x[, nargout])

Evaluate the map at points x, returning y and i such that self.map(x) = (i_1, …, i_d, y).

tensorize(fun)

Tensorize a provided function defined on self.X.support().

tensorized_function_functional_bases([h])

Return a tensap.FunctionalBases object associated with the provided basis or basis function(s) and the Tensorizer object.

u2z(u, b, d[, nargout])

Return the representation of numbers on [0, 1] in base b with resolution d.

z2u(z, b)

Return the representation of numbers in base b in decimal on [0, 1].

inverse_map(z)

Evaluate the map at points z = (i_1, …, i_d, y), returning x such that self.map(x) = z.

Parameters
zlist or numpy.ndarray

The points at which the inverse map is to be evaluated.

Returns
numpy.ndarray

The points x such that self.map(x) = z.

map(x, nargout=1)

Evaluate the map at points x, returning y and i such that self.map(x) = (i_1, …, i_d, y).

Parameters
xlist or numpy.ndarray

The points at which the map is to be evaluated.

nargoutint, optional

The number of outputs. The default is 1, returning a horizontal stack of i and y. Set to 2 to return y and i separately.

Returns
numpy.ndarray

Either (i_1, …, i_d, y) if nargout == 1, or y if nargout == 2.

numpy.ndarray

(i_1, …, i_d), if nargout == 2.

tensorize(fun)

Tensorize a provided function defined on self.X.support().

Parameters
funfunction or tensap.Function

The function to be tensorized.

Returns
tensap.TensorizedFunction

The tensorized function.

Raises
ValueError

If the provided argument is neither a tensap.Function nor a function.

tensorized_function_functional_bases(h=1)

Return a tensap.FunctionalBases object associated with the provided basis or basis function(s) and the Tensorizer object.

Parameters
htensap.FunctionalBases or tensap.FunctionalBasis or function or
list or scalar, optional

The function used to generate the basis. The default is the function 1.

Returns
tensap.FunctionalBases

The functional bases.

static u2z(u, b, d, nargout=1)

Return the representation of numbers on [0, 1] in base b with resolution d.

Parameters
ulist or numpy.ndarray

The inputs to be converted in base b with resolution d.

bint

The base.

dint

The resolution.

nargoutint, optional

The number of outputs. The default is 1, returning a horizontal stack of i and y. Set to 2 to return y and i separately.

Returns
numpy.ndarray

Either (i_1, …, i_d, y) if nargout == 1, or y if nargout == 2.

numpy.ndarray

(i_1, …, i_d), if nargout == 2.

static z2u(z, b)

Return the representation of numbers in base b in decimal on [0, 1].

Parameters
zlist or numpy.ndarray

The numbers in base b.

bint

The base.

Returns
numpy.ndarray

The decimal representation of the inputs on [0, 1].

tensap.functions.user_defined_function module

Module user_defined_function.

class tensap.functions.user_defined_function.UserDefinedFunction(fun, dim, shape=None)

Bases: tensap.functions.function.Function

Class UserDefinedFunction.

Attributes
funfunction or tensap.Function

The function defining the UserDefinedFunction object.

Methods

__call__(x[, return_f])

Call self as a function.

eval(x)

Evaluate the function at the points x.

eval_on_tensor_grid(x)

Evaluate the Function on a grid x.

fplot([support, n_points])

Plot the function on a support using a given number of points.

partial_evaluation(not_alpha, x_not_alpha)

Return the partial evaluation of a function f(x) = f(x_alpha,x_not_alpha), a function f_alpha(.) = f(., x_not_alpha) for fixed values x_not_alpha of the variables with indices not_alpha.

random([n, measure])

Evaluates the function at n points x drawn randomly according to the ProbabilityMeasure in measure if provided, or in self.measure.

store_eval(x)

Evaluate the function, reuising previous evaluations if possible, and storing the new evaluations in self.

surf([n])

Surface plot of the bivariate function.

test_error(g[, n, measure])

Compute the test error associated with the function, using a function g or some of its evaluations as a reference.

eval(x)

Evaluate the function at the points x.

Parameters
xlist or numpy.ndarray

The points at which the function is to be evaluated.

Returns
numpy.ndarray

The evaluations of the function at the points x.

Module contents