petab.v1.math

Functions for parsing and evaluating mathematical expressions.

class petab.v1.math.PetabStrPrinter(settings=None)[source]

Bases: StrPrinter

A PEtab-compatible sympy string-printer.

petab.v1.math.petab_math_str(expr: Basic | Expr | None) str[source]

Convert a sympy expression to a PEtab-compatible math expression string.

Example:

>>> expr = sp.sympify("x**2 + sin(y)")
>>> petab_math_str(expr)
'x ^ 2 + sin(y)'
>>> expr = sp.sympify("Piecewise((1, x > 0), (0, True))")
>>> petab_math_str(expr)
'piecewise(1, x > 0, 0)'
petab.v1.math.sympify_petab(expr: str | int | float | Basic, evaluate: bool = True) Expr | Basic[source]

Convert PEtab math expression to sympy expression.

Parameters:
  • expr – PEtab math expression.

  • evaluate – Whether to evaluate the expression.

Raises:

ValueError – Upon lexer/parser errors or if the expression is otherwise invalid.

Returns:

The sympy expression corresponding to expr. Boolean values are converted to numeric values.

Note

All symbols in the returned expression will have the real=True assumption.

Example:

>>> from petab.v1.math import sympify_petab
>>> sympify_petab("sin(0)")
0
>>> sympify_petab("sin(0)", evaluate=False)
sin(0.0)
>>> sympify_petab("sin(0)", evaluate=True)
0
>>> sympify_petab("1 + 2", evaluate=True)
3.00000000000000
>>> sympify_petab("1 + 2", evaluate=False)
1.0 + 2.0
>>> sympify_petab("piecewise(1, 1 > 2, 0)", evaluate=True)
0.0
>>> sympify_petab("piecewise(1, 1 > 2, 0)", evaluate=False)
Piecewise((1.0, 1.0 > 2.0), (0.0, True))
>>> # currently, boolean values are converted to numeric values
>>> #  independent of the `evaluate` flag
>>> sympify_petab("true", evaluate=True)
1.00000000000000
>>> sympify_petab("true", evaluate=False)
1.00000000000000
>>> # ... and integer values are converted to floats
>>> sympify_petab("2", evaluate=True)
2.00000000000000

Modules

SympyVisitor

PEtab-math to sympy conversion.

printer

A PEtab-compatible sympy string-printer.

sympify

PEtab math to sympy conversion.