Passing 3-dimensional numpy array to C

前端 未结 4 1077
旧巷少年郎
旧巷少年郎 2021-02-01 09:43

I\'m writing a C extension to my Python program for speed purposes, and running into some very strange behaviour trying to pass in a 3-dimensional numpy array. It works with a 2

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-01 10:22

    Rather than converting to a c-style array, I usually access numpy array elements directly using PyArray_GETPTR (see http://docs.scipy.org/doc/numpy/reference/c-api.array.html#data-access).

    For instance, to access an element of a 3-dimensional numpy array of type double use double elem=*((double *)PyArray_GETPTR3(list3_obj,i,j,k)).

    For your application, you could detect the correct number of dimensions for each array using PyArray_NDIM, then access elements using the appropriate version of PyArray_GETPTR.

提交回复
热议问题