Extreme Optimization > QuickStart Samples > Basic Vectors QuickStart Sample (C#)

Extreme Optimization QuickStart Samples

Basic Vectors QuickStart Sample (C#)

Illustrates the basic use of the Vector class (Extreme.Mathematics.LinearAlgebra namespace) for working with vectors 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;

    /// <summary>
    /// Illustrates the use of the Vector class in the 
    /// Extreme.Mathematics.LinearAlgebra namespace.
    /// </summary>
    class BasicVectors
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            //
            // Constructing vectors
            //

            // Option #1: specify the number of elements. All
            // components are set to 0.
            Vector v1 = new GeneralVector(5);
            // Option #2: specify the components:
            Vector v2 = new GeneralVector(1, 2, 3, 4, 5);
            // Option #3: specify the components as a double array.
            // By default, the components are copied to a storage
            // area internal to the Vector.
            double[] components = new double[] {1, 2, 3, 4, 5};
            Vector v3 = new GeneralVector(components);
            // Option #4: same as above, but specify whether
            // to copy the components, or use the array as 
            // internal storage.
            Vector v4 = new GeneralVector(components, false);
            // Changing a value in the original vector changes
            // the resulting vector.
            Console.WriteLine("v4 = {0}", v4);
            components[3] = 1;
            Console.WriteLine("v4 = {0}", v4);
            // Option #5: same as #3, but specify the length of
            // the Vector. The remaining elements in the component
            // array will be ignored.
            Vector v5 = new GeneralVector(4, components);
            // Option #6: same as #5, but specify whether
            // to copy the components, or use the array as 
            // internal storage.
            Vector v6 = new GeneralVector(4, components, false);

            //
            // Vector properties
            //
            
            // The Length property gives the number of components
            // of a Vector:
            Console.WriteLine("v1.Length = {0}", v1.Length);
            // The GetComponents method returns a double array
            // that contains the components of the vector.
            // This is always a copy:
            components = v2.GetComponents();
            Console.WriteLine("Effect of shared storage:");
            Console.WriteLine("v2[2] = {0}", v2[2]);
            components[2] = 1;
            Console.WriteLine("v2[2] = {0}", v2[2]);

            //
            // Accessing vector elements
            //

            // The Vector class defines an indexer property that
            // takes a zero-based index.
            Console.WriteLine("Assigning with private storage:");
            Console.WriteLine("v1[2] = {0}", v1[2]);
            // You can assign to this property:
            v1[2] = 7;
            Console.WriteLine("v1[2] = {0}", v1[2]);
            // The vectors v4 and v6 had the copy parameter in the
            // constructor set to false. As a result, they share 
            // their component storage. Changing one vector also 
            // changes the other:
            Console.WriteLine("Assigning with shared storage:");
            Console.WriteLine("v4[1] = {0}", v4[1]);
            v6[1] = 7;
            Console.WriteLine("v4[1] = {0}", v4[1]);

            // The SetValue method sets all components of a vector
            // to the same value:
            v1.SetValue(1);
            Console.WriteLine("v1 = {0}", v1);
            // The Zero method sets all components to 0:
            v1.Zero();
            Console.WriteLine("v1 = {0}", v1);

            //
            // Copying and cloning vectors
            //

            // A shallow copy of a vector constructs a vector
            // that shares the component storage with the original.
            // This is done using the ShallowCopy method:
            Console.WriteLine("Shallow copy vs. clone:");
            Vector v7 = v2.ShallowCopy();
            // The clone method creates a full copy.
            Vector v8 = v2.Clone();
            // When we change v2, v7 changes, but v8 is left
            // unchanged.
            Console.WriteLine("v2[1] = {0}", v2[1]);
            v2[1] = -2;
            Console.WriteLine("v7[1] = {0}", v7[1]);
            Console.WriteLine("v8[1] = {0}", v8[1]);
            // We can give a vector its own component storage
            // by calling the CloneData method:
            Console.WriteLine("CloneData:");
            v7.CloneData();
            // Now, changing the original v2 no longer changes v7:
            v2[1] = 4;
            Console.WriteLine("v7[1] = {0}", v7[1]);
            // The CopyTo method copies the components of a Vector
            // to a variety of destinations. It may be a Vector:
            Console.WriteLine("CopyTo:");
            v6.CopyTo(v1);
            Console.WriteLine("v6 = {0}", v6);
            Console.WriteLine("v1 = {0}", v1);
            // You can specify an index where to start copying
            // in the destination vector:
            v6.CopyTo(v1, 1);
            Console.WriteLine("v1 = {0}", v1);
            // Or you can copy to a double array:
            v6.CopyTo(components);

            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
Information
Resources
Contact Us
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