pymor.analyticalproblems.expressions

This module contains a basic symbolic expression library.

The library is used by ExpressionFunction and ExpressionParameterFunctional by calling parse_expression, which parses str expressions by replacing the names in the string with objects from this module. The result is an Expression object, which can be converted to a NumPy-vectorized function using Expression.to_numpy. In the future, more conversion routines will be added to make the same Expression usable for pymor.discretizers that use external PDE solvers. Further advantages of using this expression library:

  • meaningful error messages are generated at parse time of the str expression, instead of hard-to-debug errors in lambda functions at evaluation time,

  • expressions are automatically correctly vectorized. In particular, there is no longer a need to add ... to indexing expressions,

  • the shape of the resulting expressions is automatically determined.

In the future, we will also provide support for symbolic differentiation of the given Expressions.

Every Expression is built from the following atoms:

  • a Constant, which is a fixed value of arbitrary shape,

  • a Parameter, which is a variable of a fixed one-dimensional shape.

Note that both Parameters and input variables are treated as a Parameter in the expression. Only when calling, e.g., to_numpy, it is determined which Parameter belongs to the resulting function’s Parameters and which Parameter is treated as an input variable.

More complex expressions can be built using:

For binary operations of Expressions of different shape, the usual broadcasting rules apply.

Module Contents

pymor.analyticalproblems.expressions.builtin_max[source]
pymor.analyticalproblems.expressions.e[source]
pymor.analyticalproblems.expressions.pi[source]
class pymor.analyticalproblems.expressions.Array(array)[source]

Bases: Expression

An array of scalar-valued Expressions.

Methods

fenics_expr

Called by to_fenics.

numpy_expr

Called by to_numpy.

fenics_expr(params)[source]

Called by to_fenics.

numpy_expr()[source]

Called by to_numpy.

class pymor.analyticalproblems.expressions.BaseConstant[source]

Bases: Expression

A constant value.

Methods

fenics_expr

Called by to_fenics.

numpy_expr

Called by to_numpy.

fenics_symbol[source]
numpy_symbol[source]
shape = [][source]
fenics_expr(params)[source]

Called by to_fenics.

numpy_expr()[source]

Called by to_numpy.

class pymor.analyticalproblems.expressions.BinaryOp(first, second)[source]

Bases: Expression

Compound Expression of a binary operator acting on two sub-expressions.

Methods

fenics_expr

Called by to_fenics.

numpy_expr

Called by to_numpy.

fenics_symbol[source]
numpy_symbol[source]
fenics_expr(params)[source]

Called by to_fenics.

numpy_expr()[source]

Called by to_numpy.

class pymor.analyticalproblems.expressions.Constant(value)[source]

Bases: BaseConstant

A constant value given by a NumPy array.

Methods

fenics_expr

Called by to_fenics.

shape = [][source]
fenics_expr(params)[source]

Called by to_fenics.

class pymor.analyticalproblems.expressions.Diff(first, second)[source]

Bases: BinaryOp

Compound Expression of a binary operator acting on two sub-expressions.

fenics_symbol[source]
numpy_symbol = -[source]
class pymor.analyticalproblems.expressions.Div(first, second)[source]

Bases: BinaryOp

Compound Expression of a binary operator acting on two sub-expressions.

fenics_symbol[source]
numpy_symbol = /[source]
class pymor.analyticalproblems.expressions.E[source]

Bases: BaseConstant

A constant value.

fenics_symbol = e[source]
numpy_symbol = e[source]
class pymor.analyticalproblems.expressions.Expression[source]

Bases: pymor.parameters.base.ParametricObject

A symbolic math expression

shape[source]

The shape of the object this expression evaluates to in the sense of NumPy.

Methods

fenics_expr

Called by to_fenics.

numpy_expr

Called by to_numpy.

to_fenics

to_numpy

Convert expression to a NumPy-vectorized callable.

abstract fenics_expr(params)[source]

Called by to_fenics.

abstract numpy_expr()[source]

