VB Tridiagonal Matrix Example

← All NMath Code Examples

 

Imports System

Imports CenterSpace.NMath.Core


Namespace CenterSpace.NMath.Examples.VisualBasic

  A .NET example in Visual Basic demonstrating the features of the tridiagonal matrix classes.
  Module TridiagonalMatrixExample

    Sub Main()

      Set up the parameters that describe the shape of a tridiagonal matrix.
      Dim Rows As Integer = 8
      Dim Cols As Integer = 8

      Set up a tridiagonal matrix B by setting all the diagonals within the matrix 
      bandwidth.
      Dim B As New FloatComplexTriDiagMatrix(Rows, Cols)
      Dim Diagonal As FloatComplexVector

      Dim I As Integer
      For I = -1 To 1
        Diagonal = B.Diagonal(I)
        Diagonal.Set(Slice.All, New FloatComplex(I + 2))
      Next

      Console.WriteLine()
      Console.WriteLine("B = ")
      Console.WriteLine(B.ToTabDelimited("G3"))

      B =
      (2,0)   (3,0)   (0,0)   (0,0)   (0,0)   (0,0)   (0,0)   (0,0)
      (1,0)   (2,0)   (3,0)   (0,0)   (0,0)   (0,0)   (0,0)   (0,0)
      (0,0)   (1,0)   (2,0)   (3,0)   (0,0)   (0,0)   (0,0)   (0,0)
      (0,0)   (0,0)   (1,0)   (2,0)   (3,0)   (0,0)   (0,0)   (0,0)
      (0,0)   (0,0)   (0,0)   (1,0)   (2,0)   (3,0)   (0,0)   (0,0)
      (0,0)   (0,0)   (0,0)   (0,0)   (1,0)   (2,0)   (3,0)   (0,0)
      (0,0)   (0,0)   (0,0)   (0,0)   (0,0)   (1,0)   (2,0)   (3,0)
      (0,0)   (0,0)   (0,0)   (0,0)   (0,0)   (0,0)   (1,0)   (2,0)

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

      You can set the values of elements in the main, super, and sub diagonals
      of a tridiagonal matrix using the indexer.
      B(2, 1) = New FloatComplex(-100, 99)
      Console.Write("B[2,1] = ")
      Console.WriteLine(B(2, 1))

      But setting an element that would destroy the tridiagonal structure
      of the matrix raises a NonModifiableElementException exception.
      Try
        B(7, 0) = New FloatComplex(21)
      Catch E As NonModifiableElementException
        Console.WriteLine()
        Console.Write("NonModifiableElementException: ")
        Console.WriteLine(E.Message)
      End Try

      Scalar addition/subtractions/multiplication and matrix 
      addition/subtraction are supported.
      Dim S As New FloatComplex(-0.123F)
      Dim T As New FloatComplex(3)
      Dim C2 As FloatComplexTriDiagMatrix = FloatComplexTriDiagMatrix.Multiply(S, B)
      Dim C As FloatComplexTriDiagMatrix = FloatComplexTriDiagMatrix.Subtract(T, B)
      Dim D As FloatComplexTriDiagMatrix = FloatComplexTriDiagMatrix.Add(C2, B)

      Console.WriteLine()
      Console.WriteLine("D = ")
      Console.WriteLine(D.ToTabDelimited("F2"))

      Matrix/vector products too.
      Dim Rng As New RandGenUniform(-1, 1)
      Rng.Reset(&H124)
      Dim X As New FloatComplexVector(B.Cols, Rng)   vector of random deviates
      Dim Y As FloatComplexVector = MatrixFunctions.Product(B, X)

      Console.Write("Bx = ")
      Console.WriteLine(Y.ToString("G3"))

      You can transform the non-zero elements of a banded matrix object by using
      the Transform() method on its data vector.
      T = New FloatComplex(2)
      C2.DataVector.Transform(NMathFunctions.FloatComplexPowFunc, T)   Square every element of C2

      Console.WriteLine()
      Console.WriteLine("C2^2 =")
      Console.WriteLine(C2.ToTabDelimited("F2"))

      You can also solve linear systems.
      Dim X2 As FloatComplexVector = MatrixFunctions.Solve(B, Y)

      x and x2 should be about the same. Lets look at the l2 norm of 
      their difference.
      Dim Residual As FloatComplexVector = X - X2
      Dim ResidualL2Norm As Single = Math.Sqrt(NMathFunctions.ConjDot(Residual, Residual).Real)

      Console.Write("||x - x2|| = ")
      Console.WriteLine(ResidualL2Norm)

      Compute condition number.
      Dim RCond As Single = MatrixFunctions.ConditionNumber(B)

      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