New Version 6.0!

Try it for free with our fully functional 60-day trial version.

Download now!

QuickStart Samples

Vector Operations QuickStart Sample (C#)

Illustrates how to perform operations on Vector objects, including construction, element access, arithmetic operations in C#.

Visual Basic code F# code IronPython code Back to QuickStart Samples

using System;

namespace Extreme.Numerics.QuickStart.CSharp
{
    // The var class resides in the Extreme.Mathematics.LinearAlgebra
    // namespace.
    using Extreme.Mathematics.LinearAlgebra;
    // The delegate class resides in the Extreme.Mathematics 
    // namespace.
    using Extreme.Mathematics;

    /// <summary>
    /// Illustrates operations on var objects from the
    /// Extreme.Mathematics.LinearAlgebra namespace of the Extreme Optimization
    /// Mathematics Library for .NET.
    /// </summary>
    class VectorOperations
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            // For details on the basic workings of var 
            // objects, including constructing, copying and
            // cloning vectors, see the BasicVectors QuickStart
            // Sample.
            //
            // Let's create some vectors to work with.
            var v1 = Vector.Create(new double[] { 1, 2, 3, 4, 5 });
            var v2 = Vector.Create(new double[] { 1, -2, 3, -4, 5 });
            var v3 = Vector.Create(new double[] { 3, 2, 1, 0, -1 });
            // This one will hold results.
            Vector<double> v;

            //
            // var Arithmetic
            //
            // The var class defines overloaded addition,
            // subtraction, and multiplication and division
            // operators:
            Console.WriteLine("v1 = {0:F4}", v1);
            Console.WriteLine("v2 = {0:F4}", v2);
            Console.WriteLine("Basic arithmetic:");
            v = -v1;
            Console.WriteLine("-v1 = {0:F4}", v);
            v = v1 + v2;
            Console.WriteLine("v1 + v2 = {0:F4}", v);
            v = v1 - v2;
            Console.WriteLine("v1 - v2 = {0:F4}", v);
            // Vectors can only be multiplied or divided by
            // a real number. For dot products, see the
            // DotProduct method.
            v = 5 * v1;
            Console.WriteLine("5 * v1 = {0:F4}", v);
            v = v1 * 5;
            Console.WriteLine("v1 * 5 = {0:F4}", v);
            v = v1 / 5;
            Console.WriteLine("v1 / 5 = {0:F4}", v);

            // For each operator, there is a corresponding
            // static method. For example: v1 + v2 is
            // equivalent to:
            v = Vector.Add(v1, v2);
            // v1 - v2 corresponds to:
            v = Vector.Subtract(v1, v2);
            // You can also apply these methods to vector objects.
            // In this case, they change the first operand.
            Console.WriteLine("v3 = {0:F4}", v3);
            v3.AddInPlace(v1);
            // Note that this is different from the += operator!
            // The += operator creates a Vector.Create object, 
            // whereas the Add method above does not.
            Console.WriteLine("v3+v1 -> v3 = {0:F4}", v3);
            // This method is overloaded so you can directly
            // add a scaled vector:
            v3.AddScaledInPlace(-2, v1);
            Console.WriteLine("v3-2v1 -> v3 = {0:F4}", v3);
            // Corresponding to the * operator, we have the
            // scale method:
            v3.MultiplyInPlace(3);
            Console.WriteLine("3v3 -> v3 = {0:F4}", v3);
            Console.WriteLine();

            //
            // Norms, dot products, etc.
            //
            Console.WriteLine("Norms, dot products, etc.");
            // The dot product is calculated in one of two ways:
            // Using the static DotProduct method:
            double a = Vector.DotProduct(v1, v2);
            // Or using the DotProduct method on one of the two
            // vectors:
            double b = v1.DotProduct(v2);
            Console.WriteLine("DotProduct(v1, v2) = {0:F4} = {0:F4}", 
                a, b);
            // The Norm method returns the standard two norm 
            // of a Vector:
            a = v1.Norm();
            Console.WriteLine("|v1| = {0:F4}", a);
            // .the Norm method is overloaded to allow other norms,
            // including the one-norm:
            a = v1.Norm(1);
            Console.WriteLine("one norm(v1) = {0:F4}", a);
            // ...the positive infinity norm, which returns the
            // absolute value of the largest component:
            a = v1.Norm(double.PositiveInfinity);
            Console.WriteLine("+inf norm(v1) = {0:F4}", a);
            // ...the negative infinity norm, which returns the
            // absolute value of the smallest component:
            a = v1.Norm(double.NegativeInfinity);
            Console.WriteLine("-inf norm(v1) = {0:F4}", a);
            // ...and even the zero norm, which simply returns
            // the number of components of the vector:
            a = v1.Norm(0);
            Console.WriteLine("zero-norm(v1) = {0:F4}", a);
            // You can get the square of the two norm with the
            // NormSquared method.
            a = v1.NormSquared();
            Console.WriteLine("|v1|^2 = {0:F4}", a);
            Console.WriteLine();
            
            //
            // Largest and smallest elements
            //
            // The var class defines methods to find the
            // largest or smallest element or its index.
            Console.WriteLine("v2 = {0:F4}", v2);
            // The Max method returns the largest element:
            Console.WriteLine("Max(v2) = {0:F4}", v2.Max());
            // The AbsoluteMax method returns the element with
            // the largest absolute value.
            Console.WriteLine("Absolute max(v2) = {0:F4}", 
                v2.AbsoluteMax());
            // The Min method returns the smallest element:
            Console.WriteLine("Min(v2) = {0:F4}", v2.Min());
            // The AbsoluteMin method returns the element with
            // the smallest absolute value.
            Console.WriteLine("Absolute min(v2) = {0:F4}", 
                v2.AbsoluteMin());
            // Each of these methods has an equivalent method
            // that returns the zero-based index of the element 
            // instead of its value, for example:
            Console.WriteLine("Index of Min(v2) = {0:F4}",
                v2.MinIndex());

            // Finally, the Map method lets you apply
            // an arbitrary function to each element of the
            // vector:
            v1.MapInPlace(Math.Exp);
            Console.WriteLine("Exp(v1) = {0:F4}", v1);
            // There is also a static method that returns a 
            // new vector:
            v = Vector.Map(Math.Exp, v2);

            Console.Write("Press Enter key to exit...");
            Console.ReadLine();
        }
    }
}