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.
class
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# | Copy 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 | Copy 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# | Copy Code |
QRDecomposition qr2 = new QRDecomposition(aQR, true); |
| Visual Basic | Copy 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# | Copy 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 | Copy 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# | Copy Code |
Console.WriteLine("Q = {0}", qr.OrthogonalFactor.ToString("F4"));
Console.WriteLine("R = {0}", qr.UpperTriangularFactor.ToString("F4")); |
| Visual Basic | Copy 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# | Copy Code |
Console.WriteLine("Qx = {0}", qr.ApplyQ(xQR));
Console.WriteLine("transpose(Q)x = {0}", qr.ApplyQ(TransposeOperation.Transpose, xQR)); |
| Visual Basic | Copy 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
Copyright 2004-2008,
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 Visual Studio Logo are registered trademarks of Microsoft Corporation