Extreme Optimization > Mathematics Library for .NET > QuickStart Samples > VectorOperations QuickStart Sample (C#)

Extreme Optimization Mathematics Library for .NET

VectorOperations QuickStart Sample (C#)

Illustrates operations on Vector objects (Extreme.Mathematics.LinearAlgebra namespace) in C#.

VB.NET code Back to QuickStart Samples

using System;

namespace Extreme.Mathematics.QuickStart.CSharp
{
    // The Vector 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 Vector 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 Vector 
            // objects, including constructing, copying and
            // cloning vectors, see the BasicVectors QuickStart
            // Sample.
            //
            // Let's create some vectors to work with.
            Vector v1 = new GeneralVector(1, 2, 3, 4, 5);
            Vector v2 = new GeneralVector(1, -2, 3, -4, 5);
            Vector v3 = new GeneralVector(3, 2, 1, 0, -1);
            // This one will hold results.
            Vector v;

            //
            // Vector Arithmetic
            //
            // The Vector class defines overloaded addition,
            // subtraction, and multiplication and division
            // operators:
            Console.WriteLine("v1 = {0}", v1);
            Console.WriteLine("v2 = {0}", v2);
            Console.WriteLine("Basic arithmetic:");
            v = -v1;
            Console.WriteLine("-v1 = {0}", v);
            v = v1 + v2;
            Console.WriteLine("v1 + v2 = {0}", v);
            v = v1 - v2;
            Console.WriteLine("v1 - v2 = {0}", 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}", v);
            v = v1 * 5;
            Console.WriteLine("v1 * 5 = {0}", v);
            v = v1 / 5;
            Console.WriteLine("v1 / 5 = {0}", 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}", v3);
            v3.Add(v1);
            // Note that this is different from the += operator!
            // The += operator creates a new GeneralVector object, 
            // whereas the Add method above does not.
            Console.WriteLine("v3+v1 -> v3 = {0}", v3);
            // This method is overloaded so you can directly
            // add a scaled vector:
            v3.Add(-2, v1);
            Console.WriteLine("v3-2v1 -> v3 = {0}", v3);
            // Corresponding to the * operator, we have the
            // scale method:
            v3.Multiply(3);
            Console.WriteLine("3v3 -> v3 = {0}", 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} = {0}", 
                a, b);
            // The Norm method returns the standard two norm 
            // of a Vector:
            a = v1.Norm();
            Console.WriteLine("|v1| = {0}", a);
            // .the Norm method is overloaded to allow other norms,
            // including the one-norm:
            a = v1.Norm(1);
            Console.WriteLine("one norm(v1) = {0}", 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}", 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}", 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}", a);
            // You can get the square of the two norm with the
            // NormSquared method.
            a = v1.NormSquared();
            Console.WriteLine("|v1|^2 = {0}", a);
            Console.WriteLine();
            
            //
            // Largest and smallest elements
            //
            // The Vector class defines methods to find the
            // largest or smallest element or its index.
            Console.WriteLine("v2 = {0}", v2);
            // The Max method returns the largest element:
            Console.WriteLine("Max(v2) = {0}", v2.Max());
            // The AbsoluteMax method returns the element with
            // the largest absolute value.
            Console.WriteLine("Absolute max(v2) = {0}", 
                v2.AbsoluteMax());
            // The Min method returns the smallest element:
            Console.WriteLine("Min(v2) = {0}", v2.Min());
            // The AbsoluteMin method returns the element with
            // the smallest absolute value.
            Console.WriteLine("Absolute min(v2) = {0}", 
                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}",
                v2.MinIndex());

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

            Console.Write("Press Enter key to exit...");
            Console.ReadLine();
        }
    }
}
Overview
Introduction
Features
Documentation
QuickStart Samples
Sample Applications
Downloads
Get it now!
Download trial version
How to Buy
Search

"The Extreme Optimization Statistics Library for .NET is a major boon for those doing statistical work in .NET. I strongly recommend this product."
- Marc Brooks

"I have made it my mission to institutionalize the value of good API design.  I strongly believe that this is key to making developers more productive and happy on our platform. It is clear that you value good API design in your work, and take to heart developer productivity and synergy with the .NET framework."
- Brad Abrams,
Lead Program Manager, Microsoft.

This is a partial list of companies who are using our libraries:
ABB Robotics
Allstate
Applied Materials
Arcam
Astra Schedule
Babson College
Canadian Council on Learning
Canyon Associates
Caxton Associates
CECity
Constellation Energy
CreditSights
DeepOcean
Duke University
Dynamotive
Elecsoft
Engelhard Corporation
Epcor
Equipoise Software
Galileo International
GAM UK
Gammex
GlaxoSmithKline
Global Matrix
The Hartford
Infinera Corporation
Intel
JDS Uniphase
LaBranche & Co.
Learning & Skills Council
Jacobs Consultancy
Litman Gregory
Lucas Systems
Malvern Instruments
Medrio
Merck & Co.
Mintera.
Monitor Software
MorningStar
NanoString Technologies
Paletta Invent
Parametric Portfolio Associates
Prosanos
RATA Associates
RiskShield
Ramboll
Standard & Poor's
Strategic Analysis Corporation
Univ. of Alicante
Univ. of South Carolina
vielife
Xerox
US Army