New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrong integration results of trigonometric functions #23223
Comments
It seems to me that >>> (Integral(f, (theta, 0, 2*pi)) - Integral(f, (theta, pi/2, 3*pi/2))).doit()
2 Not sure if this is the systematic problem you are encountering "I have a lot of problems". >>> Integral(abs(cos(theta)), theta).doit()
Piecewise(
(-sin(theta), theta <= 0),
(sin(theta), theta <= pi/2),
(2 - sin(theta), theta <= 3*pi/2),
(sin(theta) + 4, theta <= 2*pi),
(4 - sin(theta), True)
) Note that wolfram alpha simply gives sin(x)*sign(cos(x)) (which seems to be correct) and it should be possible to provide this answer in SymPy too. |
Indeed, a lot of my problems are about the periodicity. I was able to workaround a few of them like you did ! But in some cases, with equations using different shifted and scaled angles, splitting integrals rapidly become an hassle. Here's another :
It outputs :
|
Another one leading to an exception :
It outputs :
I don't expect any simplification from sympy. |
Internally the integrand is rewritten as p = Piecewise((cos(theta), cos(theta) >= 0), (-cos(theta), True)) sympy/sympy/integrals/integrals.py Lines 532 to 539 in d09723f
The condition cos(x)>=0 is awkward.
That then gives a valid antiderivative (with the same awkward condition): In [1]: x = Symbol('x', real=True)
In [2]: f = Abs(cos(x))
In [3]: f
Out[3]: │cos(x)│
In [4]: f.rewrite(Piecewise)
Out[4]:
⎧cos(x) for cos(x) ≥ 0
⎨
⎩-cos(x) otherwise
In [5]: f.rewrite(Piecewise).piecewise_integrate(x)
Out[5]:
⎧sin(x) for cos(x) ≥ 0
⎨
⎩-sin(x) otherwise That then goes wrong here: sympy/sympy/integrals/integrals.py Line 711 in d09723f
In [4]: f.rewrite(Piecewise).piecewise_integrate(x)._eval_interval(x, -pi/2, pi/2)
Out[4]: 0 At this point we get some weird output: sympy/sympy/functions/elementary/piecewise.py Lines 611 to 615 in d09723f
I think that the intention is that the In [4]: p = Piecewise((y, x>2), (z, x>1), (t, x>0))
In [5]: p
Out[5]:
⎧y for x > 2
⎪
⎨z for x > 1
⎪
⎩t for x > 0
In [6]: p._intervals(x)
Out[6]: (True, [(2, oo, y, 0), (1, oo, z, 1), (0, oo, t, 2)]) The result In [12]: from sympy.solvers.inequalities import _solve_inequality
In [13]: _solve_inequality(cos(x)>0, x)
Out[13]:
⎛ π⎞ ⎛3⋅π ⎞
⎜0 ≤ x ∧ x < ─⎟ ∨ ⎜─── < x ∧ x < 2⋅π⎟
⎝ 2⎠ ⎝ 2 ⎠ That is called here: sympy/sympy/functions/elementary/piecewise.py Lines 681 to 684 in d09723f
Clearly if solve_inequality can't properly handle trigonometric functions then its output should not be trusted here without some checking.
|
I have some problems using solveset too. Is that related ? I was trying to cleanup an example or two. |
The comment in this code says "# some improper integrals are better off with Abs". Perhaps some proper integrals are also better off with Abs? (considering the |
Trying to integrate various equations, I tried to find intervals using A simple example :
It outputs :
|
|
I see, so that would have the same/a similar problem as the current piecewise function that SymPy gives? |
The integral of a piecewise integrand should be split into parts. The current code does that but fails for periodic integrals because the old solvers don't handle periodic equations properly. Given an integral like For periodic integrals the obvious approach would be to transform the integral so it is periodic with period The OP example would transform to In [1]: integrate(abs(cos(x)), (x, 3*pi/2, 2*pi)) + integrate(abs(cos(x)), (x, 0, pi/2))
Out[1]: 2 I think the inequality solvers should have an option to raise an error instead of returning incorrect results for trigonometric functions. The domain argument could also be made to handle these cases properly: In [22]: solve_univariate_inequality(cos(x)>0, x, domain=Interval(-pi/2, pi/2))
Out[22]:
π
0 ≤ x ∧ x < ─
2 |
This should be changed: sympy/sympy/solvers/inequalities.py Lines 515 to 518 in d09723f
|
The type error seems to relate to a symbolic Interval. |
I have a lot of problems using trigonometric functions, Piecewise, Abs and Integral.
Here's the simplest case :
It outputs :
I use sympy 1.10 and python 3.9.7.
The text was updated successfully, but these errors were encountered: