Any rectangular matrix can be decomposed to a product of an orthogonal matrix and an upper (right) triangular matrix , as described in [[wp:QR decomposition|QR decomposition]].
'''Task'''
Demonstrate the QR decomposition on the example matrix from the [[wp:QR_decomposition#Example_2|Wikipedia article]]:
::
and the usage for linear least squares problems on the example from [[Polynomial regression]]. The method of [[wp: Householder transformation|Householder reflections]] should be used:
'''Method'''
Multiplying a given vector , for example the first column of matrix , with the Householder matrix , which is given as
::
reflects about a plane given by its normal vector . When the normal vector of the plane is given as
::
then the transformation reflects onto the first standard basis vector
::
which means that all entries but the first become zero. To avoid numerical cancellation errors, we should take the opposite sign of :
::
and normalize with respect to the first element:
::
The equation for thus becomes:
::
or, in another form
::
with
::
Applying on then gives
::
and applying on the matrix zeroes all subdiagonal elements of the first column:
::
In the second step, the second column of , we want to zero all elements but the first two, which means that we have to calculate with the first column of the ''submatrix'' (denoted *), not on the whole second column of .
To get , we then embed the new into an identity:
::
This is how we can, column by column, remove all subdiagonal elements of and thus transform it into .
::
The product of all the Householder matrices , for every column, in reverse order, will then yield the orthogonal matrix .
::
The QR decomposition should then be used to solve linear least squares ([[Multiple regression]]) problems by solving
::
When is not square, i.e. we have to cut off the zero padded bottom rows.
::
and the same for the RHS:
::
Finally, solve the square upper triangular system by back substitution:
::