VB Tri Diag Fact Example

← All NMath Code Examples

 

Imports System

Imports CenterSpace.NMath.Core
Imports CenterSpace.NMath.Matrix

Namespace CenterSpace.NMath.Matrix.Examples.VisualBasic

  ' A .NET example in Visual Basic demonstrating the features of the factorization classes for
  ' tridiagonal matrices.
  Module TriDiagFactExample

    Sub Main()

      ' Construct a tridiagonal matrix with random entries.
      Dim Rows As Integer = 5
      Dim Cols As Integer = 5
      Dim Rng As New RandGenUniform(-1, 1)
      Rng.Reset(&H124)
      Dim Data1 As New FloatComplexVector(Cols, Rng)
      Dim Data2 As New FloatComplexVector(Cols - 1, Rng)
      Dim Data3 As New FloatComplexVector(Cols - 1, Rng)
      Dim A As New FloatComplexTriDiagMatrix(Rows, Cols)
      A.Diagonal()(Slice.All) = Data1
      A.Diagonal(1)(Slice.All) = Data2
      A.Diagonal(-1)(Slice.All) = Data3

      Console.WriteLine()
      Console.WriteLine("A =")
      Console.WriteLine(A.ToTabDelimited("F3"))

      ' A =
      ' (-0.497,0.332)  (0.560,0.306)   (0.000,0.000)   (0.000,0.000)   (0.000,0.000)
      ' (0.773,0.358)   (-0.250,0.576)  (0.220,-0.077)  (0.000,0.000)   (0.000,0.000)
      ' (0.000,0.000)   (-0.863,0.203)  (0.196,0.182)   (-0.168,-0.259) (0.000,0.000)
      ' (0.000,0.000)   (0.000,0.000)   (-0.585,0.622)  (-0.044,0.074)  (-0.924,0.621)
      ' (0.000,0.000)   (0.000,0.000)   (0.000,0.000)   (-0.705,0.124)  (-0.325,-0.280)

      ' Construct a tridiagonal factorization class.
      Dim Fact As New FloatComplexTriDiagFact(A)

      ' Check to see if A is singular.
      Dim IsSingularString As String
      If Fact.IsSingular Then
        IsSingularString = "A is singular"
      Else
        IsSingularString = "A is NOT singular"
      End If
      Console.WriteLine()
      Console.WriteLine(IsSingularString)

      ' Retrieve information about the matrix A.
      Dim Det As FloatComplex = Fact.Determinant()

      ' In order to get condition number, factor with estimateCondition = True
      Fact.Factor(A, True)
      Dim RCond As Single = Fact.ConditionNumber()

      Dim AInv As FloatComplexMatrix = Fact.Inverse()

      Console.WriteLine()
      Console.Write("Determinant of A = " & Det.ToString())

      Console.WriteLine()
      Console.Write("Reciprocal condition number = " & RCond)

      Console.WriteLine()
      Console.WriteLine()
      Console.WriteLine("A inverse =")
      Console.WriteLine(AInv.ToTabDelimited("F3"))

      ' Use the factorization to solve some linear systems Ax = y.
      Dim Y0 As New FloatComplexVector(Fact.Cols, Rng)
      Dim Y1 As New FloatComplexVector(Fact.Cols, Rng)
      Dim X0 As FloatComplexVector = Fact.Solve(Y0)
      Dim X1 As FloatComplexVector = Fact.Solve(Y1)

      Console.WriteLine()
      Console.Write("Solution to Ax = y0 is " & X0.ToString("G5"))

      Console.WriteLine()
      Console.Write("y0 - Ax0 = ")
      Console.WriteLine(FloatComplexVector.Subtract(Y0, MatrixFunctions.Product(A, X0)).ToString("G5"))

      Console.WriteLine()
      Console.Write("Solution to Ax = y1 is " & X1.ToString("G5"))

      Console.WriteLine()
      Console.Write("y1 - Ax1 = ")
      Console.WriteLine(FloatComplexVector.Subtract(Y1, MatrixFunctions.Product(A, X1)).ToString("G5"))

      ' You can also solve for multiple right-hand sides.
      Dim Y As New FloatComplexMatrix(Y1.Length, 2)
      Y.Col(0)(Slice.All) = Y0
      Y.Col(1)(Slice.All) = Y1
      Dim X As FloatComplexMatrix = Fact.Solve(Y)

      ' The first column of X should be x0 the second column should be x1.
      Console.WriteLine()
      Console.WriteLine("X =")
      Console.WriteLine(X.ToTabDelimited("G7"))

      ' Factor a different matrix.
      Dim Z As New FloatComplex(1.23F, -0.76F)
      Dim B As FloatComplexTriDiagMatrix = Z * A
      Fact.Factor(B)
      X0 = Fact.Solve(Y0)

      Console.Write("Solution to Bx = y0 is " & X0.ToString("G5"))
      Console.WriteLine()

      Console.WriteLine()
      Console.WriteLine("Press Enter Key")
      Console.Read()

    End Sub
  End Module
End Namespace

← All NMath Code Examples
Top