-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
DOC: Clarify behavior of interp in presence of duplicated x's (= discontinuities) #20028
Comments
I think this is a bug. Should have a check for strict increasing, e.g., |
Missing check here: numpy/numpy/lib/function_base.py Lines 1429 to 1432 in 7dabf22
In fact, it doesn't do any check at all with monotonicity. np.interp([0.5, 1.0, 1.5, 2.0], [0, 1, -1, 2], [5, 10, 7, 20]) is accepted without complaint. |
That is an intended speed tradeoff (#17860). |
Hmm, seems like a bad default IMO since it places too much of an expectation of expertise on the end user. Would have been better if the check could be disabled using a keyword argument, e.g., np.interp(...,...,...,check_monotonicity=False) # Default true But if a check can't be added then definitely needs clarification about what it does in the case where xp is not monotonic (including the case where it is not even weakly monotonic). |
Looks like it always chooses the rightmost value when searching, once it finds a set of xp values that are the same. This appears to be the case because it always uses a numpy/numpy/core/src/multiarray/compiled_base.c Lines 476 to 486 in 3544fae
|
I don't think so? I suppose when the needle is larg(ish) checking for monoticity would not matter speed-wise, and even if we can't change the default, I guess there may still be a point in giving the option to check. |
@seberg You are right. I only thought through the case where where
# Exact match x to xp - should be all 99.
np.interp(list(set(xp)),xp, y)
Ineact match np.interp(list(set(xp+.001)),xp, y) These seem nearly random, but are clearly deterministic and just depend on hard-to-guess initial conditions of the bisection.
|
Issue with current documentation:
Consider e.g.
If
x
does not contain the value 1, then the interpolation is well defined (it's a discontinuous piecewise linear function, going from a to b on [0, 1] and then from c to d on [1, 2]). AFAICT, np.interp indeed implements this correctly.However, the docs of interp explicitly state that
xp
needs to be (strictly) increasing (thus without any duplicated values). It may be worth explicitly stating whether the behavior in the presence of duplicated xp's (i.e., modelling a discontinuous piecewise linear function) is guaranteed, or whether one cannot rely on it.Idea or request for content:
No response
The text was updated successfully, but these errors were encountered: