[Numpy-discussion] *= operator not intuitive
Angus McMorland
amcmorl@gmail....
Wed Mar 16 08:29:05 CDT 2011
On 16 March 2011 09:24, Paul Anton Letnes <paul.anton.letnes@gmail.com> wrote:
> 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!
This is intentional: a *= b works inplace, i.e. it's the equivalent,
not of a = a * b, but of a[:] = a * b
Angus.
> 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])
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
--
AJC McMorland
Post-doctoral research fellow
Neurobiology, University of Pittsburgh
More information about the NumPy-Discussion
mailing list