summing NumPy byte arrays

Fernando Pérez fperez528 at yahoo.com
Thu Nov 29 10:28:13 EST 2001


Chris Barker wrote:


>> How can I get it to sum the array in a full-width accumulator,
>> without making an up-sized copy of the array?
> 
> I'm pretty sure you can't. That would take some preety fancy
> footwork in the typecasting code.
> 
>> ps. The array I am working with is a 2-d noncontinguous slice.
> 
> Your options are to either upcast the whole thing (you can make a
> copy of that slice, and only upcast the copy),
> 
>>>> from Numeric import *
>>>> X = arange(216, typecode='b')
>>>> X.shape = (6,6,6)
>>>> s = sum(X[3,:,:].copy().astype(Int))
>>>> s
> array([738, 744, 750, 756, 762, 768])
> 
> Or to write a little C routine, which would be pretty
> straightforward if you only need it to work for one type, and one
> rank.
> 
> -Chris
> 

Just curious: why not call reduce() with an object whose __add__ 
method does the work of adding an int +byte as int? The loop would be 
done in C (by reduce) and the tying should work ok.

Maybe I'm missing something,

f



More information about the Python-list mailing list