Skip to content

Expression

Expression(parameter, expression)

Numerical parametric expression object.

Args

  • parameter (str | Sequence[str]): Independent variable name, or sequence of names.
  • expression (str | tuple[str, str | float] | Sequence[str | tuple[str, str | float]]): Expression to be evaluated as a function of the parameter(s). A list of expressions can also be used.

When using multiple expressions, they are evaluated in sequence and previous results are available to subsequent expressions by their name. Expression names can be set by using tuples with (name, expression).

Examples

>>> e = pf.Expression("u", "2 * u")
>>> e(1.5)
3.0
>>> e = pf.Expression("u", [("a", 2), ("b", "u^(a - 1)"), "a * b"])
>>> e(3)
array([2., 3., 6.])
>>> e = pf.Expression(
... ["x", "y"], ["sqrt(x^2 + y^2)", "atan2(y, x)"]
... )
>>> e(1, -1)
array([ 1.41..., -0.78...])

Note

Expression strings support the following:

  • Constants: e, pi
  • Operators: +, -, *, /, %, ^ (exponent)
  • Functions: abs, acos, and, asin, atan2, atan, ceil, cos, cosh, exp, floor, if, ln, log (same as ln), log10, max, min, or, pow, sin, sinh, sqrt, tan, tanh

Functions max and min only accept 2 arguments.

Functions if, and, or are equivalent to the following python expressions:

  • if(a, b, c) == b if a > 0 else c
  • and(a, b) == b if a > 0 else a
  • or(a, b) == a if a > 0 else b
MethodDescription
copyCreate a copy of this expression.

copy(self)

Create a copy of this expression.

Returns

New copy.

AttributeDescription
expressionslist[tuple[str, str | float]]: Object expressions as a list (read only).
parameterslist[str]: Object parameters as a list (read only).
propertiesProperties: Object properties.

list[tuple[str, str | float]]: Object expressions as a list (read only).

list[str]: Object parameters as a list (read only).

Properties: Object properties.