## numpy reference array: Resolved

This is the place for queries that don't fit in any of the other categories.

### numpy reference array: Resolved

Is it possible to create a numpy array which points to the same data in a different numpy array (but in different order etc)?

For example:
Code: Select all
`>>> import numpy as np>>> a = np.arange(10)>>> ids = np.array([0,0,5,5,9,9,1,1])>>> b = a[ids]>>> a[0] = -1>>> b[0] #should be -1 if b[0] referenced the same data as a[0]0`

ctypes almost does it for me, but the access is inconvenient:
Code: Select all
`>>> import numpy as np>>> import ctypes>>> a = np.arange(10)>>> ids = np.array([0,0,5,5,9,9,1,1])>>> b = [a[id:id+1].ctypes.data_as(ctypes.POINTER(ctypes.c_long)) for id in ids]>>> a[0] = -1>>> b[0][0] #access is inconvenient-1`

Some more information: I've written a finite-element code, and I'm working on optimizing the python implementation. Profiling shows the slowest operation is the re-creation of an array that extracts edge degrees of freedom from the volume of the element (similar to b above). So, I'm trying to avoid copying the data every time, and just setting up 'b' once. The ctypes solution is sub-optimal since my code is mostly vectorized, that is, later I'd like to something like
Code: Select all
`c[ids] = b[ids] + d[ids]`

where c, and d are the same shape as b but contain different data.

Any thoughts? If it's not possible that will save me time searching.
Last edited by empeeu on Mon Mar 18, 2013 1:48 pm, edited 1 time in total.
empeeu

Posts: 3
Joined: Wed Mar 06, 2013 7:11 pm

### Re: numpy reference array

So, it turns out that it is possible, but under very specific circumstances. Numpy allows you to create 'views', but these views have to conform to a neat strided pattern. e.g.

Code: Select all
`import numpy as npa = np.arange(10)b = a[0:3]a [0] = -1b[0] #this gives -1`

Unfortunately, I have a somewhat arbitrary re-arrangement of my data, in which case it is not possible to create my desired view.
empeeu

Posts: 3
Joined: Wed Mar 06, 2013 7:11 pm