[Numpy-discussion] Arrays with aliased elements?
Robert Kern
robert.kern at gmail.com
Sat Jan 1 21:08:05 EST 2011
On Sat, Jan 1, 2011 at 19:42, Enzo Michelangeli <enzomich at gmail.com> 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).
See numpy.lib.stride_tricks for tools to do this, specifically the
as_strided() function. See numpy.broadcast_arrays() for the latter
functionality.
http://docs.scipy.org/doc/numpy/reference/generated/numpy.broadcast_arrays.html
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the NumPy-Discussion
mailing list