[TOC]
Imports System
Imports CenterSpace.NMath.Core
Imports CenterSpace.NMath.Matrix
Namespace CenterSpace.NMath.Matrix.Examples.VisualBasic
' Example exhibiting the features of the banded matrix classes.
Module BandMatrixExample
Sub Main()
' Set up the parameters that describe the shape of a banded matrix.
Dim UpperBandwidth As Integer = 2
Dim LowerBandwidth As Integer = 1
Dim Rows As Integer = 7
Dim Cols As Integer = 7
' Set up a banded matrix B by setting all the diagonals within the matrix
' bandwidth.
Dim B As New DoubleBandMatrix(Rows, Cols, LowerBandwidth, UpperBandwidth)
Dim I As Integer
For I = -B.LowerBandwidth To B.UpperBandwidth
B.Diagonal(I).Set(Slice.All, I + 2)
Next
Console.WriteLine()
Console.Write("B = ")
Console.WriteLine(B.ToString())
' B = 1 2 7x7 [ 2 3 4 0 0 0 0
' 1 2 3 4 0 0 0
' 0 1 2 3 4 0 0
' 0 0 1 2 3 4 0
' 0 0 0 1 2 3 4
' 0 0 0 0 1 2 3
' 0 0 0 0 0 1 2 ]
' Indexer accessor works just like it does for general matrices.
Console.WriteLine()
Console.Write("B[2,2] = ")
Console.WriteLine(B(2, 2))
Console.Write("B[5,0] = ")
Console.WriteLine(B(5, 0))
' You can set the values of elements in the bandwidth
' of a banded matrix using the indexer.
B(2, 1) = 99
Console.Write("B[2,1] = ")
Console.WriteLine(B(2, 1))
' But setting an element outside the bandwidth of the
' matrix raises a NonModifiableElementException exception.
Try
B(6, 0) = 21
Catch E As NonModifiableElementException
Console.WriteLine()
Console.Write("NonModifiableElementException: ")
Console.WriteLine(E.Message)
End Try
' Scalar multiplication and addition/subtraction is supported.
Dim C As DoubleBandMatrix = 3 * B
Dim D As DoubleBandMatrix = C - B
Console.WriteLine()
Console.Write("D = " & D.ToString())
' D = 1 2 7x7 [ 4 6 8 0 0 0 0
' 2 4 6 8 0 0 0
' 0 2 4 6 8 0 0
' 0 0 2 4 6 8 0
' 0 0 0 2 4 6 8
' 0 0 0 0 2 4 6
' 0 0 0 0 0 2 4 ]
' Matrix/vector and matrix/matrix products too.
Dim Rng As New RandGenUniform(-1, 1)
Rng.Reset(&H124)
Dim X As DoubleVector = New DoubleVector(B.Cols, Rng) ' vector of random deviates
Dim Y As DoubleVector = MatrixFunctions.Product(B, X)
Console.WriteLine()
Console.Write("Bx = " & Y.ToString())
Dim BTB As DoubleBandMatrix = MatrixFunctions.Product(B.Transpose(), B)
Console.WriteLine()
Console.Write("BTB = " & BTB.ToString())
' You can transform the non-zero elements of a banded matrix object by using
' the Transform() method on its data vector.
Dim CPi As DoubleBandMatrix = Math.PI * C
CPi.DataVector.Transform(NMathFunctions.CosFunction) ' cosine of Cpi
Console.WriteLine()
Console.Write("cos(CPi) = " & CPi.ToString())
' Since the inner product of two banded matrices is a banded matrix,
' matrix inner products is supported.
Dim P As DoubleBandMatrix = MatrixFunctions.Product(CPi, C)
Console.WriteLine()
Console.Write("Banded matrix product = " & P.ToString())
' You can also solve linear systems.
Dim X2 As DoubleVector = MatrixFunctions.Solve(B, 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|| = " & ResidualL2Norm)
' You can use the Resize() method to change the bandwidths...
CPi.Resize(CPi.Rows, CPi.Cols, 1, 1) ' matrix is now tridiagonal.
' And construct a tridiagonal matrix from it.
Dim T As DoubleTriDiagMatrix = New DoubleTriDiagMatrix(CPi)
Console.WriteLine()
Console.WriteLine("Press Enter Key")
Console.Read()
End Sub
End Module
End Namespace
[TOC]