CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
weijie-chen

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: weijie-chen/Linear-Algebra-With-Python
Path: blob/master/notebooks/Chapter 10 -Null Space vs Col Space, Row Space and Rank.ipynb
Views: 449
Kernel: Python 3
import matplotlib.pyplot as plt import numpy as np import sympy as sy sy.init_printing()

Null Space

The null space, denoted as NulA\text{Nul}A is the solution set of a homogeneous linear system, i.e. Ax=0Ax=0.

A null space is a always a subspace of Rn\mathbb{R}^n, why? Because one solution can always be the origin (0,0,...)(0, 0, ...).

As an example, consider a linear system.

2x1x2+x3=0x1+2x2+3x3=02x_1-x_2+x_3 = 0\\ x_1+2x_2+3x_3= 0

The augmented matrix is

[21101230]\left[ \begin{matrix} 2 & -1 & 1 & 0\\ 1 & 2 & 3 & 0 \end{matrix} \right]

Before solving the system, we have already known there is no unique solution since a free variable presents, due to the fact that two equation with three variables.

Solve for the reduced echelon form.

Aug = sy.Matrix([[2,-1,1,0],[1,2,3,0]]) Aug.rref()

([10100110], (0, 1))\displaystyle \left( \left[\begin{matrix}1 & 0 & 1 & 0\\0 & 1 & 1 & 0\end{matrix}\right], \ \left( 0, \ 1\right)\right)

x3x_3 is a free variable, the solution set can be written as

[x1x2x3]=[x3x3x3]=x3[111]\left[ \begin{matrix} x_1 \\ x_2 \\ x_3 \end{matrix} \right]= \left[ \begin{matrix} -x_3 \\ -x_3 \\ x_3 \end{matrix} \right]= x_3\left[ \begin{matrix} -1 \\ -1 \\ 1 \end{matrix} \right]

which is a line passing both origin (0,0,0)(0, 0, 0) and (1,1,1)(-1, -1, 1), also a subspace of R3\mathbb{R}^3.

Now consider another example, suppose we have an augmented matrix

Aug = sy.Matrix([[-3,6,-1,1,-7,0],[1,-2,2,3,-1,0],[2,-4,5,8,-4,0]]);Aug

[361170122310245840]\displaystyle \left[\begin{matrix}-3 & 6 & -1 & 1 & -7 & 0\\1 & -2 & 2 & 3 & -1 & 0\\2 & -4 & 5 & 8 & -4 & 0\end{matrix}\right]

Aug.rref()

([120130001220000000], (0, 2))\displaystyle \left( \left[\begin{matrix}1 & -2 & 0 & -1 & 3 & 0\\0 & 0 & 1 & 2 & -2 & 0\\0 & 0 & 0 & 0 & 0 & 0\end{matrix}\right], \ \left( 0, \ 2\right)\right)

The solution can be written as:

[x1x2x3x4x5]=[2x2+x43x5x22x4+2x5x4x5]=x2[21000]+x4[10210]+x5[30201]\left[ \begin{matrix} x_1 \\ x_2 \\ x_3 \\x_4 \\ x_5 \end{matrix} \right]= \left[ \begin{matrix} 2x_2+x_4-3x_5 \\ x_2 \\ -2x_4+2x_5 \\x_4 \\ x_5 \end{matrix} \right]= x_2\left[ \begin{matrix} 2 \\ 1 \\ 0 \\0 \\ 0 \end{matrix} \right] + x_4\left[ \begin{matrix} 1 \\ 0 \\ -2 \\1 \\ 0 \end{matrix} \right] +x_5\left[ \begin{matrix} -3 \\ 0 \\ 2 \\0 \\ 1 \end{matrix} \right]

The NulA\text{Nul}A is a subspace in R5\mathbb{R}^5 with dimA=3\text{dim}A=3.

Null Space vs Col Space

Consider matrix AA

A = sy.Matrix([[2,4,-2,1],[-2,-5,7,3],[3,7,-8,6]]);A

[242125733786]\displaystyle \left[\begin{matrix}2 & 4 & -2 & 1\\-2 & -5 & 7 & 3\\3 & 7 & -8 & 6\end{matrix}\right]

Column space is a subspace in Rn\mathbb{R}^n, what is nn? It is the number of rows, n=3n=3.

Null space is a subspace in Rm\mathbb{R}^m, what is mm? It is the number of columns, m=4m=4.

How to find any nonzero vector in ColA\text{Col}A and in NulA\text{Nul}A?