Called by to_numpy.

to_fenics(mesh, variable='x')[source]
to_numpy(variables)[source]

Convert expression to a NumPy-vectorized callable.

Parameters

variables

List of names of ~Parameters in the expression which shall be treated as input variables.

class pymor.analyticalproblems.expressions.GE(first, second)[source]

Bases: BinaryOp

Compound Expression of a binary operator acting on two sub-expressions.

fenics_symbol = ge[source]
numpy_symbol = >=[source]
class pymor.analyticalproblems.expressions.GT(first, second)[source]

Bases: BinaryOp

Compound Expression of a binary operator acting on two sub-expressions.

fenics_symbol = gt[source]
numpy_symbol = >[source]
class pymor.analyticalproblems.expressions.Indexed(base, index)[source]

Bases: Expression

Indexed Expression.

Methods

fenics_expr

Called by to_fenics.

numpy_expr

Called by to_numpy.

fenics_expr(params)[source]

Called by to_fenics.

numpy_expr()[source]

Called by to_numpy.

class pymor.analyticalproblems.expressions.LE(first, second)[source]

Bases: BinaryOp

Compound Expression of a binary operator acting on two sub-expressions.

fenics_symbol = le[source]
numpy_symbol = <=[source]
class pymor.analyticalproblems.expressions.LT(first, second)[source]

Bases: BinaryOp

Compound Expression of a binary operator acting on two sub-expressions.

fenics_symbol = lt[source]
numpy_symbol = <[source]
class pymor.analyticalproblems.expressions.Mod(first, second)[source]

Bases: BinaryOp

Compound Expression of a binary operator acting on two sub-expressions.

fenics_symbol[source]
numpy_symbol = %[source]
class pymor.analyticalproblems.expressions.Neg(operand)[source]

Bases: Expression

Negated Expression.

Methods

fenics_expr

Called by to_fenics.

numpy_expr

Called by to_numpy.

fenics_expr(params)[source]

Called by to_fenics.

numpy_expr()[source]

Called by to_numpy.

class pymor.analyticalproblems.expressions.Parameter(name, dim)[source]

Bases: Expression

A free parameter in an Expression.

Parameters represent both pyMOR Parameters as well as input variables. Each parameter is a vector of shape (dim,).

Parameters

name

The name of the parameter.

dim

The dimension of the parameter.

Methods

fenics_expr

Called by to_fenics.

numpy_expr

Called by to_numpy.

fenics_expr(params)[source]

Called by to_fenics.

numpy_expr()[source]

Called by to_numpy.

class pymor.analyticalproblems.expressions.Pi[source]

Bases: BaseConstant

A constant value.

fenics_symbol = pi[source]
numpy_symbol = pi[source]
class pymor.analyticalproblems.expressions.Pow(first, second)[source]

Bases: BinaryOp

Compound Expression of a binary operator acting on two sub-expressions.

fenics_symbol = elem_pow[source]
numpy_symbol = **[source]
class pymor.analyticalproblems.expressions.Prod(first, second)[source]

Bases: BinaryOp

Compound Expression of a binary operator acting on two sub-expressions.

fenics_symbol[source]
numpy_symbol = *[source]
class pymor.analyticalproblems.expressions.Sum(first, second)[source]

Bases: BinaryOp

Compound Expression of a binary operator acting on two sub-expressions.

fenics_symbol[source]
numpy_symbol = +[source]
class pymor.analyticalproblems.expressions.TransformLiterals[source]

Bases: ast.NodeTransformer

A NodeVisitor subclass that walks the abstract syntax tree and allows modification of nodes.

The NodeTransformer will walk the AST and use the return value of the visitor methods to replace or remove the old node. If the return value of the visitor method is None, the node will be removed from its location, otherwise it is replaced with the return value. The return value may be the original node in which case no replacement takes place.

Here is an example transformer that rewrites all occurrences of name lookups (foo) to data['foo']:

