Reputation: 421
I have a 1D numpy array containing complex numbers (eg. numpy.complex64). I need to create views on this array, but I don't know how to create a view given a list or range of indices to include in the view.
>>> myArray = np.ndarray(shape=(1000,), dtype=np.complex64)
I know how to create a view on consecutive items, for example the first 100 elements:
>>> myView = myArray[:100]
As I experimented, it's not possible to create a view on a singular item, like myArray[2] because if I modified that value, it wouldn't change the underlying array. This is OK, but I hope there is a way to construct a view from multiple arbitrary indices, so a function like this would be great:
>>> myView = createView(myArray, indices=(0, 1, 6, 7, 13))
which would return a view that points to those indices given in a list (or any iterable form), and if I change myView, it would change myArray like it should.
Is this possible, or is there a sane workaround? Thanks
edit:
To explain why I need this: I want to copy the array to OpenCL device memory (and back). I need a different order of the elements on the device than in the original array, and these shorter arrays would be treated as vectors, and matrix multiplication will take place in the OpenCL kernel. If the elements are not copied to the device memory in the order that is required, memory coalescing wouldn't be possible, and significant performance decrease would occur, not to mention the additional logic that would be needed on OpenCL side.
In my case, it is possible to do this with evenly spaced views, but I wonder if there's a more general way. A small CPU-side performance decrease is OK if a more general implementation can be done.
Upvotes: 2
Views: 2219
Reputation: 31
The following demonstrates the OP's request:
>>> a = numpy.array(xrange(103,114))
>>> a
array([103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113])
>>> view = numpy.array([2,6,7,9])
>>> a[view]
array([105, 109, 110, 112])
>>> a[view] += 9999
>>> a
array([ 103, 104, 10104, 106, 107, 108, 10108, 10109, 111,
10111, 113])
Upvotes: 1
Reputation: 2249
As far as I know views on ndarray are generated like this: myArray.view(dtype). Here is the documentation: http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.view.html
It does not answer to your non-consecutive items problem, but I think it's a good start.
I think you should create another filtered array with your indexes. Because I view doesn't really have to do with filtering, but rather with different representation of the same data.
Upvotes: 0
Reputation: 57920
As far as I know, you can't have a view on arbitrary indices; only on consecutive indices or regularly spaced indices. This is because the elements in the view's underlying memory store have to be separated by a constant number of bytes, otherwise all the fast NumPy routines don't work.
Upvotes: 2