numpy interp decreasing xp numpy interp decreasing xp numpy numpy

numpy interp decreasing xp


Thanks for all those that have given their input, especially @Jaime.

I have experimented a bit, and came to this conclusion:

1) Apart from rounding errors, both methods mentioned by me have the same result.

2) They both take very much the same amount of time

3) I tried the scipy version, but it would reject the assume_sorted flag. Maybe my version of scipy is outdated. I suspect that if that flag is raised, scipy internally sorts the arrays. But the values are sorted, just in the opposite direction, so it doesn't need to do this.

Anyway, I'll use the reverse-direction method:

np.interp(x, xp[::-1], fp[::-1])

Just remember that in this case, you'll also have to reverse left and right, if you need them.


If you have access to Scipy, you could use the function interp1d, which has the keyword assume_sorted=False to handle decreasing arrays.

Edit: This solution handles both the cases of ordered and non ordered x values.

import numpy as npfrom scipy import interpolateimport matplotlib.pyplot as plt# Decreasing arrayx = np.arange(0, 10)[::-1]y = np.exp(-x/3.0)# Interpolation objectf = interpolate.interp1d(x, y, assume_sorted = False)xnew = np.arange(3,5)ynew = f(xnew)   # use interpolation function returned by `interp1d`plt.plot(x, y, 'o', xnew, ynew, '-')plt.show()