Serge's World

Blogging about software development, astronomy, genealogy and more.

Vectors

A vector is simply a 1-dimensional array of numbers that often refer to coordinates, or directions. The theory of vectors is best left to wikipedia.

Implementing a vector class in C# is pretty easy. The vector is stored as an array within the class. The constructor can either set this array directly, or initialises it to a particular size.

The Zero() function sets the entire vector to 0. The Abs() function calculates the absolute value of the vector, which is the square root of the sum of the squares of the components of the vector. The Dot() function returns the dot product of the vector, with another vector. The dot product is the sum of the products of each component in the vectors. The Scale() function scales the vector by the scaling factor supplied. The + and - operators are overloaded to make it easy to add and subtract vectors.

Each component of first vector is added (or subtracted) to the equivalent component of the second vector, and then the resulting vector is returned.

namespace MathLib
{
    public class Vector
    {
        public double[] Components;
        public int Dimension;

        public Vector(double[] Values)
        {
            Components = Values;
            Dimension = Components.GetLength(0);
        }

        public Vector(int dimension)
        {
            Components = new double[dimension];
            Dimension = dimension;
            Zero();
        }


        public void Zero()
        {
            for(int i = 0; i < Dimension; i++)
            {
                Components[i] = 0;
            }
        }

        public double Abs()
        {
            double abs = 0;

            for (int i = 0; i < Dimension; i++)
            {
                abs += Components[i] * Components[i];
            }
            return Math.Sqrt(abs);
        }

        public double Dot(Vector vector)
        {
            if (vector.Dimension != Dimension)
            {
                return 0;
            }

            double dotProduct = 0;
            for (int i = 0; i < Dimension; i++)
            {
                dotProduct += Components[i] * vector.Components[i];
            }
            return dotProduct;
        }

        public Vector Scale(double factor)
        {
            double[] values = new double[Dimension];

            for (int i = 0; i < Dimension; i++)
            {
                values[i] = Components[i] * factor;
            }
            return new Vector(values);
        }

        public override string ToString()
        {
            string sStr = "";
            for (int i = 0; i < Dimension; i++)
            {
                if (i != 0)
                {
                    sStr += ", ";
                }
                sStr += Components[i];
            }
            return "(" + sStr + ")";
        }


        public static Vector operator +(Vector left, Vector right)
        {
            if (left.Dimension != right.Dimension)
            {
                return left;
            }
            double[] values = new double[left.Dimension];

            for (int i = 0; i < left.Dimension; i++)
            {
                values[i] = left.Components[i] + right.Components[i];
            }
            return (new Vector(values));
        }

        public static Vector operator -(Vector left, Vector right)
        {
            if (left.Dimension != right.Dimension)
            {
                return left;
            }
            double[] values = new double[left.Dimension];

            for (int i = 0; i < left.Dimension; i++)
            {
                values[i] = left.Components[i] - right.Components[i];
            }
            return (new Vector(values));
        }

    }
}

The full sourcecode for the MathLib library is available at https://github.com/sjmeunier/mathlib

Originally posted on my old blog, Smoky Cogs, on 23 Oct 2009

Updated 5 Oct 2016: Updated code snippet after refactoring MathLib library

Tag Cloud

Algorithms (3) Android (10) Astronomy (25) Audio (1) Audiobooks (1) Barcodes (9) C# (69) Css (1) Deep sky (6) Esoteric languages (3) Family (3) Fractals (10) Gaming (1) Genealogy (14) General (2) Geodesy (3) Google (1) Graphics (3) Hubble (2) Humour (1) Image processing (23) Java (8) Javascript (5) jQuery (3) Jupiter (3) Maths (22) Moon (5) Music (4) Pets (5) Programming (88) Saturn (1) Science (1) Spitzer (4) Sun (4) Tutorials (68) Unity (3) Web (9) Whisky (13) Windows (1) Xamarin (2)