Any column in a matrix can be a nonzero vector in ColA\text{Col}A, for instance first column: (2,2,3)T(2, -2, 3)^T.

But to find a nonzero vector in null space requires some effort, construct the augmented matrix then turn it into rref.

Aug = sy.Matrix([[2,4,-2,1,0],[-2,-5,7,3,0],[3,7,-8,6,0]]);Aug.rref()

([109000150000010], (0, 1, 3))\displaystyle \left( \left[\begin{matrix}1 & 0 & 9 & 0 & 0\\0 & 1 & -5 & 0 & 0\\0 & 0 & 0 & 1 & 0\end{matrix}\right], \ \left( 0, \ 1, \ 3\right)\right)

The solution set with a free variable x3x_3 (because column 3 has no pivot) is

[x1x2x3x4]=[9x35x3x30]\left[ \begin{matrix} x_1 \\ x_2 \\ x_3\\x_4 \end{matrix} \right]= \left[ \begin{matrix} -9x_3 \\ 5x_3 \\ x_3\\0 \end{matrix} \right]

If we pick x3=1x_3 =1, a nonzero vector in NulA\text{Nul}A is (9,5,1,0)T(-9, 5, 1, 0)^T

Now consider two vectors

$$u = \left[ \begin{matrix} 3 \\ -2 \\ -1\\ 0 \end{matrix} \right],\qquad v = \left[ \begin{matrix} 3 \\ -1\\3 \end{matrix} \right]\\$$

Is uu in NulA\text{Nul}A? It can be verified easily

u = sy.Matrix([[3],[-2],[-1],[0]]) A*u

[033]\displaystyle \left[\begin{matrix}0\\-3\\3\end{matrix}\right]

Au0Au\neq \mathbf{0}, therefore uu is not in NulA\text{Nul}A.

Is vv in ColA\text{Col}A? Construct an augmented matrix with vv, then solve it

v = sy.Matrix([[3],[-1],[3]]) A.row_join(v).rref()

([10905015030170001117], (0, 1, 3))\displaystyle \left( \left[\begin{matrix}1 & 0 & 9 & 0 & 5\\0 & 1 & -5 & 0 & - \frac{30}{17}\\0 & 0 & 0 & 1 & \frac{1}{17}\end{matrix}\right], \ \left( 0, \ 1, \ 3\right)\right)

The augmented matrix show there are solutions, i.e. vv is a linear combination of its column space basis, so vv is in ColA\text{Col}A.

Row Space

The Row space denoted as RowA\text{Row}A, contains all linear combination of row vectors and subspace in Rn\mathbb{R}^n.

If we perform row operations on AA to obtain BB, both matrices have the same row space, because BB's rows are linear combinations of AA's. However, row operation will change the row dependence.

An Example

Find the row, column and null space of

A = sy.Matrix([[-2, -5, 8, 0, -17], [1, 3, -5, 1, 5], [3, 11, -19, 7, 1], [1, 7, -13, 5, -3]]);A

[258017135153111971171353]\displaystyle \left[\begin{matrix}-2 & -5 & 8 & 0 & -17\\1 & 3 & -5 & 1 & 5\\3 & 11 & -19 & 7 & 1\\1 & 7 & -13 & 5 & -3\end{matrix}\right]

B = A.rref();B

([10101012030001500000], (0, 1, 3))\displaystyle \left( \left[\begin{matrix}1 & 0 & 1 & 0 & 1\\0 & 1 & -2 & 0 & 3\\0 & 0 & 0 & 1 & -5\\0 & 0 & 0 & 0 & 0\end{matrix}\right], \ \left( 0, \ 1, \ 3\right)\right)

The basis of the row space of BB is its first 3 rows: (1,0,1,0,1),(0,1,2,0,3),(0,0,0,1,5)(1,0,1,0,1), (0, 1, -2, 0, 3), (0, 0, 0, 1, -5) which are also the basis of the row space of AA. However it does not necessarily mean that first 3 rows of AA forms the basis for row space, because the dependence among rows changed by row operation.

In constrast, the basis of col space of AA is (2,1,3,1)T,(5,3,11,7)T,(0,1,7,5)T(-2, 1, 3, 1)^T, (-5, 3, 11, 7)^T, (0, 1, 7, 5)^T.

Aug = A.row_join(sy.zeros(4,1));Aug.rref()

([101010012030000150000000], (0, 1, 3))\displaystyle \left( \left[\begin{matrix}1 & 0 & 1 & 0 & 1 & 0\\0 & 1 & -2 & 0 & 3 & 0\\0 & 0 & 0 & 1 & -5 & 0\\0 & 0 & 0 & 0 & 0 & 0\end{matrix}\right], \ \left( 0, \ 1, \ 3\right)\right)

