[Numpy-discussion] Counting array elements

Chris Barker Chris.Barker at noaa.gov
Mon Oct 25 10:10:20 CDT 2004

A few comments on a number of posts in this thread:

Stephen Walton wrote:
>>- I'd like to write C/C++ code that would work on multiple array 
> I can't help much here, other than to say that C and C++ are pretty low
> level languages, not well suited for this level of abstraction.

Well, this is certainly true for C, but not so much for C++. I'm not 
expert, but C++ templates could be very handy here. When the numarray 
projects was just getting started, there was some discussion about using 
a template-based array package as the base, perhaps Blitz++. I still 
this this was a great idea, but I think the biggest issue at the time 
was that templates were still not constantly well supported by the wide 
variety of compilers that numarray should work with. Personally I think 
that anything supported by gcc should be fine, as anyone can use gcc on 
virtually any platform, if they want.

Anyway, it's too late to re-write numarray, but maybe a numarray <--> 
blitz++ conversion package would make it easy to write numarray 
extensions with blitz++. Perhaps even integrate it with Boost.Python. 
Another option would be to write a template-based wrapper around the 
existing Numarray objects.

By the way, my other issue with extensions is the difficulty of writing 
extensions that support discontinuous arrays, in addition to multiple 
data types. It seems someone smarter than me could use C++ classes to 
solve this one as well.

Peter Verveer wrote:

> But I do agree that it is not a good idea to introduce another set of 
> names. In my opinion functions that calculate a statistic like sum 
> should return the total in the first place, rather then over a single 
> axis.

Absolutely not! I'm far more likely to want it over a single axis, it's 
the core of "vectorizing" your code. If the data are mean the same 
thing, why aren't you storing it in a 1-d array? That being said, it 
should be easy to do various reductions over all axis, which I think 
.flat() does nicely. I thought .flat() never made a copy: am I wrong?

Stephen Walton wrote:
> It depends on the data.  I use rank-2 arrays which are images and are
> therefore homogeneous.

OK, good example.... I take back some of what I said above!

> By analogy with MATLAB (I'm guessing), sum() in Numeric and
> numarray does a one-D sum.

except Matab does it worse. If your 2-d array happens to have only one 
row, you get the sum over that..yecch!

Tim Hochberg wrote:
> I'm not sure how feasible it is, but I'd much rather an efficient, 
> non-copying, 1-D view of an noncontiguous array (from an enhanced 
> version of flat or ravel or whatever) than a bunch of extra methods. The 
> former allows all of the standard methods to just work efficiently using 
> sum(ravel(A)) or sum(A.flat) [ and max and min, etc]. Making special 
> whole array methods for everything just leads to method eplosion.

here! here! I thought that was exactly what .flat() was for. Shows what 
I know!


Christopher Barker, Ph.D.
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 Numpy-discussion mailing list