2

Adding the transpose of a square matrix to that matrix should produce a matrix that is symmetric around the diagonal. For example:

import numpy as np
W1 = np.random.random((90, 90)) 
W1 += np.transpose(W1)
(np.transpose(W1) == W1).all()

True

But this behaviour seems to break down in larger matrices:

W2 = np.random.random((91, 91)) 
W2 += np.transpose(W2)
(np.transpose(W2) == W2).all()

False

The differences are not trivial:

print( np.max(W2), np.max(W2-np.transpose(W2)), np.min(W2-np.transpose(W2)))

2.84604132441 0.987790051685 -0.987790051685

Is this a deficit of numpy or am I missing something?

Daniel Power
  • 645
  • 9
  • 22
  • 1
    I'm just guessing, but it's probably because `np.transpose` is not making a copy but just changing the way the underlying C iterates over the data. So, when you add the transpose to the data (in place), the data is constantly changing as you're doing the addition. I'm not certain why this would be _shape_ dependent, but that's my best guess. – mgilson Oct 06 '15 at 13:39
  • This is to do with buffer size; at 91x91 you've just edged over the default buffer size of 8192 which can cause these unexpected results with inplace operations. – Alex Riley Oct 06 '15 at 13:45
  • It is very well explained in the possible duplicate question – Glostas Oct 06 '15 at 13:49
  • Thanks - I accept this is a duplicate. TLDR: The issue can be resolved by copying the transposed matrix before addition. – Daniel Power Oct 06 '15 at 15:26

0 Answers0