12.07.2015 Views

Sage Developer's Guide - Mirrors

Sage Developer's Guide - Mirrors

Sage Developer's Guide - Mirrors

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Sage</strong> Developer’s <strong>Guide</strong>, Release 6.1.11. In the <strong>Sage</strong> Notebook, begin any cell with %cython. When you evaluate that cell,(a) It is saved to a file.(b) Cython is run on it with all the standard <strong>Sage</strong> libraries automatically linked if necessary.(c) The resulting shared library file (.so / .dll / .dylib) is then loaded into your running instance of <strong>Sage</strong>.(d) The functionality defined in that cell is now available for you to use in the notebook. Also, the output cellhas a link to the C program that was compiled to create the .so file.(e) A cpdef or def function, say testfunction, defined in a %cython cell in a worksheet can beimported and made available in a different %cython cell within the same worksheet by importing it asshown below:%cythonfrom __main__ import testfunction2. Create an .spyx file and attach or load it from the command line. This is similar to creating a %cython cellin the notebook but works completely from the command line (and not from the notebook).3. Create a .pyx file and add it to the <strong>Sage</strong> library.(a) First, add a listing for the Cython extension to the variable ext_modules in the fileSAGE_ROOT/src/module_list.py. See the distutils.extension.Extension class formore information on creating a new Cython extension.(b) Run sage -b to rebuild <strong>Sage</strong>.For example, the file SAGE_ROOT/src/sage/graphs/chrompoly.pyx has the lines:Extension(’sage.graphs.chrompoly’,sources = [’sage/graphs/chrompoly.pyx’]),in module_list.py. In addition, sage.graphs is included in the packages list under the Distutilssection of setup.py since chrompoly.pyx is contained in the directory sage/graphs.4.3.2 Special PragmasIf Cython code is either attached or loaded as a .spyx file or loaded from the notebook as a %cython block, thefollowing pragmas are available:• clang — may be either c or c++ indicating whether a C or C++ compiler should be used.• clib — additional libraries to be linked in, the space separated list is split and passed to distutils.• cinclude — additional directories to search for header files. The space separated list is split and passed todistutils.• cfile – additional C or C++ files to be compiled• cargs – additional parameters passed to the compilerFor example:#clang C++#clib givaro#cinclude /usr/local/include/#cargs -ggdb#cfile foo.c4.3. Coding in Cython 41

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!