[Numpy-discussion] *= operator not intuitive
Paul Anton Letnes
paul.anton.letnes@gmail....
Wed Mar 16 08:24:49 CDT 2011
Hi!
This little snippet of code tricked me (in a more convoluted form). The *= operator does not change the datatype of the left hand side array. Is this intentional? It did fool me and throw my results quite a bit off. I always assumed that 'a *= b' means exactly the same as 'a = a * b' but this is clearly not the case!
Paul.
++++++++++++++++++
>>> from numpy import *
>>> a = arange(10)
>>> b = linspace(0,1,10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> b
array([ 0. , 0.11111111, 0.22222222, 0.33333333, 0.44444444,
0.55555556, 0.66666667, 0.77777778, 0.88888889, 1. ])
>>> a * b
array([ 0. , 0.11111111, 0.44444444, 1. , 1.77777778,
2.77777778, 4. , 5.44444444, 7.11111111, 9. ])
>>> a *= b
>>> a
array([0, 0, 0, 1, 1, 2, 4, 5, 7, 9])
More information about the NumPy-Discussion
mailing list