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

edit retag close merge delete

Sort by ยป oldest newest most voted

Well, 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 ?

more

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

more