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.Sun, 22 Aug 2010 21:30:23 +0200using %cython in Cantor with sage backendhttps://ask.sagemath.org/question/7611/using-cython-in-cantor-with-sage-backend/To use the %cython magic function in the sage notebook you can do something like:
%cython
from math import sin
def f(x):
return sin(x**2)
def integral(double a, double b, int N):
cdef double dx = (b-a)/N
cdef int i
cdef double s = 0
for i in range(N):
s += f(a+dx*i)
return s * dx
But in cantor you can't since the sage mode in Cantor is not the same as the sage notebook.
How can i do something like that in Cantor without using an external editor or without using more commands?
Sun, 22 Aug 2010 19:06:38 +0200https://ask.sagemath.org/question/7611/using-cython-in-cantor-with-sage-backend/Answer by Mike Hansen for <p>To use the %cython magic function in the sage notebook you can do something like:</p>
<pre><code>%cython
from math import sin
def f(x):
return sin(x**2)
def integral(double a, double b, int N):
cdef double dx = (b-a)/N
cdef int i
cdef double s = 0
for i in range(N):
s += f(a+dx*i)
return s * dx
</code></pre>
<p>But in cantor you can't since the sage mode in Cantor is not the same as the sage notebook.
How can i do something like that in Cantor without using an external editor or without using more commands?</p>
https://ask.sagemath.org/question/7611/using-cython-in-cantor-with-sage-backend/?answer=11515#post-id-11515Based on the current design of Cantor, I don't think that there is a way to do this. When the Sage notebook receives the input from a cell, it checks to see if there are any "%" commands a the top. Then, depending on which system is specified, it does something different. With Cantor, when you choose the Sage backend, it starts up a Sage process and sends all of the input directly to Sage. I though your example with `cython("""...""")` is the best you can do based on the current architecture of Cantor.
Sun, 22 Aug 2010 21:30:23 +0200https://ask.sagemath.org/question/7611/using-cython-in-cantor-with-sage-backend/?answer=11515#post-id-11515Answer by ngativ for <p>To use the %cython magic function in the sage notebook you can do something like:</p>
<pre><code>%cython
from math import sin
def f(x):
return sin(x**2)
def integral(double a, double b, int N):
cdef double dx = (b-a)/N
cdef int i
cdef double s = 0
for i in range(N):
s += f(a+dx*i)
return s * dx
</code></pre>
<p>But in cantor you can't since the sage mode in Cantor is not the same as the sage notebook.
How can i do something like that in Cantor without using an external editor or without using more commands?</p>
https://ask.sagemath.org/question/7611/using-cython-in-cantor-with-sage-backend/?answer=11514#post-id-11514Well, let me answer my own question
At least i can do this in Cantor, within a code block:
cython(r"""
from math import sin
def f(x):
return sin(x**2)
def integral(double a,double b,int N):
cdef dx = (b-a)/N
cdef double s = 0
cdef int i
for i in range(N):
s+=f(a+dx*i)
return s*dx
""")
It is not that clean as in the notebook but works nice.
Is there another solution ?Sun, 22 Aug 2010 19:09:48 +0200https://ask.sagemath.org/question/7611/using-cython-in-cantor-with-sage-backend/?answer=11514#post-id-11514