[Numpy-discussion] Arrays with aliased elements?

Enzo Michelangeli enzomich at gmail.com
Sat Jan 1 20:42:02 EST 2011


Is there any way, not involving compilation of C code, to define ndarrays
where some rows or columns share the same data buffers? For example, 
something built by a hypothetical variant of the np.repeat() function, such 
that, if a = array([2,3]), calling:

   b = np.aliasedrepeat(x, [1, 2], axis=0)

would return in b:

   array([[2, 3],
          [2, 3],
          [2, 3]])

...with the understanding that the three rows would actually share the same
data, so setting e.g.:

   b[0,1] = 5

...would change b into:

   array([[2, 5],
          [2, 5],
          [2, 5]])

In other words, something with a behaviour similar to a list of lists:

>>> a = [2,3]
>>> b = [a,a,a]
>>> b
[[2, 3], [2, 3], [2, 3]]
>>> b[0][1] = 5
>>> b
[[2, 5], [2, 5], [2, 5]]

This would save memory (and time spent in unnecessary copying) in some
applications with large arrays, and would allow to cope with the current
inability of weave.blitz to understand broadcasting rules, e.g. for
calculating outer products (I mentioned this in a previous thread).

Enzo




More information about the NumPy-Discussion mailing list