ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 14 May 2013 15:07:58 +0200Importing Sage functions into Cython?https://ask.sagemath.org/question/10121/importing-sage-functions-into-cython/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.
%cython
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? Tue, 14 May 2013 14:46:04 +0200https://ask.sagemath.org/question/10121/importing-sage-functions-into-cython/Answer by vdelecroix for <p>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 <code>numerical_integral()</code> from Sage somehow, but I don't know exactly how to do that.</p>
<pre><code>%cython
numerical_integral(x^2, 0,1)
</code></pre>
<p>(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.)</p>
<p>EDIT: after trying out some things, I realized that <code>sage.gsl.integration.numerical_integral()</code> has the same effect as <code>numerical_integral()</code> in Sage. So, somehow, I should import the function from <code>sage.gsl.integration</code>, if that even makes any sense? </p>
https://ask.sagemath.org/question/10121/importing-sage-functions-into-cython/?answer=14924#post-id-14924Hi,
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).Tue, 14 May 2013 15:04:10 +0200https://ask.sagemath.org/question/10121/importing-sage-functions-into-cython/?answer=14924#post-id-14924Comment by Alan Chang for <p>Hi,</p>
<p>To import a function you need to use a syntax like</p>
<pre><code>from the_module import my_function
</code></pre>
<p>in your case it will be</p>
<pre><code>from sage.gsl.integration import numerical_integral
</code></pre>
<p>To find out where is what, there is a magic function in Sage</p>
<pre><code>sage: import_statements(numerical_integral)
from sage.gsl.integration import numerical_integral
</code></pre>
<p>(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).</p>
https://ask.sagemath.org/question/10121/importing-sage-functions-into-cython/?comment=17701#post-id-17701Thanks! 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.)Tue, 14 May 2013 15:07:58 +0200https://ask.sagemath.org/question/10121/importing-sage-functions-into-cython/?comment=17701#post-id-17701