Proposal to Improve Numeric Precision by Algebraic Rewriting Before Evaluation #28537

13 views
Skip to first unread message

Ashutosh Chauhan

unread,
Nov 3, 2025, 11:47:17 AM (3 days ago) Nov 3
to sympy

I suggest enhancing SymPy’s numeric evaluation process by adding a step to algebraically rewrite expressions into simpler or more numerically stable forms before substituting numeric values. This mirrors human mathematical practice and can significantly improve floating-point evaluation accuracy.
Direct substitution followed by numeric evaluation in SymPy can suffer from precision loss due to floating-point errors and cancellation, especially for expressions involving differences of similar terms or logarithmic combinations. Applying algebraic transformations first reduces this risk.

Examples:

Difference of squares in subtraction

a, b = symbols('a b') expr = a**2 - b**2 rewritten = (a - b) * (a + b) val_expr = expr.subs({a: 1.0001, b: 1.0}).evalf() val_rewritten = rewritten.subs({a: 1.0001, b: 1.0}).evalf()

SymPy includes rewriting functions (rewrite()) and simplification tools (simplify()) but does not systematically use algebraic rewriting as a preprocessing step before numeric evaluation (evalf). Automating this algebraic-simplification step as the default optional behavior (enabled via optimize_for_precision=True), and introduce another flag such as (skip_algebraic_optimization=True) to explicitly disable this pre-evaluation symbolic conversion when needed.
Benefits:

  • Improved precision in numeric evaluation, preventing floating-point cancellation errors.

  • More mathematically natural evaluation, reducing manual intervention for users.

  • Potentially wider real-world applicability in science and engineering computations.

I welcome feedback on this idea’s usefulness and suggestions on possible integration paths. I am happy to contribute example implementations or testing if this feature is accepted for consideration.

Oscar Benjamin

unread,
Nov 4, 2025, 3:45:02 PM (2 days ago) Nov 4
to sy...@googlegroups.com
On Mon, 3 Nov 2025 at 16:47, Ashutosh Chauhan <ashuto...@gmail.com> wrote:
>
> I suggest enhancing SymPy’s numeric evaluation process by adding a step to algebraically rewrite expressions into simpler or more numerically stable forms before substituting numeric values.

There have been previous proposals around the Herbie algorithm which
is for this. In general a difficulty of using something like this is
that SymPy expressions don't preserve things like the order of
associative operations e.g. there is no distinction between (x + y) +
z and x + (y + z) even though these can be very different for floating
point accuracy.

Note that in your example it is recommended to use

expr.evalf(subs={x:0.1})

rather than

expr.subs({x:0.1}).evalf()

The former is recommended because it is more accurate.

--
Oscar
Reply all
Reply to author
Forward
0 new messages