NMath User's Guide

TOC |  Previous |  Next |  Index

17.1 QR Decompositions (.NET, C#, CSharp, Visual Basic, VB.NET)

A QR decomposition is a representation of a matrix A of the form:


where P is a permutation matrix, Q is orthogonal, and R is upper trapezoidal (or upper triangular if A has more rows than columns and full rank).

Creating QR Decompositions

NMath provides QR decomposition classes for four datatypes: single- and double-precision floating point numbers, and single- and double-precision complex numbers. The classnames are FloatQRDecomp, DoubleQRDecomp, FloatComplexQRDecomp, and DoubleComplexQRDecomp.

Instances of the QR decomposition classes are constructed from general matrices of the appropriate datatype. For example, this code creates a FloatQRDecomp from a FloatMatrix:

FloatMatrix A =
  new FloatMatrix( "5x3 [ 1 2 3  4 5 6  7 8 9  0 1 2  3 4 5 ]" );
FloatQRDecomp qr = new FloatQRDecomp( A );

By default, pivoting is done so that the entries along the diagonal of R are non-increasing. For greater control, NMath provides QR decomposition server classes that create QR decomposition objects with non-default decomposition parameters. The classnames are FloatQRDecompServer, DoubleQRDecompServer, FloatComplexQRDecompServer, and DoubleComplexQRDecompServer.

The QR decomposition server classes all have the same interface:

For example, this code uses a DoubleComplexQRDecompServer to turn off pivoting:

DoubleComplexQRDecompServer qrs = new 
  DoubleComplexQRDecompServer();
qrs.Pivoting = false;

int rows = 10, cols = 3;
DoubleComplexMatrix A = new DoubleComplexMatrix( rows, cols,
  new RandGenUniform( -1, 1 ) );
DoubleComplexQRDecomp qr = qrs.GetDecomp( A );

This code moves column 7 to the beginning of AP before the computation, and fixes it in place during the computation:

DoubleQRDecompServer qrs = new DoubleQRDecompServer();
qrs.SetIntialColumn( 7 );

int rows = 20, cols = 12;
DoubleMatrix A = new DoubleMatrix( rows, cols,
  new RandGenUniform(-1,1) );
DoubleQRDecomp qr = qrs.GetDecomp( A );

Using QR Decompositions

Once a QR decomposition object has been constructed from a matrix, various read-only properties are provided for retrieving the elements of the decomposition, and for retrieving information about the original matrix:

For example:

int rows = 10, cols = 3;
DoubleMatrix A =
  new DoubleMatrix( rows, cols, new RanGenUniform( 1, -1 ) );

DoubleQRDecomp qr = new DoubleQRDecomp( A );
DoubleMatrix Q = qr.Q;
DoubleMatrix R = qr.R;
DoubleMatrix P = qr.P;

Methods are also provided for manipulating the component matrices P, Q, or R:

These methods are more efficient than retrieving a component matrix using the P, Q, and R properties and manipulating it yourself.

For example:

int rows = 12, cols = 20;
FloatComplexMatrix A = new FloatComplexMatrix( rows, cols, rng );
FloatComplexQRDecomp qr = new FloatComplexQRDecomp( A );
      
FloatComplexVector x = new FloatComplexVector( qr.P.Cols, 1, 1 );
FloatComplexVector y = qr.Px( x );

Reusing QR Decompositions

An existing decomposition object can be reused with another matrix using the Factor() method:

int rows = 10, cols = 3;
RandGenUniform rng = new RandGenUniform( -1, 1 );
FloatMatrix A = new FloatMatrix( rows, cols, rng );

FloatQRDecomp qr = new FloatQRDecomp( A );
FloatMatrix Q1 = qr.Q;
FloatMatrix R1 = qr.R;
FloatMatrix P1 = qr.P;

rows = 7;
cols = 7;
FloatMatrix B = new FloatMatrix( rows, cols, rng );

qr.Factor( B );
FloatMatrix Q2 = qr.Q;
FloatMatrix R2 = qr.R;
FloatMatrix P2 = qr.P;

TOC |  Previous |  Next |  Index