python - 将 PyArrayObject 数据类型转换为 C 数组

标签 python numpy python-c-api

我想在 C 扩展中使用我的 Numpy 数组。 本例中的很多例子都使用了PyArrayObject的结构,

array->data,array->strides[0],array->strides[1],...

为了访问数据的指针,如果我想以一种对我来说更熟悉(或更整洁)的方式访问我的数组,使用像

这样的索引

数组[i][j]

我应该怎么做呢?我应该强制转换 (bool *) array->data 并使用我创建的 C 数组吗? (我的元素是 bool 值)

我现在的函数声明是(当然还没有完成)

static PyObject *
xor_masking(PyObject *self, PyObject *args)
{

PyObject *input;
PyObject *mask;
PyObject *adjacency;
PyObject *state;
PyArrayObject *arr_mask;
PyArrayObject *arr_adjacency;
PyArrayObject *arr_state;
PyArrayObject *arr_next_state;

double sum;
int counter_node, n_nodes;

/*  PyArg_ParseTuple
 *  checks if from args, the pointers of type "O" can be extracted, and extracts them
 */

if (!PyArg_ParseTuple(args, "OOO:xor_masking_C", &mask, &adjacency, &state))
    return NULL;

/*
 *  The pointer returned by PyArray_ContiguousFromObject is typecasted to
 *  a PyArrayObject Pointer and array is pointed to the same address.
 */

arr_mask = (PyArrayObject *)
PyArray_ContiguousFromObject(mask, PyArray_BOOL, 2, 2);
arr_adjacency = (PyArrayObject *)
PyArray_ContiguousFromObject(adjacency, PyArray_BOOL, 2, 2);
arr_state = (PyArrayObject *)
PyArray_ContiguousFromObject(state, PyArray_BOOL, 2, 2);

if (array == NULL)
    return NULL;

int n_mask_0 = mask->dimensions[0];
int n_mask_1 = mask->dimensions[1];
int n_adjacency_0 = adjacency->dimensions[0];
int n_adjacency_1 = adjacency->dimensions[1];
int n_state_0 = state->dimensions[0];
int n_nodes = n_state_0;
/*
 * if the dimensions don't match, return NULL
 */

bool c_mask[n_nodes][n_nodes];

if (n_mask_0 != n_mask_1 || n_adjacency_0 != n_adjacency_1 ||
n_adjacency_0 != n_mask_0 || n_adjacency_0 != n_adjacency_1) {
    return NULL;
}

/*
 *    The 2D arrays are introduced as follows
 *    array[i][j] = (array->data + i*array->strides[0] + j*array->strides[1])
 */

for (counter_node = 0; i < n_mask; i++){
    *row_start = (array->data + i*array->strides[0]);
}


//Py_DECREF();

//return PyFloat_FromDouble();
}

谢谢!

最佳答案

我不确定这是否能回答您的问题,但是,要在 C 中访问您的 NumPy 数据,您可以尝试创建一个迭代器来循环访问 C 中的数组。它不会为您提供索引( [i][j]) 但它覆盖了整个数组

static PyObject *func1(PyObject *self, PyObject *args) {
    PyArrayObject *X;
    int ndX;
    npy_intp *shapeX;
    NpyIter *iter;
    NpyIter_IterNextFunc *iternext;
    PyArray_Descr *dtype;
    double **dataptr;

    PyArg_ParseTuple(args, "O!", &PyArray_Type, &X);
    ndX = PyArray_NDIM(X);
    shapeX = PyArray_SHAPE(X);
    dtype = PyArray_DescrFromType(NPY_DOUBLE);
    iter = NpyIter_New(X, NPY_ITER_READONLY, NPY_KEEPORDER, NPY_NO_CASTING, dtype);
    iternext = NpyIter_GetIterNext(iter, NULL);
    dataptr = (double **) NpyIter_GetDataPtrArray(iter);
    do {
        cout << **dataptr << endl; //Do something with the data in your array
    } while (iternext(iter));   
    NpyIter_Deallocate(iter);
    return Py_BuildValue(...);
}

关于python - 将 PyArrayObject 数据类型转换为 C 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7744149/

相关文章:

Python单元测试断言引发错误

python "Too many indices for array"

python - 在 Python 中实现 MATLAB 的 col2im 'sliding'

python - 分段仿射变换+扭曲输出看起来很奇怪

c - 当从纯 C 切换到使用 Numpy 对象的 C 时,看似无意义的运行时间会增加

python - 如何取消选择单选按钮 tkinter

python - 使用 Python 进行 zappa 调度

python - 为什么python找不到我安装了django-lockdown的这个模块?

python - 使用 python c api 的嵌套模块(包)?

python - fatal error : Python. h:没有这样的文件或目录