Ask Your Question

Importing Sage functions into Cython?

asked 2013-05-14 14:46:04 +0200

Alan Chang gravatar image

updated 2014-06-09 12:57:32 +0200

slelievre gravatar image

I am playing around with Cython but I can't get it to work very well with Sage. As a very simple example of the problems I am having, the following code doesn't work. I know that the reason is that I need to import numerical_integral() from Sage somehow, but I don't know exactly how to do that.


numerical_integral(x^2, 0,1)

(This is something that's been confusing me for a few days now. I'm not sure if there's something about how Cython is supposed to be used that I am not understanding properly.)

EDIT: after trying out some things, I realized that sage.gsl.integration.numerical_integral() has the same effect as numerical_integral() in Sage. So, somehow, I should import the function from sage.gsl.integration, if that even makes any sense?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2013-05-14 15:04:10 +0200

vdelecroix gravatar image


To import a function you need to use a syntax like

from the_module import my_function

in your case it will be

from sage.gsl.integration import numerical_integral

To find out where is what, there is a magic function in Sage

sage: import_statements(numerical_integral)
from sage.gsl.integration import numerical_integral

(Note that the code you wrote won't run faster with cython: the function call will be in Cython but what is inside numerical_integral won't be different from a python call).

edit flag offensive delete link more



Thanks! Wow, import_statements() is amazing!. Yes, I realized that this wouldn't be faster. I just wanted to give a stand-alone example of calling Sage functions from Cython. (In my code, apart from Sage functions, there are other blocks which I believe would speed up with Cython.)

Alan Chang gravatar imageAlan Chang ( 2013-05-14 15:07:58 +0200 )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


Asked: 2013-05-14 14:46:04 +0200

Seen: 850 times

Last updated: May 14 '13