# 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 ?
edit retag close merge delete

Sort by » oldest newest most voted

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.

more

Ok, thank you ! Does this line import all the libraries or just the main functions ?

( 2013-07-02 10:51:40 +0200 )edit
1

This 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.

( 2013-07-02 11:14:58 +0200 )edit

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.

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

more

Thank 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.

( 2013-07-02 10:55:47 +0200 )edit

It's not a magic function in the same sense. It says, "this is a cell that contains Cython code to be compiled".

( 2013-07-02 11:14:12 +0200 )edit

Ok thanks. Do you confirm that %cython does not import automatically sage.all ?

( 2013-07-02 11:48:26 +0200 )edit

Volker seems to imply it doesn't.

( 2013-07-02 12:13:49 +0200 )edit

I didn't understand its sentence. So he says it doesn't but he thinks it should. So let's do it.

( 2013-07-02 12:41:50 +0200 )edit