Build the orthogonal projection matrix onto the column space of a given matrix.
The projection matrix P = A(AᵀA)⁻¹Aᵀ projects any vector onto the column space of A. It is used in least-squares regression: the best-fit solution minimises ||b − Ax|| by projecting b onto Col(A). P is symmetric and idempotent (P² = P).
Projection Matrix
P = A(AᵀA)⁻¹Aᵀ
It maps any vector to its closest point in the column space of A. The result is the orthogonal projection — the residual (b − Pb) is perpendicular to the column space.
Then AᵀA is singular and cannot be inverted. Remove dependent columns first, or use the pseudoinverse.