Jul-29-2019, 03:25 PM
I am running Python3.6
Running similar example from
https://docs.python.org/3.6/extending/em...-embedding
I want to pass in a matrix into a Python program from a Python/C API program. The Python program will then convert the matrix into a Numpy array.
For example
Python Program:
Is this possible? Will I have to make this myself in C then convert it into a PyList?
Please advise
Running similar example from
https://docs.python.org/3.6/extending/em...-embedding
I want to pass in a matrix into a Python program from a Python/C API program. The Python program will then convert the matrix into a Numpy array.
For example
Python Program:
import numpy as np def mat_vector_mul(aP): #aP np.array([[ 5, 1 ,3], # [ 1, 1 ,1], # [ 1, 2 ,1]]) a = np.array([[aP]) b = np.array([1, 2, 3]) print(a.dot(b))Python/C API program calling Python Program mat_vector_mul function.
//https://docs.scipy.org/doc/numpy/reference/generated/numpy.matmul.html #include</usr/include/python3.6m/Python.h> int main(){ PyObject *pFunc, *pValue, *pModule, *pNumpyArray; Py_Initialize(); PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append(\".\")"); pModule = PyImport_ImportModule("mat_vector"); if(pModule){ pFunc = PyObject_GetAttrString(pModule,"mat_vector_mul"); pNumpyArray = PyObject_GetAttrString(pModule,"[5, 1, 3], [1, 1, 1], [1, 2, 1]"); Py_DECREF(pModule); if(pFunc && PyCallable_Check(pFunc)){ printf("[+] Function is Is callable\n"); } }else{ printf("[-] Module not loaded\n"); } printf("Here\n"); pValue = PyObject_CallFunctionObjArgs(pFunc,pNumpyArray,NULL); if( pValue != NULL){ printf("[+] Success\n"); Py_DECREF(pFunc); Py_DECREF(pValue); Py_DECREF(pNumpyArray); }else{ Py_DECREF(pFunc); Py_DECREF(pValue); printf("[-] Error\n"); } Py_Finalize(); return 0; }The Python/C API program will call the Python program and pass in a matrix. The Python program will then convert argument into Numpy array.
Is this possible? Will I have to make this myself in C then convert it into a PyList?
Please advise