[Numpy-discussion] odd behaviour with basic operations
robert.kern at gmail.com
Sun Nov 1 21:19:28 EST 2009
On Sun, Nov 1, 2009 at 21:09, Benjamin Deschamps <bdesc100 at gmail.com> wrote:
> 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.
Please provide a self-contained example that demonstrates the problem.
Also, be aware that where C_02==0, sign(C_02)==0. You will need to
consider what should happen then.
A better way to do what you want is to use where():
Pd = numpy.where(C_02 > 0.0, Pd_pos, Pd_neg)
Ps = numpy.where(C_02 > 0.0, Ps_pos, Ps_neg)
Change the > to >= if you want C_02==0 to use the pos values.
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the NumPy-Discussion