# [Numpy-discussion] Counting array elements

Stephen Walton stephen.walton at csun.edu
Mon Oct 25 09:20:02 CDT 2004

```On Mon, 2004-10-25 at 10:26 +0200, Peter Verveer wrote:
> On 25 Oct 2004, at 04:17, Stephen Walton wrote:
> >
> > I don't think we need sumall.  The methods and the functions should
> > simply work the same way.  If one wants sumall, use A.flat.sum() or, if
> > you can't use the methods or attributes on your old version of Python,
> > sum(ravel(A)).
>
> 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 don't think
sum(ravel(A)) looks any worse than sum(sum(sum(A))) for a rank 3 array,
but ugly is in the eye of the beholder.

> In my opinion functions that calculate a statistic like sum
> should return the total in the first place, rather then over a single
> axis.

It depends on the data.  I use rank-2 arrays which are images and are
therefore homogeneous.  Even there, though, I often want the sum of all
rows or all columns.  For heterogeneous data (e.g., columns of different
Y's as a function of X), the present sum() makes sense.  In other words,
we will always need ways to sum over just one dimension and over all
dimensions.  By analogy with MATLAB (I'm guessing), sum() in Numeric and
numarray does a one-D sum.

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

```

More information about the Numpy-discussion mailing list