0

I want to get a 2d array generated from Python in C. The Python file generates a 2d array, and I want to pass it to C. However, it doesn't work. My Python code is:

import numpy as np
def generate(N):
    mean=[0,0]
    cov=[[10,0],[0,20]]
    position=np.random.multivariate_normal (mean,cov,size=N)
    return position

My C code is:

#include<Python.h>
#include<stdio.h>
#include<stdlib.h>
void cpp2python() {
    Py_Initialize();
    PyObject* pName, * pModule, * pDict, * pFunc, *pArg, * result = NULL;
    pName = PyUnicode_FromString("random_generate");
    pModule = PyImport_Import(pName);
    pDict = PyModule_GetDict(pModule);
    pFunc = PyDict_GetItemString(pDict, "generate");
    pArg = Py_BuildValue("(i)", 100000);
    result = PyEval_CallObject(pFunc, pArg);
    double out[2][100000];
    PyArg_Parse(result, "i", &out);
    Py_Finalize();
}
int main()
{
    cpp2python();
    system("pause");
    return 0;
}

Can anyone tell how to properly pass this 2d array to C?

0 Answers0