简体   繁体   中英

Iterate over array of arrays with scipy.weave.inline

I have a numpy.ndarray of dtype object containing exclusively other arrays of different length. I have C code, that does some computations with the nested arrays, but I'm not sure how to grab the inner array and it's size when iterating over it using the numpy C-API. So currently it looks something like this:

from scipy.weave import inline
import numpy as np

arrs = np.zeros(10, dtype=object)
for i in xrange(10):
    arrs[i] = np.arange(i*i)

for arr in arrs:
    inline(ccode, ['arr', 'other', 'args'])

I know, that it's not an optimal structure, but neither would be sparse matrices I guess. arrs is quite lengthy, about 100k, so including this python loop into C would be a great speedup, as it would eliminate the overhead of calling inline all the time. But how do I get arr in an iterated way from within C?

So I finally figured out how to do it. scipy.weave seems not to like object arrays, so first I converted it to a list. Then the list items can be grabbed using the python C-API. The object conversion is directly taken from some other precompiled inlined C-Code.

arrs = list()
for i in xrange(5):
    arrs.append(np.arange(i * i, dtype=int))
code = r"""
    long arrs_size = PyList_Size(arrs);
    for (long i=0; i<arrs_size; i++) {
        PyArrayObject* arr_array = convert_to_numpy(PyList_GetItem(arrs,i), "arr");
        conversion_numpy_check_type(arr_array,PyArray_LONG, "arr");
        npy_intp* Narr = arr_array->dimensions;
        npy_intp* Sarr = arr_array->strides;
        int Darr = arr_array->nd;
        long* arr = (long*) arr_array->data;
        long arr_size = 1;
        for (int n=0; n<Darr; n++) arr_size *= Narr[n];
        for (int j=0; j<arr_size; j++) printf("%ld ", arr[j]);
        printf("\n");
    }
"""
inline(code, ['arrs'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM