Ask Your Question
1

using %cython in Cantor with sage backend

asked 2010-08-22 19:06:38 +0100

ngativ gravatar image

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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2010-08-22 21:30:23 +0100

Mike Hansen gravatar image

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.

edit flag offensive delete link more
0

answered 2010-08-22 19:09:48 +0100

ngativ gravatar image

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 ?

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2010-08-22 19:06:38 +0100

Seen: 957 times

Last updated: Aug 22 '10