将PyArrayObject数据类型转换为C数组 [英] typecasting PyArrayObject data to a C array

查看:641
本文介绍了将PyArrayObject数据类型转换为C数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C扩展中使用我的Numpy数组. 在这种情况下,许多示例都使用PyArrayObject的结构,

I want to work with my Numpy arrays in a C extension. Many examples in this case uses the structure of PyArrayObject,

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

如果要以更熟悉(或更整洁)的方式到达数组,并使用

pointers in order to reach the data, if I wanted to reach my array in a more familiar (or tidier) way to me, with indices like

array[i][j]

我应该如何进行?我应该转换(bool *)array-> data并使用我创建的C数组吗? (我的元素是布尔)

how should I proceed so? Should I typecast (bool *) array->data and work with the C array I created? (my elements are bools)

我的函数声明到目前为止(当然还没有完成)

My function declaration for now is (not finished, of course)

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();
}

谢谢!

推荐答案

我认为您需要查看以下内容:

I think you'll want to look at this: http://docs.scipy.org/doc/numpy/reference/c-api.array.html

尤其是

void* PyArray_GETPTR3(PyObject* obj, <npy_intp> i, <npy_intp> j, <npy_intp> k)

和朋友.如果API不提供这些功能,David Heffernan将会感到惊讶.

and friends. Those are the functions that David Heffernan would be surprised if the API didn't provide.

这篇关于将PyArrayObject数据类型转换为C数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