← All NMath Code Examples
Imports System
Imports CenterSpace.NMath.Core
Namespace CenterSpace.NMath.Examples.VisualBasic
A .NET example in Visual Basic 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.ToTabDelimited())
B =
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.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.WriteLine(D.ToTabDelimited())
D =
4 6 8 0 0 0 0
2 4 6 8 0 0 0
0 198 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.Write("Bx = " & Y.ToString("G5"))
Dim BTB As DoubleBandMatrix = MatrixFunctions.Product(B.Transpose(), B)
Console.WriteLine()
Console.WriteLine("BTB = ")
Console.WriteLine(BTB.ToTabDelimited("G5"))
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.CosFunc) cosine of Cpi
Console.WriteLine("cos(Cpi) =")
Console.WriteLine(CPi.ToTabDelimited("G5"))
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("Banded matrix product =")
Console.WriteLine(P.ToTabDelimited("G5"))
You can also solve linear systems.
Dim X2 As DoubleVector = MatrixFunctions.Solve(B, Y)
x and x2 should be the same. Lets 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("||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
← All NMath Code Examples