# [Numpy-discussion] Arrays with aliased elements?

Zachary Pincus zachary.pincus at yale.edu
Sat Jan 1 20:53:13 EST 2011

```def repeat(arr, num):
arr = numpy.asarray(arr)
return numpy.ndarray(arr.shape+(num,), dtype=arr.dtype,
buffer=arr, strides=arr.strides+(0,))

There are limits to what these sort of stride tricks can accomplish,
but repeating as above, or similar, is feasible.

On Jan 1, 2011, at 8:42 PM, Enzo Michelangeli wrote:

> 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
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

```