Gram-Schmidt Orthogonalization
Convert any linearly independent vectors into an orthonormal basis using the Gram-Schmidt process. Supports 2D–4D with full step-by-step projection working.
Input vector v1
Input vector v2
Why Orthonormal Bases Matter
An orthonormal basis makes everything simpler. Projections become just dot products. Coordinates are found by simple dot products (no matrix inversion). Distances and angles are preserved. Numerical computations are more stable.
Applications
- QR Decomposition: Q has orthonormal columns; R captures the original coordinates in the new basis. Used in least-squares solvers and eigenvalue algorithms (QR algorithm).
- Least Squares: Finding the best-fit line/plane/curve uses projection onto the column space — Gram-Schmidt finds an orthonormal basis for that space.
- Signal Processing: Fourier series is Gram-Schmidt applied to {1, cos(x), sin(x), cos(2x), ...} with an inner product of integration.
- Quantum Mechanics: State vectors in a Hilbert space are orthonormal; operators have eigenstates that form orthonormal bases.
Frequently Asked Questions
Is Gram-Schmidt numerically stable?
Classical Gram-Schmidt (as implemented here) can accumulate floating-point errors for nearly-dependent vectors. In practice, Modified Gram-Schmidt (MGS) and Householder reflections are preferred for numerical stability. MATLAB's qr() uses Householder; scipy.linalg.qr() defaults to LAPACK. For educational purposes and well-conditioned vectors, classical Gram-Schmidt is accurate.
Does the output depend on the order of input vectors?
Yes — the orthonormal basis produced depends on the order of the input vectors. The first output vector is always the normalization of the first input. Different orderings give different (but equally valid) orthonormal bases for the same subspace. If you want a specific orientation, choose your input order accordingly.