Simplification#
Simplification of symbolic models.
Genetic programming produces correct but cluttered expressions: introns
(x - x), constant subtrees (sin(2.1)), and redundant parameters
(2.0*(3.1*x)) that make the model hard to read and the parameter estimation
ill-conditioned (de França and Kronberger, 2023; Kronberger and de França, 2025).
This module removes them in stages:
fold_constants()– every subtree without variables becomes one parameter;apply_rules()– algebraic rewriting to a fixed point (identities, inverse pairs, cancellation) and the merging of parameters in chains of additions and multiplications, a rule-based form of the parameter reduction of de França and Kronberger (2023);simplify_sympy()– algebraic simplification by SymPy, accepted only if the result is not larger and evaluates to the same values;round_coefficients()– the shortest decimal representation of the parameters that keeps the error within a tolerance;simplify()– the whole pipeline, with optional re-estimation of the parameters.
Protected operators are respected: a rewrite is applied only if it holds for the
protected semantics as well (for example x/x -> 1 holds for protected
division, exp(log(x)) -> abs(x) for the protected logarithm).
Examples#
>>> from eqdisco import Equation
>>> from eqdisco.expr import Expression as E
>>> from eqdisco.simplify import simplify
>>> e = E.apply("add", E.apply("mul", E.const(2.0), E.apply("mul", E.const(3.0), E.var(0))),
... E.apply("add", E.apply("sub", E.var(1), E.var(1)), E.apply("sin", E.const(0.5))))
>>> print(Equation(e, ["x0", "x1"]))
2 * 3 * x0 + x1 - x1 + sin(0.5)
>>> print(simplify(Equation(e, ["x0", "x1"])))
6 * x0 + 0.4794
Rewrite rules, parameter merging, SymPy simplification, coefficient rounding.
Functions#
|
Replace every subtree without variables by its value (a parameter if it contained one). |
|
Rewrite to a fixed point: constant folding, identities, cancellation and parameter merging. |
|
Merge the parameters of every addition chain and multiplication chain into one. |
|
SymPy simplification, kept only if it is not larger and (on |
|
Round all parameters to the fewest significant digits that keep the MSE within |
|
Simplification pipeline: rules, optional SymPy, optional refit and rounding. |
|
Canonical string (SymPy simplified, rounded coefficients) for comparing models. |