ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 02 Jul 2013 05:41:50 -0500About Cython and the main Sage commandshttp://ask.sagemath.org/question/10308/about-cython-and-the-main-sage-commands/I read the "How to import some commands?" and "Importing sage functions into cython?" topics, so I know how importing new commands in Sage and also under %cython.
I found that main commands like "factor" or "identity_matrix" which are automatically available in Sage are not under %cython. Then, it's necessary to add the lines:
"from sage.rings.arith import factor"
"from sage.matrix.constructor import identity_matrix"
**Questions**:
- Why the main commands are not automatically available under %cython ?
- Is there a line importing directly all the main commands under %cython ?
- Or, is it a good idea to improve the Sage software so that it works directly ?
I read also that such imported commands are not "cythonised" (and so not 100 times faster).
- Are there cythonised equivalents of all the main commands ?
- If not, is there a project to cythonised all the main commands ?
- Why Sage does not use directly Cython ?
Tue, 02 Jul 2013 01:16:52 -0500http://ask.sagemath.org/question/10308/about-cython-and-the-main-sage-commands/Answer by ppurka for <p>I read the "How to import some commands?" and "Importing sage functions into cython?" topics, so I know how importing new commands in Sage and also under %cython. </p>
<p>I found that main commands like "factor" or "identity_matrix" which are automatically available in Sage are not under %cython. Then, it's necessary to add the lines: </p>
<p>"from sage.rings.arith import factor" <br/>
"from sage.matrix.constructor import identity_matrix" </p>
<p><strong>Questions</strong>: </p>
<ul>
<li>Why the main commands are not automatically available under %cython ? </li>
<li>Is there a line importing directly all the main commands under %cython ? </li>
<li>Or, is it a good idea to improve the Sage software so that it works directly ? </li>
</ul>
<p>I read also that such imported commands are not "cythonised" (and so not 100 times faster). </p>
<ul>
<li>Are there cythonised equivalents of all the main commands ? </li>
<li>If not, is there a project to cythonised all the main commands ? </li>
<li>Why Sage does not use directly Cython ? </li>
</ul>
http://ask.sagemath.org/question/10308/about-cython-and-the-main-sage-commands/?answer=15169#post-id-15169Just add
from sage.all import *
at the beginning of the file, or immediately after `%cython`.
Quite a lot of the code in sage *is* implemented in cython. Just have a look at the [source code](https://github.com/sagemath/sage/tree/master/src/sage).Tue, 02 Jul 2013 02:59:26 -0500http://ask.sagemath.org/question/10308/about-cython-and-the-main-sage-commands/?answer=15169#post-id-15169Comment by Sébastien Palcoux for <p>Just add</p>
<pre><code>from sage.all import *
</code></pre>
<p>at the beginning of the file, or immediately after <code>%cython</code>.</p>
<p>Quite a lot of the code in sage <em>is</em> implemented in cython. Just have a look at the <a href="https://github.com/sagemath/sage/tree/master/src/sage">source code</a>.</p>
http://ask.sagemath.org/question/10308/about-cython-and-the-main-sage-commands/?comment=17386#post-id-17386Ok, thank you !
Does this line import all the libraries or just the main functions ?Tue, 02 Jul 2013 03:51:40 -0500http://ask.sagemath.org/question/10308/about-cython-and-the-main-sage-commands/?comment=17386#post-id-17386Comment by kcrisman for <p>Just add</p>
<pre><code>from sage.all import *
</code></pre>
<p>at the beginning of the file, or immediately after <code>%cython</code>.</p>
<p>Quite a lot of the code in sage <em>is</em> implemented in cython. Just have a look at the <a href="https://github.com/sagemath/sage/tree/master/src/sage">source code</a>.</p>
http://ask.sagemath.org/question/10308/about-cython-and-the-main-sage-commands/?comment=17383#post-id-17383This imports the same functions normally imported into Sage. That is a lot, of course. But this essentially creates a separate ".pyx" file, which (like any non-Sage file) doesn't have things automatically imported.Tue, 02 Jul 2013 04:14:58 -0500http://ask.sagemath.org/question/10308/about-cython-and-the-main-sage-commands/?comment=17383#post-id-17383Answer by Volker Braun for <p>I read the "How to import some commands?" and "Importing sage functions into cython?" topics, so I know how importing new commands in Sage and also under %cython. </p>
<p>I found that main commands like "factor" or "identity_matrix" which are automatically available in Sage are not under %cython. Then, it's necessary to add the lines: </p>
<p>"from sage.rings.arith import factor" <br/>
"from sage.matrix.constructor import identity_matrix" </p>
<p><strong>Questions</strong>: </p>
<ul>
<li>Why the main commands are not automatically available under %cython ? </li>
<li>Is there a line importing directly all the main commands under %cython ? </li>
<li>Or, is it a good idea to improve the Sage software so that it works directly ? </li>
</ul>
<p>I read also that such imported commands are not "cythonised" (and so not 100 times faster). </p>
<ul>
<li>Are there cythonised equivalents of all the main commands ? </li>
<li>If not, is there a project to cythonised all the main commands ? </li>
<li>Why Sage does not use directly Cython ? </li>
</ul>
http://ask.sagemath.org/question/10308/about-cython-and-the-main-sage-commands/?answer=15170#post-id-15170If your code spends most of its time in external libraries (that are already
C/Cython) then it won't benefit from being rewritten. As a general strategy I'd recommend to first write a Python prototype, then identify speed-critical parts, and then selectively rewrite these bits in C/Cython.
Cython can and will call back into Python code, in particular you can import any Python module.
The `%cython` magic probably should automatically import `sage.all`Tue, 02 Jul 2013 03:28:12 -0500http://ask.sagemath.org/question/10308/about-cython-and-the-main-sage-commands/?answer=15170#post-id-15170Comment by Sébastien Palcoux for <p>If your code spends most of its time in external libraries (that are already
C/Cython) then it won't benefit from being rewritten. As a general strategy I'd recommend to first write a Python prototype, then identify speed-critical parts, and then selectively rewrite these bits in C/Cython.</p>
<p>Cython can and will call back into Python code, in particular you can import any Python module.</p>
<p>The <code>%cython</code> magic probably should automatically import <code>sage.all</code></p>
http://ask.sagemath.org/question/10308/about-cython-and-the-main-sage-commands/?comment=17385#post-id-17385Thank you for your answer. I don't understand a point: it's %cython which is a magic function, there is not another function called "%cython magic" ? Becasue about %cython, I do the test and the functions "factor" and "identity_matrix" are not automatically imported.Tue, 02 Jul 2013 03:55:47 -0500http://ask.sagemath.org/question/10308/about-cython-and-the-main-sage-commands/?comment=17385#post-id-17385Comment by Sébastien Palcoux for <p>If your code spends most of its time in external libraries (that are already
C/Cython) then it won't benefit from being rewritten. As a general strategy I'd recommend to first write a Python prototype, then identify speed-critical parts, and then selectively rewrite these bits in C/Cython.</p>
<p>Cython can and will call back into Python code, in particular you can import any Python module.</p>
<p>The <code>%cython</code> magic probably should automatically import <code>sage.all</code></p>
http://ask.sagemath.org/question/10308/about-cython-and-the-main-sage-commands/?comment=17381#post-id-17381Ok thanks. Do you confirm that %cython does not import automatically sage.all ?Tue, 02 Jul 2013 04:48:26 -0500http://ask.sagemath.org/question/10308/about-cython-and-the-main-sage-commands/?comment=17381#post-id-17381Comment by Sébastien Palcoux for <p>If your code spends most of its time in external libraries (that are already
C/Cython) then it won't benefit from being rewritten. As a general strategy I'd recommend to first write a Python prototype, then identify speed-critical parts, and then selectively rewrite these bits in C/Cython.</p>
<p>Cython can and will call back into Python code, in particular you can import any Python module.</p>
<p>The <code>%cython</code> magic probably should automatically import <code>sage.all</code></p>
http://ask.sagemath.org/question/10308/about-cython-and-the-main-sage-commands/?comment=17378#post-id-17378I didn't understand its sentence. So he says it doesn't but he thinks it should. So let's do it.Tue, 02 Jul 2013 05:41:50 -0500http://ask.sagemath.org/question/10308/about-cython-and-the-main-sage-commands/?comment=17378#post-id-17378Comment by kcrisman for <p>If your code spends most of its time in external libraries (that are already
C/Cython) then it won't benefit from being rewritten. As a general strategy I'd recommend to first write a Python prototype, then identify speed-critical parts, and then selectively rewrite these bits in C/Cython.</p>
<p>Cython can and will call back into Python code, in particular you can import any Python module.</p>
<p>The <code>%cython</code> magic probably should automatically import <code>sage.all</code></p>
http://ask.sagemath.org/question/10308/about-cython-and-the-main-sage-commands/?comment=17384#post-id-17384It's not a magic function in the same sense. It says, "this is a cell that contains Cython code to be compiled".Tue, 02 Jul 2013 04:14:12 -0500http://ask.sagemath.org/question/10308/about-cython-and-the-main-sage-commands/?comment=17384#post-id-17384Comment by kcrisman for <p>If your code spends most of its time in external libraries (that are already
C/Cython) then it won't benefit from being rewritten. As a general strategy I'd recommend to first write a Python prototype, then identify speed-critical parts, and then selectively rewrite these bits in C/Cython.</p>
<p>Cython can and will call back into Python code, in particular you can import any Python module.</p>
<p>The <code>%cython</code> magic probably should automatically import <code>sage.all</code></p>
http://ask.sagemath.org/question/10308/about-cython-and-the-main-sage-commands/?comment=17379#post-id-17379Volker seems to imply it doesn't.Tue, 02 Jul 2013 05:13:49 -0500http://ask.sagemath.org/question/10308/about-cython-and-the-main-sage-commands/?comment=17379#post-id-17379