VB Vector Slice Example

← All NMath Code Examples

 

Imports System

Imports CenterSpace.NMath.Core

Namespace CenterSpace.NMath.Examples.VisualBasic

  ' A .NET example in Visual Basic showing how to use the indexing class Slice with the vector classes.
  Module VectorSliceExample

    Sub Main()

      Dim length As Integer = 10

      Console.WriteLine()

      Dim u As New FloatVector(length, 0, 1)
      Console.WriteLine("u = " & u.ToString())   ' u = [0 1 2 3 4 5 6 7 8 9]

      ' Last five elements of the vector u:
      Dim uLast5 As FloatVector = u(New Slice(5, 5))
      Console.WriteLine("uLast5 = " & uLast5.ToString())   ' uLast5 = [5 6 7 8 9]

      ' You can use negative strides too. Here is the vector u, reversed.
      Dim uRev As FloatVector = u(New Slice(length - 1, length, -1))
      Console.WriteLine("uRev = " & uRev.ToString())   ' uRev = [9 8 7 6 5 4 3 2 1 0]

      ' Notice that when you create a new FloatVector using the Range or Slice class,
      ' you are creating a different "view" of the vectors data. That is, the 
      ' FloatVector instance returned by the indexing operator taking a Range object,
      ' and the FloatVector instance being indexed share the same data:
      uRev(0) = 0
      Console.WriteLine("uRev = " & uRev.ToString())   ' uRev = [0 8 7 6 5 4 3 2 1 0]
      Console.WriteLine("u = " & u.ToString())   ' u = [0 1 2 3 4 5 6 7 8 0]

      ' Finally, we use the FloatVector method Set(Slice) to change the contents of u
      ' to alternate values 0 and 1
      Dim evenElts As New Slice(0, length / 2, 2)
      Dim oddElts As New Slice(1, length / 2, 2)
      u.Set(evenElts, 0)
      u.Set(oddElts, 1)
      Console.WriteLine("u = " & u.ToString())   ' u = [0 1 0 1 0 1 0 1 0 1]

      Console.WriteLine()
      Console.WriteLine("Press Enter Key")
      Console.Read()

    End Sub

  End Module

End Namespace


← All NMath Code Examples
Top