Re: [Numpy-discussion] numpy reference array

2013-03-18 Thread Matt U
Chris Barker - NOAA Federal chris.barker at noaa.gov writes:
 check out stride tricks for clever things you can do.
 
 But numpy does require that the data in your array be a contiguous
 block, in order, so you can't arbitrarily re-arrange it while keeping
 a view.
 
 HTH,
   -Chris
 

Hi Chris, 
Thanks for the reply, you've just saved me a lot of time. I did run across
'views' but it looked like I couldn't have my data arbitrarily arranged. Thank
you for confirming that. Unfortunately my desired view does not fit a neat
striding pattern. 
Cheers,
Matt


___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] numpy reference array

2013-03-13 Thread Matt U
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: 
--
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. I would like to
access b as a regular numpy array:

Code: 
--
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:
--
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.

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] numpy reference array

2013-03-13 Thread Chris Barker - NOAA Federal
On Wed, Mar 13, 2013 at 6:56 AM, Matt U mpuec...@mit.edu wrote:
 Is it possible to create a numpy array which points to the same data in a
 different numpy array (but in different order etc)?

You can do this (easily), but only if the different order can be
defined in terms of strides. A simple example is a transpose:

In [3]: a = np.arange(12).reshape((3,4))

In [4]: a
Out[4]:
array([[ 0,  1,  2,  3],
   [ 4,  5,  6,  7],
   [ 8,  9, 10, 11]])

In [5]: b = a.T

In [6]: b
Out[6]:
array([[ 0,  4,  8],
   [ 1,  5,  9],
   [ 2,  6, 10],
   [ 3,  7, 11]])

# b is the transpose of a
# but a view on the same data block:
# change a:
In [7]: a[2,1] = 44


In [8]: a
Out[8]:
array([[ 0,  1,  2,  3],
   [ 4,  5,  6,  7],
   [ 8, 44, 10, 11]])

# b is changed, too.
In [9]: b
Out[9]:
array([[ 0,  4,  8],
   [ 1,  5, 44],
   [ 2,  6, 10],
   [ 3,  7, 11]])

check out stride tricks for clever things you can do.

But numpy does require that the data in your array be a contiguous
block, in order, so you can't arbitrarily re-arrange it while keeping
a view.

HTH,
  -Chris

-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/ORR(206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115   (206) 526-6317   main reception

chris.bar...@noaa.gov
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion