[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 triangular matrix classes.
Module TriangularMatrixExample
Sub Main()
' Set up the parameters that describe the shape of a Hermitian banded matrix.
Dim Order As Integer = 5
' Create upper and lower triangular matrices.
Dim Rng As New RandGenUniform(-1, 1)
Rng.Reset(&H124)
Dim A As New DoubleMatrix(Order, Order, Rng)
Dim LU As New DoubleLUFact(A)
Dim U As New DoubleUpperTriMatrix(LU.U)
Dim L As New DoubleLowerTriMatrix(LU.L)
Console.WriteLine()
Console.Write("U = ")
Console.WriteLine(U.ToString("F4"))
Console.WriteLine()
Console.Write("L = ")
Console.WriteLine(L.ToString("F4"))
Console.WriteLine()
' U = 5x5 [ 0.5764 -0.3249 -0.0770 0.7734 0.6222
' 0.0000 -0.1691 -0.1416 0.0948 -0.9162
' 0.0000 0.0000 0.5759 0.3533 0.2067
' 0.0000 0.0000 0.0000 -1.4304 -1.0101
' 0.0000 0.0000 0.0000 0.0000 -0.5806 ]
'
' L = 5x5 [ 1.0000 0.0000 0.0000 0.0000 0.0000
' 0.3403 1.0000 0.0000 0.0000 0.0000
' -0.8630 0.5814 1.0000 0.0000 0.0000
' 0.5751 -0.8428 0.4012 1.0000 0.0000
' -0.4343 0.3979 0.4225 -0.5379 1.0000 ]
' Indexer accessor works just like it does for general matrices.
Console.Write("U[2,2] = ")
Console.WriteLine(U(2, 2))
Console.Write("L[0,3] = ")
Console.WriteLine(L(0, 3))
' You can set the values of elements in the upper triangular part
' of a upper triangular matrix and the lower part of a lower
' triangular matrix.
Dim Scalar As Double = 99
L(2, 1) = Scalar
Console.Write("L[2,1] = ")
Console.WriteLine(L(2, 1)) ' 99
U(0, 2) = Scalar + 1
Console.Write("U[0,2] = ")
Console.WriteLine(U(0, 2)) ' 100
' But setting the values of elements in the lower triangular part
' of a upper triangular matrix or the upper part of a lower
' triangular matrix raises a NonModifiableElementException.
Try
U(3, 0) = Scalar
Catch E As NonModifiableElementException
Console.WriteLine()
Console.Write("NonModifiableElementException: ")
Console.WriteLine(E.Message)
End Try
Try
L(0, 3) = Scalar
Catch E As NonModifiableElementException
Console.WriteLine()
Console.Write("NonModifiableElementException: ")
Console.WriteLine(E.Message)
End Try
' Scalar multiplication and matrix addition/subtraction are supported.
Dim C As DoubleUpperTriMatrix = Scalar * U
Dim D As DoubleUpperTriMatrix = C + U
Console.WriteLine()
Console.Write("D = ")
Console.WriteLine(D.ToString("F4"))
' Matrix/vector inner products too.
Dim X As New DoubleVector(L.Cols, Rng)
Dim Y As DoubleVector = MatrixFunctions.Product(L, X)
Console.WriteLine()
Console.Write("Lx = ")
Console.WriteLine(Y.ToString())
' You can transform the non-zero elements of a triangular matrix object
' by using the Transform() method on its data vector.
' Change every element of C to its absolute value.
C.DataVector.Transform(NMathFunctions.AbsFunction)
Console.WriteLine()
Console.Write("abs(C) = ")
Console.WriteLine(C.ToString("F4"))
' You can also solve linear systems.
Dim X2 As DoubleVector = MatrixFunctions.Solve(L, Y)
' x and x2 should be the same. Let's look at the l2 norm of
' their difference.
Dim Residual As DoubleVector = X - X2
Dim ResidualL2Norm As Double = Math.Sqrt(NMathFunctions.Dot(Residual, Residual))
Console.WriteLine()
Console.Write("||x - x2|| = ")
Console.WriteLine(ResidualL2Norm)
' You can calculate the determinant too.
Dim Det As Double = MatrixFunctions.Determinant(U)
Console.WriteLine()
Console.Write("Determinant of U = ")
Console.WriteLine(Det)
' You can use the Resize() method to change the bandwidths.
D.Resize(6)
Console.WriteLine()
Console.Write("D resized = ")
Console.WriteLine(D.ToString("F4"))
Console.WriteLine()
Console.WriteLine("Press Enter Key")
Console.Read()
End Sub
End Module
End Namespace
[TOC]