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

Classes

Expression

A symbolic math expression

BaseConstant

A constant value.

Constant

A constant value given by a NumPy array.

Parameter

A free parameter in an Expression.

Array

An array of scalar-valued Expressions.

BinaryOp

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

Neg

Negated Expression.

Indexed

Indexed Expression.

UnaryFunctionCall

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

UnaryReductionCall

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

Sum

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

Diff

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

Prod

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

Div

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

Pow

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

Mod

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

LE

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

GE

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

LT

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

GT

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

sin

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

cos

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

tan

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

arcsin

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

arccos

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

arctan

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

sinh

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

cosh

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

tanh

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

arcsinh

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

arccosh

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

arctanh

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

exp

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

exp2

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

log

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

log2

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

log10

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

sqrt

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

abs

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

sign

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

angle

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

norm

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

min

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

max

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

sum

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

prod

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

Pi

A constant value.

E

A constant value.

Functions

parse_expression

_convert_to_expression

Used internally to convert literals/list constructors to an Expression.

_broadcastable_shapes

Attributes

builtin_max

pi

e

_numpy_functions

pymor.analyticalproblems.expressions.builtin_max[source]
pymor.analyticalproblems.expressions.parse_expression(expression, parameters={}, values={})[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.

to_numpy(self, 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.

abstract numpy_expr(self)[source]

Called by to_numpy.

__getitem__(self, index)[source]
__add__(self, other)[source]
__radd__(self, other)[source]
__sub__(self, other)[source]
__rsub__(self, other)[source]
__mul__(self, other)[source]
__rmul__(self, other)[source]
__truediv__(self, other)[source]
__rtruediv__(self, other)[source]
__pow__(self, other)[source]
__rpow__(self, other)[source]
__mod__(self, other)[source]
__rmod__(self, other)[source]
__neg__(self)[source]
__le__(self, other)[source]

Return self<=value.

__ge__(self, other)[source]

Return self>=value.

__lt__(self, other)[source]

Return self<value.

__gt__(self, other)[source]

Return self>value.

class pymor.analyticalproblems.expressions.BaseConstant[source]

Bases: Expression

A constant value.

numpy_symbol[source]
shape = [][source]
numpy_expr(self)[source]

Called by to_numpy.

__str__(self)[source]

Return str(self).

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

Bases: BaseConstant

A constant value given by a NumPy array.

__str__(self)[source]

Return str(self).

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.

numpy_expr(self)[source]

Called by to_numpy.

__str__(self)[source]

Return str(self).

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

Bases: Expression

An array of scalar-valued Expressions.

numpy_expr(self)[source]

Called by to_numpy.

__str__(self)[source]

Return str(self).

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

Bases: Expression

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

numpy_symbol[source]
numpy_expr(self)[source]

Called by to_numpy.

__str__(self)[source]

Return str(self).

class pymor.analyticalproblems.expressions.Neg(operand)[source]

Bases: Expression

Negated Expression.

numpy_expr(self)[source]

Called by to_numpy.

__str__(self)[source]

Return str(self).

class pymor.analyticalproblems.expressions.Indexed(base, index)[source]

Bases: Expression

Indexed Expression.

numpy_expr(self)[source]

Called by to_numpy.

__str__(self)[source]

Return str(self).

class pymor.analyticalproblems.expressions.UnaryFunctionCall(*arg)[source]

Bases: Expression

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

The function is applied component-wise.

numpy_symbol[source]
numpy_expr(self)[source]

Called by to_numpy.

__str__(self)[source]

Return str(self).

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

Bases: Expression

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.

numpy_expr(self)[source]

Called by to_numpy.

__str__(self)[source]

Return str(self).

pymor.analyticalproblems.expressions._convert_to_expression(obj)[source]

Used internally to convert literals/list constructors to an Expression.

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

Bases: BinaryOp

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

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

Bases: BinaryOp

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

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

Bases: BinaryOp

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

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

Bases: BinaryOp

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

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

Bases: BinaryOp

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

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

Bases: BinaryOp

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

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

Bases: BinaryOp

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

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

Bases: BinaryOp

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

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

Bases: BinaryOp

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

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

Bases: BinaryOp

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

numpy_symbol = >[source]
class pymor.analyticalproblems.expressions.sin(*arg)[source]

Bases: UnaryFunctionCall

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

The function is applied component-wise.

numpy_symbol = sin[source]
class pymor.analyticalproblems.expressions.cos(*arg)[source]

Bases: UnaryFunctionCall

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

The function is applied component-wise.

numpy_symbol = cos[source]
class pymor.analyticalproblems.expressions.tan(*arg)[source]

Bases: UnaryFunctionCall

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

The function is applied component-wise.

numpy_symbol = tan[source]
class pymor.analyticalproblems.expressions.arcsin(*arg)[source]

Bases: UnaryFunctionCall

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

The function is applied component-wise.

numpy_symbol = arcsin[source]
class pymor.analyticalproblems.expressions.arccos(*arg)[source]

Bases: UnaryFunctionCall

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

The function is applied component-wise.

numpy_symbol = arccos[source]
class pymor.analyticalproblems.expressions.arctan(*arg)[source]

Bases: UnaryFunctionCall

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

The function is applied component-wise.

numpy_symbol = arctan[source]
class pymor.analyticalproblems.expressions.sinh(*arg)[source]

Bases: UnaryFunctionCall

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

The function is applied component-wise.

numpy_symbol = sinh[source]
class pymor.analyticalproblems.expressions.cosh(*arg)[source]

Bases: UnaryFunctionCall

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

The function is applied component-wise.

numpy_symbol = cosh[source]
class pymor.analyticalproblems.expressions.tanh(*arg)[source]

Bases: UnaryFunctionCall

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

The function is applied component-wise.

numpy_symbol = tanh[source]
class pymor.analyticalproblems.expressions.arcsinh(*arg)[source]

Bases: UnaryFunctionCall

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

The function is applied component-wise.

numpy_symbol = arcsinh[source]
class pymor.analyticalproblems.expressions.arccosh(*arg)[source]

Bases: UnaryFunctionCall

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

The function is applied component-wise.

numpy_symbol = arccosh[source]
class pymor.analyticalproblems.expressions.arctanh(*arg)[source]

Bases: UnaryFunctionCall

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

The function is applied component-wise.

numpy_symbol = arctanh[source]
class pymor.analyticalproblems.expressions.exp(*arg)[source]

Bases: UnaryFunctionCall

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

The function is applied component-wise.

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

Bases: UnaryFunctionCall

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

The function is applied component-wise.

numpy_symbol = exp2[source]
class pymor.analyticalproblems.expressions.log(*arg)[source]

Bases: UnaryFunctionCall

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

The function is applied component-wise.

numpy_symbol = log[source]
class pymor.analyticalproblems.expressions.log2(*arg)[source]

Bases: UnaryFunctionCall

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

The function is applied component-wise.

numpy_symbol = log2[source]
class pymor.analyticalproblems.expressions.log10(*arg)[source]

Bases: UnaryFunctionCall

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

The function is applied component-wise.

numpy_symbol = log10[source]
class pymor.analyticalproblems.expressions.sqrt(*arg)[source]

Bases: UnaryFunctionCall

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

The function is applied component-wise.

numpy_symbol = sqrt[source]
class pymor.analyticalproblems.expressions.abs(*arg)[source]

Bases: UnaryFunctionCall

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

The function is applied component-wise.

numpy_symbol = abs[source]
class pymor.analyticalproblems.expressions.sign(*arg)[source]

Bases: UnaryFunctionCall

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

The function is applied component-wise.

numpy_symbol = sign[source]
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.

numpy_symbol = angle[source]
class pymor.analyticalproblems.expressions.norm(*arg)[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.

numpy_symbol = norm[source]
class pymor.analyticalproblems.expressions.min(*arg)[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.

numpy_symbol = min[source]
class pymor.analyticalproblems.expressions.max(*arg)[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.

numpy_symbol = max[source]
class pymor.analyticalproblems.expressions.sum(*arg)[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.

numpy_symbol = sum[source]
class pymor.analyticalproblems.expressions.prod(*arg)[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.

numpy_symbol = prod[source]
class pymor.analyticalproblems.expressions.Pi[source]

Bases: BaseConstant

A constant value.

numpy_symbol = pi[source]
class pymor.analyticalproblems.expressions.E[source]

Bases: BaseConstant

A constant value.

numpy_symbol = e[source]
pymor.analyticalproblems.expressions.pi[source]
pymor.analyticalproblems.expressions.e[source]
pymor.analyticalproblems.expressions._numpy_functions[source]