VB Band 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
  ' banded matrices.
  Module BandFactExample

    Sub Main()

      ' Construct a banded matrix with random entries from a data vector
      ' of the appropriate length.
      Dim UpperBandwidth As Integer = 2
      Dim LowerBandwidth As Integer = 1
      Dim Rows As Integer = 7
      Dim Cols As Integer = 7
      Dim Rng As New RandGenUniform(-1, 1)
      Rng.Reset(&H124)
      Dim Data As New FloatVector((LowerBandwidth + UpperBandwidth + 1) * Cols, Rng)
      Dim A As New FloatBandMatrix(Data, Rows, Cols, LowerBandwidth, UpperBandwidth)

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

      ' A =
      ' -0.25   0.182   -0.325  0       0       0       0
      ' 0.576   -0.0443 -0.28   0.22    0       0       0
      ' 0       0.0738  0.56    -0.077  -0.924  0       0
      ' 0       0       0.306   -0.168  0.621   -0.863  0
      ' 0       0       0       -0.259  0.773   0.203   -0.705
      ' 0       0       0       0       0.358   -0.585  0.124
      ' 0       0       0       0       0       0.622   0.87

      ' Construct a band factorization class.
      Dim Fact As New FloatBandFact(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(IsSingularString)

      ' Retrieve information about the matrix A.
      Dim Det As Single = 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 FloatMatrix = Fact.Inverse()

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

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

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

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

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

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

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

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

      ' You can also solve for multiple right-hand sides.
      Dim Y As New FloatMatrix(Y1.Length, 2)
      Y.Col(0)(Slice.All) = Y0
      Y.Col(1)(Slice.All) = Y1
      Dim X As FloatMatrix = 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("G5"))

      ' Factor a different matrix.
      Dim B As FloatBandMatrix = 1.2 * A
      Fact.Factor(B)
      X0 = Fact.Solve(Y0)
      Console.Write("Solution to Bx = y0 is ")
      Console.WriteLine(X0.ToString("G3"))

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

    End Sub
  End Module
End Namespace

← All NMath Code Examples
Top