Ask Your Question
0

About Cython and the main Sage commands

asked 2013-07-02 08:16:52 +0100

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 flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
2

answered 2013-07-02 09:59:26 +0100

ppurka gravatar image

updated 2013-07-02 10:01:44 +0100

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

edit flag offensive delete link more

Comments

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

Sébastien Palcoux gravatar imageSébastien Palcoux ( 2013-07-02 10:51:40 +0100 )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.

kcrisman gravatar imagekcrisman ( 2013-07-02 11:14:58 +0100 )edit
1

answered 2013-07-02 10:28:12 +0100

Volker Braun gravatar image

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

edit flag offensive delete link more

Comments

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.

Sébastien Palcoux gravatar imageSébastien Palcoux ( 2013-07-02 10:55:47 +0100 )edit

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

kcrisman gravatar imagekcrisman ( 2013-07-02 11:14:12 +0100 )edit

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

Sébastien Palcoux gravatar imageSébastien Palcoux ( 2013-07-02 11:48:26 +0100 )edit

Volker seems to imply it doesn't.

kcrisman gravatar imagekcrisman ( 2013-07-02 12:13:49 +0100 )edit

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

Sébastien Palcoux gravatar imageSébastien Palcoux ( 2013-07-02 12:41:50 +0100 )edit

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: 2013-07-02 08:16:52 +0100

Seen: 715 times

Last updated: Jul 02 '13