First time here? Check out the FAQ!

Ask Your Question
1

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

asked 14 years ago

ngativ gravatar image

updated 13 years ago

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?

Preview: (hide)

3 Answers

Sort by » oldest newest most voted
2

answered 14 years ago

Mike Witt gravatar image

updated 14 years ago

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

Preview: (hide)
link

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 ( 14 years ago )

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

ngativ gravatar imagengativ ( 14 years ago )
2

answered 14 years ago

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.

Preview: (hide)
link
2

answered 14 years ago

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
Preview: (hide)
link

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: 14 years ago

Seen: 1,710 times

Last updated: Aug 24 '10