code
codegen.code
Attributes
| Name | Description |
|---|---|
| ARRAY_FUNCTION_MAPPINGS |
Classes
| Name | Description |
|---|---|
| FortranPrinter | |
| JaxPrinter | |
| JuliaPrinter | |
| LEMSPrinter | Printer for LEMS (Low Entropy Model Specification) math expressions. |
| MTKPrinter | Printer for ModelingToolkit.jl mtkmodel? equations. |
| NumPyPrinter | |
| PythonCodePrinter |
FortranPrinter
codegen.code.FortranPrinter(settings=None)JaxPrinter
codegen.code.JaxPrinter(settings=None, module='jnp')JuliaPrinter
codegen.code.JuliaPrinter(settings=None)LEMSPrinter
codegen.code.LEMSPrinter(settings=None)Printer for LEMS (Low Entropy Model Specification) math expressions.
Key differences from plain StrPrinter: - Powers use ^ instead of ** - Natural log is log (both SymPy and LEMS log are natural log) - abs instead of Abs - sign(x) → (H(x) - H(-1*x)) (Heaviside decomposition) - Mod(x, y) → (x + y*ceil(-(x/y))) - Relational operators use LEMS dot-notation: .gt., .geq., .lt., .leq., .eq., .neq. - Boolean operators: .and., .or., .not. - Piecewise rendered via Heaviside trick (H(cond)*val)
Parameters
settings : dict, optional Printer settings. Recognised key:
``parameters`` : list of str
Model symbol names. When a SymPy ``Function`` whose name matches
a parameter is encountered, it is printed as implicit multiplication
(``gamma*x``) instead of a function call (``gamma(x)``). This
defends against symbols that were parsed without proper
``parameters=`` overrides.
MTKPrinter
codegen.code.MTKPrinter(settings=None)Printer for ModelingToolkit.jl mtkmodel? equations.
MTK equations are scalar symbolic, so we use plain +, -, *, /, ^ instead of Julia’s element-wise .+, .-, .*, ./, .^.
NumPyPrinter
codegen.code.NumPyPrinter(settings=None, module='np')PythonCodePrinter
codegen.code.PythonCodePrinter(settings=None)Functions
| Name | Description |
|---|---|
| get_printer | |
| inline_functions | Inline all function applications in an expression. |
| print_Piecewise | Print Piecewise expressions as nested np.where statements. |
| render_equation | Render an equation to a target format. |
| render_equation_cse | Render equation as (setup, final) with common subexpressions hoisted. |
| render_expression | Render a SymPy expression or string to target format code. |
get_printer
codegen.code.get_printer(format, parameters=None, order=None)inline_functions
codegen.code.inline_functions(expr, func_defs)Inline all function applications in an expression.
Parameters
expr : sympy.Expr The expression containing function calls to inline. func_defs : dict Dictionary mapping function name -> (arg_names, body_expr) where arg_names is a list of argument names and body_expr is the sympy expression for the function body.
Returns
sympy.Expr Expression with all function calls replaced by their inlined bodies.
Example
from sympy import symbols, Function x, y, v = symbols(‘x y v’) # Define Sigm(v) = 2e0/(1 + exp(r(v0 - v))) func_defs = {‘Sigm’: ([‘v’], 2e0/(1 + exp(r(v0 - v))))} expr = ASigm(x - y) inline_functions(expr, func_defs) 2Ae0/(1 + exp(r(v0 - x + y)))
print_Piecewise
codegen.code.print_Piecewise(Printer, expr, verbose=False)Print Piecewise expressions as nested np.where statements.
render_equation
codegen.code.render_equation(
equation,
format='jax',
local_dict={},
user_functions={},
replace=None,
remove=None,
inline_funcs=None,
preserve_order=False,
**kwargs,
)Render an equation to a target format.
Parameters
equation : Equation The equation to render. format : str Target format: ‘jax’, ‘numpy’, ‘python’, ‘julia’, ‘fortran’, ‘latex’. local_dict : dict Dictionary of local symbols/functions for parsing. user_functions : dict Custom function mappings for the printer. replace : dict Symbol replacements {old_name: new_name}. remove : list Symbols to replace with zero. inline_funcs : dict, optional Dictionary mapping function name -> (arg_names, body_expr) for inlining custom functions. The body_expr should be a sympy expression. Example: {‘Sigm’: ([‘v’], 2e0/(1 + exp(r(v0 - v))))} preserve_order : bool If True, keep the source term order (no SymPy canonicalization). **kwargs Additional arguments passed to parse_eq.
Returns
str The rendered equation string.
render_equation_cse
codegen.code.render_equation_cse(
equation,
format='numpy',
local_dict={},
user_functions={},
replace=None,
remove=None,
inline_funcs=None,
preserve_order=False,
symbol_prefix='_cse',
**kwargs,
)Render equation as (setup, final) with common subexpressions hoisted.
setup is a list of (name, expr_str) assignments (dependency order) and final is the return-expression string. Repeated subexpressions — notably repeated model-function calls such as muV(fe, fi, ...) — are computed once via :func:sympy.cse. Interpreted backends (numpy / TVB) would otherwise re-evaluate every occurrence; the jax path keeps the flat render_equation form and leans on XLA’s JIT-time CSE. setup is empty when nothing is shared.
render_expression
codegen.code.render_expression(
expression,
format='jax',
user_functions={},
parameters=None,
infer_broadcasting=False,
preserve_order=False,
)Render a SymPy expression or string to target format code.
Uses parse_eq for proper handling of indexed expressions and Sum/Product.
Parameters
expression : str or sympy.Expr The expression to render. format : str Target format (‘jax’, ‘numpy’, ‘julia’, ‘python’, etc.) user_functions : dict Custom function name mappings for the printer. These are also passed to parse_eq so they’re recognized as functions (not implicit multiplication). parameters : list of str, optional Parameter names to define as Symbols. These OVERRIDE SymPy built-in functions (e.g., ‘gamma’ becomes Symbol(‘gamma’), not the gamma function). infer_broadcasting : bool If True, analyze indexed expressions and automatically add broadcasting dimensions (e.g., rmse[i] -> rmse[:, None] when used with a[i,j]). This enables mathematically correct notation to generate correct array code. preserve_order : bool If True, keep the source term order (no SymPy Add/Mul canonicalization) so generated code matches reference code operation-for-operation.