Skip to content
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

Merged
merged 1 commit into from
Jan 30, 2018
Merged

Conversation

ev-br
Copy link
Member

@ev-br ev-br commented Jan 27, 2018

closes gh-7968

@@ -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:
Copy link
Contributor

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?

Copy link
Member Author

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.

@@ -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,
Copy link
Member

@pv pv Jan 28, 2018

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.

@ev-br
Copy link
Member Author

ev-br commented Jan 29, 2018

Updated. No more wrapping of integrand into a lambda, so a LowLevelCallable passes through without extra overhead.

@@ -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
Copy link
Contributor

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)

Copy link
Member Author

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:]
Copy link
Contributor

@andyfaff andyfaff Jan 30, 2018

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.

Copy link
Contributor

@andyfaff andyfaff left a 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.

@andyfaff andyfaff merged commit 327ef9c into scipy:master Jan 30, 2018
@ev-br ev-br added this to the 1.1.0 milestone Jan 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

integrate.quad handles decreasing limits (b<a) inconsistently
3 participants