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.