Linear Systems Solvers
Choose a solver below
Solving Systems
Decompositions
Vector Spaces
Need Help?
Check the educational content below each solver for detailed explanations.
View all tutorials →QR Decomposition Calculator: Gram-Schmidt Orthogonalization
Perform QR Decomposition of a matrix A into orthogonal matrix Q and upper triangular matrix R: A = QR. Optionally solve the linear system Ax = b using the QR factorization.
Calculator
Enter your matrix below and click "Calculate" to see the step-by-step solution.
Enter a matrix A to compute its QR decomposition A = QR.
Optionally provide vector b to solve the system Ax = b.
Learn About Qr_Decomposition
Understanding the concepts behind the calculations.
📑 Quick Navigation
What is QR Decomposition?
QR decomposition (or QR factorization) factors a matrix A into the product of an orthogonal matrix Q and an upper triangular matrix R:
Where:
- Q is an m×n matrix with orthonormal columns (
QᵀQ = I) - R is an n×n upper triangular matrix (all entries below diagonal are zero)
Key Insight: QR decomposition orthogonalizes the columns of A, transforming them into a set of orthonormal basis vectors. This is the foundation of many numerical algorithms.
Visual Representation
The Gram-Schmidt Process
The most common method for computing QR decomposition is the Gram-Schmidt orthogonalization process. For a matrix A = [a₁, a₂, ..., aₙ]:
Step 1: Orthogonalization
For each column j, subtract its projections onto all previous orthogonal vectors:
Step 2: Normalization
Convert orthogonal vectors to orthonormal by dividing by their length:
Step 3: Build R Matrix
The entries of R come from the projections:
💡 Intuition: Think of Gram-Schmidt as progressively "stripping away" components of previous vectors, leaving only the new orthogonal direction.
Complete Example
Problem: Find QR decomposition of A = [[1, 1, 0], [1, 0, 1], [0, 1, 1]]
Step 1: First column
Step 2: Second column
Step 3: Third column
Step 4: Build R
✓ Verification: A = QR — multiply Q and R to confirm you recover the original matrix!
Solving Systems with QR
QR decomposition provides an efficient way to solve linear systems, especially when A is not square:
For Square Systems (Ax = b)
- Decompose:
A = QR - Multiply both sides by Qᵀ:
QᵀAx = Qᵀb → Rx = Qᵀb(since QᵀQ = I) - Back substitute to solve the upper triangular system
Rx = Qᵀb
For Overdetermined Systems (Least Squares)
When solving Ax = b with more equations than unknowns, QR gives the least squares solution:
This minimizes ‖Ax - b‖₂ (the sum of squared errors).
Why QR for least squares? Unlike normal equations (AᵀAx = Aᵀb), QR doesn't square the condition number, making it more stable for ill-conditioned problems.
Applications
📊 Least Squares Regression
Finding the best-fit line/curve when data exceeds parameters. QR is the preferred numerical method.
📈 Principal Component Analysis (PCA)
QR is used in the first step of many PCA algorithms for dimensionality reduction.
🔢 QR Algorithm for Eigenvalues
The most widely used algorithm for computing all eigenvalues of a matrix (Francis 1959-1961).
📡 Signal Processing
Adaptive filtering, subspace tracking, and array processing applications.
🔄 Recursive Least Squares
QR updates for real-time parameter estimation in control systems.
🔬 Scientific Computing
Solving linear systems, computing matrix ranks, and orthonormal bases.
QR vs LU: Which to Use?
| Feature | QR Decomposition | LU Decomposition |
|---|---|---|
| Numerical Stability | ✓ Very stable (even without pivoting) | Requires pivoting |
| Non-square matrices | ✓ Works for m×n (any shape) | ✗ Requires square matrices |
| Orthogonal Q | ✓ QᵀQ = I (preserves length) | ✗ No orthogonal factor |
| Best for | Least squares, eigenvalues, ill-conditioned problems | Solving square systems, multiple right-hand sides |
| Computational cost (n×n) | ≈ 2n³/3 operations | ≈ 2n³/3 operations |
Recommendation: Use QR for least squares and ill-conditioned problems. Use LU for solving multiple square systems with the same A (more efficient when factoring once).
Variants & Numerical Stability
Three Main Algorithms
- Classical Gram-Schmidt (CGS) - Simple but numerically unstable (loses orthogonality)
- Modified Gram-Schmidt (MGS) - More stable, standard for small to medium matrices
- Householder Reflections - Most stable, preferred for large or ill-conditioned matrices
- Givens Rotations - Good for sparse matrices or when updating QR
Stability Comparison
This means QR preserves the condition number, while normal equations square it—potentially causing catastrophic rounding errors.
⚠️ Warning: For very ill-conditioned matrices (κ(A) > 10¹⁰), even QR may struggle. Consider using SVD or specialized algorithms.
Frequently Asked Questions
Q: Is QR decomposition unique?
A: For full-rank matrices, Q and R are unique if we require positive diagonal entries in R. Otherwise, signs can vary (multiplying Q by -1 and R by -1 gives another decomposition).
Q: Why is QR better than normal equations for least squares?
A: Normal equations compute AᵀA, which squares the condition number, causing loss of precision. QR avoids this by working directly with A.
Q: Can QR handle rank-deficient matrices?
A: Basic QR assumes full column rank. For rank-deficient matrices, use QR with column pivoting or SVD instead.
Q: How does QR compare to SVD?
A: SVD is more general (handles rank deficiency, gives singular values) but slower. QR is faster and sufficient for most least squares problems.
Q: Why is Q orthogonal?
A: Orthogonality preserves vector lengths (‖Qx‖ = ‖x‖). This is crucial for numerical stability and geometric interpretations.
Try It Yourself!
Use the calculator above to compute QR decompositions:
- Enter your matrix A (up to 4×4)
- Click "Calculate" to see:
- The orthogonal matrix Q (with orthonormal columns)
- The upper triangular matrix R
- Verification that A = QR
- Step-by-step Gram-Schmidt process
📐 Try these examples:
- 2×2:
[[3, 1], [1, 2]]- Simple QR decomposition - 3×3:
[[1, 1, 0], [1, 0, 1], [0, 1, 1]]- The example from this guide - Rectangular (overdetermined):
[[1, 1], [1, 2], [1, 3]]- Watch Q be m×n!
💡 Pro Tip: For least squares problems, QR is the gold standard. Use it instead of normal equations for better numerical accuracy!