Ask Your Question
0

scipy interpolate in sage

asked 2011-09-30 04:53:55 -0500

anonymous user

Anonymous

updated 2015-01-22 14:31:58 -0500

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

edit retag flag offensive close merge delete

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 ( 2011-09-30 05:22:51 -0500 )edit

3 answers

Sort by » oldest newest most voted
1

answered 2011-09-30 05:19:49 -0500

Jason Grout gravatar image

updated 2011-09-30 05:21:02 -0500

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?

edit flag offensive delete link more

Comments

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

John Curran gravatar imageJohn Curran ( 2013-07-18 03:58:32 -0500 )edit
1

answered 2011-09-30 05:18:06 -0500

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))
edit flag offensive delete link more
0

answered 2011-09-30 06:00:52 -0500

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.

edit flag offensive delete link more

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 ( 2011-09-30 06:04:50 -0500 )edit

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: 2011-09-30 04:53:55 -0500

Seen: 539 times

Last updated: Sep 30 '11