I tried to make a random symmetric matrix to test my program. I don't care about the data at all as long as it is symmetric (sufficient randomness is no concern at all).
My first attempt was this:
x=np.random.random((100,100))
x+=x.T
However, np.all(x==xT)
returns False. print x==xT
yields
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)
I tried to run a small test example with n=10 to see what was going on, but that example works just as you would expect it to.
If I do it like this instead:
x=np.random.random((100,100))
x=x+x.T
then it works just fine.
What's going on here? Aren't these statements semantically equivalent? What's the difference?
Those statements aren't semantically equivalent. xT
returns a view of the original array. in the +=
case, you're actually changing the values of x
as you iterate over it (which changes the values of xT
).
Think of it this way ... When your algorithm gets to index (3,4)
, it looks something like this in pseudocode:
x[3,4] = x[3,4] + x[4,3]
now, when your iteration gets to (4,3)
, you do
x[4,3] = x[4,3] + x[3,4]
but, x[3,4]
is not what it was when you started iterating.
In the second case, you actually create a new (empty) array and change the elements in the empty array (never writing to x
). So the pseudocode looks something like:
y[3,4] = x[3,4] + x[4,3]
and
y[4,3] = x[4,3] + x[3,4]
which obviously will give you a symmetric matrix ( y
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.