cppODE(), cvode() and cppFUN() share one code
generator for equations in the syntax of expressions.
Symbols follow the notation
table.
Expression graph and emitted code
The generator emits straight-line C++ templated over the scalar type, one source for value, dual and nested-dual instantiations. Automatic differentiation [1] on an expression graph forms the Jacobian, the discrete-adjoint contractions, and the time derivatives of right-hand side and event roots at code-generation time.
Expression graph. Python’s parser reads each expression into a directed acyclic graph. States, parameters, forcings, time and constants are leaves; sums, products, powers and function calls are inner nodes. Nodes are hash-consed [2], [3]: identified by operation, operands and constant attributes, each is created once, so a repeated subexpression is one node without an elimination pass. Sums and products are flat with numeric coefficients and exponents; equal constants are one node. SymPy reads syntax the parser does not and differentiates functions without a graph rule.

Expression graph of \(f_A = f_B = -k_1 A B + k_2 C\) and \(f_C = k_1 A B - k_2 C\); edge labels are coefficients.
Example. Nodes used twice or more become temporaries:
const double _t0 = params[4]*x[2]; // k2*C
const double _t1 = params[3]*x[0]*x[1]; // k1*A*B
const double _t2 = _t0 - _t1;
dxdt[0] = _t2;
dxdt[1] = _t2;
dxdt[2] = -_t0 + _t1;Derivatives. Forward and reverse sweeps over the graph form Jacobian-vector and vector-Jacobian products in time linear in the graph size [1], as new nodes of the same graph, so derivative and value share common parts. The reverse contractions \(J^{\mathsf T}\lambda\), \((\partial f/\partial p)^{\mathsf T}\lambda\) are vector-Jacobian products; Rosenbrock stages and event localization use Jacobian-vector products along \(\dot x = f\).
\(J = \partial f/\partial x\) is assembled by entries or by coloring:
- Entries. Each state-dependent node holds a sparse state gradient; those of the graph roots are the nonzero entries of \(J\). The cost \(W_{\mathrm{ent}} = \sum_{v} |\mathrm{supp}\,\nabla v|\) over the \(E_s\) state-dependent nodes \(v\) of \(f\) grows with the density of \(J\).
- Coloring, when \(W_{\mathrm{ent}} > \max(4E_s, 50\,000)\). A greedy distance-2 coloring of the column intersection graph groups structurally orthogonal columns [4], [5], [6]. With at most \(n_x/4\) colors, one Jacobian-vector product per color, seeded with ones on the columns of that color, recovers those columns; with more colors, entries are kept.
Linear tables. A sum of twelve or more terms \(c_j\,x_j\,\phi\), numeric \(c_j\), common state-free \(\phi\), becomes a row of a constant matrix \(C\), \(f(x) = \psi(x, Cx)\). The code evaluates \(z = Cx\) once per call from a sparse or dense table and assembles \[ J = \frac{\partial \psi}{\partial x} + \frac{\partial \psi}{\partial z}\,C, \tag{1} \] while the reverse contractions apply \(C^{\mathsf T}\). A dense coupling, such as a dipolar interaction of all states, costs one table, not a sum of expressions per entry.
Loops. Statements whose graphs agree up to state indices and constants, such as method-of-lines grid points, form a class. Sixteen or more instances become one loop over index and constant tables, a sum of many like terms an inner loop. Differentiation stays per instance, so generator work grows with the instance count; the emitted code of a regular model does not.