Skip to content

Commit

Permalink
Merge branch 'pr-2946-interp1d' into master.
Browse files Browse the repository at this point in the history
Reviewed at #2946
  • Loading branch information
rgommers committed Feb 4, 2014
2 parents 9c7017e + 18b822d commit 5309706
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 154 deletions.
46 changes: 28 additions & 18 deletions scipy/interpolate/interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def __call__(self, x, y, dx=0, dy=0):
class interp1d(_Interpolator1D):
"""
interp1d(x, y, kind='linear', axis=-1, copy=True, bounds_error=True,
fill_value=np.nan)
fill_value=np.nan, assume_sorted=False)
Interpolate a 1-D function.
Expand All @@ -288,7 +288,7 @@ class interp1d(_Interpolator1D):
Parameters
----------
x : (N,) array_like
A 1-D array of monotonically increasing real values.
A 1-D array of real values.
y : (...,N,...) array_like
A N-D array of real values. The length of `y` along the interpolation
axis must be equal to the length of `x`.
Expand All @@ -314,6 +314,10 @@ class interp1d(_Interpolator1D):
If provided, then this value will be used to fill in for requested
points outside of the data range. If not provided, then the default
is NaN.
assume_sorted : bool, optional
If False, values of `x` can be in any order and they are sorted first.
If True, `x` has to be an array of monotonically increasing values.
See Also
--------
Expand All @@ -337,7 +341,8 @@ class interp1d(_Interpolator1D):
"""

def __init__(self, x, y, kind='linear', axis=-1,
copy=True, bounds_error=True, fill_value=np.nan):
copy=True, bounds_error=True, fill_value=np.nan,
assume_sorted=False):
""" Initialize a 1D linear interpolation class."""
_Interpolator1D.__init__(self, x, y, axis=axis)

Expand All @@ -358,6 +363,11 @@ def __init__(self, x, y, kind='linear', axis=-1,
x = array(x, copy=self.copy)
y = array(y, copy=self.copy)

if not assume_sorted:
ind = np.argsort(x)
x = x[ind]
np.take(y, ind, axis=axis, out=y)

if x.ndim != 1:
raise ValueError("the x array must have exactly one dimension.")
if y.ndim == 0:
Expand Down Expand Up @@ -578,7 +588,7 @@ def extend(self, c, x, right=True):
"""
c = np.asarray(c)
x = np.asarray(x)

if c.ndim < 2:
raise ValueError("invalid dimensions for c")
if x.ndim != 1:
Expand Down Expand Up @@ -1035,7 +1045,7 @@ class BPoly(_PPolyBase):
This creates a 2nd order polynomial
.. math::
B(x) = 1 \\times b_{0, 2}(x) + 2 \\times b_{1, 2}(x) + 3 \\times b_{2, 2}(x) \\\\
= 1 \\times (1-x)^2 + 2 \\times 2 x (1 - x) + 3 \\times x^2
Expand Down Expand Up @@ -1076,9 +1086,9 @@ def derivative(self, nu=1):
if nu == 0:
c2 = self.c.copy()
else:
# For a polynomial
# For a polynomial
# B(x) = \sum_{a=0}^{k} c_a b_{a, k}(x),
# we use the fact that
# we use the fact that
# b'_{a, k} = k ( b_{a-1, k-1} - b_{a, k-1} ),
# which leads to
# B'(x) = \sum_{a=0}^{k-1} (c_{a+1} - c_a) b_{a, k-1}
Expand Down Expand Up @@ -1151,29 +1161,29 @@ def from_derivatives(cls, xi, yi, orders=None, direction=None,
``yi[i][j]`` is the ``j``-th derivative known at ``xi[i]``
orders : None or int or array_like of ints. Default: None.
Specifies the degree of local polynomials. If not None, some
derivatives are ignored.
derivatives are ignored.
axis : int, optional
Interpolation axis, default is 0.
extrapolate : bool, optional
Whether to extrapolate to ouf-of-bounds points based on first
and last intervals, or to return NaNs. Default: True.
and last intervals, or to return NaNs. Default: True.
Notes
-----
If ``k`` derivatives are specified at a breakpoint ``x``, the
constructed polynomial is exactly ``k`` times continuously
constructed polynomial is exactly ``k`` times continuously
differentiable at ``x``, unless the ``order`` is provided explicitly.
In the latter case, the smoothness of the polynomial at
the breakpoint is controlled by the ``order``.
Deduces the number of derivatives to match at each end
from ``order`` and the number of derivatives available. If
possible it uses the same number of derivatives from
each end; if the number is odd it tries to take the
extra one from y2. In any case if not enough derivatives
are available at one end or another it draws enough to
make up the total from the other end.
If the order is too high and not enough derivatives are available,
an exception is raised.
Expand Down Expand Up @@ -1206,7 +1216,7 @@ def from_derivatives(cls, xi, yi, orders=None, direction=None,
raise NotImplementedError
if direction is not None:
raise NotImplementedError

xi = np.asarray(xi)
if len(xi) != len(yi):
raise ValueError("xi and yi need to have the same length")
Expand All @@ -1224,7 +1234,7 @@ def from_derivatives(cls, xi, yi, orders=None, direction=None,
if isinstance(orders, integer_types):
orders = [orders] * m
k = max(k, max(orders))

if any(o <= 0 for o in orders):
raise ValueError("Orders must be positive.")

Expand Down Expand Up @@ -1256,7 +1266,7 @@ def from_derivatives(cls, xi, yi, orders=None, direction=None,

@staticmethod
def _construct_from_derivatives(xa, xb, ya, yb):
"""Compute the coefficients of a polynomial in the Bernstein basis
"""Compute the coefficients of a polynomial in the Bernstein basis
given the values and derivatives at the edges.
Return the coefficients of a polynomial in the Bernstein basis
Expand Down Expand Up @@ -1303,10 +1313,10 @@ def _construct_from_derivatives(xa, xb, ya, yb):
.. math:: Q_a = \sum_{j=0}^{q} (-)^{j+q} comb(q, j) c_{j+a}
This way, only `a=0` contributes to :math: `B^{q}(x = xa)`, and
This way, only `a=0` contributes to :math: `B^{q}(x = xa)`, and
`c_q` are found one by one by iterating `q = 0, ..., na`.
At `x = xb` it's the same with `a = n - q`.
At `x = xb` it's the same with `a = n - q`.
"""
ya, yb = np.asarray(ya), np.asarray(yb)
Expand Down Expand Up @@ -1344,7 +1354,7 @@ def _construct_from_derivatives(xa, xb, ya, yb):
def _raise_degree(c, d):
"""Raise a degree of a polynomial in the Bernstein basis.
Given the coefficients of a polynomial degree `k`, return (the
Given the coefficients of a polynomial degree `k`, return (the
coefficients of) the equivalent polynomial of degree `k+d`.
Parameters
Expand Down
Loading

0 comments on commit 5309706

Please sign in to comment.