VB Hermitian Matrix 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 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.WriteLine("S = ")
      Console.WriteLine(S.ToTabDelimited("F5"))

      ' S =
      ' (3.12186,0.00000)       (0.12935,0.76321)       (-0.59263,-0.51912)     (1.01693,-0.48541)      (-0.62109,-0.74390)
      ' (0.12935,-0.76321)      (1.01859,0.00000)       (-0.61581,0.58225)      (0.34714,-1.17980)      (-0.37649,0.35263)
      ' (-0.59263,0.51912)      (-0.61581,-0.58225)     (2.66911,0.00000)       (-0.78612,2.23106)      (0.09417,0.18527)
      ' (1.01693,0.48541)       (0.34714,1.17980)       (-0.78612,-2.23106)     (4.10411,0.00000)       (0.59635,0.69605)
      ' (-0.62109,0.74390)      (-0.37649,-0.35263)     (0.09417,-0.18527)      (0.59635,-0.69605)      (3.97634,0.00000)

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

      ' 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.WriteLine("S[2,1] = " & S(2, 1).ToString())   ' (100, -99)
      Console.WriteLine("S[1,2] = " & S(1, 2).ToString())   ' (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.WriteLine("D = ")
      Console.WriteLine(D.ToTabDelimited("F5"))

      ' 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("Sx = {0}", Y.ToString("G3"))

      ' 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.DoubleComplexCoshFunc)
      Console.WriteLine()
      Console.WriteLine("cosh(C2) = ")
      Console.WriteLine(C2.ToTabDelimited("G5"))

      ' 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.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

← All NMath Code Examples
Top