A basis describes coordinates, but not necessarily lengths or angles. An inner product supplies geometry. Orthogonal coordinates then turn the closest-point problem into a collection of independent one-dimensional problems and produce the QR factorization.
1 Inner products and orthogonality
A real inner product is symmetric, bilinear, and positive definite: for . Its norm is . Orthogonal vectors have inner product zero; an orthonormal list consists of mutually orthogonal unit vectors.
The standard product on is . A weighted product is an inner product when is symmetric positive definite. On , is another: a nonzero polynomial has only finitely many roots, so it is nonzero at some point of ; by continuity its square is positive on a subinterval of , so the integral of its square is positive.
For the real inner-product space just described, the following estimates hold.
Theorem 3.1.1
(Cauchy-Schwarz inequality)
For all vectors ,
Equality holds if and only if and are linearly dependent.
Proof
(The quadratic-polynomial proof)
First suppose . Both sides of Cauchy-Schwarz are then , so equality is immediate; moreover, a pair containing the zero vector is linearly dependent. Thus both the inequality and the stated equality condition hold in this case.
Now suppose and consider the function
Since a norm-square is always nonnegative, for every real . Expanding gives
Because its leading coefficient is , this is a genuine quadratic polynomial in . It is nonnegative for every real , so its discriminant is nonpositive:
Therefore
In the case , equality in Cauchy-Schwarz occurs exactly when the discriminant is zero, equivalently when has a real root. Such a root means for some real , so is a scalar multiple of . Conversely, if and are linearly dependent, then for some real , and direct substitution gives equality. Together with the case , this proves the equality condition, including all cases in which one or both vectors are zero.
Theorem 3.1.2
(Triangle inequality)
For all vectors ,
Equality holds if and only if at least one of and is a nonnegative scalar multiple of the other, including when one vector is zero.
Proof
(The equality case must survive both inequalities)
There are two successive equality gaps to track:
Equality in the first gap controls the sign of the inner product; equality in the second controls linear dependence. The zero-vector cases are kept separate, so no normalization is needed there.
Start with
By Cauchy-Schwarz,
Therefore
Taking square roots gives
For equality, both estimates in the chain must be tight. The first requires , so the inner product is nonnegative. The Cauchy-Schwarz bound is tight exactly when and are linearly dependent. If both vectors are nonzero, write ; then forces . If one vector is zero, equality is immediate, and the zero vector is a nonnegative scalar multiple of the other. Conversely, vectors satisfying this nonnegative-multiple condition give equality by direct substitution. Thus nonzero vectors pointing in opposite directions give equality in Cauchy-Schwarz, but not in the triangle inequality.
Finite basis existence and extension, without a counting assumption
Let be a real vector space supplied with a finite spanning list . First prove that every independent list has length at most . After insertions, maintain a spanning list consisting of and surviving old vectors. Expand in this list. Some coefficient on a surviving old vector is nonzero; otherwise would be in the span of the earlier , contradicting independence. Solve for that old vector and replace it by . The new list spans the same space and keeps every earlier insertion. If , after steps the first independent vectors already span , contradicting independence of . This proves the bound, including .
Every subspace has a finite basis. Start with the empty independent list. If its current span is not , choose one vector of outside that span. A relation with nonzero coefficient on the new vector would express it in the old span, so the enlarged list remains independent. The proved bound permits at most additions; therefore the process stops with an independent spanning list for . If , it stops immediately with the empty basis. Starting instead with any given independent list in proves that list extends to a basis of . Only a bounded finite selection is used. Finally, if two bases have sizes , use each as the finite spanning list in the bound to obtain and . Thus basis size, called dimension, is well-defined. This supplies the finite bases, and their extensions, to which the projection formula and the Gram–Schmidt process below are applied.
2 Projection and the closest point
For an orthonormal basis of a subspace , define
Then is orthogonal to every basis vector and therefore to .
Projection theorem. is the unique point in minimizing .
Proof. For , decompose . These terms are orthogonal, so . The second term vanishes exactly at .
For the line spanned by nonzero , the general real-inner-product formula is . In the standard Euclidean inner product it becomes . For the weighted inner product with real symmetric positive-definite , it instead becomes . For example, , and give , since and . Projection is generally not the componentwise rounding or truncation of .
Counterexample mode
The projection formula needs an orthonormal basis
In take the basis , and let . Since , its projection is itself, yet . The residual is not orthogonal to , so the orthogonality step behind the theorem fails. A basis that is merely independent must first be orthonormalized, as in the Gram–Schmidt process below.
3 Gram–Schmidt and QR
Apply Gram–Schmidt to the independent columns , writing and .
Gram–Schmidt converts an independent list to an orthonormal list spanning the same successive subspaces:
Each is orthogonal to previous and is nonzero because otherwise would lie in the span of its predecessors. A zero or dependent input requires omitting a vector rather than dividing by zero.
Thus , where has orthonormal columns and is upper triangular with for and positive .
Worked example. Take and . Then
and
For , . Solving gives . The prediction is and residual . Its inner product with each original column is zero.
Why does this solve least squares? Split into perpendicular components. Then
The first term can be made zero because is invertible; the second cannot be changed by . In floating-point work, modified Gram–Schmidt or Householder QR is preferred over a naive classical implementation for difficult inputs. This observation is about numerical stability, not a change in the exact formulas.
Proof X-ray
Where orthonormal columns carry the least-squares argument
The displayed identity rests on one fact, used twice: . Since , write . The first vector lies in the column space of , and the second is orthogonal to that space because ; this is where the Pythagorean splitting of the squared norm comes from. The second use removes from the first term: . Full column rank enters only afterwards, through the invertibility of . In the worked example the minimum of is therefore the squared length of the residual, , and no choice of can lower it.
Reflecting one vector onto an axis
A QR factorization can be built by changing the coordinates of all columns at once. The basic move is a reflection that sends a selected vector onto a coordinate axis. Because a reflection preserves Euclidean lengths and inner products, it does not distort the geometry while it creates zeros. We will derive the reflection, check its exceptional cases, and assemble the transformations into a factorization before discussing why one sign choice avoids a particular subtraction of nearby numbers.
For a nonzero vector , define
The denominator is positive because . For any vector , the quantity is its orthogonal projection onto the line spanned by . Thus reverses the component parallel to and leaves every component perpendicular to unchanged: it is reflection across the hyperplane perpendicular to . The formula also proves its matrix properties. The rank-one matrix satisfies and , so
Consequently is orthogonal and its own inverse. For any , the equality follows directly. A reflection is not the same object as a projection: a projection removes the parallel component, whereas a reflection reverses it. Confusing the two would give a noninvertible transformation instead of an orthogonal one.
Choosing the reflector and its sign
Let be nonzero, let be the first standard basis vector, and write . Choose when and when . Set
This convention also specifies what happens when the first component is zero. The first component of has magnitude , so is certainly nonzero. Direct expansion gives . Therefore
The calculation uses exact arithmetic. If , do not form the displayed quotient or choose a sign for a zero norm: take the identity transformation, which already leaves all entries zero. A vector already on the axis is not problematic under this sign convention. For example, a positive multiple of is reflected to its negative; a later sign adjustment can make a QR diagonal positive if that convention is desired.
Why choose this sign? With the opposite target, the first component of the vector defining the reflector would involve subtracting numbers of nearly equal magnitude when the input is already close to the axis. The chosen formula instead adds magnitudes in that component. This is a local cancellation-avoidance observation, not a proof that every rounded implementation is backward stable. Computing a norm itself can overflow or underflow unless the implementation is designed for it; this exact proof supplies neither an error bound nor a complete implementation recipe.
A wrong exceptional-case convention can destroy the construction. If one chooses unconditionally and , then and the reflection formula is undefined. The existence of an obvious axis-aligned answer does not make division by zero valid. Taking the identity is another possible choice in that special case, but any algorithm must state it explicitly rather than silently use the invalid formula.
Build the factorization by blocks
Let with . At the first step apply a reflector to the whole first column, making every entry below its first position zero. At step , form a reflector from the part of column in rows through , and embed it in an identity matrix on the earlier coordinates. This block transformation leaves the first rows unchanged and sends the active tail to the first axis of its smaller coordinate space. If that tail is zero, use the identity block.
Why are earlier columns not spoiled? After step , each earlier column is zero in rows through . The new block acts only on those rows, so it sends that zero tail to zero. Induction therefore yields an upper trapezoidal matrix after all columns have been processed. If the embedded reflectors are , then
Each embedded reflector is orthogonal and symmetric, so reversing the product gives the inverse. The resulting is an orthogonal matrix; is , with zeros below its diagonal positions. This full factorization is valid even when columns are dependent. A zero diagonal certifies that full column rank fails: the leading square triangular block is singular, and all rows below it are zero. But counting nonzero diagonal entries does not necessarily give the rank. For example, has rank one although both diagonal entries vanish. Column ordering and the remaining entries matter.
When has full column rank, the leading triangular block of is invertible. Indeed, if it sent a nonzero coefficient vector to zero, the remaining rows of the upper trapezoidal matrix would also give zero; multiplying by the invertible orthogonal factor would contradict independence of the original columns. Taking the first columns of and the leading block of then gives a thin QR factorization. The signs of its diagonal entries depend on reflector choices. Multiply a column of the thin factor and the corresponding row of its triangular factor by to change a negative diagonal entry without changing the product or orthonormality. This produces the positive-diagonal convention in the full-rank case.
The geometric and computational statements should not be conflated. Orthogonal multiplication preserves the exact least-squares residual norm, and triangular equations can be solved by back substitution when the relevant diagonal is nonzero. But a computed residual and an exact residual differ when arithmetic is rounded. Even a small residual alone need not imply a small forward error. This chapter proves only the exact factorization and points out one avoided subtraction; it does not prove a global stability bound.
Checking an answer in layers
For an exact calculation, check each reflector before multiplying a long chain: its defining vector must be nonzero unless the identity case was selected, and applying it to the intended active tail must produce the claimed zeros. Then verify that the embedded transformation acts as the identity on earlier coordinates. Finally multiply the stated factors and check orthogonality. These checks answer different questions: orthogonal factors alone need not reconstruct the given matrix, while a product that reconstructs the matrix need not have an orthogonal first factor. If a diagonal sign is changed, inspect both the column and the corresponding row so that their two sign changes cancel in the product.
G1 — A single reflector
Construct a reflector sending to a multiple of , using the stated sign convention.
Solution. The norm is , and the first component is positive, so and . Here . The reflector is
Direct multiplication gives . The defining formula shows ; direct dot products of its rows provide an independent check. The target has the same length as the input, as orthogonality requires. The sign choice made the first entry of equal to , rather than a cancellation-prone difference.
G2 — QR with a sign adjustment
For , construct a thin QR factorization with positive diagonal using reflections.
Solution. The first column has norm . Reflecting it using sends it to and sends the second column to . On the remaining two rows, reflect the tail to . The resulting leading triangular block has first diagonal and second diagonal . Reverse the first column of the thin orthogonal factor and the first row of that block. This gives
The columns of have unit length and zero dot product. Multiplication gives the original two columns, and both diagonal entries are positive. Thus the sign adjustment was accounted for in both factors rather than changing the product. The second reflection is not skipped merely because the active tail already lies on its axis: under our chosen convention it reverses that nonzero tail.
4 Graded practice
E1 Foundation. Project onto the line spanned by and find the distance.
E2 Gram–Schmidt. Orthonormalize and in that order.
E3 Least squares. Find the closest vector to in .
E4 Proof. For orthonormal-column , prove satisfies and . Explain the geometric meaning of .
5 Full solutions
S1. The coefficient is , so the projection is . Residual has norm .
S2. . Subtracting its component from the second vector gives with norm , so . Their dot product is zero and their norms are one.
S3. A normal to is . Subtract the normal projection: . It lies in and its residual is perpendicular to , proving optimality.
S4. Transposition gives ; . Thus projects onto the column space. projects onto its orthogonal complement, the residual directions no linear combination of columns can fit.
Summary
Inner products turn coordinates into geometry. Orthogonal projection is a unique closest point, not merely a convenient formula. Gram–Schmidt constructs orthonormal coordinates, and QR uses them to solve independent-column least-squares problems without forming normal equations.