VB.NET Hermitian Matrix 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 Hermitian matrix classes.
  Module HermitianMatrixExample

    Sub Main()

      Dim Order As Integer = 5
      Dim NumberFormatString As String = "F4" ' Format number strings as fixed, 4 digits.

      ' Set up a Hermitian matrix S as the conjugate transpose product of a general 
      ' matrix with itself (which is Hermitian).
      Dim Rng As New RandGenUniform(-1, 1)
      Rng.Reset(&H124)
      Dim A As New DoubleComplexMatrix(Order, Order, Rng)

      Dim S As New DoubleHermitianMatrix(NMathFunctions.ConjTransposeProduct(A, A))
      Console.WriteLine()
      Console.Write("S = ")
      Console.WriteLine(S.ToString(NumberFormatString))

      ' S = 5x5 [ (3.1219,0.0000)  (0.1293,0.7632)   (-0.5926,-0.5191) (1.0169,-0.4854) (-0.6211,-0.7439)  
      '           (0.1293,-0.7632) (1.0186,0.0000)   (-0.6158,0.5822)  (0.3471,-1.1798) (-0.3765,0.3526)  
      '           (-0.5926,0.5191) (-0.6158,-0.5822) (2.6691,0.0000)   (-0.7861,2.2311) (0.0942,0.1853)  
      '           (1.0169,0.4854)  (0.3471,1.1798)   (-0.7861,-2.2311) (4.1041,0.0000)  (0.5963,0.6960)  
      '           (-0.6211,0.7439) (-0.3765,-0.3526) (0.0942,-0.1853)  (0.5963,-0.6960) (3.9763,0.0000) ]

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

      ' You can set the values of elements in a Hermitian matrix using the 
      ' indexer. Note that setting the element in row i and column j to
      ' a value implicitly sets the element in column j and row i to the 
      ' complex conjugate of that value.
      S(2, 1) = New DoubleComplex(100.0, -99.0)
      Console.Write("S[2,1] = ")
      Console.WriteLine(S(2, 1))   ' (100, -99)
      Console.Write("S[1,2] = ")
      Console.WriteLine(S(1, 2))   ' (100, 99)

      ' Scalar multiplication and matrix addition/subtraction are supported.
      Dim Scalar As New DoubleComplex(-0.123)
      Dim C2 As DoubleHermitianMatrix = Scalar * S
      Dim D As DoubleHermitianMatrix = C2 + S
      Console.WriteLine()
      Console.Write("D = ")
      Console.WriteLine(D.ToString(NumberFormatString))

      ' Matrix/vector products too.
      Dim X As New DoubleComplexVector(S.Cols, Rng)   ' vector of random deviates
      Dim Y As DoubleComplexVector = MatrixFunctions.Product(S, X)
      Console.WriteLine()
      Console.Write("Sx = ")
      Console.WriteLine(Y.ToString(NumberFormatString))

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

      ' x and x2 should be about the same. Let's look at the l2 norm of 
      ' their difference.
      Dim Residual As DoubleComplexVector = X - X2
      Dim ResidualL2Norm As Double = Math.Sqrt(NMathFunctions.ConjDot(Residual, Residual).Real)
      Console.WriteLine()
      Console.Write("||x - x2|| = ")
      Console.WriteLine(ResidualL2Norm)

      ' You can transform the elements of a Hermitian matrix object by using
      ' the Transform() method.
      C2.DataVector.Transform(NMathFunctions.DoubleComplexCoshFunction)
      Console.WriteLine()
      Console.Write("cosh(C2) = ")
      Console.WriteLine(C2.ToString(NumberFormatString))

      ' For a matrix to satisfy the strict definition of a Hermitian matrix,
      ' its diagonal elements must be real. The Hermitian matrix classes provide
      ' a MakeDigaonalReal() method to ensure that your matrix satisfies the
      ' the strict definition of Hermitian.
      C2.MakeDiagonalReal()
      Console.WriteLine()
      Console.Write("Diagonal element is real: ")
      Console.WriteLine(C2(3, 3).Imag.Equals(0.0))  ' True

      ' Compute condition number
      Dim RCond As Double = 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

[TOC]