class RewriteName(NodeTransformer):

    def visit_Name(self, node):
        return Subscript(
            value=Name(id='data', ctx=Load()),
            slice=Constant(value=node.id),
            ctx=node.ctx
        )

Keep in mind that if the node you’re operating on has child nodes you must either transform the child nodes yourself or call the generic_visit method for the node first.

For nodes that were part of a collection of statements (that applies to all statement nodes), the visitor may also return a list of nodes rather than just a single node.

Usually you use the transformer like this:

node = YourTransformer().visit(node)
in_subscript = False[source]
visit_Constant(node)[source]
visit_List(node)[source]
visit_Num(node)[source]
visit_Subscript(node)[source]
class pymor.analyticalproblems.expressions.UnaryFunctionCall(arg, *args)[source]

Bases: Expression

Compound Expression of an unary function applied to a sub-expression.

The function is applied component-wise.

Methods

fenics_expr

Called by to_fenics.

numpy_expr

Called by to_numpy.

fenics_symbol[source]
numpy_symbol[source]
fenics_expr(params)[source]

Called by to_fenics.

numpy_expr()[source]

Called by to_numpy.

class pymor.analyticalproblems.expressions.UnaryReductionCall(arg, *args)[source]

Bases: UnaryFunctionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied to the entire vector/matrix/tensor the sub-expression evaluates to, returning a single number.

Methods

fenics_expr

Called by to_fenics.

numpy_expr

Called by to_numpy.

fenics_op[source]
numpy_symbol[source]
fenics_expr(params)[source]

Called by to_fenics.

numpy_expr()[source]

Called by to_numpy.

class pymor.analyticalproblems.expressions.abs(arg, *args)[source]

Bases: UnaryFunctionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied component-wise.

Methods

fenics_expr

Called by to_fenics.

numpy_symbol = abs[source]
fenics_expr(params)[source]

Called by to_fenics.

class pymor.analyticalproblems.expressions.angle(arg)[source]

Bases: UnaryFunctionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied component-wise.

Methods

fenics_expr

Called by to_fenics.

fenics_op[source]
numpy_symbol = angle[source]
fenics_expr(params)[source]

Called by to_fenics.

class pymor.analyticalproblems.expressions.arccos(arg, *args)[source]

Bases: UnaryFunctionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied component-wise.

fenics_symbol = acos[source]
numpy_symbol = arccos[source]
class pymor.analyticalproblems.expressions.arccosh(arg, *args)[source]

Bases: UnaryFunctionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied component-wise.

Methods

fenics_expr

Called by to_fenics.

numpy_symbol = arccosh[source]
fenics_expr(params)[source]

Called by to_fenics.

class pymor.analyticalproblems.expressions.arcsin(arg, *args)[source]

Bases: UnaryFunctionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied component-wise.

fenics_symbol = asin[source]
numpy_symbol = arcsin[source]
class pymor.analyticalproblems.expressions.arcsinh(arg, *args)[source]

Bases: UnaryFunctionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied component-wise.

Methods

fenics_expr

Called by to_fenics.

numpy_symbol = arcsinh[source]
fenics_expr(params)[source]

Called by to_fenics.

class pymor.analyticalproblems.expressions.arctan(arg, *args)[source]

Bases: UnaryFunctionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied component-wise.

fenics_symbol = atan[source]
numpy_symbol = arctan[source]
class pymor.analyticalproblems.expressions.arctanh(arg, *args)[source]

Bases: UnaryFunctionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied component-wise.

Methods

fenics_expr

Called by to_fenics.

numpy_symbol = arctanh[source]
fenics_expr(params)[source]

Called by to_fenics.

class pymor.analyticalproblems.expressions.cos(arg, *args)[source]

Bases: UnaryFunctionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied component-wise.

fenics_symbol = cos[source]
numpy_symbol = cos[source]
class pymor.analyticalproblems.expressions.cosh(arg, *args)[source]

Bases: UnaryFunctionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied component-wise.

