VB.NET Band Fact Example

[TOC]

Imports System

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

Namespace CenterSpace.NMath.Matrix.Examples.VisualBasic

  ' A .NET example in VB.NET 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.Write("A = ")
      Console.WriteLine(A.ToString())
      Console.WriteLine()

      ' A = 1 2 7x7 [ -0.250345   0.1820724  -0.3249055  0           0           0          0
      '                0.5763928 -0.04433365 -0.2796601  0.2204346   0           0          0 
      '                0          0.07382878  0.5600654 -0.07704575 -0.9237711   0          0  
      '                0          0           0.306021  -0.1677762   0.6205534  -0.8629085  0 
      '                0          0           0         -0.2589606   0.7733703   0.2029475 -0.7045102  
      '                0          0           0          0           0.3579623  -0.5847561  0.1235993 
      '                0          0           0          0           0           0.6222407  0.870272 ]

      ' 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()
      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.Write("A inverse = ")
      Console.WriteLine(AInv.ToString())
      Console.WriteLine()

      ' 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())
      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())

      ' 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.Write("X = ")
      Console.WriteLine(X.ToString())

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

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

    End Sub
  End Module
End Namespace

[TOC]