Numpy symmetric matrix generation semantics

I tried to create a random symmetric matrix to test my program. I don’t care at all about the data as long as it is symmetrical (enough randomness does not cause any concern).

My first attempt:

x=np.random.random((100,100))
x+=x.T

However, np.all(x==x.T)returns False. print x==x.Tgives

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 case with n = 10 to see what happens, but this example works just as you expected.

If I do it like this:

x=np.random.random((100,100))
x=x+x.T

then it works great.

What's going on here? Are these statements semantically equivalent? Who cares?

+1
source share
1 answer

. x.T . += x, ( x.T).

... (3,4), :

x[3,4] = x[3,4] + x[4,3]

, (4,3),

x[4,3] = x[4,3] + x[3,4]

x[3,4] , , .


() ( x). , :

y[3,4] = x[3,4] + x[4,3]

y[4,3] = x[4,3] + x[3,4]

, , (y.

+2

Source: https://habr.com/ru/post/1543871/


All Articles