Gaussian elimination

In [3]:
import numpy as np
In [18]:
np.random.seed(5)
n = 4
A = np.round(np.random.randn(n, n) * 5)
A
Out[18]:
array([[  2.,  -2.,  12.,  -1.],
       [  1.,   8.,  -5.,  -3.],
       [  1.,  -2.,  -6.,  -1.],
       [ -2.,   3.,  -8.,  -4.]])

Now compute A1 to eliminate A[1,0]:

In [23]:
A1 = A.copy()
A1[1] -= 1/2*A1[0]
A1
Out[23]:
array([[  2. ,  -2. ,  12. ,  -1. ],
       [  0. ,   9. , -11. ,  -2.5],
       [  1. ,  -2. ,  -6. ,  -1. ],
       [ -2. ,   3. ,  -8. ,  -4. ]])

And A2 with A[2,0] == 0:

In [25]:
A2 = A1.copy()
A2[2] -= 1/2*A[0]
A2
Out[25]:
array([[  2. ,  -2. ,  12. ,  -1. ],
       [  0. ,   9. , -11. ,  -2.5],
       [  0. ,  -1. , -12. ,  -0.5],
       [ -2. ,   3. ,  -8. ,  -4. ]])