a is a numpy array and a.T is it’s transpose. Once I add a and a.T as a += a.T, the answer isn’t expected. Could any one tell me why? Thanks.
import numpy
a = numpy.ones((100, 100))
a += a.T
a
array([[ 2., 2., 2., ..., 2., 2., 2.],
[ 2., 2., 2., ..., 2., 2., 2.],
[ 2., 2., 2., ..., 2., 2., 2.],
...,
[ 3., 3., 3., ..., 2., 2., 2.],
[ 3., 3., 3., ..., 2., 2., 2.],
[ 3., 3., 3., ..., 2., 2., 2.]])
Note that
a.Tis only a view ona, which means they hold the same data. Now:Adds
a.Tin place toa, but while doing so, changesa.T(asa.Tpoints at the same data). Since the order of accessingais a bit more complex, this fails (and you should not trust the result to be reproducable, because it will change when you changenp.setbufsize.To avoid it both of these will work, though the first version seems cleaner to me.