[Numpy-discussion] Single view on multiple arrays
Sun Nov 1 13:09:13 CST 2009
2009/11/1 Bill Blinn <firstname.lastname@example.org>:
> What is the best way to create a view that is composed of sections of many
> different arrays?
The short answer is, you can't. Numpy arrays must be located
contiguous blocks of memory, and the elements along any dimension must
be equally spaced. A view is simply another array that references
(some of) the same underlying memory, possibly with different strides.
> For example, imagine I had
> a = np.array(range(0, 12)).reshape(3, 4)
> b = np.array(range(12, 24)).reshape(3, 4)
> c = np.array(range(24, 36)).reshape(3, 4)
> v = multiview((3, 4))
> #the idea of the following lines is that the 0th row of v is
> #a view on the first row of a. the same would hold true for
> #the 1st and 2nd row of v and the 0th rows of b and c, respectively
> v = a
> v = b
> v = c
> #change the underlying arrays
> a[0, 0] = 50
> b[0, 0] = 51
> c[0, 0] = 52
> #I would want all these assertions to pass because the view
> #refers to the rows of a, b and c
> assert v[0, 0] == 50
> assert v[1, 0] == 51
> assert v[2, 0] == 52
> Is there any way to do this?
If you need to be able to do this, you're going to have to rearrange
your code somewhat, so that your original arrays are views of parts of
an initial array.
It's worth noting that if what you're worried about is the time it
takes to copy data, you might well be surprised at how cheap data
copying and memory allocation really are. Given that numpy is written
in python, only for really enormous arrays will copying data be
expensive, and allocating memory is really a very cheap operation
(modern malloc()s average something like a few cycles). What's more,
since modern CPUs are so heavily cache-bound, using strided views can
be quite slow, since you end up loading whole 64-byte cache lines for
each 8-byte double you need.
In short, you probably need to rethink your design, but while you're
doing it, don't worry about copying data.
More information about the NumPy-Discussion