在C扩展中,我正在访问传递给函数的两个数组:

PyObject *xw_array = PyArray_FROM_OTF(xw_obj, NPY_DOUBLE, NPY_IN_ARRAY);
PyObject *x1_array = PyArray_FROM_OTF(x1_obj, NPY_DOUBLE, NPY_IN_ARRAY);


然后我想对这两个数组使用PyArray_SearchSorted-我目前正在做:

PyObject *ix_array = PyArray_SearchSorted(xw_array, x1_array);


但这导致以下错误:

propagate_pure.c:123:138: error: too few arguments to function call, expected 4, have 2
PyObject *ix_array = (*(PyObject * (*)(PyArrayObject *, PyObject *, NPY_SEARCHSIDE, PyObject *)) PyArray_API[131])(xw_array, x1_array);
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                   ^


使用PyArray_SearchSorted的正确方法是什么?需要四个参数? documentation仅提及两个。

最佳答案

PyArray_SearchSorted的完整声明为here

NPY_NO_EXPORT PyObject *
PyArray_SearchSorted(PyArrayObject *op1, PyObject *op2,
                     NPY_SEARCHSIDE side, PyObject *perm)


您需要提供一个sideperm参数。默认值为NPY_SEARCHLEFTNULL。因此,以下应该工作:

PyObject *ix_array = PyArray_SearchSorted(xw_array, x1_array,
                                          NPY_SEARCHLEFT, NULL);

关于python - 如何在Numpy C API中使用PyArray_SearchSorted,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22211101/

10-09 13:37