Jul-11-2019, 01:54 PM
I want to call the nelderMead Python function from C, and pass in the keyword rosen that I've imported from from scipy.optimize import minimize, rosen, rosen_der from C into the nelderMead function. The problem is whenever is pass in a string or PyObject, python reads it as a type string but rosen is a type class. So the rosen parameter does not get invoked as a class but as a string.
C Code below:
//https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html
#include</usr/include/python3.6m/Python.h>
int main(){
PyObject *pName,*pFunc, *pValue, *pModule, *pOpt;
Py_Initialize();
//Access file in local folder where C file is stored
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append(\".\")");
//Convert String to PyObject
pName = PyUnicode_DecodeFSDefault("Rosenbrock_Function");
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if(pModule){
pFunc = PyObject_GetAttrString(pModule,"nelderMead");
Py_DECREF(pModule);
}
pOpt = PyUnicode_DecodeFSDefault("rosen");
pValue = PyObject_CallFunctionObjArgs(pFunc,pOpt,NULL);
Py_FinalizeEx();
return 0;
}
C Code below:
//https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html
#include</usr/include/python3.6m/Python.h>
int main(){
PyObject *pName,*pFunc, *pValue, *pModule, *pOpt;
Py_Initialize();
//Access file in local folder where C file is stored
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append(\".\")");
//Convert String to PyObject
pName = PyUnicode_DecodeFSDefault("Rosenbrock_Function");
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if(pModule){
pFunc = PyObject_GetAttrString(pModule,"nelderMead");
Py_DECREF(pModule);
}
pOpt = PyUnicode_DecodeFSDefault("rosen");
pValue = PyObject_CallFunctionObjArgs(pFunc,pOpt,NULL);
Py_FinalizeEx();
return 0;
}