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
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.