fenics_symbol = cosh[source]
numpy_symbol = cosh[source]
class pymor.analyticalproblems.expressions.exp(arg, *args)[source]

Bases: UnaryFunctionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied component-wise.

fenics_symbol = exp[source]
numpy_symbol = exp[source]
class pymor.analyticalproblems.expressions.exp2(arg, *args)[source]

Bases: UnaryFunctionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied component-wise.

Methods

fenics_expr

Called by to_fenics.

numpy_symbol = exp2[source]
fenics_expr(params)[source]

Called by to_fenics.

class pymor.analyticalproblems.expressions.log(arg, *args)[source]

Bases: UnaryFunctionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied component-wise.

fenics_symbol = ln[source]
numpy_symbol = log[source]
class pymor.analyticalproblems.expressions.log10(arg, *args)[source]

Bases: UnaryFunctionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied component-wise.

Methods

fenics_expr

Called by to_fenics.

numpy_symbol = log10[source]
fenics_expr(params)[source]

Called by to_fenics.

class pymor.analyticalproblems.expressions.log2(arg, *args)[source]

Bases: UnaryFunctionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied component-wise.

Methods

fenics_expr

Called by to_fenics.

numpy_symbol = log2[source]
fenics_expr(params)[source]

Called by to_fenics.

class pymor.analyticalproblems.expressions.max(arg, *args)[source]

Bases: UnaryReductionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied to the entire vector/matrix/tensor the sub-expression evaluates to, returning a single number.

fenics_op = max_value[source]
numpy_symbol = max[source]
class pymor.analyticalproblems.expressions.min(arg, *args)[source]

Bases: UnaryReductionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied to the entire vector/matrix/tensor the sub-expression evaluates to, returning a single number.

fenics_op = min_value[source]
numpy_symbol = min[source]
class pymor.analyticalproblems.expressions.norm(arg, *args)[source]

Bases: UnaryReductionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied to the entire vector/matrix/tensor the sub-expression evaluates to, returning a single number.

Methods

fenics_expr

Called by to_fenics.

numpy_symbol = norm[source]
fenics_expr(params)[source]

Called by to_fenics.

class pymor.analyticalproblems.expressions.prod(arg, *args)[source]

Bases: UnaryReductionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied to the entire vector/matrix/tensor the sub-expression evaluates to, returning a single number.

fenics_op[source]
numpy_symbol = prod[source]
class pymor.analyticalproblems.expressions.sign(arg, *args)[source]

Bases: UnaryFunctionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied component-wise.

fenics_symbol = sign[source]
numpy_symbol = sign[source]
class pymor.analyticalproblems.expressions.sin(arg, *args)[source]

Bases: UnaryFunctionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied component-wise.

fenics_symbol = sin[source]
numpy_symbol = sin[source]
class pymor.analyticalproblems.expressions.sinh(arg, *args)[source]

Bases: UnaryFunctionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied component-wise.

fenics_symbol = sinh[source]
numpy_symbol = sinh[source]
class pymor.analyticalproblems.expressions.sqrt(arg, *args)[source]

Bases: UnaryFunctionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied component-wise.

fenics_symbol = sqrt[source]
numpy_symbol = sqrt[source]
class pymor.analyticalproblems.expressions.sum(arg, *args)[source]

Bases: UnaryReductionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied to the entire vector/matrix/tensor the sub-expression evaluates to, returning a single number.

fenics_op[source]
numpy_symbol = sum[source]
class pymor.analyticalproblems.expressions.tan(arg, *args)[source]

Bases: UnaryFunctionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied component-wise.

fenics_symbol = tan[source]
numpy_symbol = tan[source]
class pymor.analyticalproblems.expressions.tanh(arg, *args)[source]

Bases: UnaryFunctionCall

Compound Expression of an unary function applied to a sub-expression.

The function is applied component-wise.

fenics_symbol = tanh[source]
numpy_symbol = tanh[source]
pymor.analyticalproblems.expressions.parse_expression(expression, parameters={}, values={})[source]