symnum.codegen module#
Utility functions to generate NumPy function code.
- class symnum.codegen.FunctionExpression(args, return_val)[source]#
Bases:
object
Function defined by a symbolic expression and set of symbolic arguments.
- Parameters:
args (tuple[Union[sympy.Expr, SymbolicArray], ...]) – Symbolic arguments to function.
return_val (Union[sympy.Expr, SymbolicArray]) – Symbolic expression in arguments corresponding to function return value.
- __call__(*args)[source]#
Evaluate symbolic function.
- Parameters:
*args (Union[sympy.Expr, SymbolicArray]) – Symbolic values to pass as positional arguments to function.
- Returns:
Symbolic value corresponding to return value for function for passed symbolic arguments.
- Return type:
Union[sympy.Expr, SymbolicArray]
- args#
- return_val#
- class symnum.codegen.TupleArrayPrinter(settings=None)[source]#
Bases:
NumPyPrinter
SymPy printer which uses nested tuples for array literals.
Rather than printing array-like objects as numpy.array calls with a nested list argument (which is not numba compatible) a nested tuple argument is used instead.
settings is passed to CodePrinter.__init__() module specifies the array module to use, currently ‘NumPy’, ‘CuPy’ or ‘JAX’.
- doprint(expr, assign_to=None)#
Print the expression as code.
- Parameters:
expr (
Expression
) – The expression to be printed.assign_to (
Symbol
,string
,MatrixSymbol
,list
ofstrings
orSymbols (optional)
) – If provided, the printed code will set the expression to a variable or multiple variables with the name or names given inassign_to
.
- emptyPrinter(expr)#
- language = 'Python'#
- modules = None#
- property order#
- parenthesize(item, level, strict=False)#
- reserved_words = {'False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'}#
- classmethod set_global_settings(**settings)#
Set system-wide printing settings.
- stringify(args, sep, level=0)#
- tab = ' '#
- symnum.codegen.generate_func(inputs, exprs, func_name, *, printer=None, numpy_module=None, namespace=None, jit=False, simplify=False)[source]#
Generate a Python function from symbolic expression(s).
- Parameters:
inputs (Iterable[Union[sympy.Expr, SymbolicArray]]) – List of symbolic inputs to use as function arguments.
exprs (Iterable[Union[sympy.Expr, SymbolicArray, FunctionExpression]]) – List of symbolic expressions to use as function return values.
func_name (str) – Name to define generated function with.
printer (Optional[Printer]) – Instance of SymPy
sympy.printing.printer.Printer
(sub)class which produces string representations of symbolic expressions to be used in code for generated function. Defaults to an instance ofTupleArrayPrinter
, a subclass ofsympy.printing.numpy.NumpyPrinter
, which prints object argument tonumpy.array()
as a nested tuple rather than a nested list, which retains compatibility withnumba
which only supports tuple array literals.numpy_module (Optional[ModuleType]) – Module implementing NumPy API to use in NumPy API calls in generated function. Defaults to
numpy
.namespace (Optional[dict]) – Namespace to define generated function in. Default is to create a temporary module and define function in that namespace. Set
namespace=globals()
to define in current global namespace.jit (bool) – If
True
enables just-in-time compilation of the generated function using Numba (requiresnumba
package to be installed in the current Python environment). Default isjit=False
.simplify (bool) – Whether to try to simplify symbolic expressions in
exprs
before generating code.
- symnum.codegen.numpify(*arg_shapes, **kwargs)[source]#
Decorator to convert SymPy symbolic array function to a NumPy function.
- Parameters:
*arg_shapes (tuple[int]) – Variable length list of tuples and integers defining shapes of array arguments to func, e.g. if func takes two arguments x and y with x an array with shape (2, 2) and y an array with shape (2, 4, 3) the call signature would be of the form numpify((2, 2), (2, 4, 3))(func).
**kwargs – Any keyword arguments to
generate_func()
.
- Returns:
Decorator which takes a SymPy function of which accepts one or more
SymbolicArray
as arguments and returns a symbolic scalar orSymbolicArray
value, and returns a corresponding NumPy function which accepts one or morenumpy.ndarray
arguments and returns a scalar ornumpy.ndarray
.- Return type:
Callable[[Callable[…, Union[sympy.Expr, SymbolicArray]]], Callable[…, Union[ScalarLike, NDArray]]]
- symnum.codegen.numpify_func(sympy_func, *arg_shapes, **kwargs)[source]#
Generate a NumPy function from a SymPy symbolic array function.
- Parameters:
sympy_func (Callable[..., Union[sympy.Expr, SymbolicArray]]) – Function which takes one or more
SymbolicArray
as arguments and returns a symbolic scalar expression orSymbolicArray
value.*arg_shapes (tuple[int]) – Variable length list of tuples or integers defining shapes of array arguments to func, e.g. if func takes two arguments x and y with x an array with shape (2, 2) and y an array with shape (2, 4, 3) the call signature would be of the form numpify_func(func, (2, 2), (2, 4, 3)).
**kwargs – Any keyword arguments to
generate_func()
.
- Returns:
Generated NumPy function which takes one or more numpy.ndarray arguments and return a scalar or numpy.ndarray value.
- Return type:
Callable[…, Union[ScalarLike, NDArray]]