Imports System Imports CenterSpace.NMath.Core Imports Range = CenterSpace.NMath.Core.Range Namespace CenterSpace.NMath.Examples.VisualBasic A .NET example in Visual Basic showing how to use the LU factorization class to solve linear systems, and to compute matrix inverses, condition numbers and determinants. Module LUFactorizationExampple Sub Main() Dim A As New DoubleMatrix("3x3 [2 1 1 4 1 0 -2 2 1]") Dim lu As New DoubleLUFact(A) Console.WriteLine() Check to see if the input matrix is singular. If (lu.IsSingular) Then Console.WriteLine("Sorry, A is singular.") Return End If A is not singular, so we can look at the LU components. Console.WriteLine("L...") Console.WriteLine(lu.L.ToTabDelimited()) Console.WriteLine("U...") Console.WriteLine(lu.U.ToTabDelimited()) And the permutation matrix P. Console.WriteLine("P...") Console.WriteLine(lu.P.ToTabDelimited()) We can also compute the determinant and condition number. Console.WriteLine("Determinant of A = {0}", lu.Determinant()) We can choose to estimate the condition number (quick), or compute it directly (accurate). For small matrices these are usually the same. Dim estCond As Double = lu.ConditionNumber(NormType.InfinityNorm, True) Dim computedCond As Double = lu.ConditionNumber(NormType.OneNorm, False) Console.WriteLine("Estimated condition number, infinity-norm = {0}", estCond.ToString("F3")) Console.WriteLine("Computed condition number, 1-norm = {0}", computedCond.ToString("F3")) Finally, we can compute the inverse of A. Console.WriteLine() Console.WriteLine("A inverse =") Console.WriteLine(lu.Inverse().ToTabDelimited()) Console.WriteLine() We can use the LU factorization to solve for one right-hand side. Dim v As New DoubleVector("[8 11 3]") Dim u As DoubleVector = lu.Solve(v) Console.WriteLine("The solution, u, Au=v is...") Console.WriteLine(u) Console.WriteLine() Or we can solve for multiple right hand sides. Dim B As New DoubleMatrix(3, 3) Set all the columns of B to be the vector v. Dim i As Integer For i = 0 To 2 B.Col(i)(Range.All) = v Next Now solve for AX=B. The columns of A should have the same values as the vector u from above. Dim X As DoubleMatrix = lu.Solve(B) Console.WriteLine("The solution, X, to AX=B is...") Console.WriteLine(X.ToTabDelimited()) Console.WriteLine() Console.WriteLine("Press Enter Key") Console.Read() End Sub End Module End Namespace← All NMath Code Examples