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.Tue, 24 Aug 2010 19:20:05 +0200How to enable %cython magic function in sage terminal mode?https://ask.sagemath.org/question/7610/how-to-enable-cython-magic-function-in-sage-terminal-mode/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?
Sun, 22 Aug 2010 07:54:36 +0200https://ask.sagemath.org/question/7610/how-to-enable-cython-magic-function-in-sage-terminal-mode/Answer by Mike Witt for <p>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?)</p>
<p>My goal is to enable %cython within the Sage command line mode, how can i do that?</p>
https://ask.sagemath.org/question/7610/how-to-enable-cython-magic-function-in-sage-terminal-mode/?answer=11511#post-id-11511Here 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
Sun, 22 Aug 2010 12:52:33 +0200https://ask.sagemath.org/question/7610/how-to-enable-cython-magic-function-in-sage-terminal-mode/?answer=11511#post-id-11511Comment by Mike Witt for <p>Here is what worked for me. First I create a file:</p>
<p>/home/mike/cython.spyx</p>
<p>with the following contents:</p>
<pre><code>cdef class c_class:
cdef int number
def __init__(self, n):
self.number = n
cpdef show(self):
print('The number was: %s' %self.number)
</code></pre>
<p>Then I start up sage:</p>
<pre><code>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
</code></pre>
<p>-- 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 :-)</p>
<p>-Mike</p>
https://ask.sagemath.org/question/7610/how-to-enable-cython-magic-function-in-sage-terminal-mode/?comment=22936#post-id-22936By the way, if it's possible to define the cython code directly inside a .sage file, I'd love to know how.Sun, 22 Aug 2010 13:39:55 +0200https://ask.sagemath.org/question/7610/how-to-enable-cython-magic-function-in-sage-terminal-mode/?comment=22936#post-id-22936Comment by ngativ for <p>Here is what worked for me. First I create a file:</p>
<p>/home/mike/cython.spyx</p>
<p>with the following contents:</p>
<pre><code>cdef class c_class:
cdef int number
def __init__(self, n):
self.number = n
cpdef show(self):
print('The number was: %s' %self.number)
</code></pre>
<p>Then I start up sage:</p>
<pre><code>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
</code></pre>
<p>-- 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 :-)</p>
<p>-Mike</p>
https://ask.sagemath.org/question/7610/how-to-enable-cython-magic-function-in-sage-terminal-mode/?comment=22934#post-id-22934Thanks, but , this is the only way to use cython without the notebook?Sun, 22 Aug 2010 14:42:42 +0200https://ask.sagemath.org/question/7610/how-to-enable-cython-magic-function-in-sage-terminal-mode/?comment=22934#post-id-22934Answer by Mike Hansen for <p>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?)</p>
<p>My goal is to enable %cython within the Sage command line mode, how can i do that?</p>
https://ask.sagemath.org/question/7610/how-to-enable-cython-magic-function-in-sage-terminal-mode/?answer=11513#post-id-11513There currently isn't any way to have a "%cython" mode at the command-line. One could imagine using IPython's [magic][1] 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.
[1]: http://ipython.scipy.org/doc/stable/html/api/generated/IPython.Magic.htmlSun, 22 Aug 2010 16:47:45 +0200https://ask.sagemath.org/question/7610/how-to-enable-cython-magic-function-in-sage-terminal-mode/?answer=11513#post-id-11513Answer by William Stein for <p>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?)</p>
<p>My goal is to enable %cython within the Sage command line mode, how can i do that?</p>
https://ask.sagemath.org/question/7610/how-to-enable-cython-magic-function-in-sage-terminal-mode/?answer=11539#post-id-11539Check 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
Tue, 24 Aug 2010 19:20:05 +0200https://ask.sagemath.org/question/7610/how-to-enable-cython-magic-function-in-sage-terminal-mode/?answer=11539#post-id-11539