On 02.07.2014 13:44, Mads Ipsen wrote: > > > On 02/07/14 12:46, Julian Taylor wrote: >> On Wed, Jul 2, 2014 at 12:15 PM, Mads Ipsen <mads.ip...@gmail.com> wrote: >>> Hi, >>> >>> If you setup an M x N array like this >>> >>> a = 1.0*numpy.arange(24).reshape(8,3) >>> >>> you can access the data from a C function like this >>> >>> void foo(PyObject * numpy_data) >>> { >>> // Get dimension and data pointer >>> int const m = static_cast<int>(PyArray_DIMS(numpy_data)[0]); >>> int const n = static_cast<int>(PyArray_DIMS(numpy_data)[1]); >>> double * const data = (double *) PyArray_DATA(numpy_data); >>> >>> // Access data >>> ... >>> } >>> >>> Now, suppose I have an irregular shaped numpy array like this >>> >>> a1 = numpy.array([ 1.0, 2.0, 3.0]) >>> a2 = numpy.array([-2.0, 4.0]) >>> a3 = numpy.array([5.0]) >>> b = numpy.array([a1,a2,a3]) >>> >>> How can open up the doors to the array data of b on the C-side? >>> >> >> numpy does not directly support irregular shaped arrays (or ragged arrays). >> If you look at the result of your example you will see this: >> In [5]: b >> Out[5]: array([array([ 1., 2., 3.]), array([-2., 4.]), array([ >> 5.])], dtype=object) >> >> b has datatype object, this means it is a 1d array containing more >> array objects. Numpy does not directly know about the shapes or types >> the sub arrays. It is not necessarily homogeneous anymore, but >> compared to a regular python list you still have elementwise >> operations (if the contained python objects support them) and it can >> have multiple dimensions. >> >> In C you would access such an array it like this: >> >> PyArrayObject * const data = (PyArrayObject *) PyArray_DATA(numpy_data); >> for (i=0; i < PyArray_DIMS(numpy_data)[0]; i++) { >> assert(PyArray_Check(data[i])); >> double * const sub_data = (double *) PyArray_DATA(data[i]); >> } >> _______________________________________________ >> NumPy-Discussion mailing list >> NumPy-Discussion@scipy.org >> http://mail.scipy.org/mailman/listinfo/numpy-discussion >> > > Thanks - that'll get me going! >
another thing, don't use int as the index to the array, use npy_intp which is large enough to also index arrays > 4GB if the platform supports it. Also note that object arrays are not very well optimized in numpy, so numerous operations can be slow. _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion