VB Sym 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
  ' symmetric matrices.
  Module SymFactExample

    Sub Main()

      ' Construct a symmetric matrix as the product of the transpose of a 
      ' matrix with itself.
      Dim Rows As Integer = 5
      Dim Cols As Integer = 5
      Dim Rng As New RandGenUniform(-1, 1)
      Rng.Reset(&H124)
      Dim A As New DoubleMatrix(Rows, Cols, Rng)
      Dim ATA As DoubleMatrix = NMathFunctions.TransposeProduct(A, A)
      Dim S As New DoubleSymmetricMatrix(ATA)

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

      ' S =
      ' 0.791   -0.366  -0.31   0.183   0.863
      ' -0.366  0.224   0.177   -0.312  -0.214
      ' -0.31   0.177   0.49    -0.411  -0.48
      ' 0.183   -0.312  -0.411  2.03    -0.0979
      ' 0.863   -0.214  -0.48   -0.0979 2.01

      ' Construct a symmetric factorization class.
      Dim Fact As New DoubleSymFact(S)

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

      ' Retrieve information about the matrix S.
      Dim Det As Double = Fact.Determinant()

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

      Dim SInv As DoubleSymmetricMatrix = Fact.Inverse()

      Console.WriteLine()
      Console.WriteLine("Determinant of S = " & Det)

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

      Console.WriteLine("S inverse =")
      Console.WriteLine(SInv.ToTabDelimited("G3"))

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

      Console.WriteLine("Solution to Ax = y0 is {0}", X0.ToString("G5"))

      Console.WriteLine()
      Console.WriteLine("y0 - Ax0 = {0}", (Y0 - MatrixFunctions.Product(S, X0)).ToString("G5"))

      Console.WriteLine()
      Console.WriteLine("Solution to Ax = y1 is {0}", X1.ToString("G5"))

      Console.WriteLine()
      Console.WriteLine("y1 - Ax1 = {0}", (Y1 - MatrixFunctions.Product(S, X1)).ToString("G5"))

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

      ' Factor a different matrix.
      Dim B As DoubleSymmetricMatrix = DoubleSymmetricMatrix.Multiply(1.2, S)
      Fact.Factor(B)
      X0 = Fact.Solve(Y0)

      Console.WriteLine("Solution to Bx = y0 is {0}", X0.ToString("G5"))

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

    End Sub
  End Module
End Namespace

← All NMath Code Examples
Top