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.
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/sympy/87cb38d4-30d1-49d7-b2d8-cb0f901c1cd3n%40googlegroups.com.