Ask Your Question

Revision history [back]

Although this probably won't work (see last paragraph of this response) I think your best chance is to use Cython. Cython (included with Sage) converts Python-like code into C which can then be compiled into a shared-object library and used by Python / Sage. It's intended use is to easily write code that can be used by Python but is optimized to use fast C data types instead of slower Python data structures.

Create a .pyx file containing your code and execute

$ sage -cython -a mycythonfile.pyx

This will output a corresponding .c file which can be compiled into a shared object library and imported in Python along with an .html file that neatly shows the C equivalent of each line of code.

Using Cython, you could make a copy of $SAGE_ROOT/devel/sage/crypto/lattice.py, called lattice.pyx, and then run the above command on it. However, I doubt that this will work since lattice.py probably depends on other Sage modules and objects. One would need to convert those to C as well and link them together and that sounds like quite a task just to send some matrices to a C program.

Now, if your goal is to just send these matrices generated by sage.crypto.lattice to your C program, I'd recommend using Python's file-handling routines to save the string representations of these matrices (using __repr__()) to a text file (or binary file if you have a lot of them) and then read them in using your C program. You can find out more about writing strings to files in the Python "Input and Output" Documentation.