Skip to content

Commit

Permalink
BUG: scipy/interpolate: fix PPoly/Cubic*Spline roots() extrapolation …
Browse files Browse the repository at this point in the history
…for single-interval case

Fix mistake in PPoly.roots(extrapolate=True) when a piecewise polynomial
has only a single interval. Previously, roots to the right of the
interval were not considered.
  • Loading branch information
pv committed Dec 13, 2019
1 parent d3ce7d4 commit 8e58423
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
7 changes: 5 additions & 2 deletions scipy/interpolate/_ppoly.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,11 @@ def real_roots(double[:,:,::1] c, double[::1] x, double y, bint report_discont,
wr[i] += x[interval]
if interval == 0 and extrapolate:
# Half-open to the left/right.
if (ascending and not wr[i] <= x[interval+1] or
not ascending and not wr[i] >= x[interval + 1]):
# Might also be the only interval, in which case there is
# no limitation.
if (interval != c.shape[1] - 1 and
(ascending and not wr[i] <= x[interval+1] or
not ascending and not wr[i] >= x[interval + 1])):
continue
elif interval == c.shape[1] - 1 and extrapolate:
# Half-open to the right/left.
Expand Down
13 changes: 13 additions & 0 deletions scipy/interpolate/tests/test_polyint.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,3 +693,16 @@ def test_CubicHermiteSpline_error_handling():

dydx_with_nan = [1, 0, np.nan]
assert_raises(ValueError, CubicHermiteSpline, x, y, dydx_with_nan)


def test_roots_extrapolate_gh_11185():
x = np.array([0.001, 0.002])
y = np.array([1.66066935e-06, 1.10410807e-06])
dy = np.array([-1.60061854, -1.600619])
p = CubicHermiteSpline(x, y, dy)

# roots(extrapolate=True) for a polynomial with a single interval
# should return all three real roots
r = p.roots(extrapolate=True)
assert_equal(p.c.shape[1], 1)
assert_equal(r.size, 3)

0 comments on commit 8e58423

Please sign in to comment.