The null space is

[x1x2x3x4x5]=[x3x52x33x5x35x5x5]=x3[12100]+x5[13051]\left[ \begin{matrix} x_1 \\ x_2 \\ x_3\\x_4 \\x_5 \end{matrix} \right]= \left[ \begin{matrix} -x_3-x_5 \\ 2x_3-3x_5 \\ x_3\\5x_5 \\x_5 \end{matrix} \right]= x_3\left[ \begin{matrix} -1 \\ 2 \\ 1\\0 \\0 \end{matrix} \right]+ x_5 \left[ \begin{matrix} -1 \\ -3 \\ 0\\5 \\1 \end{matrix} \right]

Rank

Definition of rank: The rank is the dimension of the column space of AA. The nullity of AA is the dimension of the null space.

The Rank Theorem

The dimensions of the column space and the row space of an m×nm \times n matrix AA are equal. Therefore, the rank is the same for either column space and row space.

This common dimension, the rank of AA, also equals the number of pivot positions in AA and satisfies the equation: rankA+dimNulA=n \operatorname{rank} A + \operatorname{dim} \mathrm{Nul} A = n

The intuition behind this is that when a matrix AA is converted into its reduced row echelon form (rref) BB, we can indirectly determine the basis of the column space by matching the columns of BB with those of AA. The columns in AA that correspond to pivot columns in BB form the basis of the column space.

In the rref, we can also directly see the basis of the row space. Each row in the basis of the row space must contain a pivot. The rows that do not contain pivots correspond to the free variables, which determine the dimension of the null space.

Example 1

If AA is 45×5045 \times 50 matrix with a 1010-dimension nullity, what is the rank of AA?

1010-DD nullity means 10 free variables, so the pivots are 5010=4050-10=40, which is also the rank of AA.

Example 2

The matrices below are row equivalent. A=[2116812432781031045704],B=[1243203912120000000000] A=\left[\begin{array}{rrrrr} 2 & -1 & 1 & -6 & 8 \\ 1 & -2 & -4 & 3 & -2 \\ -7 & 8 & 10 & 3 & -10 \\ 4 & -5 & -7 & 0 & 4 \end{array}\right], \quad B=\left[\begin{array}{rrrrr} 1 & -2 & -4 & 3 & -2 \\ 0 & 3 & 9 & -12 & 12 \\ 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 \end{array}\right]

  1. Find rank AA and dim\operatorname{dim} Nul AA

  2. Find bases for Col AA and Row AA.

  3. What is the next step to perform to find a basis for Nul AA ?

  4. How many pivot columns are in a row echelon form of AT?A^{T} ?

  1. rank(A)=2rank(A)=2, because BB has two pivots. And nullity is the number of free variables, there are 3, so dim NulA=3\text{dim Nul}A = 3.

A = sy.Matrix([[2,-1,1,-6,8,0], [1,-2,-4,3,-2,0], [-7,8,10,3,-10,0], [4,-5,-7,0,4,0]]) A.rref()

([102560013440000000000000], (0, 1))\displaystyle \left( \left[\begin{matrix}1 & 0 & 2 & -5 & 6 & 0\\0 & 1 & 3 & -4 & 4 & 0\\0 & 0 & 0 & 0 & 0 & 0\\0 & 0 & 0 & 0 & 0 & 0\end{matrix}\right], \ \left( 0, \ 1\right)\right)

  1. Bases for ColA\text{Col}A is (2,1,7,4)T,(1,2,8,5)T(2,1,-7,4)^T, (-1,-2,8,-5)^T, and for RowA\text{Row}A is (1,2,4,3,2),(0,3,9,12,12)(1,-2,-4,3,-2),(0,3,9,-12,12).

The NulA\text{Nul}A and basis is

[x1x2x3x4x5]=[2x3+5x46x53x3+4x44x5x3x4x5]=x3[23100]+x4[54010]+x5[64001]\left[ \begin{matrix} x_1 \\ x_2 \\ x_3\\x_4 \\x_5 \end{matrix} \right]= \left[ \begin{matrix} -2x_3+5x_4-6x_5 \\ -3x_3+4x_4-4x_5 \\ x_3\\x_4 \\x_5 \end{matrix} \right]= x_3 \left[ \begin{matrix} -2 \\ -3 \\ 1\\0 \\0 \end{matrix} \right]+ x_4 \left[ \begin{matrix} 5 \\ 4 \\ 0\\1 \\0 \end{matrix} \right]+ x_5 \left[ \begin{matrix} -6 \\ -4 \\ 0\\0 \\1 \end{matrix} \right]
  1. Perform rref on augmented AA

  1. Transpose AA then do rref.

