# [Numpy-discussion] How to make scipy.interpolate give a an extrapolated result beyond the input range?

Fri Apr 30 11:18:39 CDT 2010

```I'm trying to port a program which currently uses a hand-rolled C++
interpolator (developed by a mathematician colleage) over to use the
interpolators provided by scipy. I'd like to use or wrap the scipy
interpolator so that it's behavior is as close as possible behavior to
our old interpolator.

A key difference between the two functions is that in our original
interpolator - if the input value is above or below the input range, our
original interpolator will extrapolate the result. If you try this with
the scipy interpolator it raises a ValueError.  Consider this program as
an example:

# EXAMPLE
import numpy as np
from scipy import interpolate

x = np.arange(0,10)
y = np.exp(-x/3.0)
f = interpolate.interp1d(x, y)

print f(9)
print f(11) ##### Causes ValueError, because it's greater than max(x) #
END OF EXAMPLE

In the example above, I'd like the last line not to raise a ValueError,
but to return a value calculated from the gradient of the line between
f(x[-2]) and f(x[-1]).

Is there a sensible way to make it so that instead of crashing, the
final line will simply do a linear extrapolate, continuing the gradients
defined by the first and last pairs of input data-points to infinity?

I know that this is a simple enough function to write myself, however
I'd rather not re-invent the wheel, especially as if I wanted to
introduce new basic math functions into our library they would need to
be validated by a number of gate-keepers before they were permitted into
our library!

I'm on Python 2.4, scipy 0.7 on Windows XP, 32bit

Incidentally, I have seen this tutorial which has a "left" and "right"
argument on the interpolator. This does not seem to exist on any version
of the interp1d function which I can use on Windows Python 2.4 - can
anybody speculate which version of Scipy this tutorial is intended for?
http://projects.scipy.org/scipy/browser/branches/Interpolate1D/docs/tuto
rial.rst?rev=4591

Sal
-------------- next part --------------
This email does not create a legal relationship between any member of the Cr?dit Agricole group and the recipient or constitute investment advice.
The content of this email should not be copied or disclosed (in whole or part) to any other person. It may contain information which is
confidential, privileged or otherwise protected from disclosure. If you are not the intended recipient, you should notify us and delete it from your system. Emails may be monitored, are not secure and may be amended, destroyed or contain viruses and in communicating with us such conditions are accepted. Any content which does not relate to business matters is not endorsed by us.

Cr?dit Agricole Corporate and Investment Bank is authorised by the Comit? des Etablissements de Cr?dit et des Entreprises d'Investissement (CECEI) and supervised by the Commission Bancaire in France and subject to limited regulation by the Financial Services Authority.
Details about the extent of our regulation by the Financial Services Authority are available from us on request.
Cr?dit Agricole Corporate and Investment Bank is incorporated in France with limited liability and registered in England & Wales. Registration number: FC008194.
Registered office: Broadwalk House, 5 Appold Street, London, EC2A 2DA.
```