Julia find basis of column space of matrix

When working with matrices in Julia, it is often necessary to find the basis of the column space. The column space of a matrix is the span of its column vectors, and finding its basis can provide valuable insights into the structure and properties of the matrix.

Option 1: Using the nullspace function

One way to find the basis of the column space in Julia is by using the nullspace function from the LinearAlgebra package. This function returns a basis for the nullspace of a matrix, which is the orthogonal complement of the column space.


using LinearAlgebra

# Define the matrix
A = [1 2 3; 4 5 6; 7 8 9]

# Find the basis of the column space
basis = nullspace(A)

In this example, the matrix A is defined as a 3×3 matrix. The nullspace function is then used to find the basis of its column space. The resulting basis is stored in the variable ‘basis’.

Option 2: Using the qr function

Another way to find the basis of the column space is by using the qr function from the LinearAlgebra package. This function performs a QR decomposition of a matrix, which can be used to find the basis of its column space.


using LinearAlgebra

# Define the matrix
A = [1 2 3; 4 5 6; 7 8 9]

# Perform QR decomposition
Q, R = qr(A)

# Find the basis of the column space
basis = Q[:, 1:size(R, 2)]

In this example, the matrix A is defined as a 3×3 matrix. The qr function is then used to perform a QR decomposition of A, which returns the orthogonal matrix Q and the upper triangular matrix R. The basis of the column space is obtained by selecting the first ‘size(R, 2)’ columns of Q.

Option 3: Using the svd function

A third way to find the basis of the column space is by using the svd function from the LinearAlgebra package. This function performs a singular value decomposition of a matrix, which can also be used to find the basis of its column space.


using LinearAlgebra

# Define the matrix
A = [1 2 3; 4 5 6; 7 8 9]

# Perform singular value decomposition
U, Σ, V = svd(A)

# Find the basis of the column space
basis = U[:, 1:size(Σ, 1)]

In this example, the matrix A is defined as a 3×3 matrix. The svd function is then used to perform a singular value decomposition of A, which returns the orthogonal matrices U and V, and the diagonal matrix Σ. The basis of the column space is obtained by selecting the first ‘size(Σ, 1)’ columns of U.

After considering these three options, it is clear that the best approach depends on the specific problem at hand. The nullspace function is a straightforward and efficient way to find the basis of the column space, but it requires the installation of the LinearAlgebra package. On the other hand, the qr and svd functions are built-in Julia functions that can be used without any additional packages. The qr function provides a QR decomposition, which can be useful in other contexts as well. The svd function, on the other hand, performs a singular value decomposition, which can provide additional insights into the matrix. Therefore, the best option ultimately depends on the specific requirements and constraints of the problem.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents