The QR Decomposition | Extreme Optimization Numerical Libraries for .NET Professional |

The QR decomposition or QR factorization of a matrix expresses the matrix as the product of an orthogonal matrix and an upper triangular matrix. The matrix can be of any type, and of any shape. The QR decomposition is usually written as

A = QR,

where Q is a square, orthogonal matrix, and R is an upper-triangular matrix. The QR algorithm uses orthogonal transformations. This gives the QR decomposition much better numerical stability than the LU decomposition, even though the computation takes twice as long. When the matrix is ill-conditioned, or high accuracy is required, the longer running time is justified.

The QR decomposition is implemented by the
QRDecomposition

var A = Matrix.Create(5, 3, new double[] { 2.0, 2.0, 1.6, 2.0, 1.2, 2.5, 2.5,-0.4,-0.5,-0.3, 2.5, 2.5, 2.8, 0.5,-2.9 }, MatrixElementOrder.ColumnMajor); var qr = A.GetQRDecomposition();

The Decompose method performs the actual decomposition. This method copies the matrix if necessary. It then calls the appropriate LAPACK routine to perform the actual decomposition. This method is called by other methods as needed. You will rarely need to call it explicitly.

Once the decomposition is computed, a number of operations can be performed in much less time. You can repeatedly solve a system of simultaneous linear equations with different right-hand sides. If the system is overdetermined, you can use the LeastSquaresSolve method to obtain a least squares solution to an over-determined system. If the matrix is square, you can also calculate the determinant and the inverse of the base matrix:

The OrthogonalFactor
property returns orthogonal matrix, Q, of the decomposition.
The UpperTriangularFactor
property returns a TriangularMatrix

var Q = qr.OrthogonalFactor; var R = qr.UpperTriangularFactor; var R2 = qr.TrimmedUpperTriangularFactor;

The matrix Q is kept in a compact format. Although it is possible to convert it to a regular dense matrix, the calculation is fairly expensive. Fortunately, this is hardly ever necessary. Operations like transposition, multiplication, and solving (which is equivalent to multiplying by the transpose for an orthogonal matrix) avoid the conversion:

Copyright Â© 2004-20116,
Extreme Optimization. All rights reserved.

*Extreme Optimization,* *Complexity made simple*, *M#*, and *M
Sharp* are trademarks of ExoAnalytics Inc.

*Microsoft*, *Visual C#, Visual Basic, Visual Studio*, *Visual
Studio.NET*, and the *Optimized for Visual Studio* logo

are
registered trademarks of Microsoft Corporation.