pymor.parameters package¶
Submodules¶
base module¶
This module contains the implementation of pyMOR’s parameter handling facilities.
Use the ParametricObject base class to define immutable (mathematical) objects that
depend on some Parameters. Each Parameter in pyMOR has a name and a fixed dimension
(number of scalar components of the parameter vector). In particular, scalar parameters
are treated as parameter vectors of dimension 1. Mappings of Parameters to
parameter values are stored in pyMOR using dedicated Mu objects.
To sample parameter values within a given range, ParameterSpace objects can be used.
-
class
pymor.parameters.base.Mu(*args, **kwargs)[source]¶ Bases:
pymor.tools.frozendict.SortedFrozenDictImmutable mapping of
Parameternames to parameter values.Parameters
Anything that dict accepts for the construction of a dictionary. Values are automatically converted to immutable one-dimensional
NumPy arrays, unless the Python interpreter runs with the-Oflag.Methods
dictfromkeys,get,items,keys,values,__contains__,__getitem__,__sizeof__Attributes
clear,pop,popitem,setdefault,update-
parameters¶ The
Parametersto which the mapping assigns values.
-
static
__new__(cls, *args, **kwargs)[source]¶ Create and return a new object. See help(type) for accurate signature.
-
allclose(mu)[source]¶ Compare two dicts of
parameter valuesusingfloat_cmp_all.Parameters
- mu
The
parameter valueswith which to compare.
Returns
Trueif bothparameter valuedicts contain values for the sameParametersand all components of the parameter values are almost equal, elseFalse.
-
-
class
pymor.parameters.base.ParameterSpace(*args, **kwargs)[source]¶ Bases:
pymor.parameters.base.ParametricObjectA set of
Parameterswith allowed ranges for their values.ParameterSpacesare mostly used to create sample set ofparameter valuesfor givenParameterswithin a specified range.Parameters
- parameters
The
Parameterswhich are part of the space.- ranges
Allowed ranges for the
parameter values. Either:two numbers specifying the lower and upper bound for all parameter value components,
a list/tuple of two numbers specifying these bounds,
or a dict of those tuples, specifying upper and lower bounds individually for each parameter of the space.
Methods
contains,sample_randomly,sample_uniformlyAttributes
parameters,parameters_inherited,parameters_internal,parameters_own,parametric-
sample_randomly(count=None, random_state=None, seed=None)[source]¶ Randomly sample
parameter valuesfrom the space.Parameters
- count
Noneor number of random samples (see below).- random_state
RandomStateto use for sampling. IfNone, a new random state is generated usingseedas random seed, or thedefaultrandom state is used.- seed
If not
None, a new random state with this seed is used.
Returns
If
countisNone, an inexhaustible iterator returning randomparameter valuedicts. Otherwise a list ofcountrandomparameter valuedicts.
-
sample_uniformly(counts)[source]¶ Uniformly sample
parameter valuesfrom the space.Parameters
- counts
Number of samples to take per parameter and component of the parameter. Either a dict of counts per
Parameteror a single count that is taken for allParameters
Returns
List of
parameter valuedicts.
-
class
pymor.parameters.base.Parameters(*args, **kwargs)[source]¶ Bases:
pymor.tools.frozendict.SortedFrozenDictImmutable dict mapping parameter names to parameter dimensions.
Each key of a
Parametersdict is a string specifying the name of a parameter. The corresponding value is a non-negativeintspecifying the dimension (number of scalar components) of the parameter.Methods
assert_compatible,is_compatible,of,parse,space,why_incompatible,__le__dictcopy,fromkeys,get,items,keys,values,__contains__,__getitem__,__sizeof__Attributes
clear,pop,popitem,setdefault,update-
__le__(mu)[source]¶ Check if
parameter valuesare compatible with the givenParameters.Each of the parameter must be contained in
muand the dimensions have to match, i.e.mu[parameter].size == self[parameter]
-
assert_compatible(mu)[source]¶ Assert that
parameter valuesare compatible with the givenParameters.Each of the parameter must be contained in
muand the dimensions have to match, i.e.mu[parameter].size == self[parameter]
Otherwise, an
AssertionErrorwill be raised.
-
property
dim¶ The sum of the dimensions of all parameters.
-
is_compatible(mu)[source]¶ Check if
parameter valuesare compatible with the givenParameters.Each of the parameter must be contained in
muand the dimensions have to match, i.e.mu[parameter].size == self[parameter]
-
classmethod
of(*args)[source]¶ Computes the total set of
Parametersa collection of objects depends on.If two objects depend on a parameter with the same name, both parameters must have the same dimension.
Parameters
- args
Each positional argument must either be
None, aParametricObjector lists, tuples, dicts orNumPy arraysof such objects. The latter will be traversed recursively.
-
parse(mu)[source]¶ Takes a user input
muand interprets it as set ofparameter valuesaccording to the givenParameters.Depending on the
Parameters,mucan be given as a dict, list, tuple,NumPy arrayor scalar. In the latter cases, multiple parameters will be concatenated by alphabetical ordering. E.g.:Parameters(b=2, a=1).parse([1,2,3])
will assign to parameter
athe value[1]and to parameterbthe values[2, 3].Parameters
- mu
The user input which shall be interpreted as
parameter values.
Returns
The resulting object of
parameter values.Raises
- ValueError
Is raised if
mucannot be interpreted asparameter valuesfor the givenParameters.
-
space(*ranges)[source]¶ Create a
ParameterSpacewith given ranges.This is a shorthand for
ParameterSpace(self, *range)
See
ParameterSpacefor allowed range arguments.
-
-
class
pymor.parameters.base.ParametricObject(*args, **kwargs)[source]¶ Bases:
pymor.core.base.ImmutableObjectBase class for immutable mathematical entities depending on some
Parameters.Each
ParametricObjectlists theParametersit depends on in theparametersattribute. Usually, theseParametersare automatically derived as the union of allParametersof the object’s__init__arguments.Additional
Parametersintroduced by the object itself can be specified by setting theparameters_ownattribute in__init__. In case the object fixes someParametersit’s child objects depend on to concrete values, thoseParameterscan be removed from theparametersattribute by settingparameters_internal.Alternatively,
parameterscan be initialized manually in__init__.Attributes
parameters,parameters_inherited,parameters_internal,parameters_own,parametric-
parameters¶ The
Parametersthe object depends on.
-
parameters_own¶ The
Parametersthe object depends on which are not inherited from a child object the object depends on. Each item ofparameters_ownis also an item ofparameters.
-
parameters_inherited¶ The
Parametersthe object depends on because some child object depends on them. Each item ofparameters_ownis also an item ofparameters.
-
parameters_internal¶ The
Parameterssome of the object’s child objects may depend on, but which are fixed to a concrete value by this object. All items ofparameters_internalare removed fromparametersandparameters_inherited. When initializingparameters_ownandparameters_internal, it has to be ensured that both dicts are disjoint.
-
parametric¶ Trueif the object really depends on a parameter, i.e.parametersis not empty.
-
functionals module¶
-
class
pymor.parameters.functionals.BaseMaxThetaParameterFunctional(*args, **kwargs)[source]¶ Bases:
pymor.parameters.functionals.ParameterFunctionalParameterFunctionalimplementing a generalization of the max-theta approach from [Haa17] (Exercise 5.12).Let V denote a Hilbert space and let a: V x V -> K denote a continuous bilinear form or l: V -> K a continuous linear functional, either with affine decomposition
a(u, v, mu) = sum_{q = 1}^Q theta_q(mu) a_q(u, v) or l(v, mu) = sum_{q = 1}^Q theta_q(mu) l_q(v)
for Q coefficient
ParameterFunctionaltheta_1, …, theta_Q and continuous bilinear forms a_1, …, a_Q: V x V -> K or continuous linear functionals l_q: V -> K. Let mu_bar be a parameter with respect to which the continuity constant of a(., ., mu_bar) or l(., mu_bar) is known, i.e. we known gamma_mu_bar > 0, s.t.a(u, v, mu_bar) <= gamma_mu_bar |u|_V |v|_V or l(v, mu_bar) <= gamma_mu_bar |v|_V.
The max-theta approach (in its generalized form) from [Haa17] (Exercise 5.12) allows to obtain a computable bound for the continuity constant of another bilinear form a_prime(., ., mu) or linear form l_prime(., mu) with the same affine decomposition but different theta_prime_q for arbitrary parameters mu, since
a_prime(u, v, mu=mu) <= |max_{q = 1}^Q theta_prime_q(mu)/theta_q(mu_bar)| |a(u, v, mu=mu_bar)|
or
l_prime(v, mu=mu) <= |max_{q = 1}^Q theta_prime_q(mu)/theta_q(mu_bar)| |l(v, mu=mu_bar)|,
if all theta_q(mu_bar) != 0.
Given a list of the thetas, the
parameter valuesmu_bar and the constant gamma_mu_bar, this functional thus evaluates to|gamma_mu_bar * max_{q = 1}^Q theta_prime_q(mu)/theta_q(mu_bar)|
Note that we also get an upper bound if theta_prime_q(mu) == 0 for any q. However, if theta_prime_q(mu) == 0 for at least one q, we need to use the absolute value in the denominator, i.e.
|gamma_mu_bar * max_{q = 1}^Q theta_prime_q(mu)/|theta_q(mu_bar)| |
Parameters
- thetas
List or tuple of
ParameterFunctionalof the base bilinear form a which is used for estimation.- thetas_prime
List or tuple of
ParameterFunctionalof the bilinear form a_prime for the numerator of the MaxThetaParameterFunctional.- mu_bar
Parameter associated with gamma_mu_bar.
- gamma_mu_bar
Known continuity constant of the base bilinear form a.
- name
Name of the functional.
Methods
Attributes
parameters,parameters_inherited,parameters_internal,parameters_own,parametric-
d_mu(component, index=())[source]¶ Return the functionals’s derivative with respect to a given parameter.
Parameters
Returns
New
ParameterFunctionalrepresenting the partial derivative.
-
evaluate(mu=None)[source]¶ Evaluate the functional for given
parameter valuesmu.
-
class
pymor.parameters.functionals.ConjugateParameterFunctional(*args, **kwargs)[source]¶ Bases:
pymor.parameters.functionals.ParameterFunctionalConjugate of a given
ParameterFunctionalEvaluates a given
ParameterFunctionaland returns the complex conjugate of the value.Parameters
- functional
The
ParameterFunctionalof which the complex conjuate is taken.- name
Name of the functional.
Methods
Attributes
parameters,parameters_inherited,parameters_internal,parameters_own,parametric-
evaluate(mu=None)[source]¶ Evaluate the functional for given
parameter valuesmu.
-
class
pymor.parameters.functionals.ConstantParameterFunctional(*args, **kwargs)[source]¶ Bases:
pymor.parameters.functionals.ParameterFunctionalParameterFunctionalreturning a constant value for each parameter.Parameters
- constant_value
value of the functional
- name
Name of the functional.
Methods
Attributes
parameters,parameters_inherited,parameters_internal,parameters_own,parametric-
d_mu(parameter, index=0)[source]¶ Return the functionals’s derivative with respect to a given parameter.
Parameters
Returns
New
ParameterFunctionalrepresenting the partial derivative.
-
evaluate(mu=None)[source]¶ Evaluate the functional for given
parameter valuesmu.
-
class
pymor.parameters.functionals.ExpressionParameterFunctional(*args, **kwargs)[source]¶ Bases:
pymor.parameters.functionals.GenericParameterFunctionalTurns a Python expression given as a string into a
ParameterFunctional.Some
NumPyarithmetic functions likesin,log,minare supported. For a full list see thefunctionsclass attribute.Warning
evalis used to evaluate the given expression. Using this class with expression strings from untrusted sources will cause mayhem and destruction!Parameters
- expression
A Python expression in the parameter components of the given
parameters.- parameters
The
Parametersthe functional depends on.- name
The name of the functional.
- derivative_expressions
A dict containing a Python expression for the partial derivatives of each parameter component.
- second_derivative_expressions
A dict containing a list of dicts of Python expressions for all second order partial derivatives of each parameter component i and j.
Methods
Attributes
functionsparameters,parameters_inherited,parameters_internal,parameters_own,parametric
-
class
pymor.parameters.functionals.GenericParameterFunctional(*args, **kwargs)[source]¶ Bases:
pymor.parameters.functionals.ParameterFunctionalA wrapper making an arbitrary Python function a
ParameterFunctionalNote that a GenericParameterFunctional can only be
pickledif the function it is wrapping can be pickled. For this reason, it is usually preferable to useExpressionParameterFunctionalinstead ofGenericParameterFunctional.Parameters
- mapping
The function to wrap. The function has signature
mapping(mu).- parameters
The
Parametersthe functional depends on.- name
The name of the functional.
- derivative_mappings
A dict containing all partial derivatives of each
Parameterand index with the signaturederivative_mappings[parameter][index](mu)- second_derivative_mappings
A dict containing all second order partial derivatives of each
Parameterand index with the signaturesecond_derivative_mappings[parameter_i][index_i][parameter_j][index_j](mu)
Methods
Attributes
parameters,parameters_inherited,parameters_internal,parameters_own,parametric-
d_mu(parameter, index=0)[source]¶ Return the functionals’s derivative with respect to a given parameter.
Parameters
Returns
New
ParameterFunctionalrepresenting the partial derivative.
-
evaluate(mu=None)[source]¶ Evaluate the functional for given
parameter valuesmu.
-
class
pymor.parameters.functionals.LincombParameterFunctional(*args, **kwargs)[source]¶ Bases:
pymor.parameters.functionals.ParameterFunctionalA
ParameterFunctionalrepresenting a linear combination ofParameterFunctionals.The coefficients must be provided as scalars.
Parameters
- functionals
List of
ParameterFunctionalswhose linear combination is formed.- coefficients
A list of scalar coefficients.
- name
Name of the functional.
Methods
Attributes
parameters,parameters_inherited,parameters_internal,parameters_own,parametric-
functionals¶
-
coefficients¶
-
d_mu(parameter, index=0)[source]¶ Return the functionals’s derivative with respect to a given parameter.
Parameters
Returns
New
ParameterFunctionalrepresenting the partial derivative.
-
evaluate(mu=None)[source]¶ Evaluate the functional for given
parameter valuesmu.
-
class
pymor.parameters.functionals.MaxThetaParameterFunctional(*args, **kwargs)[source]¶ Bases:
pymor.parameters.functionals.BaseMaxThetaParameterFunctionalParameterFunctionalimplementing the max-theta approach from [Haa17] (Exercise 5.12).This is a specialized version of BaseMaxThetaParameterFunctional which allows to obtain a computable bound for the continuity constant of the actual a(., ., mu) or l(., mu) for arbitrary parameters mu, since
a(u, v, mu=mu) <= |max_{q = 1}^Q theta_q(mu)/theta_q(mu_bar)| |a(u, v, mu=mu_bar)|
or
l(v, mu=mu) <= |max_{q = 1}^Q theta_q(mu)/theta_q(mu_bar)| |l(v, mu=mu_bar)|,
if all theta_q(mu_bar) != 0.
Given a list of the thetas, the
parameter valuesmu_bar and the constant gamma_mu_bar, this functional thus evaluates to|gamma_mu_bar * max{q = 1}^Q theta_q(mu)/theta_q(mu_bar)|
Parameters
- thetas
List or tuple of
ParameterFunctional- mu_bar
Parameter associated with gamma_mu_bar.
- gamma_mu_bar
Known continuity constant.
- name
Name of the functional.
Methods
-
class
pymor.parameters.functionals.MinThetaParameterFunctional(*args, **kwargs)[source]¶ Bases:
pymor.parameters.functionals.ParameterFunctionalParameterFunctionalimplementing the min-theta approach from [Haa17] (Proposition 2.35).Let V denote a Hilbert space and let a: V x V -> K denote a parametric coercive bilinear form with affine decomposition
a(u, v, mu) = sum_{q = 1}^Q theta_q(mu) a_q(u, v),
for Q positive coefficient
ParameterFunctionaltheta_1, …, theta_Q and positive semi-definite component bilinear forms a_1, …, a_Q: V x V -> K. Let mu_bar be a parameter with respect to which the coercivity constant of a(., ., mu_bar) is known, i.e. we known alpha_mu_bar > 0, s.t.alpha_mu_bar |u|_V^2 <= a(u, u, mu=mu_bar).
The min-theta approach from [Haa17] (Proposition 2.35) allows to obtain a computable bound for the coercivity constant of a(., ., mu) for arbitrary parameters mu, since
a(u, u, mu=mu) >= min_{q = 1}^Q theta_q(mu)/theta_q(mu_bar) a(u, u, mu=mu_bar).
Given a list of the thetas, the
parameter valuesmu_bar and the constant alpha_mu_bar, this functional thus evaluates toalpha_mu_bar * min_{q = 1}^Q theta_q(mu)/theta_q(mu_bar)
Parameters
- thetas
List or tuple of
ParameterFunctional- mu_bar
Parameter associated with alpha_mu_bar.
- alpha_mu_bar
Known coercivity constant.
- name
Name of the functional.
Methods
Attributes
parameters,parameters_inherited,parameters_internal,parameters_own,parametric-
evaluate(mu=None)[source]¶ Evaluate the functional for given
parameter valuesmu.
-
class
pymor.parameters.functionals.ParameterFunctional(*args, **kwargs)[source]¶ Bases:
pymor.parameters.base.ParametricObjectInterface for
Parameterfunctionals.A parameter functional is simply a function mapping
Parametersto a number.Methods
Attributes
parameters,parameters_inherited,parameters_internal,parameters_own,parametric-
d_mu(parameter, index=0)[source]¶ Return the functionals’s derivative with respect to a given parameter.
Parameters
Returns
New
ParameterFunctionalrepresenting the partial derivative.
-
abstract
evaluate(mu=None)[source]¶ Evaluate the functional for given
parameter valuesmu.
-
-
class
pymor.parameters.functionals.ProductParameterFunctional(*args, **kwargs)[source]¶ Bases:
pymor.parameters.functionals.ParameterFunctionalForms the product of a list of
ParameterFunctionalsor numbers.Parameters
- factors
A list of
ParameterFunctionalsor numbers.- name
Name of the functional.
Methods
Attributes
parameters,parameters_inherited,parameters_internal,parameters_own,parametric-
d_mu(parameter, index=0)[source]¶ Return the functionals’s derivative with respect to a given parameter.
Parameters
Returns
New
ParameterFunctionalrepresenting the partial derivative.
-
evaluate(mu=None)[source]¶ Evaluate the functional for given
parameter valuesmu.
-
class
pymor.parameters.functionals.ProjectionParameterFunctional(*args, **kwargs)[source]¶ Bases:
pymor.parameters.functionals.ParameterFunctionalParameterFunctionalreturning a component value of the given parameter.For given parameter map
mu, this functional evaluates tomu[parameter][index]
Parameters
- parameter
The name of the parameter to return.
- size
The size of the parameter.
- index
See above.
- name
Name of the functional.
Methods
Attributes
parameters,parameters_inherited,parameters_internal,parameters_own,parametric-
d_mu(parameter, index=0)[source]¶ Return the functionals’s derivative with respect to a given parameter.
Parameters
Returns
New
ParameterFunctionalrepresenting the partial derivative.
-
evaluate(mu=None)[source]¶ Evaluate the functional for given
parameter valuesmu.