Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

call SageMath from C

I wrote a little Graph Theory / Group Theory function in SageMath (using stuff from GAP, mostly), and now I want to call that from a simulation that I have in C. I have worked out how to call Python from C (here, basically: https://docs.python.org/3/extending/embedding.html) but I can't quite get the sage to work. I've tried things like starting the .py file with

#!/usr/local/bin/sage -python

import sage.all

and that got some things running, but it seems like many jobs were being created all at once, and then it just seg faulted. Anyone have experience with this?

[ I will note there is another question just like this on this site: https://ask.sagemath.org/question/54161/to-call-sagemath-program-jupyter-notebook-from-c-program/ ....]

call SageMath from C

I wrote a little Graph Theory / Group Theory function in SageMath (using stuff from GAP, mostly), and now I want to call that from a simulation that I have in C. I have worked out how to call Python from C (here, basically: https://docs.python.org/3/extending/embedding.html) but I can't quite get the sage to work. I've tried things like starting the .py file with

#!/usr/local/bin/sage -python

import sage.all

and that got some things running, but it seems like many jobs were being created all at once, and then it just seg faulted. Anyone have experience with this?

[ I will note there is another question just like this on this site: https://ask.sagemath.org/question/54161/to-call-sagemath-program-jupyter-notebook-from-c-program/ ....]....although I already have a Sage script, so I don't want to actually run a notebook, just the script, and then grab some values back into C]

call SageMath from C

I wrote a little Graph Theory / Group Theory function in SageMath (using stuff from GAP, mostly), and now I want to call that from a simulation that I have in C. I have worked out how to call Python from C (here, basically: https://docs.python.org/3/extending/embedding.html) but I can't quite get the sage to work. I've tried things like starting the .py file with

#!/usr/local/bin/sage -python

import sage.all

and that got some things running, but it seems like many jobs were being created all at once, and then it just seg faulted. Anyone have experience with this?

[ I will note there is another question just like this on this site: https://ask.sagemath.org/question/54161/to-call-sagemath-program-jupyter-notebook-from-c-program/ ....although I already have a Sage script, so I don't want to actually run a notebook, just the script, and then grab some values back into C]

MWE follows....it doesn't work, but this is what's giving me the behavior. Previously, when this was a .py file instead of .sage (and that "import.sage" was missing), it ran fine.

 #include <Python.h>

int main(int argc,char *argv[])
{
float res;
  res=(float)call(2,3);
  printf("Result of Python script: %f\n",res);
}


int call(int int1,int int2){

  long res;
  Py_Initialize();

  /* Two added lines to get the path right */
  PyRun_SimpleString("import sys");
  /*PyRun_SimpleString("import sage.all");*/
  PyRun_SimpleString("sys.path.append(\".\")");

  PyObject* moduleName = PyUnicode_FromString("test_multiply_test");
  PyObject* module = PyImport_Import(moduleName);
  Py_DECREF(moduleName);

  PyObject* functionName = PyObject_GetAttrString(module, "multiply");

  PyObject* args =PyTuple_New(2);
  PyObject* arg1 = PyLong_FromLong(int1);
  PyObject* arg2 = PyLong_FromLong(int2);
  PyTuple_SetItem(args, 0, arg1);
  PyTuple_SetItem(args, 1, arg2);

  PyObject* result = PyObject_CallObject(functionName, args);

  /* Check if the call was successful */
  if (result != NULL) {
    /* Convert the result to a C string */
    res = PyLong_AsLong(result);
    printf("%ld\n", res);

    /* Clean up the result object */
    Py_DECREF(result);
  } else {
    /* Handle the error */
    PyErr_Print();
  }

  Py_DECREF(arg1);
  Py_DECREF(arg2);
  Py_DECREF(args);
  Py_DECREF(result);

  Py_DECREF(functionName);
  Py_DECREF(module);

  Py_Finalize();

  return res;
}

Oh, and the python file is

def multiply(a,b):
    print("Will compute", a, "times", b)
    c = 0
    for i in range(0, a):
        c = c + b
    return c