Topics
This technique is applicable to matrices (not limited to square matrices) and decomposes a matrix (A) into an orthogonal matrix (Q) and an upper triangular matrix (R).
The matrix Q has dimensions , and R is an upper triangular matrix. QR decomposition is frequently used to solve systems of linear equations.
import numpy as np
# Example 3×2 matrix (m ≥ n)
A = np.array([[12, -51],
[ 6, 167],
[-4, 24]], dtype=float)
# Compute QR decomposition
Q, R = np.linalg.qr(A)
print("Q (Orthogonal):\n", Q)
print("R (Upper-triangular):\n", R)
# Verify: A == Q @ R
print("Reconstruct:", np.allclose(A, Q @ R))