VB Matrix Example

← All NMath Code Examples


Imports System

Imports CenterSpace.NMath.Core
Imports Range = CenterSpace.NMath.Core.Range

Namespace CenterSpace.NMath.Examples.VisualBasic

  A .NET example in Visual Basic showing some of the basic functionality of the matrix classes.
  Module MatrixExample

    Sub Main()

      Dim A As New DoubleMatrix("3x3 [1 2 3  4 5 6  7 8 9]")

      You can obtain vector "views" of the rows and columns.
      Dim row1 As DoubleVector = A.Row(1)


      Remember, indexing starts at 0 so row one really means the second
      row of the matrix.
      Console.WriteLine("Row 1 = {0}", row1.ToString())   [4 5 6]

      Dim col0 As DoubleVector = A.Col(0)
      Console.WriteLine("Column 0 = {0}", col0.ToString())   [1 4 7]

      Remember that these vectors are different views of the data in
      the matrix A. Changing a value in one of these vectors will change
      the corresponding value in the matrix.
      row1(0) = 10
      Console.WriteLine("Value of A[1,0] == 10 is {0}", A(1, 0).Equals(10))  True

      You can make sure that your data is not being shared with anyone
      else by invoking the DeepenThisCopy method. This will insure that
      you have your own private copy of the data and that it occupies
      contiguous storage.
      row1(0) = 100
      Console.WriteLine("Value of A[1,0] == 100 is {0}", A(1, 0) = 100)  False

      You can also obtain vector views of the various diagonals of A.
      Heres is one way to create the 3 by 3 matrix B
          |1  2 0|
      B = |-2 1 2|
          |0 -2 1|
      That is, 1s on the main diagonal, 2s in the super-diagonal, and 
      -2s on the sub-diagonal.
      Dim B As New DoubleMatrix(3, 3)   A 3x3 matrix with all zeroes
      B.Diagonal().Set(Range.All, 1.0)
      B.Diagonal(1).Set(Range.All, 2.0)
      B.Diagonal(-1).Set(Range.All, -2.0)
      Console.WriteLine(B.ToTabDelimited())   B = 3x3 [1 2 0  -2 1 2  0 -2 1]

      Sum the columns of B
      Dim Totals As DoubleVector = NMathFunctions.Sum(B)
      Console.WriteLine("Column totals of B = {0}", Totals.ToString())

      There are methods to compute matrix norms with respect to the one
      and infinity norms.
      Console.WriteLine("The one-norm of A = {0}", A.InfinityNorm())
      Console.WriteLine("The infinity-norm of A = {0}", A.OneNorm())

      The Resize method changes the dimensions of a matrix. Values are
      truncated or padded with zeros as appropriate.
      A.Resize(5, 5)

      A will have its original values in the top-left 3 by 3 corner. 
      The last two rows and the last two columns will zeros.
      Console.WriteLine("A resized...")

      The indexers can be used to obtain vector views of slices of the
      rows or columns of A.
      Construct a Range object which starts at 0, goes to the end and has
      a stride of 2.
      Dim everyOtherElt As New Range(0, Position.End, 2)

      Create a vector that views every other element of the last
      last column of A.
      Dim lastColEveryOther As DoubleVector = A(everyOtherElt, A.Cols - 1)

      Use this vector to set every other element in the last column
      of A to 2.
      lastColEveryOther.Set(Range.All, 2.0)
      Console.WriteLine("lastColEveryOther = {0}", lastColEveryOther.ToString())   [1 1 1 1 1]

      Last column of A is [2 0 2 0 2]
      Console.WriteLine("Last column of A = {0}", A.Col(A.Cols - 1).ToString())

      Matrix/Matrix and Matrix/Vector products are computed using the appropriate
      static methods in the NMathFunctions class.
      B = New DoubleMatrix(5, 5, 1, 1)
      Dim C As DoubleMatrix = NMathFunctions.Product(A, B)
      Console.WriteLine("Inner product of A and B is...")

      Dim v As New DoubleVector("[5 4 3 2 1]")
      Dim u As DoubleVector = NMathFunctions.Product(A, v)
      Console.WriteLine("Inner product of A and v = ", u.ToString())

      Transpose products are available too.
      C = NMathFunctions.TransposeProduct(A, B)
      Console.WriteLine("The inner product of the transpose of A with B is...")

      For complex matrices, conjugate transpose products are available.
      Dim d As New DoubleComplex(1, 1)
      Construct a two matrices whose values are multiples of c and 2c
      Dim F As New DoubleComplexMatrix(3, 3, d, d)
      Dim G As New DoubleComplexMatrix(3, 3, d, 2 * d)
      Dim H As DoubleComplexMatrix = NMathFunctions.ConjTransposeProduct(F, G)
      Console.WriteLine("The inner product of the complex conjugate of F with G is...")

      Console.WriteLine("Press Enter Key")

    End Sub

  End Module

End Namespace

← All NMath Code Examples