code

codegen.code

Classes

Name Description
LEMSPrinter Printer for LEMS (Low Entropy Model Specification) math expressions.
MTKPrinter Printer for ModelingToolkit.jl mtkmodel? equations.

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 ln (SymPy log is natural; LEMS log is base-10) - abs instead of Abs - 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 .*, ./, .^.

Functions

Name Description
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_expression Render a SymPy expression or string to target format code.

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,
    **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))))} **kwargs Additional arguments passed to parse_eq.

Returns

str The rendered equation string.

render_expression

codegen.code.render_expression(
    expression,
    format='jax',
    user_functions={},
    parameters=None,
    infer_broadcasting=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.