[Numpy-discussion] Accessing irregular sized array data from C

Chris Barker chris.barker at noaa.gov
Wed Jul 2 13:24:53 EDT 2014


On Wed, Jul 2, 2014 at 3:46 AM, Julian Taylor <jtaylor.debian at googlemail.com
> wrote:

> 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.
>

All true, but afiew notes:

1) you probably wan to look at Cython for making this sor tof thing easier.

2) a numpy=based ragged array implementation might make sense as well. You
essentially store the data in a rank-1 shaped numpy array, and provide
custom indexing to get the "rows" out. This would allow you to have all the
data in a single memory block available to C (or Cython), so that you could
fully optimize indexing and access, and have a data structure that makes
sense in pure C.

I've enclosed a start off such a class ( I honestly can't remember how far
I got with it!, but it was at least useful for one project of mine.)

HTH,
  -Chris

-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20140702/7ecdca2d/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ragged_array.py
Type: text/x-python-script
Size: 4305 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20140702/7ecdca2d/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_ragged_array.py
Type: text/x-python-script
Size: 3068 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20140702/7ecdca2d/attachment-0001.bin>


More information about the NumPy-Discussion mailing list