Ask Your Question
0

scipy interpolate in sage

asked 13 years ago

anonymous user

Anonymous

updated 10 years ago

FrédéricC gravatar image

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

Preview: (hide)

Comments

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... ?

kcrisman gravatar imagekcrisman ( 13 years ago )

3 Answers

Sort by » oldest newest most voted
1

answered 13 years ago

DSM gravatar image

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))
Preview: (hide)
link
1

answered 13 years ago

Jason Grout gravatar image

updated 13 years ago

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?

Preview: (hide)
link

Comments

What role does the star in the zip function directly after the leading parenthesis play?

John Curran gravatar imageJohn Curran ( 11 years ago )
0

answered 13 years ago

Aquiles gravatar image

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.

Preview: (hide)
link

Comments

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.

DSM gravatar imageDSM ( 13 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 13 years ago

Seen: 1,187 times

Last updated: Sep 30 '11