5.2 Examples

There are several ways to create and build Cython code in Sage.

  1. In the Sage Notebook begin any cell with %cython. When you evaluate that cell,
    1. It is saved to a file.
    2. Cython is run on it with all the standard Sage libraries automatically linked if necessary.
    3. The resulting .so file is then loaded into your running instance of Sage.
    4. The functionality defined in that cell is now available for you to use in the notebook. Also, the output cell has a link to the C program that was compiled to create the .so file.
    5. Below we will denote notebook sessions as follows:
      {{{
        INPUT TEXT
      ///
        OUTPUT TEXT
      }}}
      ...
      
      The triple braces delimit cells. You should not actually type in the curly braces or triple forward slashes.

  2. Create an .spyx file and attach or load it from the command line. This is similar to creating a %cython cell in the notebook but works completely from the command line.

  3. Create a .pyx file in the Sage library, add it to SAGE_ROOT/devel/sage/setup.py (note - you have to follow the template of how something else in there is done - we simply haven't had the time to properly document this process yet!), and type sage -br to build it. This is how much of the core arithmetic in the Sage library is implemented.



Subsections
See About this document... for information on suggesting changes.