Task: Compute the Column Space of a Matrix
In this task, you are required to implement a function matrix_image(A)
that calculates the column space of a given matrix A
. The column space, also known as the image or span, consists of all linear combinations of the columns of A
. To find this, you'll use concepts from linear algebra, focusing on identifying independent columns that span the matrix's image. Your task: Implement the function matrix_image(A)
to return the basis vectors that span the column space of A
. These vectors should be extracted from the original matrix and correspond to the independent columns.
Example:
Input:
matrix = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) print(matrix_image(matrix))
Output:
# [[1, 2], # [4, 5], # [7, 8]]
Reasoning:
The column space of the matrix is spanned by the independent columns [1, 2], [4, 5], and [7, 8]. These columns form the basis vectors that represent the image of the matrix.
import numpy as np
def matrix_image(A):
"""
Compute the column space (image) of a matrix A.
Args:
A: Input matrix as a numpy array
Returns:
A numpy array containing the basis vectors of the column space as columns
"""
# Make a copy to avoid modifying the original
A_copy = A.copy()
# Convert to floating point for numerical stability
A_copy = A_copy.astype(float)
# Get dimensions
m, n = A_copy.shape
# Step 1: Compute the reduced row echelon form (RREF)
# First transpose A to work with columns
A_T = A_copy.T
# Number of rows and columns in A_T
rows, cols = A_T.shape
# Initialize pivot positions
pivot_positions = []
# Gaussian elimination to get row echelon form
r = 0 # current row
for c in range(cols):
# Find the pivot row
pivot_row = -1
for i in range(r, rows):
if abs(A_T[i, c]) > 1e-10: # Numerical tolerance for zero
pivot_row = i
break
if pivot_row == -1:
# No pivot in this column, move to the next column
continue
# Swap the pivot row with the current row
if pivot_row != r:
A_T[[r, pivot_row]] = A_T[[pivot_row, r]]
# Normalize the pivot row
pivot = A_T[r, c]
A_T[r] = A_T[r] / pivot
# Eliminate other rows
for i in range(rows):
if i != r:
A_T[i] = A_T[i] - A_T[i, c] * A_T[r]
# Record the pivot position
pivot_positions.append(c)
r += 1
if r == rows:
# No more rows to process
break
# Step 2: Extract linearly independent columns from the original matrix
# The pivot columns in the row echelon form correspond to linearly independent columns in the original matrix
basis = A[:, pivot_positions]
return basis
Test Results3/3
官方题解
import numpy as np
def rref(A):
# Convert to float for division operations
A = A.astype(np.float32)
n, m = A.shape
for i in range(n):
if A[i, i] == 0:
nonzero_current_row = np.nonzero(A[i:, i])[0] + i
if len(nonzero_current_row) == 0:
continue
A[[i, nonzero_current_row[0]]] = A[[nonzero_current_row[0], i]]
A[i] = A[i] / A[i, i]
for j in range(n):
if i != j:
A[j] -= A[i] * A[j, i]
return A
def find_pivot_columns(A):
n, m = A.shape
pivot_columns = []
for i in range(n):
nonzero = np.nonzero(A[i, :])[0]
if len(nonzero) != 0:
pivot_columns.append(nonzero[0])
return pivot_columns
def matrix_image(A):
# Find the RREF of the matrix
Arref = rref(A)
# Find the pivot columns
pivot_columns = find_pivot_columns(Arref)
# Extract the pivot columns from the original matrix
image_basis = A[:, pivot_columns]
return image_basis