Choose the target subspace
Orthogonal projection approximates x by a vector p in a chosen subspace, with residual x−p perpendicular to every direction in that subspace. The discarded part is not assumed to vanish. Projecting onto the horizontal axis removes the vertical component, but this coordinate-copying rule is a special case of an orthogonal coordinate system. Specify both the target subspace and the inner product. A tilted line requires a different calculation. Orthogonality is defined relative to the chosen geometry; changing feature scaling or using a weighted inner product can change the resulting projection even when the raw coordinates look unchanged.
Project onto a line with a dot product
For a nonzero vector u spanning a line, the projection is u times the dot product u transpose x divided by u transpose u. With u=(1,1) and x=(2,0), the coefficient is one, giving p=(1,1). The residual (1,−1) has dot product zero with u. Omitting the denominator is valid only when u has unit length. Rescaling u should not change the projected point because it leaves the line unchanged. This provides a useful implementation check: the formula must depend on the subspace rather than on an arbitrary magnitude chosen for its spanning vector.
Explain the least-squares property
For another candidate q in the subspace, x−q is the sum of the orthogonal residual x−p and the in-subspace difference p−q. The squared Euclidean distance is therefore the sum of their squared lengths. Moving away from p cannot reduce it. This explains the least-squares property rather than merely asserting that a drawn perpendicular looks shortest. The residual must be orthogonal to the whole subspace, not only one arbitrarily selected direction. The result is tied to the Euclidean geometry or specified inner product. Minimizing another norm is a different optimization problem and need not use the same projection formula.
Extend the calculation to several columns
For a matrix X with independent columns, projection onto its column space leads to the normal equations X transpose X c = X transpose x. An inverse formula exists algebraically, but explicitly forming the inverse is not generally the preferred numerical method. QR or SVD can provide a more reliable computation. Dependent columns make X transpose X singular, invalidating that inverse formula. Nevertheless, the orthogonal projection onto the specified subspace remains unique. Distinguish uniqueness of the coefficient representation from uniqueness of the projected vector: several coefficient vectors can represent the same point when the columns are dependent.
Check symmetry and idempotence
For the standard real inner product, an orthogonal projection matrix is symmetric and idempotent: P transpose equals P and P squared equals P. Applying it again does not change a point already in the subspace. Idempotence alone permits nonorthogonal projections, so it is not sufficient to establish orthogonality. Numerically check residual orthogonality and repeated application using tolerances suited to the data scale. Floating-point arithmetic need not produce exact symbolic equalities. If projection is used for dimension reduction, also evaluate the downstream meaning of discarded directions; geometric least-squares optimality does not establish that those directions are irrelevant to prediction.
Apply a projection twice
For projection onto the horizontal axis, P=diag(1,0) maps (2,3) to (2,0). Applying it again leaves the result unchanged, and the residual (0,3) has zero dot product with (1,0). Combining membership, repeated application, and residual orthogonality checks tests the geometric contract. No single convenient coordinate example proves a general implementation correct, but small exact cases help expose transposition, normalization, and dimension mistakes before larger numerical experiments.