# scipy interpolate in sage Anonymous Hi there,

I am trying to interpolate a 1D function in sage using scipy.interpolate.interp1d(). I have an array of X values and an array of Y values. I generate the interpolation function like:

f = scipy.interpolation.interp1d(x, y, kind='cubic')

So far so good. Now, when I try to calculate the value of the function at a given value:

f(1.0)

I get the following error:

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_2.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -- coding: utf-8 --\n" + _support_.preparse_worksheet_cell(base64.b64decode("c2lsaWNvbl9yZWFsKDIwMC4wKQ=="),globals())+"\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in <module>

File "/tmp/tmpEKr8Bl/___code___.py", line 3, in <module> exec compile(u'silicon_real(_sage_const_200p0 ) File "", line 1, in <module>

NameError: name 'f' is not defined

edit retag close merge delete

I think we need more information. I cannot reconstruct this. What are x and y? Also, Sage 4.7.1 does not have interpolation, but it does have interpolate... ?

Sort by » oldest newest most voted

This seems to work well for me:

import scipy
import scipy.interpolate
# create x,y coordinates
x,y=zip(*[(i,i^3-i^3*sin(i)) for i in srange(0,10,step=0.1)])
f = scipy.interpolate.interp1d(x, y, kind='cubic')
f(1.0)


As DSM says, you misspelled the scipy "interpolate" module, so maybe that was the problem? Or maybe your worksheet timed out?

more

What role does the star in the zip function directly after the leading parenthesis play? Are you sure that's the first error message? You write

f = scipy.interpolation.interp1d(x, y, kind='cubic')


but the module is "scipy.interpolate", so I don't think this line can be succeeding, and that would explain why f isn't defined afterwards.

FWIW, it works for me:

sage: reset()
sage: import scipy.interpolate
sage: x = [1,2,3,4]
sage: y = [4,5,6,7]
sage: f = scipy.interpolate.interp1d(x,y,kind='cubic')
sage: f(1.0), f(1.5), f(2.0)
(array(3.9999999999999996), array(4.4999999999999982), array(4.9999999999999991))

more

I just misspelled the line, I am using the correct call in my program. Since it seems to work for you, I'll check it out further and come back if I find something.

Thanks.

more

Sure! It's always best to cut-and-paste to avoid this problem. If f is really being built, then it's probably out of scope for some reason. Find the minimal case and we can help more.