Skip to content

Commit

Permalink
MAINT: integrate: handle b < a in quad
Browse files Browse the repository at this point in the history
  • Loading branch information
ev-br committed Jan 29, 2018
1 parent 8be7f95 commit fc013a6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
9 changes: 8 additions & 1 deletion scipy/integrate/quadpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,20 @@ 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):

# check the limits of integration: \int_a^b, expect a < b
flip, a, b = b < a, min(a, b), max(a, b)

if weight is None:
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:]

ier = retval[-1]
if ier == 0:
return retval[:-1]
Expand Down
33 changes: 33 additions & 0 deletions scipy/integrate/tests/test_quadpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,39 @@ def myfunc(x, a):
assert_quad(quad(myfunc, 0, 5, args=0.4, weight='cauchy', wvar=2.0),
tabledValue, errTol=1.9e-8)

def test_b_less_than_a(self):
def f(x, p, q):
return p * np.exp(-q*x)

val_1, err_1 = quad(f, 0, np.inf, args=(2, 3))
val_2, err_2 = quad(f, np.inf, 0, args=(2, 3))
assert_allclose(val_1, -val_2, atol=max(err_1, err_2))

def test_b_less_than_a_2(self):
def f(x, s):
return np.exp(-x**2 / 2 / s) / np.sqrt(2.*s)

val_1, err_1 = quad(f, -np.inf, np.inf, args=(2,))
val_2, err_2 = quad(f, np.inf, -np.inf, args=(2,))
assert_allclose(val_1, -val_2, atol=max(err_1, err_2))

def test_b_less_than_a_3(self):
def f(x):
return 1.0

val_1, err_1 = quad(f, 0, 1, weight='alg', wvar=(0, 0))
val_2, err_2 = quad(f, 1, 0, weight='alg', wvar=(0, 0))
assert_allclose(val_1, -val_2, atol=max(err_1, err_2))

def test_b_less_than_a_full_output(self):
def f(x):
return 1.0

res_1 = quad(f, 0, 1, weight='alg', wvar=(0, 0), full_output=True)
res_2 = quad(f, 1, 0, weight='alg', wvar=(0, 0), full_output=True)
err = max(res_1[1], res_2[1])
assert_allclose(res_1[0], -res_2[0], atol=err)

def test_double_integral(self):
# 8) Double Integral test
def simpfunc(y, x): # Note order of arguments.
Expand Down

0 comments on commit fc013a6

Please sign in to comment.