12.1 — Using SciPy, System of Linear Equations

A system of linear equations is a collection of one or more linear equations:

a0,0x0+a0,1x2++a0,nxn=b0a1,0x0+a1,1x2++a1,nxn=b1am,0x0+am,1x2++am,nxn=bm\small \begin{aligned} a_{0,0}x_0 + a_{0,1}x_2 + \cdots + a_{0,n}x_n & = b_0 \\ a_{1,0}x_0 + a_{1,1}x_2 + \cdots + a_{1,n}x_n & = b_1 \\ & \vdots \\ a_{m,0}x_0 + a_{m,1}x_2 + \cdots + a_{m,n}x_n & = b_m \\ \end{aligned}

In matrix notation, the linear system is given by Ax=b\mathbf{Ax = b}, where

A=[a0,0a0,1a0,na1,0a1,1a1,nam,0am,1am,n]  ,  x=[x0x1xn]  ,  b=[b0b1bm]\small A = \begin{bmatrix} a_{0,0} & a_{0,1} & \cdots & a_{0,n} \\ a_{1,0} & a_{1,1} & \cdots & a_{1,n} \\ \vdots & & & \vdots \\ a_{m,0} & a_{m,1} & \cdots & a_{m,n} \\ \end{bmatrix} \ \ , \ \ \mathbf{x} = \begin{bmatrix} x_0 \\ x_1 \\ \vdots \\ x_n \end{bmatrix} \ \ , \ \ \mathbf{b} = \begin{bmatrix} b_0 \\ b_1 \\ \vdots \\ b_m \end{bmatrix}

Gauss-Jordan elimination to solve linear system

High-level overview of Gauss-Jordan elimination:

  1. Given the matrices A\mathbf{A} and b\mathbf{b} of the linear system Ax=b\mathbf{Ax = b} we obtain augment matrix M=[Ab]\mathbf{M = [A | b]} by stacking b\mathbf{b} as the last column to the matrix A\mathbf{A}.
    M=[a0,0a0,1a0,nb0a1,0a1,1a1,nb1am,0am,1am,nbm] \small M = \left[ \begin{array}{cccc|c} a_{0,0} & a_{0,1} & \cdots & a_{0,n} & b_0 \\ a_{1,0} & a_{1,1} & \cdots & a_{1,n} & b_1 \\ \vdots & & & \vdots & \vdots \\ a_{m,0} & a_{m,1} & \cdots & a_{m,n} & b_m \\ \end{array} \right]

  1. Using elementary row operations, convert the augmented matrix MM into reduced row-echelon form (RREF).
    • In this form, the solution is simply the last column of the matrix MM (if there is a unique solution). For example,
    [0842251541011] \small \left[ \begin{array}{ccc|c} 0 & 8 & 4 & 2 \\ 2 & 5 & 1 & 5 \\ 4 & 10 & -1 & 1 \\ \end{array} \right]

        \implies

    [1004.1250101.250013.] \small \left[ \begin{array}{ccc|c} 1 & 0 & 0 & 4.125 \\ 0 & 1 & 0 & -1.25 \\ 0 & 0 & 1 & 3. \\ \end{array} \right]

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 11 (called the leading 11).

  • If a column contains a leading 11 (i.e. first non-zero entry in the column is 11), then all other entries in that column is 00.

  • The leading 11 of any given row is always to the right of the leading 11 of the row above it.

  • Any rows consisting entirely of zeroes are placed at the bottom of the matrix.

Examples:

A=[10100102],B=[100010001]\small A = \begin{bmatrix} 1 & 0 & 1 & 0\\ 0 & 1 & 0 & 2 \end{bmatrix} \quad , \quad B = \begin{bmatrix} 1 & 0 & 0\\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix}
C=[1001000000],D=[100400010300001310000001]\small C = \begin{bmatrix} 1 &0 \\ 0 & 1\\ 0 & 0\\ 0 & 0\\ 0 & 0 \end{bmatrix} \quad , \quad D = \begin{bmatrix} 1 & 0 & 0 & 4 & 0 & 0 \\ 0 & 1 & 0 & 3 & 0 & 0\\ 0 & 0 & 1 & 3 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 \end{bmatrix}

Elementary row operations

We use the following operations to convert a matrix M into RREF:

  1. Row Swap. Exchange any two rows: M[i]M[j]M[i] \leftrightarrow M[j]

  2. Scalar Multiplication. Multiply any row by a non-zero constant:
    M[i]kM[i],k0M[i] \gets k \cdot M[i] , k \neq 0

  3. Row Addition. Add multiple of one row to another row:
    M[i]M[i]+kM[j],ijM[i] \gets M[i] + k \cdot M[j] , i \neq 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).

Using scipy to solve linear systems

1import numpy as np
2import scipy.linalg as la
3
4A = np.array([[0, 8, 4], [2, 5, 1], [4, 10, -1]], dtype=float)
5b = np.array([2, 5, 1], dtype=float)
6
7result = la.solve(A, b)
8print(result)
9# [ 4.125 -1.25 3. ]