A system of linear equations is a collection of one or more linear equations:
In matrix notation, the linear system is given by Ax=b, where
Gauss-Jordan elimination to solve linear system
High-level overview of Gauss-Jordan elimination:
- Given the matrices A and b of the linear system Ax=b we obtain augment matrix M=[A∣b] by stacking b as the last column to the matrix A.
M=⎣⎡a0,0a1,0⋮am,0a0,1a1,1am,1⋯⋯⋯a0,na1,n⋮am,nb0b1⋮bm⎦⎤
- Using elementary row operations, convert the augmented matrix M into reduced row-echelon form (RREF).
- In this form, the solution is simply the last column of the matrix M (if there is a unique solution). For example,
[024851041−1251]⟹
[1000100014.125−1.253.]
Reduced row-echelon form
A matrix is in reduced row-echelon form (RREF) if it satisfies all of the following conditions.
-
In every row the leftmost non-zero entry is 1 (called the leading 1).
-
If a column contains a leading 1 (i.e. first non-zero entry in the column is 1), then all other entries in that column is 0.
-
The leading 1 of any given row is always to the right of the leading 1 of the row above it.
-
Any rows consisting entirely of zeroes are placed at the bottom of the matrix.
Examples:
Elementary row operations
We use the following operations to convert a matrix M into RREF:
-
Row Swap. Exchange any two rows: M[i]↔M[j]
-
Scalar Multiplication. Multiply any row by a non-zero constant:
M[i]←k⋅M[i],k=0 -
Row Addition. Add multiple of one row to another row:
M[i]←M[i]+k⋅M[j],i=j
Download file elementary_row_ops.py from Ed.
Gauss-Jordan Elimination algorithm using NumPy
Check this link to understand how the algorithm works.
Check the program gaussjordan.py for an implementation of the algorithm. (Just for understanding; not for exam).
- Based roughly on the pseudocode for ToReducedRowEchelonForm in https://rosettacode.org/wiki/Reduced_row_echelon_form
Using scipy to solve linear systems
1import numpy as np2import scipy.linalg as la34A = np.array([[0, 8, 4], [2, 5, 1], [4, 10, -1]], dtype=float)5b = np.array([2, 5, 1], dtype=float)67result = la.solve(A, b)8print(result)9# [ 4.125 -1.25 3. ]