A.T.rref()

([102101320000000000000000], (0, 1))\displaystyle \left( \left[\begin{matrix}1 & 0 & -2 & 1\\0 & 1 & -3 & 2\\0 & 0 & 0 & 0\\0 & 0 & 0 & 0\\0 & 0 & 0 & 0\\0 & 0 & 0 & 0\end{matrix}\right], \ \left( 0, \ 1\right)\right)

There are 2 pivot columns.

Actually, we don't need any calculation to know the rank of ATA^T, because

rank(A)=rank(AT)rank(A)=rank(A^T)

Orthogonality of NulA\text{Nul}A and RowA\text{Row}A

NulARowA\text{Nul}A \perp \text{Row}A

Here is the intersting connections of these subspaces we have discussed. Consider

A = sy.Matrix([[5, 8, 2], [10, 16, 4], [3, 4, 1]]);A

[58210164341]\displaystyle \left[\begin{matrix}5 & 8 & 2\\10 & 16 & 4\\3 & 4 & 1\end{matrix}\right]

A.rref()

([1000114000], (0, 1))\displaystyle \left( \left[\begin{matrix}1 & 0 & 0\\0 & 1 & \frac{1}{4}\\0 & 0 & 0\end{matrix}\right], \ \left( 0, \ 1\right)\right)

The basis of row space of AA is (1,0,0)(1, 0, 0) and (0,1,.25)(0, 1, .25).And the RowA\text{Row}A is

RowA=s[100]+t[010.25]\text{Row}A= s\left[ \begin{matrix} 1 \\ 0\\ 0 \end{matrix} \right]+ t\left[ \begin{matrix} 0 \\ 1\\ 0.25 \end{matrix} \right]

The NulA\text{Nul}A is [x1x2x3]=x3[0.251] \left[ \begin{matrix} x_1 \\ x_2\\ x_3 \end{matrix} \right]= x_3 \left[ \begin{matrix} 0 \\ -.25\\ 1 \end{matrix} \right]

Now we can visualize their relations geometrically. Again keep in mind that Matplotlib does not render 3D properly, so you need some imagination as well.

Here is what we observe.

The RowA\text{Row}A is a plane and NulA\text{Nul}A is a line which is perpendicular to the plane (maybe 3D plot not so obvious about it). It is easy to grasp the idea if you notice that in a homogeneous system Ab=0Ab = \mathbf{0}, it breaks down into dot products

Ab=[A1bA2bA3b]=[000]Ab =\left[ \begin{matrix} A_{1\cdot}\cdot b \\ A_{2\cdot}\cdot b\\ A_{3\cdot}\cdot b \end{matrix} \right] = \left[ \begin{matrix} 0 \\ 0 \\ 0 \end{matrix} \right]

where A1,A2,A3A_{1\cdot}, A_{2\cdot}, A_{3\cdot} are the rows of AA. In later chapters we will prove when the dot product of two vectors equals zero, they are geometrically perpendicular.

# Generate the grid s = np.linspace(-1, 1, 10) t = np.linspace(-1, 1, 10) S, T = np.meshgrid(s, t) X = S Y = T Z = T * 0.25 # Create the figure and the 3D axis fig = plt.figure(figsize=(8, 8)) ax = fig.add_subplot(111, projection='3d') # Plot the surface ax.plot_surface(X, Y, Z, alpha=0.9, cmap=plt.cm.coolwarm) # Plot the line x3 = np.linspace(-1, 1, 10) x1 = 0 * x3 x2 = -0.25 * x3 ax.plot(x1, x2, x3, lw=5) # Set axis labels ax.set_xlabel('x-axis', size=18) ax.set_ylabel('y-axis', size=18) ax.set_zlabel('z-axis', size=18) # Set axis limits ax.set_xlim([-1, 1]) ax.set_ylim([-1, 1]) ax.set_zlim([-0.25, 0.25]) # Add text annotations ax.text(1, -1, -0.25, r'$Row\ A$', size=17) ax.text(0, -0.25, 1, r'$Nul\ A$', size=17) # Set the view angle ax.view_init(elev=7, azim=20) # Show the plot plt.show()
Image in a Jupyter notebook

