ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 07 Feb 2011 09:45:25 +0100Including a sage library in c codehttps://ask.sagemath.org/question/7923/including-a-sage-library-in-c-code/I have a .c file implementing some calculations on lattice basis. I use NTL, GMP and MPFR for my data types and functios. I'd like to use as input for my calculations some basis matrices generated by sage.crypto.lattice.gen_lattice.
1. Is it possible for me to include "something" so I can call this function in my code and get the matrices?
2. Is there any other way to export this matrices and import them to my c code? (I need to do these calculations for at least 100 different basis of 200,250,500 size so the process must be as "automated" as possible)Sat, 05 Feb 2011 13:34:11 +0100https://ask.sagemath.org/question/7923/including-a-sage-library-in-c-code/Answer by cswiercz for <p>I have a .c file implementing some calculations on lattice basis. I use NTL, GMP and MPFR for my data types and functios. I'd like to use as input for my calculations some basis matrices generated by sage.crypto.lattice.gen_lattice. </p>
<ol>
<li><p>Is it possible for me to include "something" so I can call this function in my code and get the matrices?</p></li>
<li><p>Is there any other way to export this matrices and import them to my c code? (I need to do these calculations for at least 100 different basis of 200,250,500 size so the process must be as "automated" as possible)</p></li>
</ol>
https://ask.sagemath.org/question/7923/including-a-sage-library-in-c-code/?answer=12070#post-id-12070Although 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](http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files). Mon, 07 Feb 2011 09:45:25 +0100https://ask.sagemath.org/question/7923/including-a-sage-library-in-c-code/?answer=12070#post-id-12070