Ask Your Question
1

How to enable %cython magic function in sage terminal mode?

asked 2010-08-22 07:54:36 +0100

ngativ gravatar image

updated 2011-06-16 14:50:20 +0100

Kelvin Li gravatar image

In the notebook mode of sage there are a full list of available magic functions, like %cython, that doesn't exist in the terminal (or iphyton interface?)

My goal is to enable %cython within the Sage command line mode, how can i do that?

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
2

answered 2010-08-22 12:52:33 +0100

Mike Witt gravatar image

updated 2010-08-22 13:52:31 +0100

Here is what worked for me. First I create a file:

/home/mike/cython.spyx

with the following contents:

cdef class c_class:
    cdef int number
    def __init__(self, n):
        self.number = n
    cpdef show(self):
        print('The number was: %s' %self.number)

Then I start up sage:

sage: version()
'Sage Version 4.5.1, Release Date: 2010-07-19'
sage: load '/home/mike/cython.spyx'
Compiling /home/mike/cython.spyx...
sage: instance = c_class(5)
sage: instance.show()
The number was: 5

-- I hope this is readable. This is the first question I've tried to answer and I'm having a little trouble with the formatting :-)

-Mike

edit flag offensive delete link more

Comments

By the way, if it's possible to define the cython code directly inside a .sage file, I'd love to know how.

Mike Witt gravatar imageMike Witt ( 2010-08-22 13:39:55 +0100 )edit

Thanks, but , this is the only way to use cython without the notebook?

ngativ gravatar imagengativ ( 2010-08-22 14:42:42 +0100 )edit
2

answered 2010-08-22 16:47:45 +0100

Mike Hansen gravatar image

There currently isn't any way to have a "%cython" mode at the command-line. One could imagine using IPython's magic commands, but they seem to just take a single line of input which isn't that useful when writing Cython code. Honestly, your best option is to do something like Mike Witt posted above and write the Cython code in a separate file and use the load or attach commands to load it into your command-line session.

edit flag offensive delete link more
2

answered 2010-08-24 19:20:05 +0100

William Stein gravatar image

Check out the cython command that is available by default in Sage. It does what you want and I think it happens to be the command that I used to implement the%cython mode in the notebook.

sage: cython?
....
       Given a block of Cython (Sage's variant of Pyrex) code (as a text
       string), this function compiles it using your C compiler, and
       includes it into the global scope of the module that called this
       function.
sage: cython('def f(double n): return n*n')
sage: f(292908234982340982)
8.5795234120470249e+34

Here is a longer example:

sage: cython("""
....: def f(int i):
....:     cdef int j
....:     for j in range(i):
....:         i += j
....:     return i
....: """)
sage: f(100)
5050
sage: timeit('f(100)')
625 loops, best of 3: 465 ns per loop
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

1 follower

Stats

Asked: 2010-08-22 07:54:36 +0100

Seen: 1,671 times

Last updated: Aug 24 '10