[Numpy-discussion] Determining if two arrays share data

Albert Strasheim fullung at gmail.com
Thu Jul 6 04:45:02 CDT 2006


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





More information about the Numpy-discussion mailing list