From: Bill B. <wb...@gm...> - 2006-07-06 02:39:25
|
Often when I'm doing interactive prototyping I find myself wanting to check whether two arrays are sharing a copy of the same data. It seems like there ought to be a concise way to do that, but right now seems like the only way is with a little function like this: def same_base(a,b): ab = a.base if ab is None: ab = a bb = b.base if bb is None: bb = b return ab is bb is there some easier built-in way? Does the above function even cover all the bases? (so to speak...) It would be easier if a non-copy array pointed to itself as base rather than 'None'. Then you could just check a.base == b.base. --bb |
From: Stefan v. d. W. <st...@su...> - 2006-07-06 07:21:29
|
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 c= heck > whether two arrays are sharing a copy of the same data. >=20 > It seems like there ought to be a concise way to do that, but right now= seems > like the only way is with a little function like this: >=20 > def same_base(a,b): > ab =3D a.base > if ab is None: ab =3D a > bb =3D b.base > if bb is None: bb =3D b > return ab is bb =20 >=20 > is there some easier built-in way? Does the above function even cover = all the > bases? (so to speak...) Say you have x =3D N.array([1,2,3,4]) and y =3D x.reshape((2,2)) then x and y share the same data. You can see this when you do x.__array_interface__['data'][0] =3D=3D y.__array_interface__['data'][0] Still, this only holds for full data views. If you had z =3D y[1:,1:] then the data memory position would differ. Cheers St=E9fan |
From: Rob W.W. H. <ro...@ho...> - 2006-07-06 09:04:34
|
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 || ro...@ho... || http://www.hooft.net/people/rob/ |
From: Albert S. <fu...@gm...> - 2006-07-06 09:44:55
|
Hello all > <snip> > 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"! I think we might be talking about two related but different concepts. One is sharing of data between arrays, the other is whether the data overlaps. Let's assume we can get at the starting address of the array in memory via the array interface or whatever, and the length of the array in bytes. To determine whether two arrays overlap, find the smallest data address of the two arrays. If the data address of the other array is smaller than the sum of the smallest data address and its corresponding length, you have overlap. I'm not quite sure how to check whether two arrays share the same underlying data buffer. Flags don't tell the whole story here. Regards, Albert |
From: Rob W.W. H. <ro...@ho...> - 2006-07-06 11:11:14
|
Albert Strasheim wrote: > I think we might be talking about two related but different concepts. One is > sharing of data between arrays, the other is whether the data overlaps. When is it useful to know whether data buffers overlap? As long as they are disjoint, it should be irrelevant at the higher level of the program. > Let's assume we can get at the starting address of the array in memory via > the array interface or whatever, and the length of the array in bytes. > > To determine whether two arrays overlap, find the smallest data address of > the two arrays. If the data address of the other array is smaller than the > sum of the smallest data address and its corresponding length, you have > overlap. Don't forget the strides. Rob -- Rob W.W. Hooft || ro...@ho... || http://www.hooft.net/people/rob/ |