utilities.symbolic.lambdify_expression#
- utilities.symbolic.lambdify_expression(expression: str | Basic, axes: List[Symbol], parameters: Dict[str, Any] | None = None) Callable [source]#
Convert a Sympy expression (scalar, Matrix, or N-dimensional Array) or a string into a callable function. The returned function depends on the given
axes
and optionalparameters
, and returns either:A scalar or array of shape matching the input arguments (if the expression is scalar).
A 2D or higher-dimensional NumPy array (if the expression is a
sympy.matrices.Matrix
or N-dim Array).
The returned function utilizes numpy functions to ensure that it is relatively optimized for use on array valued arguments.
Note
While
lambdify_expression()
is designed to perform relatively well on array inputs, it should not be called in large loops as the call overhead can be quite large.- Parameters:
expression (
str
orsympy.Basic
) – The expression to be converted into a callable function. This may be any validsympy
expression or a string which can be parsed directly to a valid sympy expression usingsympy.sympify()
. Inexpression
is a sympy matrix or other non-scalar object, each element is lambdified independently and then combined again in the callable that is returned.axes (
list
ofSymbol
) – The list of coordinate symbols which will become the arguments of the resulting function. This should be a list of validSymbol
objects, each corresponding to an independent variable in the expression. The order of theaxes
will determine the expected order of inputs when using the resulting callable function.parameters (
dict
, optional) – A mapping from symbol name to numerical value (or another valid Sympy expression) which will be substituted into the final expression. Defaults toNone
.
- Returns:
A function
f(*axes)
that evaluates the expression numerically:If the expression is scalar: returns float or NumPy array.
If the expression is Matrix/Array: returns a NumPy array of shape
(rows, cols)
or the corresponding multi-dimensional shape. If the first argument is an array of length N, then the result generally has shape(N, rows, cols, ...)
etc.
- Return type:
Callable
- Raises:
If sympifying the expression fails. - If there are unrecognized symbols that do not match any of the axes or parameters. - If a constant expression cannot be converted to float.
Examples
>>> import sympy as sp >>> x = sp.Symbol('x') >>> expr = sp.Matrix([[1, 0], [x, 1]]) >>> f = lambdify_expression(expr, axes=[x]) >>> import numpy as np >>> print(f(2.0)) [[1. 0.] [2. 1.]] >>> # For an array of x-values, you'll get shape (N, 2, 2). >>> xs = np.linspace(0, 1, 3) >>> print(f(xs).shape) (3, 2, 2)
Notes
This operation proceeds in the following basic steps:
Parse and sympify string expressions.
Substitute known parameters.
Validate that any remaining free symbols are among the provided axes.
Identify whether the expression is a scalar, Matrix, or Array.
Recursively lambdify each entry (for Matrix/Array), or do a scalar lambdify.
Return a function that evaluates those sub-lambdas and aggregates them into a NumPy array of the correct shape (for Matrix/Array) or a scalar/array (for scalar expressions).