PyArray_SimpleNewFromData example

后端 未结 2 1533
臣服心动
臣服心动 2020-12-03 03:43

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

相关标签:
2条回答
  • 2020-12-03 04:31

    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.

    0 讨论(0)
  • 2020-12-03 04:35

    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)
    
    0 讨论(0)
提交回复
热议问题