Numeric question
Chris Barker
Chris.Barker at noaa.gov
Mon Apr 29 13:33:38 EDT 2002
Cameron Hooper wrote:
> Thanks for the replies. So my understanding now is that the concatenate
> function really combines two references to two separate locations in memory
> into a single object. The two locations are not contiguous. Data is not
> 'moved around' so to speak. So x = concatenate(y,z) can be viewed as
> returning a reference to two non-contiguous references. But x =
> array(concatenate(y,z)) (or using copy()) actually copies data.
Not quite. frankly, I'm a bit confused about how concatenate works, as I
don't see how it would be possible to re-use the data in the input
arrays. Also, a test shows that contatonate does not return a reference
to the same data:
>>> from Numeric import *
>>> a = ones((2,3))
>>> b = ones((3,3)) * 4
>>>
>>> c = concatenate((a,b))
>>> a
array([[1, 1, 1],
[1, 1, 1]])
>>> b
array([[4, 4, 4],
[4, 4, 4],
[4, 4, 4]])
>>> c
array([[1, 1, 1],
[1, 1, 1],
[4, 4, 4],
[4, 4, 4],
[4, 4, 4]])
>>> c.iscontiguous()
1
# so in this case, c is contiguous!
>>>
>>> c[0,0] = 5
>>> c
array([[5, 1, 1],
[1, 1, 1],
[4, 4, 4],
[4, 4, 4],
[4, 4, 4]])
# change a value in c
>>> a
array([[1, 1, 1],
[1, 1, 1]])
# a does not change, so they are not sharing data.
# this is different than slicing, which does result in two arrays
sharing data:
>>> d = c[:,2]
>>> d
array([1 1 4 4 4])
>>> d[2] = 6
>>> d.iscontiguous()
0
# slicing often results in discontiguous arrays
>>> c
array([[5, 1, 1],
[1, 1, 1],
[4, 4, 6],
[4, 4, 4],
[4, 4, 4]])
# so in this case, changing d did change c, as they do share data.
I have to say that I'mm in the dark as to why concatenate does not
always result in a contiguous array, but maybe with some more thought I
could figure it out.
-CHB
--
Christopher Barker, Ph.D.
Oceanographer
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
More information about the Python-list
mailing list