NMath User's Guide

TOC | Previous | Next | Index

7.3 Using LU Factorizations (.NET, C#, CSharp, VB, Visual Basic, F#)

Once an LU factorization is constructed from a matrix (see Section 7.2), it can be reused to solve for different right hand sides, to compute inverses, to compute condition numbers, and so on.

Component Matrices

Read-only properties provide access to the component matrices of the LU factorization:

P gets the permutation matrix.

L gets the lower triangular matrix.

U gets the upper triangular matrix.

Pivots gets an array of pivot indices, where row i was interchanged with Pivots[i].

Solving for Right-Hand Sides

You can use an LU factorization to solve for right-hand sides using the Solve() method. For instance, this code solves for one right-hand side.:

Code Example – C# LU factorization

var A = new DoubleMatrix( "3x3 [2 1 1 4 1 0 -2 2 1]" );
var lu = new DoubleLUFact( A );
 
var v = new DoubleVector( "[8 11 3]" );
DoubleVector x = lu.Solve( v );

Code Example – VB LU factorization

Dim A As New DoubleMatrix("3x3 [2 1 1 4 1 0 -2 2 1]")
Dim LU As New DoubleLUFact(A)

Dim V As New DoubleVector("[8 11 3]")
Dim X As DoubleVector = LU.Solve(V)

The returned vector x is the solution to the linear system Ax = v. Note that the length of vector v must be equal to the number of rows in the factored matrix A or a MismatchedSizeException is thrown. (See Section 38.1.)

Similarly, you can use the Solve() method to solve for multiple right-hand sides:

Code Example – C# LU factorization

var A = new FloatMatrix( "3x3 [2 1 1  4 1 0 -2 2 1]" );
var lu = new FloatLUFact( A );

var B = new FloatMatrix( "3x2[8 3  11 11  3 8]" );
FloatMatrix X = fact.Solve( B );

Code Example – VB LU factorization

Dim A As New FloatMatrix("3x3 [2 1 1  4 1 0 -2 2 1]")
Dim LU As New FloatLUFact(A)

Dim B As New FloatMatrix("3x2[8 3  11 11  3 8]")
Dim X As FloatMatrix = Fact.Solve(B)

The returned matrix X is the solution to the linear system AX= B. That is, the right-hand sides are the columns of B, and the solutions are the columns of X. Matrix B must have the same number of rows as the factored matrix A.

SolveInPlace() methods are also provided which place the solution in the given vector or matrix, without allocating new memory. The given right-hand side data must have unit stride.

Computing Inverses, Determinants, and Condition Numbers

You can use an LU factorization to compute inverses using the Inverse() method, and determinants using the Determinant() method. For example:

Code Example – C# LU factorization

var A = new FloatMatrix( "3x3 [2 1 1  4 1 0 -2 2 1]" );
var lu = new FloatLUFact( A );

FloatMatrix AInv = lu.Inverse();
float ADet = lu.Determinant();

Code Example – VB LU factorization

Dim A As New FloatMatrix("3x3 [2 1 1  4 1 0 -2 2 1]")
Dim LU As New FloatLUFact(A)

Dim AInv As FloatMatrix = LU.Inverse()
Dim ADet As Single = LU.Determinant()

The ConditionNumber() method computes the condition number in a specified norm type. The condition number of a matrix A is:

kappa = ||A|| ||AInv||

where AInv is the inverse of the matrix A.

NOTE—The ConditionNumber() method returns the reciprocal of the condition num­ber, rho, where rho = 1/kappa.

The provided NormType enumeration contains values for specifying the matrix norm. You can also choose to estimate the condition number, which is faster but less accurate, or to compute it directly. For small matrices, the results are usually the same. Thus, this code estimates the condition number in the infinity-norm:

Code Example – C# LU factorization

var A = new DoubleMatrix( "3x3 [2 1 1   4 3 3   8 7 9 ]" );
var lu = new DoubleLUFact( A );

double AEstimatedConditionNum =
  lu.ConditionNumber( NormType.InfinityNorm, true );

Code Example – VB LU factorization

Dim A As New DoubleMatrix("3x3 [2 1 1   4 3 3   8 7 9 ]")
Dim LU As New DoubleLUFact(A)

Dim AEstimatedConditionNum As Double = 
  LU.ConditionNumber(NormType.InfinityNorm, True)

This code computes the condition number directly in the 1-norm:

Code Example – C# LU factorization

double AComputedConditonNum = 
   lu.ConditionNumber( NormType.OneNorm, false );

Code Example – VB LU factorization

Dim AComputedConditonNum As Double = 
  LU.ConditionNumber(NormType.OneNorm, False)

Top

Top