Приведение типов данных PyArrayObject в массив C

Я хочу работать с моими массивами Numpy в расширении C. Во многих примерах в этом случае используется структура из PyArrayObject,

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

указателей для доступа к данным, если я хочу достичь своего массива в более знакомый (или более аккуратный) способ с индексами вроде

array [i] [j]

, как мне поступить так? Должен ли я привести тип (bool *) array-> data и работать с созданным мной массивом C? (мои элементы - bools)

Объявление моей функции на данный момент (конечно, не закончено)

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

Спасибо!

6
задан mehmet.ali.anil 12 October 2011 в 17:35
поделиться