[Numpy-discussion] Counting array elements

Stephen Walton stephen.walton at csun.edu
Mon Oct 25 09:35:06 CDT 2004


On Mon, 2004-10-25 at 09:19 -0700, Stephen Walton wrote:
> On Mon, 2004-10-25 at 10:26 +0200, Peter Verveer wrote:
>
> > I think this may be inefficient, because ravel and flat may make a copy 
> > of the data. Also I think using flat/ravel in such a way is plain ugly 
> > and a complex way to do it.
> 
> You may be right about the copying, I couldn't say. 

I just looked at the source (numeric-1.1/Lib/generic.py).  The comment
to the ravel() function states that it returns a view, not a copy;  but
it calls reshape() which does make a copy if the input array is not
contiguous.  I just tested this:

A=arange(25,shape=(5,5))
A.transpose()		# now A is not contiguous
v=ravel(A)
A[2,2]=-17
v			# verifies that v did not change.

So, in the above, it does look like ravel() made a copy, and your fears
about inefficiency are warranted.  Another test shows that changing
ravel(A) to A.flat above also results in a copy.  Mayhaps we need
sumall() after all.

-- 
Stephen Walton, Professor of Physics and Astronomy,
California State University, Northridge
stephen.walton at csun.edu





More information about the Numpy-discussion mailing list