Extreme Optimization > User's Guide > Vector and Matrix Library > Matrix Decompositions > The QR Decomposition

Extreme Optimization User's Guide

User's Guide

Up: Matrix Decompositions Next: The Cholesky Decomposition Previous: The LU Decomposition Contents

The QR Decomposition

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.

In the Extreme Optimization Mathematics Library for .NET, the QR decomposition is implemented by the QRDecomposition class.

The QRDecompositionclass

The QRDecomposition class represents the QR decomposition of a matrix. It has two constructors. The first variant takes a Matrix as its only parameter.

C# CopyCode imageCopy Code
GeneralMatrix aQR = new GeneralMatrix(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
    });
QRDecomposition qr = new QRDecomposition(aQR);
Visual Basic CopyCode imageCopy Code
Dim aQR As GeneralMatrix = New GeneralMatrix(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 _
    })
Dim qr As QRDecomposition = New QRDecomposition(aQR)

The second constructor takes a GeneralMatrix as its first parameter. The second parameter is a Boolean value that specifies whether the contents of the first parameter should be overwritten by the QR decomposition.

C# CopyCode imageCopy Code
QRDecomposition qr2 = new QRDecomposition(aQR, true);
Visual Basic CopyCode imageCopy Code
Dim qr2 As QRDecomposition = New QRDecomposition(aQR, True)

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 minimal norm solution in the least squares sense. You can also calculate the determinant and the inverse of the base matrix:

C# CopyCode imageCopy Code
GeneralVector bQR = new GeneralVector(1.1, 0.9, 0.6, 0.0,-0.8);
GeneralVector xQR = (GeneralVector)qr.LeastSquaresSolve(bQR);
Console.WriteLine("Ax = b -> x = {0}", xQR);
Console.WriteLine("Inv A = {0}", lu.GetInverse().ToString("F4"));
Console.WriteLine("Det A = {0}", lu.GetDeterminant());
Visual Basic CopyCode imageCopy Code
Dim bQR As GeneralVector = New GeneralVector(1.1, 0.9, 0.6, 0.0, -0.8)
Dim xQR As GeneralVector = CType(qr.LeastSquaresSolve(bQR), GeneralVector)
Console.WriteLine("Ax = b -> x = {0}", xQR)
Console.WriteLine("Inv A = {0}", qr.GetInverse().ToString("F4"))
Console.WriteLine("Det A = {0}", qr.GetDeterminant())

The OrthogonalFactor and UpperTriangularFactor properties return a GeneralMatrix and a TriangularMatrix, respectively, containing the orthogonal matrix, Q , and the upper triangular matrix, R of the decomposition.

C# CopyCode imageCopy Code
Console.WriteLine("Q = {0}", qr.OrthogonalFactor.ToString("F4"));
Console.WriteLine("R = {0}", qr.UpperTriangularFactor.ToString("F4"));
Visual Basic CopyCode imageCopy Code
Console.WriteLine("Q = {0}", qr.OrthogonalFactor.ToString("F4"))
Console.WriteLine("R = {0}", qr.UpperTriangularFactor.ToString("F4"))

The explicit calculation of Q is fairly expensive, and should be avoided unless it is absolutely necessary. Instead, the ApplyQ method lets you multiply a vector or a matrix by Q or its transpose directly.

C# CopyCode imageCopy Code
Console.WriteLine("Qx = {0}", qr.ApplyQ(xQR));
Console.WriteLine("transpose(Q)x = {0}", qr.ApplyQ(TransposeOperation.Transpose, xQR));
Visual Basic CopyCode imageCopy Code
Console.WriteLine("Qx = {0}", qr.ApplyQ(xQR))
Console.WriteLine("transpose(Q)x = {0}", qr.ApplyQ(TransposeOperation.Transpose, xQR))

Up: Matrix Decompositions Next: The Cholesky Decomposition Previous: The LU Decomposition Contents

Overview
Introduction
Features
Documentation
QuickStart Samples
Sample Applications
Downloads
Get it now!
Download trial version
How to Buy
Information
Resources
Contact Us
Search

"The Extreme Optimization Statistics Library for .NET is a major boon for those doing statistical work in .NET. I strongly recommend this product."
- Marc Brooks

"I have made it my mission to institutionalize the value of good API design.  I strongly believe that this is key to making developers more productive and happy on our platform. It is clear that you value good API design in your work, and take to heart developer productivity and synergy with the .NET framework."
- Brad Abrams,
Lead Program Manager, Microsoft.

This is a partial list of companies who are using our libraries:
ABB Robotics
Allstate
Applied Materials
Arcam
Astra Schedule
Babson College
Canadian Council on Learning
Canyon Associates
Caxton Associates
CECity
Constellation Energy
CreditSights
DeepOcean
Duke University
Dynamotive
Elecsoft
Engelhard Corporation
Epcor
Equipoise Software
Galileo International
GAM UK
Gammex
GlaxoSmithKline
Global Matrix
The Hartford
Infinera Corporation
Intel
JDS Uniphase
LaBranche & Co.
Learning & Skills Council
Jacobs Consultancy
Litman Gregory
Lucas Systems
Malvern Instruments
Medrio
Merck & Co.
Mintera.
Monitor Software
MorningStar
NanoString Technologies
Paletta Invent
Parametric Portfolio Associates
Prosanos
RATA Associates
RiskShield
Ramboll
Standard & Poor's
Strategic Analysis Corporation
Univ. of Alicante
Univ. of South Carolina
vielife
Xerox
US Army