Aug-17-2017, 09:58 AM
I would like to send an array of double to C to perform an optimization problem in python. The code is as follows:
**mathHelper.py**
**runMe.c**
>> There is a problem.
>> TypeError: addVector() takes exactly 1 argument (0 given)
Can anyone help me out here? Thanks,
**mathHelper.py**
import sys from numpy import * def addVector(a): c = 2*a return cThe C code is as follows:
**runMe.c**
#include </usr/include/python2.7/Python.h> #include <stdlib.h> #include <string.h> #include <numpy/arrayobject.h> int main(int argc, char *argv[]) { PyObject *pName, *pModule, *pDict, *pFunc; PyObject *pArgs, *pValue, *pVec; Py_Initialize(); // this macro is defined by NumPy and must be included import_array(); int nLenslet; double* h_slopes; h_slopes = (double *)malloc( nLenslet * sizeof(double)); for (int i = 0; i < nLenslet; i++){ h_slopes[i] = i; } npy_double data_slopes[nLenslet]; pVec = PyArray_SimpleNewFromData( nLenslet, data_slopes, NPY_DOUBLE, h_slopes ); if (pVec == NULL) printf("There is a problem.\n"); // load the python file PyObject *pval; PySys_SetPath("/home/roger/Desktop/PythonInC"); // path to the module to import char *fileID = "mathHelper"; pName = PyString_FromString(fileID); pModule = PyImport_Import(pName); char *functionID = "addVector"; if (pModule != NULL) { pFunc = PyObject_GetAttrString(pModule, functionID); if (pFunc && PyCallable_Check(pFunc)) { pValue = PyObject_CallFunction(pFunc, pVec); if (pValue != NULL) { printf("Value returned from the function %s", PyString_AsString(pValue)); } else { PyErr_Print(); } } else { if (PyErr_Occurred()) PyErr_Print(); fprintf(stderr, "Cannot find function \"%s\"\n", functionID); } } else { PyErr_Print(); fprintf(stderr, "Failed to load file\n"); return 1; } free(h_slopes); }It is compiled using
gcc -o final runMe.c -lpython2.7 -std=c99However, I get the following error message:
>> There is a problem.
>> TypeError: addVector() takes exactly 1 argument (0 given)
Can anyone help me out here? Thanks,