title: How to solve 4 x 4 array with 16 unknown with Python (Update)
body: <p>I need to solve a 2 by 2 array with 4 unknown</p>
  
  <p>A B<BR>
  C D</p>
  
  <p>I know all horizontal sum A+B=11, C+D=7<BR>
  I know all vertical sum   A+C=10, B+D=8<BR>
  I know all diagonal sum   A+D=15, B+C=3<BR></p>
  
  <p>I then use Python to solve for A,B,C,D<BR></p>
  
  <pre><code>import numpy as np
  
  A = [[1, 1, 1, 1], 
       [1, 0, 0, 1], 
       [1, 0, 1, 0], 
       [0, 0, 1, 1]]
  a = [18, 15, 10, 7]
  answera = np.linalg.solve(A, a)
  print(answera)
  </code></pre>
  
  <p>And the answer is [9. 2. 1. 6.] which is correct</p>
  
  <p>Now I need to solve 4 by 4 array with 16 unknown</p>
  
  <p>A B C D<BR>
  E F G H<BR>
  I J K L<BR>
  M N O P<BR></p>
  
  <p>I know horizontal sum A+B+C+D=10, E+F+G+H=26, I+J+K+L=42, M+N+O+P=58<BR>
  I know vertical sum A+E+I+M=28, B+F+J+N=32, C+G+K+O=36, D+H+L+P=40<BR>
  I know diagonal sum M=13, I+N=23, E+J+O=30, A+F+K+P=34, B+G+L=21, C+H=11, D=4<br>
  The other diagonal sum A=1, B+E=7, C+F+I=18, D+G+J+M=34, H+K+N=33, L+O=27, P=16<br>
  Which mean I know the value of the 4 corners.</p>
  
  <p>I tried the following code but did not work</p>
  
  <pre><code>C = [[1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
       [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], 
       [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
       [0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],
       [0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0],
       [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
       [0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
       [0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] 
  
  c = [10, 26, 42, 58, 7, 21, 39, 33, 27, 11, 23, 35, 30, 23, 32, 136]
  answerc = np.linalg.solve(C, c)
  print(answerc)
  </code></pre>
  
  <p>The correct answer should be [1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.] but I got error message<br></p>
  
  <pre><code>Traceback (most recent call last):
  answerc = np.linalg.solve(C, c)
  r = gufunc(a, b, signature=signature, extobj=extobj)
   raise LinAlgError("Singular matrix")
  numpy.linalg.LinAlgError: Singular matrix
  </code></pre>
  
  <p>Am I in the right direction? I will need to solve 5X5 with 25 unknown, 6X6 with 36 unknown and so on. Is there an easier way?<br><br>
  <code>-----------------------------------------------------------------------------</code><br><br>
  <strong>Following Mr. Rory Daulton solution, I can solve the above 1 to 16 4X4 array without problem, but when I use it in another array with negative number, it doesn't give answer as expected;</strong></p>
  
  <p>The negative 4X4 array as follow</p>
  
  <pre><code>-20   -10    -5    0
  -10   -20   -10   -5
  -5      0   -10   -20
  -10   -20   -10   -5
  </code></pre>
  
  <p>My python code as follow</p>
  
  <pre><code>import numpy as np
  G = [[1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],  # horizontal rows
       [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], 
       [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
       [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0],  # vertical columns
       [0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0],
       [0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0],
       [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],  # forward diagonals
       [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
       [0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0],
       [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
       [0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],  # back diagonals
       [0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
      ]
  
  g = [-35, -45, -35, -45,  # horizontal rows
       -45, -50, -35, -30,  # vertical columns
       -10, -25, -20, -55, -40, -10, 0,  # forward diagonals
       -20, -20, -30, -20, -35, -30, -5,  # back diagonals
      ]
  
  answerg = np.linalg.lstsq(G, g, rcond=None)
  print(answerg[0])
  </code></pre>
  
  <p>The output is not exactly the original array</p>
  
  <pre><code>[-2.00000000e+01 -1.31250000e+01 -1.87500000e+00  8.88178420e-15
   -6.87500000e+00 -2.00000000e+01 -1.00000000e+01 -8.12500000e+00
   -8.12500000e+00  2.13162821e-14 -1.00000000e+01 -1.68750000e+01
   -1.00000000e+01 -1.68750000e+01 -1.31250000e+01 -5.00000000e+00]
  </code></pre>
  
  <p>What should I try? Thank you in advance.</p>