Serge's World

Blogging about software development, astronomy, genealogy and more.

Complex Numbers

Complex numbers are a vital part of many mathematical calculations, yet Visual Studio does not natively support complex numbers, but fortunately, this is a very easy oversight to fix.

First, a little maths lesson. Imaginary numbers are numbers that do not exist, like for example the square root of -1. This number is denoted by i and is central to complex numbers. Complex numbers are defined as a number that is made up of an imaginary component as well as a real component, and is denoted by the formula x*i + y.

So some examples of complex numbers are 5i + 4, 2i + 0 (which simplifies to 2i), and 0i + 6 (which simplifies to simply 6).

What the above example shows as well, is that the range of complex numbers also includes all the real numbers.

The complex number class contains two variables to contain the real and imaginary components, namely, re and im. Most of the rest of the class are overloaded arithmetic operators.

The ^ operator takes a complex number and raises it to a specified power, returning a new complex number.

The + and - operators add and subtract two complex numbers, which entails adding or subtracting the real and imaginary parts of the one to the real and imaginary parts of the other complex number.

The * and / operators are a bit more complicated in how they are calculated, but the net result is the multiplication and division of the complex numbers.

The Abs() function returns the absolute value of the complex number, which is the square root of the sum of the squares of the real and imaginary components - very much like the absolute value of a normal vector.

Lastly, the Arg() function returns the arg of the complex number in degrees, which allows the complex number to be expressed in polar notation.

namespace MathLib
{

    public class Complex
    {
        public double Real;
        public double Imaginary;

        public Complex(double real, double imaginary)
        {
            Real = real;
            Imaginary = imaginary;
        }

        public static Complex operator ^(Complex arg1, int arg2)
        {
            int i = 0;
            Complex x = new Complex(0.0, 0.0);

            if (arg2 == 0)
            {
                return x;
            }
            else
            {
                x = arg1;
                for (i = 1; i < arg2; i++)
                {
                    x = x * arg1;
                }
                return x;
            }
        }


        public static Complex operator +(Complex arg1, Complex arg2)
        {
            return (new Complex(arg1.Real + arg2.Real, arg1.Imaginary + arg2.Imaginary));
        }

        public static Complex operator -(Complex arg1)
        {
            return (new Complex(-arg1.Real, -arg1.Imaginary));
        }

        public static Complex operator -(Complex arg1, Complex arg2)
        {
            return (new Complex(arg1.Real - arg2.Real, arg1.Imaginary - arg2.Imaginary));
        }

        public static Complex operator *(Complex arg1, Complex arg2)
        {
            return (new Complex(arg1.Real * arg2.Real - arg1.Imaginary * arg2.Imaginary, arg1.Real * arg2.Imaginary + arg2.Real * arg1.Imaginary));
        }

        public static Complex operator /(Complex arg1, Complex arg2)
        {
            double c1, c2, d;
            d = arg2.Real * arg2.Real + arg2.Imaginary * arg2.Imaginary;
            if (d == 0)
            {
                return (new Complex(0, 0));
            }
            c1 = arg1.Real * arg2.Real + arg1.Imaginary * arg2.Imaginary;
            c2 = arg1.Imaginary * arg2.Real - arg1.Real * arg2.Imaginary;
            return (new Complex(c1 / d, c2 / d));
        }

        public double Abs()
        {
            return (Math.Sqrt(Real * Real + Imaginary * Imaginary));
        }

        //Arg of complex number in degrees

        public double Arg()
        {
            double ret = 0;
            if (Real != 0)
                ret = (180 / Math.PI) * Math.Atan(Imaginary / Real);
            return (ret);
           
        }

        

        public override string ToString()
        {
            return (String.Format("Complex: ({0}, {1})", Real, Imaginary));
        }

        public string ToComplexString(int iRounding)
        {
            string ComplexNumber = Convert.ToString(Math.Round(Real, iRounding)) + "+" + Convert.ToString(Math.Round(Imaginary, iRounding)) + "i";
            return ComplexNumber;
        }
    }
}

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)