[Numpy-discussion] odd behaviour with basic operations

Benjamin Deschamps bdesc100 at gmail.com
Sun Nov 1 21:09:01 EST 2009

I am getting strange behaviour with the following code:

Pd = ((numpy.sign(C_02) == 1) * Pd_pos) + ((numpy.sign(C_02) == -1) *  
Ps = ((numpy.sign(C_02) == 1) * Ps_pos) + ((numpy.sign(C_02) == -1) *  

where Pd, Ps, C_02, Pd_pos, Pd_neg, Ps_pos and Ps_neg are all Float32  
numpy arrays of the same shape.

The problem is that the first line evaluates correctly (Pd is what it  
should be), but the second line does not. However, if I run the same  
line of code manually in IDLE, then it evaluates correctly! In other  
words, Ps as returned by the function does not match the value that I  
should get and obtain when entering the exact same code in IDLE.

Basically, (numpy.sign(C_02) == 1) evaluates to either True or False,  
and multiplying with another array will give either 0 (when false) or  
the value of the array. The purpose of this code is to compute Pd and  
Ps without loops, and to take the value from Pd_pos or Ps_pos when  
C_02 is positive or of Pd_neg and Ps_neg when C_02 is negative.

Using loops, it looks like this:

for index in numpy.ndindex(ysize, xsize):
     if numpy.sign(C_02[index]) == 1:
         Pd[index] = Pd_pos[index]
         Ps[index] = Ps_pos[index]
     elif numpy.sign(C_02[index]) == -1:
         Pd[index] = Pd_neg[index]
         Ps[index] = Ps_neg[index]

which also works fine, but takes much longer.

Python 2.6.3, IDLE 2.6.1, Numpy 1.3.0, Snow Leopard, the script also  
uses some GDAL, matplotlib and scipy functions...


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20091101/4f1070d8/attachment.html>

More information about the NumPy-Discussion mailing list