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.Sun, 21 Sep 2014 19:38:44 +0200What's the best way to access sage from a C program?https://ask.sagemath.org/question/24107/whats-the-best-way-to-access-sage-from-a-c-program/I'm new to Python/Sage and confused about what my options are here. I have a C++ program and occasionally I want to process some data with Sage. It would be far too slow to simply have Sage running like a server and then use the C++ program like a client that queries the server.
What I really want to do is write a standalone Python function that uses Sage library functions, convert that function and any relevant portions of Sage into C, and then with my own compiler, compile that C code into a library that my C++ program can call.
I'm confused about how all this could work with Cython and spyx files and so forth. Is this even possible? Would it be possible but slower to instead embed the Python function in the C++ program? Thanks for any help.
Fri, 12 Sep 2014 22:42:23 +0200https://ask.sagemath.org/question/24107/whats-the-best-way-to-access-sage-from-a-c-program/Answer by Andrew732 for <p>I'm new to Python/Sage and confused about what my options are here. I have a C++ program and occasionally I want to process some data with Sage. It would be far too slow to simply have Sage running like a server and then use the C++ program like a client that queries the server. </p>
<p>What I really want to do is write a standalone Python function that uses Sage library functions, convert that function and any relevant portions of Sage into C, and then with my own compiler, compile that C code into a library that my C++ program can call.</p>
<p>I'm confused about how all this could work with Cython and spyx files and so forth. Is this even possible? Would it be possible but slower to instead embed the Python function in the C++ program? Thanks for any help.</p>
https://ask.sagemath.org/question/24107/whats-the-best-way-to-access-sage-from-a-c-program/?answer=24113#post-id-24113 Anybody else ever need to do something like this?
Sun, 14 Sep 2014 12:30:26 +0200https://ask.sagemath.org/question/24107/whats-the-best-way-to-access-sage-from-a-c-program/?answer=24113#post-id-24113Answer by Volker Braun for <p>I'm new to Python/Sage and confused about what my options are here. I have a C++ program and occasionally I want to process some data with Sage. It would be far too slow to simply have Sage running like a server and then use the C++ program like a client that queries the server. </p>
<p>What I really want to do is write a standalone Python function that uses Sage library functions, convert that function and any relevant portions of Sage into C, and then with my own compiler, compile that C code into a library that my C++ program can call.</p>
<p>I'm confused about how all this could work with Cython and spyx files and so forth. Is this even possible? Would it be possible but slower to instead embed the Python function in the C++ program? Thanks for any help.</p>
https://ask.sagemath.org/question/24107/whats-the-best-way-to-access-sage-from-a-c-program/?answer=24234#post-id-24234I would turn it around, instead of controlling Sage from within your C++ program make Sage/IPython your user interface. Then call into your C++ code for whatever you want it to do quickly. Will be a much nicer interface than calling your binary with different arguments.
Typically C/C++ code does some fast numerics after you have done some symbolic preprocessing, so usually you don't need to call back into Python then. But you definitely can using the Python C API (tedious), or by linking against Cython module and calling cdef functions, or boost:python.
Simple example of a C++ lattice simulation code controlled from the Sage commandline: https://github.com/vbraun/lattice_phi4Sun, 21 Sep 2014 19:38:44 +0200https://ask.sagemath.org/question/24107/whats-the-best-way-to-access-sage-from-a-c-program/?answer=24234#post-id-24234