-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
MAINT: integrate: handle b < a in quad #8336
Conversation
scipy/integrate/quadpack.py
Outdated
@@ -330,9 +330,15 @@ def quad(func, a, b, args=(), full_output=0, epsabs=1.49e-8, epsrel=1.49e-8, | |||
(0.0, 0.0) | |||
|
|||
""" | |||
if b < a: |
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.
How about:
a, b = min(a, b), max(a, b)
instead of adding this clause?
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.
Not quite, because below it integrates either f(x) or -f(x), depending on whether a<b or not.
scipy/integrate/quadpack.py
Outdated
@@ -330,9 +330,15 @@ def quad(func, a, b, args=(), full_output=0, epsabs=1.49e-8, epsrel=1.49e-8, | |||
(0.0, 0.0) | |||
|
|||
""" | |||
if b < a: | |||
return quad(lambda x, *args: -func(x, *args), b, a, args, full_output, |
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.
Don't change the integrand function to a lambda (what if the input function is a LowLevelCallable?). Instead, swap the sign of the result.
Updated. No more wrapping of integrand into a lambda, so a LowLevelCallable passes through without extra overhead. |
scipy/integrate/quadpack.py
Outdated
@@ -332,13 +332,22 @@ def quad(func, a, b, args=(), full_output=0, epsabs=1.49e-8, epsrel=1.49e-8, | |||
""" | |||
if not isinstance(args, tuple): | |||
args = (args,) | |||
if (weight is None): | |||
|
|||
flip = False |
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.
flip, a, b = b < a, min(a, b), max(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.
OK, done.
retval = _quad(func, a, b, args, full_output, epsabs, epsrel, limit, | ||
points) | ||
else: | ||
retval = _quad_weight(func, a, b, args, full_output, epsabs, epsrel, | ||
limlst, limit, maxp1, weight, wvar, wopts) | ||
|
||
if flip: | ||
retval = (-retval[0],) + retval[1:] |
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.
I started looking through stats.quadpack._quad
to check this line - eek, no docstring saying what it accepts, and what the return values are. Definitely a case for a documentation effort at a future point in time.
EDIT: I was being dense, I understand what this line is doing, but _quad could probably do with more doco.
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.
I think this looks good now. One could possibly use np.sign
instead of having to use the if flip:
, but it's clear this way as well.
closes gh-7968