gEconpy.model.compile.sympy_to_pytensor#

gEconpy.model.compile.sympy_to_pytensor(inputs, outputs, cache=None, cse=True)#

Convert sympy expressions to pytensor graph nodes.

This is the sympytensor bridge: it takes sympy symbols and expressions and returns the corresponding pytensor input and output nodes, along with the updated cache that maintains the mapping between sympy and pytensor symbols.

When cse=True (default), runs sp.cse on the output expressions first and binds the extracted intermediates as named pytensor nodes before converting the reduced outputs. This produces a graph with explicit subgraph sharing, which keeps pt.grad from materializing the same backward subgraph hundreds of times. For DSGE-scale workloads the speedup on gradient compile is two orders of magnitude.

Parameters:
inputslist of sympy Symbol

Sympy input symbols.

outputslist of sympy expression, or sympy MutableDenseMatrix

Sympy output expressions.

cachedict, optional

Dictionary mapping sympytensor cache keys to pytensor variables. Used to maintain a consistent namespace across multiple conversions. Default is an empty dictionary.

csebool, optional

If True, run sympy’s common subexpression elimination on outputs before converting to pytensor. Set False to bypass when outputs are guaranteed atomic (e.g., a single Symbol passthrough). Default True.

Returns:
input_nodeslist of TensorVariable

Pytensor input nodes corresponding to the sympy inputs.

output_nodeslist of TensorVariable

Pytensor output nodes corresponding to the sympy outputs.

cachedict

Updated cache dictionary.