CARVIEW |
Navigation Menu
-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Integrate gives a wrong result #14991
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
Conversation
The PR fixes issue 14782. integrate(sqrt(-x**2 + 1)*(-x**2 + x), [x, -1, 1]) gives 0 instead -pi/8. The antiderivative works correctly, but it returns a function containing a Piecewise expression, defined in the open interval (-1, 1). The function _eval_interval() does not work correctly, since the antiderivative is not a Piecewise expression. Hence, separating the terms of type Piecewise and using the _eval_interval() for Piecewise() solves the issue. Added a test. Unfortunately, a test in test_ode.py fails due to the fact that a minus sign compares inside Piecewise intead that outside. So I modified such a test, without changing the final result.
âś… Hi, I am the SymPy bot (v121). I'm here to make sure this pull request has a release notes entry. Please read the guide on how to write release notes. Click here to see the pull request description that was parsed.
Your release notes are in good order. Here is what the release notes will look like:
This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.2.1. Note: This comment will be updated with the latest check if you edit the pull request. You need to reload the page to see it. Update The release notes on the wiki have been updated. |
sympy/integrals/integrals.py
Outdated
else: | ||
others.append(f) | ||
uneval = Add(*[eval_factored(f, x, a, b) | ||
for f in integrals]) | ||
try: | ||
evalued = Add(*others)._eval_interval(x, a, b) | ||
function = uneval + evalued | ||
evalued_pw = Add(*piecewises)._eval_interval(x, a, b) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What will happen if there are several items in piecewises?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You right. It does not work, since Add(*piecewises)
is an Add
and not Piecewise
.
evalued_pw = piecewise_fold(Add(*piecewises))._eval_interval(x, a, b)
seems to work.
sympy/solvers/tests/test_ode.py
Outdated
@@ -1584,7 +1584,7 @@ def test_1st_homogeneous_coeff_ode3(): | |||
sol = Eq(log(f(x)), C1 - Piecewise( | |||
(-acosh(f(x)/x), abs(f(x)**2)/x**2 > 1), | |||
(I*asin(f(x)/x), True))) | |||
assert dsolve(eq, hint='1st_homogeneous_coeff_subs_indep_div_dep') == sol | |||
assert simplify(dsolve(eq, hint='1st_homogeneous_coeff_subs_indep_div_dep') - sol) == 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of simplify
is discouraged as it is an expensive operation. In this case, it is better to edit sol
.
Thanks, this looks good. |
References to other Issues or PRs
Fixes #14782
Brief description of what is fixed or changed
integrate(sqrt(-x**2 + 1)*(-x**2 + x), [x, -1, 1])
gives0
instead-pi/8
.The antiderivative works correctly, but it returns a function containing
a
Piecewise()
expression, defined in the open interval(-1, 1)
.The function
_eval_interval()
does not work correctly, since the antiderivativeis not a
Piecewise()
expression.Hence, separating the terms of type
Piecewise()
and using the_eval_interval()
forPiecewise()
solves the issue.Other comments
Added a test.
Unfortunately, a test in
test_ode.py
fails due to the fact that a minus sign comparesinside
Piecewise()
instead that outside. So I modified such a test, without changing thefinal result.
Release Notes