Convert SymPy expressions into trainable Equinox modules
mainUse sympy2jax.SymbolicModule to transform a PyTree of SymPy expressions into an Equinox module.
In the resulting module:
- SymPy symbols become the inputs to the module.
- SymPy constants (floats, integers, rationals, etc.) become the leaves (parameters) of the module.
Because the output is an Equinox module, you can optimize these symbolic expressions via gradient descent using standard JAX/Equinox workflows.
import jax
import sympy
import sympy2jax
x_sym = sympy.symbols("x_sym")
cosx = 1.0 * sympy.cos(x_sym)
sinx = 2.0 * sympy.sin(x_sym)
# Create the module from a list of expressions
mod = sympy2jax.SymbolicModule([cosx, sinx])
# Execute the module by passing symbol names as keyword arguments
x = jax.numpy.zeros(3)
out = mod(x_sym=x)
# Access the trainable parameters (the constants from the SymPy expressions)
params = jax.tree_leaves(mod) # Returns [1.0, 2.0]