VB Vector Example

← All NMath Code Examples


Imports System

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

Namespace CenterSpace.NMath.Core.Examples.VisualBasic

  ' A .NET example in Visual Basic showing some of the basic functionality of the vector classes.
  Module VectorExample

    ' The main entry point for the application.
    Sub Main()


      Dim v As New DoubleVector("[1 2 3 4 5 6]")

      ' You can obtain different vector "views" of the data in v.
      ' For example, vector viewing every other element of v.
      Dim everyOther As DoubleVector = v(New Range(0, Position.End, 2))
      Console.WriteLine("everyOther = {0}", everyOther.ToString())   ' [1 3 5]

      ' Remember that this vector is a different views of the data in
      ' v. Changing a value in everyOther will change
      ' the corresponding value in v.
      everyOther(1) = 10
      Console.WriteLine("Value of v[2] == 10 is {0}", v(2).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.
      everyOther(1) = 100
      Console.WriteLine("Value of v[2] == 100 is {0}", v(2).Equals(100))  ' False

      ' The Resize method changes the length of a vector. Values are
      ' truncated or padded with zeros as appropriate.
      ' First let's save v's original values.
      Dim vOrig As DoubleVector = v.Clone()

      ' Now resize v.
      v.Resize(v.Length + 2)

      ' v will have its original values in the first v.Length-1 elements.
      ' The last two elements will be zero.
      Dim vOrig1 As DoubleVector = v(New Slice(0, vOrig.Length))
      Console.WriteLine("values of v preserved during resize is {0}", vOrig.Equals(vOrig1))
      Console.WriteLine("v resized = {0}", v.ToString())

      ' vector dot, or inner, products are available as
      ' static methods in the NMathFunctions class.
      Dim u As New DoubleVector(v.Length, 1, 1)
      Dim dp As Double = NMathFunctions.Dot(v, u)
      Console.WriteLine("The dot product of {0} with {1} = {2}", v.ToString(), u.ToString(), dp)

      ' There are also functions for computing mean and variance of the numbers in a vector.
      Dim mean As Double = NMathFunctions.Mean(v)
      Dim var As Double = NMathFunctions.Variance(v)
      Console.WriteLine("The mean of v is {0}, and the variance is {1}", mean, var)

      Console.WriteLine("Press Enter Key")

    End Sub

  End Module

End Namespace

← All NMath Code Examples