[TOC]
using System;
using CenterSpace.NMath.Core;
namespace CenterSpace.NMath.Core.Examples.CSharp
{
/// <summary>
/// A .NET example in C# showing some of the basic functionality of the vector classes.
/// </summary>
class VectorExample
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
DoubleVector v = new 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.
DoubleVector everyOther = 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;
Console.WriteLine("Value of v[2] == 10 is {0}", v[2] == 10); // 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();
everyOther[1] = 100;
Console.WriteLine("Value of v[2] == 100 is {0}", v[2] == 100); // 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.
DoubleVector vOrig = (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.
DoubleVector vOrig1 = v[new Slice(0, vOrig.Length)];
Console.WriteLine("values of v preserved during resize is {0}", vOrig == vOrig1);
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.
DoubleVector u = new DoubleVector(v.Length, 1, 1);
double 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.
double mean = NMathFunctions.Mean(v);
double 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();
} // Main
}// class
}// namespace
[TOC]