[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 factorization classes for
' tridiagonal matrices.
Module TriDiagFactExample
Sub Main()
' Construct a tridiagonal matrix with random entries.
Dim Rows As Integer = 5
Dim Cols As Integer = 5
Dim Rng As New RandGenUniform(-1, 1)
Rng.Reset(&H124)
Dim Data1 As New FloatComplexVector(Cols, Rng)
Dim Data2 As New FloatComplexVector(Cols - 1, Rng)
Dim Data3 As New FloatComplexVector(Cols - 1, Rng)
Dim A As New FloatComplexTriDiagMatrix(Rows, Cols)
A.Diagonal()(Slice.All) = Data1
A.Diagonal(1)(Slice.All) = Data2
A.Diagonal(-1)(Slice.All) = Data3
Console.WriteLine()
Console.Write("A = " & A.ToString("F4"))
' A = 5x5 [ (-0.4974,0.3315) (0.5601,0.3060) (0.0000,0.0000) (0.0000,0.0000) (0.0000,0.0000)
' (0.7734,0.3580) (-0.2503,0.5764) (0.2204,-0.0770) (0.0000,0.0000) (0.0000,0.0000)
' (0.0000,0.0000) (-0.8629,0.2029) (0.1961,0.1821) (-0.1678,-0.2590) (0.0000,0.0000)
' (0.0000,0.0000) (0.0000,0.0000) (-0.5848,0.6222) (-0.0443,0.0738) (-0.9238,0.6206)
' (0.0000,0.0000) (0.0000,0.0000) (0.0000,0.0000) (-0.7045,0.1236) (-0.3249,-0.2797) ]
' Construct a tridiagonal factorization class.
Dim Fact As New FloatComplexTriDiagFact(A)
' Check to see if A is singular.
Dim IsSingularString As String
If Fact.IsSingular Then
IsSingularString = "A is singular"
Else
IsSingularString = "A is NOT singular"
End If
Console.WriteLine()
Console.WriteLine(IsSingularString)
' Retrieve information about the matrix A.
Dim Det As FloatComplex = Fact.Determinant()
Dim RCond As Single = Fact.ConditionNumber()
Dim AInv As FloatComplexMatrix = Fact.Inverse()
Console.WriteLine()
Console.Write("Determinant of A = " & Det.ToString())
Console.WriteLine()
Console.Write("Reciprocal condition number = " & RCond)
Console.WriteLine()
Console.Write("A inverse = ")
Console.WriteLine(AInv.ToString())
' Use the factorization to solve some linear systems Ax = y.
Dim Y0 As New FloatComplexVector(Fact.Cols, Rng)
Dim Y1 As New FloatComplexVector(Fact.Cols, Rng)
Dim X0 As FloatComplexVector = Fact.Solve(Y0)
Dim X1 As FloatComplexVector = Fact.Solve(Y1)
Console.WriteLine()
Console.Write("Solution to Ax = y0 is " & X0.ToString())
Console.WriteLine()
Console.Write("y0 - Ax0 = ")
Console.WriteLine(FloatComplexVector.Subtract(Y0, MatrixFunctions.Product(A, X0)).ToString())
Console.WriteLine()
Console.Write("Solution to Ax = y1 is " & X1.ToString())
Console.WriteLine()
Console.Write("y1 - Ax1 = ")
Console.WriteLine(FloatComplexVector.Subtract(Y1, MatrixFunctions.Product(A, X1)).ToString())
' You can also solve for multiple right-hand sides.
Dim Y As New FloatComplexMatrix(Y1.Length, 2)
Y.Col(0)(Slice.All) = Y0
Y.Col(1)(Slice.All) = Y1
Dim X As FloatComplexMatrix = Fact.Solve(Y)
' The first column of X should be x0 the second column should be x1.
Console.WriteLine()
Console.Write("X = " & X.ToString())
' Factor a different matrix.
Dim Z As New FloatComplex(1.23F, -0.76F)
Dim B As FloatComplexTriDiagMatrix = Z * A
Fact.Factor(B)
X0 = Fact.Solve(Y0)
Console.WriteLine()
Console.Write("Solution to Bx = y0 is " & X0.ToString())
Console.WriteLine()
Console.WriteLine("Press Enter Key")
Console.Read()
End Sub
End Module
End Namespace
[TOC]