Implement QR Decomposition using the Gram-Schmidt process. Given a matrix A, decompose it into an orthogonal matrix Q and an upper triangular matrix R such that A = QR.
import numpy as np
def qr_decomposition(A: list[list[float]]) -> tuple:
A = np.array(A, dtype=float)
m, n = A.shape
Q = np.zeros((m, n))
R = np.zeros((n, n))
for j in range(n):
v = A[:, j].copy()
for i in range(j):
R[i, j] = np.dot(Q[:, i], A[:, j])
v = v - R[i, j] * Q[:, i]
R[j, j] = np.linalg.norm(v)
if R[j, j] < 1e-10:
Q[:, j] = 0
else:
Q[:, j] = v / R[j, j]
return Q, Rv = A[:, j].v is orthogonal to all previous columns. Normalize it to get Q[:, j], and store its norm in R[j, j].