[Numpy-discussion] Determining if two arrays share data

Rob W.W. Hooft rob at hooft.net
Thu Jul 6 04:03:15 CDT 2006


Stefan van der Walt wrote:
> On Thu, Jul 06, 2006 at 11:39:19AM +0900, Bill Baxter wrote:
> 
>>Often when I'm doing interactive prototyping I find myself wanting to check
>>whether two arrays are sharing a copy of the same data.
> 
> 
> Say you have
> 
> x = N.array([1,2,3,4])
> 
> and
> 
> y = x.reshape((2,2))
> 
> then x and y share the same data.  You can see this when you do
> 
> x.__array_interface__['data'][0] == y.__array_interface__['data'][0]
> 
> Still, this only holds for full data views.  If you had
> 
> z = y[1:,1:]
> 
> then the data memory position would differ.

I am still using Numeric, but even with that background I think that the 
problem is ill-defined. Consider the following interactive session:

Python 2.3b1+ (#2, Jun 10 2003, 20:53:51)
[GCC 3.0.2 20010905 (Red Hat Linux 7.1 3.0.1-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> import Numeric
 >>> a=Numeric.arange(20)
 >>> b=a[::2]
 >>> c=a[1::2]
 >>> a
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 
16, 17, 18,
             19])
 >>> b
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])
 >>> c
array([ 1,  3,  5,  7,  9, 11, 13, 15, 17, 19])
 >>> b[0]=99
 >>> c[0]=99
 >>> a
array([99, 99,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 
16, 17, 18,
             19])


Do "b" and "c" share a copy of the same data? None of the values in 
either array is visible in the other, but both share data with "a"!

Regards,

Rob

-- 
Rob W.W. Hooft  ||  rob at hooft.net  ||  http://www.hooft.net/people/rob/




More information about the Numpy-discussion mailing list