VB Symmetric Matrix Example

← All NMath Code Examples

 

Imports System

Imports CenterSpace.NMath.Core


Namespace CenterSpace.NMath.Examples.VisualBasic

  A .NET example in Visual Basic demonstrating the features of the symmetric matrix classes.
  Module SymmetricMatrixExample

    Sub Main()

      Dim Order As Integer = 6

      Set up a symmetric matrix S as the transpose product of a general 
      matrix with itself (which is symmetric).
      Dim Rng As New RandGenUniform(-2, 2)
      Rng.Reset(&H124)
      Dim A As New FloatMatrix(Order, Order, Rng)

      Dim S As New FloatSymmetricMatrix(NMathFunctions.TransposeProduct(A, A))

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

      S =
      3.3     0.529   -1.24   0.262   2.76    1.44
      0.529   2.39    -0.864  0.315   1.59    -0.543
      -1.24   -0.864  5.55    4.65    -2.57   -0.0683
      0.262   0.315   4.65    8.96    -4.57   -1.7
      2.76    1.59    -2.57   -4.57   11.3    5.73
      1.44    -0.543  -0.0683 -1.7    5.73    4.4

      Indexer accessor works just like it does for general matrices. 
      Console.WriteLine("S[2,2] = " & S(2, 2))
      Console.WriteLine("S[3,0] = " & S(3, 0))

      You can set the values of elements in a symmetric matrix using the 
      indexer. Note that setting the element in row i and column j implicitly
      sets the element in column j and row i to the same value
      S(2, 1) = 100
      Console.WriteLine("S[2,1] = " & S(2, 1))
      Console.WriteLine("S[1,2] = " & S(1, 2))
      Console.WriteLine()

      Scalar addition/subtractions/multiplication and matrix 
      addition/subtraction are supported.
      Dim Scalar As Single = -0.123F
      Dim C2 As FloatSymmetricMatrix = Scalar * S
      Scalar = 3.0
      Dim C As FloatSymmetricMatrix = Scalar - S
      Dim D As FloatSymmetricMatrix = C2 + S
      Console.WriteLine("D =")
      Console.WriteLine(D.ToTabDelimited("G3"))

      Matrix/vector products too.
      Rng = New RandGenUniform(-1, 1)
      Rng.Reset(&H124)
      Dim X As FloatVector = New FloatVector(S.Cols, Rng)   vector of random deviates
      Dim Y As FloatVector = MatrixFunctions.Product(S, X)
      Console.WriteLine("Sx = {0}", Y.ToString())

      You can transform the elements of a symmetric matrix object by using
      the Transform() method.
      C2.Transform(NMathFunctions.FloatPowFunc, 2)   Square every element of C2
      Console.WriteLine()
      Console.WriteLine("C2^2 =")
      Console.WriteLine(C2.ToTabDelimited("G9"))

      You can also solve linear systems.
      Dim X2 As FloatVector = MatrixFunctions.Solve(S, Y)

      x and x2 should be about the same. Lets look at the l2 norm of 
      their difference.
      Dim Residual As FloatVector = FloatVector.Subtract(X, X2)
      Dim ResidualL2Norm As Single = Math.Sqrt(NMathFunctions.Dot(Residual, Residual))
      Console.Write("||x - x2|| = ")
      Console.WriteLine(ResidualL2Norm)

      Compute condition number.
      Dim RCond As Single = MatrixFunctions.ConditionNumber(S)
      Console.WriteLine()
      Console.Write("Reciprocal condition number = ")
      Console.WriteLine(RCond)

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

    End Sub
  End Module
End Namespace

← All NMath Code Examples
Top