[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 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.Write("S = ")
Console.WriteLine(S.ToString())
' S = 6x6 [ 6.511227 0.7702899 2.189394 -2.113362 2.268176 -4.007094
' 0.7702899 3.701933 -2.814527 -0.8870333 -3.837288 -2.518311
' 2.189394 -2.814527 5.310741 -2.078353 3.270024 -1.440627
' -2.113362 -0.8870333 -2.078353 6.054404 0.336647 5.635363
' 2.268176 -3.837288 3.270024 0.336647 13.44877 2.772367
' -4.007094 -2.518311 -1.440627 5.635363 2.772367 7.547361 ]
' Indexer accessor works just like it does for general matrices.
Console.WriteLine()
Console.Write("S[2,2] = " & S(2, 2))
Console.Write("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.Write("S[2,1] = " & S(2, 1))
Console.Write("S[1,2] = " & S(1, 2))
' 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()
Console.Write("D = " & D.ToString())
' 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()
Console.Write("Sx = ")
Console.WriteLine(Y.ToString())
' You can transform the elements of a symmetric matrix object by using
' the Transform() method.
C2.Transform(NMathFunctions.FloatPowFunction, 2) ' Square every element of C2
Console.WriteLine()
Console.Write("C2^2 = ")
Console.WriteLine(C2.ToString())
' You can also solve linear systems.
Dim X2 As FloatVector = 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 FloatVector = FloatVector.Subtract(X, X2)
Dim ResidualL2Norm As Single = Math.Sqrt(NMathFunctions.Dot(Residual, Residual))
Console.WriteLine()
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
[TOC]