How to Find the Null Space of a Matrix in R

Jesse John Feb 02, 2024
  1. Use nullspace() of the pracma Package to Find the Null Space of a Matrix in R
  2. Check the Result of the nullspace() Function in R
  3. Decimal Elements in the Null Space Basis in R
How to Find the Null Space of a Matrix in R

The null space of matrix A contains all the vectors x, satisfying the equation Ax = 0. This article demonstrates how to find the null space of a matrix in R.

Use nullspace() of the pracma Package to Find the Null Space of a Matrix in R

The Practical Numerical Math Functions package, pracma, provides the nullspace() function to find the null space of a matrix. We need to install this package (one-time task), load it, and then use the nullspace() function.

The function returns another matrix. The columns of this result are linearly independent and span the null space of the matrix.

In other words, they form a basis of the null space of the original matrix. If the null space only contains the zero vector, the function returns NULL.

In the sample code, we will create a 2-row, 5-column matrix. The matrix that the nullspace() function returns will have as many rows as the number of columns in our matrix.

For our example matrix, it will have 5 rows.

Example code:

# Install the pracma package. (One-time task.)
install.packages(pracma)

# Load the pracma package.
library(pracma)

# Create a sample matrix, A.
A = matrix(c(11:20), nrow = 2, byrow = TRUE)
A

# Find the nullspace of the matrix A.
# Here, we save the nullspace as N to check the result.
N = nullspace(A)
N

Output:

> # Create a sample matrix, A.
> A = matrix(c(11:20), nrow = 2, byrow = TRUE)
> A
      [,1] [,2] [,3] [,4] [,5]
[1,]   11   12   13   14   15
[2,]   16   17   18   19   20
> # Find the nullspace of the matrix A.
> # Here, we save the nullspace as N to check the result.
> N = nullspace(A)
> N
            [,1]       [,2]       [,3]
[1,] -0.40544227 -0.3635422 -0.3216421
[2,]  0.04947538  0.3931700  0.7368647
[3,]  0.84401032 -0.1910660 -0.2261424
[4,] -0.21467770  0.6567907 -0.4717409
[5,] -0.27336573 -0.4953525  0.2826606

Check the Result of the nullspace() Function in R

Each column vector of the null space basis must satisfy the equation Ax = 0. Every linear combination of those vectors must also satisfy this equation.

However, we must bear in mind that rounding errors will be present. Elements of the order 1e-10 are almost equal to 0.

Example code:

# Multiply A with a column of N.
A %*% N[,1]

# Multiply A with a linear combination of all columns of N.
A %*% (5*N[,1]+2*N[,2]+3*N[,3])

Output:

> # Multiply A with a column of N.
> A %*% N[,1]
             [,1]
[1,] 5.329071e-15
[2,] 1.776357e-15
>
> # Multiply A with a linear combination of all columns of N.
> A %*% (5*N[,1]+2*N[,2]+3*N[,3])
              [,1]
[1,]  1.776357e-14
[2,] -3.197442e-14

Decimal Elements in the Null Space Basis in R

When we compute the null space manually on small matrices with integer elements, we often get a null space basis with rational number elements.

However, the matrix that R returns is comprised of decimal numbers.

Though not on R, the MathWorks help page for its null space function differentiates between an orthonormal basis and a rational basis for the null space. The description implies that both are equivalent.

It also means that returning an orthonormal basis is numerically more accurate from an algorithmic perspective.

Author: Jesse John
Jesse John avatar Jesse John avatar

Jesse is passionate about data analysis and visualization. He uses the R statistical programming language for all aspects of his work.

Related Article - R Matrix