sympy / sympy

A computer algebra system written in pure Python
https://sympy.org/
Other
12.49k stars 4.32k forks source link

sympy.integrate gives inconsistent results on a mathematically equivalent expression #26579

Open terben opened 3 weeks ago

terben commented 3 weeks ago

Here is my code:

import sympy as sp

u, b, x = sp.symbols('u, b, x', real=True)

sp.integrate(sp.sinh(x / u + b / u)**2 + 1, x)

sp.integrate(sp.sinh((x + b) / u)**2 + 1, x)

SymPy evaluates the first integral but it gives back the second one unevaluated.

The only difference is that the argument of the sinh in the first integral is written as x / u + b / u and in the second one as (x + b) / u.

sympy.__version__ returns 1.12

I would expect that both expressions return the same result.

asmeurer commented 3 weeks ago

The result is coming from heurisch. It's too surprising heurisch sensitive to the input format.

A good fix here would be to make powers of hyperbolic functions work in manualintegrate. The strategy should be similar to the one used for powers of regular trig functions. For example, $\sinh(x)^2 = \frac{\sinh(x)}{2} - \frac{1}{2}$ (that identity may need to be implemented somewhere, I'm not sure if it already is). Manualintegrate should handle the argument being (x + b)/u automatically via u-substitution.

Actually, I imagine quite a bit of manualintegrate's trig functionality could be extended to work with hyperbolic trig functions.

smichr commented 2 weeks ago

If possible, hyperbolics can be recast to normal trig functions with the Osborne transformation (private functions in fu.py), simplified, and recast to hyperbolics before integration.