I am creating symmetric matrices/arrays in Python with NumPy, using a standard method:
x = rand(500,500)
x = (x+x.T)
all(x==x.T)
> True
Now let's be clever:
x = rand(500,500)
x += x.T
all(x==x.T)
> False
Wait, what?
x==x.T
> array([[ True, True, True, ..., False, False, False],
[ True, True, True, ..., False, False, False],
[ True, True, True, ..., False, False, False],
...,
[False, False, False, ..., True, True, True],
[False, False, False, ..., True, True, True],
[False, False, False, ..., True, True, True]], dtype=bool)
The upper left and lower right segments are symmetrical. What if I chose a smaller array?
x = rand(50,50)
x += x.T
all(x==x.T)
> True
OK....
x = rand(90,90)
x += x.T
all(x==x.T)
> True
x = rand(91,91)
x += x.T
all(x==x.T)
> False
And just to be sure...
x = rand(91,91)
x = (x+x.T)
all(x==x.T)
> True
Is this a bug, or am I about to learn something crazy about +=
and NumPy arrays?
See Question&Answers more detail:
os