Let me write a summary:

  1. The dimension of the row space is equal to the dimension of the column space (both are the rank of the matrix).

  2. The row space of AA is orthogonal to the null space of AA.

  3. The column space of AA is orthogonal to the left null space of AA.

NulATColA\text{Nul}A^T \perp \text{Col}A

The nullity of ATA^T is

A = sy.Matrix([[5, 8, 2], [10, 16, 4], [3, 4, 1]]);A.T.rref()

([120001000], (0, 2))\displaystyle \left( \left[\begin{matrix}1 & 2 & 0\\0 & 0 & 1\\0 & 0 & 0\end{matrix}\right], \ \left( 0, \ 2\right)\right)

The NulAT\text{Nul}A^T is

[x1x2x3]=x2[210]\left[ \begin{matrix} x_1 \\ x_2\\ x_3 \end{matrix} \right]= x_2 \left[ \begin{matrix} -2 \\ 1\\ 0 \end{matrix} \right]

The ColA\text{Col}A is

A.rref()

([1000114000], (0, 1))\displaystyle \left( \left[\begin{matrix}1 & 0 & 0\\0 & 1 & \frac{1}{4}\\0 & 0 & 0\end{matrix}\right], \ \left( 0, \ 1\right)\right)

ColA=s[5103]+t[8164]\text{Col}A= s\left[ \begin{matrix} 5 \\ 10\\ 3 \end{matrix} \right]+ t\left[ \begin{matrix} 8 \\ 16\\ 4 \end{matrix} \right]

ColA\text{Col}A is a plane and NulAT\text{Nul}A^T is a line perpendicular to the plane. The intuition is similar to NulARowA\text{Nul}A \perp \text{Row}A, here you can think of a system look like bTA=0Tb^TA = \mathbf{0}^T.

%matplotlib inline s = np.linspace(-1, 1, 10) t = np.linspace(-1, 1, 10) S, T = np.meshgrid(s, t) X = 5*S+8*T Y = 10*S+16*T Z = 3*S+4*T fig = plt.figure(figsize = (8,8)) ax = fig.add_subplot(111,projection='3d') ax.plot_surface(X, Y, Z, cmap=plt.cm.coolwarm) x2 = np.linspace(-1, 1, 10) x3 = x2*0 x1 = -2*x2 ax.plot(x1,x2,x3, lw = 3) ax.set_xlabel('x-axis', size = 18) ax.set_ylabel('y-axis', size = 18) ax.set_zlabel('z-axis', size = 18) ax.axis([-1,1,-1,1]) ax.view_init(-67, 35)
Image in a Jupyter notebook

Rank Decomposition

Consider a matrix AA, the purpose is to decompose it into the multiplication of CC, RR, which are the bases of column space and row space respectively.

A=CRA = CR
A = sy.Matrix([[2, 4, 1, -1], [4, 2, -4, 2], [2, -2, -5, 3], [1, 9, -3, 2]]);A

[2411424222531932]\displaystyle \left[\begin{matrix}2 & 4 & 1 & -1\\4 & 2 & -4 & 2\\2 & -2 & -5 & 3\\1 & 9 & -3 & 2\end{matrix}\right]

Arref = A.rref();Arref

([10042101016300143630000], (0, 1, 2))\displaystyle \left( \left[\begin{matrix}1 & 0 & 0 & - \frac{4}{21}\\0 & 1 & 0 & \frac{1}{63}\\0 & 0 & 1 & - \frac{43}{63}\\0 & 0 & 0 & 0\end{matrix}\right], \ \left( 0, \ 1, \ 2\right)\right)

Get the basis of ColA\text{Col}A.

ColA_basis = A[:,:3];ColA_basis

[241424225193]\displaystyle \left[\begin{matrix}2 & 4 & 1\\4 & 2 & -4\\2 & -2 & -5\\1 & 9 & -3\end{matrix}\right]

Then get the RowA\text{Row}A.

RowA_basis = Arref[0][0:3,:];RowA_basis

[1004210101630014363]\displaystyle \left[\begin{matrix}1 & 0 & 0 & - \frac{4}{21}\\0 & 1 & 0 & \frac{1}{63}\\0 & 0 & 1 & - \frac{43}{63}\end{matrix}\right]

Multiply CRCR, we are getting back AA.

ColA_basis*RowA_basis

[2411424222531932]\displaystyle \left[\begin{matrix}2 & 4 & 1 & -1\\4 & 2 & -4 & 2\\2 & -2 & -5 & 3\\1 & 9 & -3 & 2\end{matrix}\right]

Verify if CRCR equals AA.

ColA_basis*RowA_basis == A
True