F# Vector Example

[TOC]

#light

namespace CenterSpace.NMath.Core.Examples.FSharp

open System
open CenterSpace.NMath.Core

module VectorExample =

  /// <summary>
  /// A .NET example in C# showing some of the basic functionality of the vector classes.
  /// </summary>

  let v : DoubleVector = new CenterSpace.NMath.Core.DoubleVector("[1 2 3 4 5 6]")

  Console.WriteLine()

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

// 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.0
  Console.WriteLine("Value of v[2] == 10 is {0}", v.[2] = 10.0) // True
  Console.WriteLine()

  // 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.
  v.DeepenThisCopy() |> ignore
  everyOther.[1] <- 100.0
  Console.WriteLine("Value of v[2] == 100 is {0}", v.[2] = 100.0) // False
  Console.WriteLine()

  // 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.
  let vOrig : DoubleVector = v.Clone() :?> DoubleVector

  // Now resize v.
  v.Resize(v.Length + 2) |> ignore
  // v will have its original values in the first v.Length-1 elements.
  // The last two elements will be zero.
  let vOrig1 = v.[new Slice(0, vOrig.Length)]
  Console.WriteLine("values of v preserved during resize is " + (vOrig = vOrig1).ToString())
  Console.WriteLine()
  Console.WriteLine("v resized = {0}", v.ToString())
  Console.WriteLine()

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

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

  Console.WriteLine()
  Console.WriteLine("Press Enter Key")
  Console.Read() |> ignore 

[TOC]