Search code examples
pythonc++arraysnumpypython-extensions

Can't correctly convert Numpy array to c array for Python Extension (c++)


I'm developing a Python extension in c++. I'm am really rusty in c++ however and don't have the necessary experience to figure this out it seems. I'm trying to read in numpy arrays, do the calculations I want to do and then return a numpy array. The problem I'm having is converting the numpy array to something of a normal Double array in 'c format'. I tried two methods to convert the data but both result in the same, seems to be storing the memory locations when I print out the arrays, not the actual values

Here is the code with some comments.

static PyObject* mymodule_function(PyObject *self, PyObject *args){
    PyArrayObject *x_obj,*y_obj;
    double *x, *y;

    if (!PyArg_ParseTuple(args, "O!O!", &PyArray_Type, &x_obj,&PyArray_Type, &y_obj))  return NULL;
    if (NULL == x_obj)  return NULL;
    if (NULL == y_obj)  return NULL;

    npy_intp N = PyArray_DIM(x_obj, 0);
    std::cout << int(N) << std::endl;   //Correctly prints out size of array 

    //method 1 I tried to convert data
    x = (double*)x_obj->data;
    //method 2 that I tried
    y = (double*)PyArray_DATA(y_obj);

    // Debug printing.
    for (int i = 0; i < (int)N; i ++){
        std::cout << x[i] << std::endl; 
        std::cout << y[i] << std::endl; 
    }
    //prints out array correctly

    double z[N];
    myfunction(x,y,z,(int)N);  

    // Debug printing.
    for (int i = 0; i < (int)N; i ++){
        std::cout << z[i] << std::endl; 
    }
    //prints out array correctly    

    npy_intp dims[1];
    dims[0] = N;
    PyObject *pyArray = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, z);

    PyObject *ret = Py_BuildValue("O", pyArray);
    return ret;
}

and the Python code I use:

import numpy as np
import mymodule as mm

a = np.array([1,2,3],dtype=np.float64)
b = np.array([4,5,6],dtype=np.float64)
c = np.zeros(shape=(1,3),dtype=np.float64)
c = mm.function(a,b)
print(c)

Solution

  • Specifying the data type in your python deceleration for a,b,c as dtype=np.float64. Double in C parlance is 64 bit float. using np.array like the way you've used it usually returns np.int64. using np.array like so will return a np.float64

    a=np.array([1.,2.,3.])