VB Sparse Vector Example

← All NMath Code Examples


Imports System

Imports CenterSpace.NMath.Core

Namespace CenterSpace.NMath.Examples.VisualBasic

  A .NET example in Visual Basic showing simple general sparse vector functionality.
  Module SparseVectorExample

    Sub Main()

      Construct a sparse vector from a set of nonzero values and the indices for
      those values. Indices are 0-based.
      Dim Indices As IndexArray = New IndexArray(1, 12, 2, 15)
      Dim Values() As Double = {2, 3.14, -4, -0.6}
      Dim V As DoubleSparseVector = New DoubleSparseVector(Values, Indices)


      Console.WriteLine("v = " & V.ToString())

      Dot product with a dense vector.
      Dim W As New DoubleVector(66, 1.2)
      Dim Dot As Double = MatrixFunctions.Dot(W, V)
      Console.WriteLine("w dot v = " & Dot)

      Some miscellaneous sparse vector functions...
      Dim SumOfAbsValues As Double = MatrixFunctions.AbsSum(V)
      Console.WriteLine("Sum of the absolute values in v = " & SumOfAbsValues)
      Dim MaxAbsValueIndex As Integer = MatrixFunctions.MaxAbsIndex(V)
      Console.WriteLine("Index of the largest absolute values in v = " & MaxAbsValueIndex)

      Construct a sparse vector from a dense vector by specifying the indices in the 
      dense vector to "gather" into the sparse vector.
      Dim T As New DoubleVector(200, 1, 1.3)
      Dim TSparse As DoubleSparseVector = MatrixFunctions.Gather(T, Indices.Length, Indices)
      Console.WriteLine("tSparse = " & TSparse.ToString())

      Construct a dense vector from a sparse vector by specifying the length of the
      dense vector and "scattering" the nonzero values from the sparse vector into the 
      dense vector.
      Dim DenseT As DoubleVector = MatrixFunctions.Scatter(TSparse, 20)
      Console.WriteLine("denseT = " & DenseT.ToString())

      Console.WriteLine("Press Enter Key")

    End Sub
  End Module
End Namespace

← All NMath Code Examples