I know that this thing has been answered a lot of times and I have also read the documentation as well but still I am not able to clearly understand how is this working. As
Make sure that you plug the memory leak that the above approach entrails. I am guessing in the above x
is a pointer of type void *
. Check this out.
The function is:
PyObject *
PyArray_SimpleNewFromData(
int nd,
npy_intp* dims,
int typenum,
void* data)
The last argument (data
) is a buffer to the data. Let's dispense with that.
The second argument (dims
) is a buffer, whose each entry is a dimension; so for a 1d array, it could be a length-1 buffer (or even an integer, since each integer is a length-1 buffer)
Since the second argument is a buffer, the first argument (nd
) tells its length
The third argument (typenum
) indicates the type.
For example, say you have 4 64-bit ints at x
:
To create an array, use
int dims[1];
dims[0] = 4;
PyArray_SimpleNewFromData(1, dims, NPY_INT64, x)
To create a 2X2 matrix, use
int dims[2];
dims[0] = dims[1] = 2;
PyArray_SimpleNewFromData(2, dims, NPY_INT64, x)