On Thu, Feb 11, 2010 at 4:28 PM, Burcin Erocal <bur...@erocal.org> wrote: > Hi, > > I'm in Kaiserslautern, at a workshop on connecting mathematical > software for polyhedral geometry with software packages for Gröbner > basis (polymake, gfan, singular). > > I tried to code a small example demonstrating how to call Sage from > other C programs. I have a Cython module with the following: > > import sage.all > from sage.rings.arith import factor > > cdef public do_something_in_sage(int** res, int n): > print "do_something_in_sage called, input:",n > t = factor(n) > print t > > > Now I want to link/load this as a C library. It seems that initializing > Python with > > Py_Initialize(); > PySys_SetArgv(1, argv); > > then importing the module with > > PyObject* sage_module = PyImport_ImportModule("sage_link"); > > doesn't quite work. Python decides to include the directory > $SAGE_LOCAL/lib64/python2.6/lib-dynload/ instead of > $SAGE_LOCAL/lib/python2.6/lib-dynload/ (Note lib64 vs. lib). If I add > the latter to sys.path manually, things work as expected. > > Does anyone know where this lib64/lib problem comes from? > > Is it possible to avoid having to import the Cython module? Or to make > the necessary blurb to call cython functions less scary? > > > You can find the latest version of my attempts here: > > http://sage.math.washington.edu/home/burcin/sage_link/
I never tried Sage, but I routinely use Python (numpy, scipy, ...) from inside our C++ solvers, here is what I do in the main.cpp: Py_Initialize(); PySys_SetArgv(argc, argv); if (import_hermes2d___hermes2d()) throw std::runtime_error("hermes2d failed to import."); where the import_hermes2d___hermes2d was automatically generated by Cython from my hermes2d/_hermes2d.pyx file. Here is the whole file: http://github.com/certik/hermes2d/blob/6def0aab2ab3da425b1dd4f500783107e80ae72b/python/hermes2d/_hermes2d.pyx copying some relevant parts: import sys import traceback global_namespace = {"verbose": False} cdef api void cmd(char *text): n = run_cmd(text, global_namespace) global_namespace.update(n) cdef api void set_verbose_cmd(int verbose): global_namespace["verbose"] = verbose cdef api object run_cmd(char *text, object namespace): try: verbose = namespace.get("verbose") if verbose: print "got a text:", text if verbose: print "evaluting in the namespace:" print namespace code = compile(text, "", "exec") eval(code, {}, namespace) if verbose: print "new namespace:" print namespace return namespace except SystemExit, e: try: exit_code = int(e) except: exit_code = -1 exit(exit_code) except: etype, value, tb = sys.exc_info() s = "".join(traceback.format_exception(etype, value, tb)) s = "Exception raised in the Python code:\n" + s throw_exception(s) and here is how I use matplotlib to plot some finite element base functions from within C++: insert_object("v", array_double_c2numpy(v->val, n)); insert_object("x", array_double_c2numpy(e->x, n)); insert_object("y", array_double_c2numpy(e->y, n)); insert_object("counter", int_c2py(counter)); cmd("import util"); cmd("util.plotxy(x, y, v, counter)"); where util.py is my python script which plots it. It works like a charm. So keep me posted how you use Sage from C/C++, I am interested in your approach. Ondrej -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org