Brief C# Complex Number Tutorial

The C# language supports a complex number type. Complex numbers aren’t used very often in machine learning, but they are needed when computing eigenvalues and eigenvectors of a matrix because an eigenvalue can be, and often is, a complex number.

Computing eigenvalues and eigenvectors of a matrix is one of the most difficult tasks in numerical computing. Eigenvalues and eigenvectors apply only to square matrices. Luckily, if a source marix is symmetric, all the eigenvalues are ordinary real values, and computing the eigenvalues is merely extemely difficult (rather than nutso-difficult) via several algorithms (Jacobi, QR, etc.) But for a general, non-symmetric square matrix, computing the eigenvalues is mind-boggling tricky.

I’ve never been motivated to spend the several days needed to implement a C# function to compute the eigenvalues of a general matrix, in part because of having to deal with complex numbers. But C# has a complex number type and so I figured I’d review the type in case I ever decide to bite the bullet and implement a function to compute the eigenvalues of a general matrix.

I try not to criticize the good-faith efforts of others, but the official Microsoft documentation introduction to the Complex type I found was . . well . . not very good at all, to put it mildly. So, I made a C# Complex type quick tutorial. The output of the demo:

Begin C# complex numbers demo

c1 = {6.5; 2.4i}
c2 = {7.9; 3.1i}

c1 + c2 = {14.4; 5.5i}

c1 * c2 = {43.9100; 39.1100i}

Sqrt(c1) = {2.5912; 0.4631i}

Conjugate of c1 = {6.5; -2.4i}

Example complex vector:
{1.00; 1.00i} {2.00; 2.00i} {3.00; 3.00i}

End demo

Complex numbers are paradoxically simple and complicated. Simple operations such as addition and conjugate are easy. But operations such as the square root of a complex number are surprisingly tricky and unobvious.

I implemented an AsString() function to display a Complex value in a non-standard way, but the function can be easily modified.

If I ever get around to tackling the eigenvalues of a general matrix using C#, where the eigenvalues can be complex numbers, I’ll probably refactor the GNU code at https://fossies.org/dox/gsl-2.8/nonsymmv_8c_source.html — but I know it will be a difficult, multi-day project.



I remember first learning about complex numbers in my senior year in high school (Servite HS in Anaheim, California). I thought complex numbers were strange and mysterious.

I enjoy learning about old gambling machines. This is “The Mysterious Eye”. It was made by the Western Equipment and Supply Company, St. Louis, from 1933-1935. The payout table isn’t visible and I could find no solid information about the machine, so it will have to remain a mystery to me for now.


Demo program:

using System;
using System.Numerics;

namespace CSharpComplexTutorial
{
  internal class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("\nBegin C# complex numbers demo ");

      Complex c1 = new Complex(6.5, 2.4);
      Complex c2 = new Complex(7.9, 3.1);

      Console.WriteLine("\nc1 = " + AsString(c1, 1));
      Console.WriteLine("c2 = " + AsString(c2, 1));

      // (a+bi) + (c+di) = (a+c) + (b+d)i
      Complex c3 = c1 + c2;
      Console.WriteLine("\nc1 + c2 = " + AsString(c3, 1));

      // (a+bi) * (c+di) = (ac - bd) + (ad + bc)i 
      Complex c4 = c1 * c2;
      Console.WriteLine("\nc1 * c2 = " + AsString(c4, 4));

      // note: (a+bi) / (c+di) =
      // (ac + bd)/(c^2 + d^2) + ((bc - ad)/(c^2 + d^2))i

      // Pow(), Cos(), Exp(), Sqrt(), etc. are very tricky

      Complex c5 = Complex.Sqrt(c1);  // tricky
      Console.WriteLine("\nSqrt(c1) = " + AsString(c5, 4));

      // (a + bi) -- (a + -bi)
      Complex c6 = Complex.Conjugate(c1);
      Console.WriteLine("\nConjugate of c1 = " + AsString(c6, 1));

      Complex[] vec = new Complex[3]; // complex vector
      vec[0] = new Complex(1.0, 1.0);
      vec[1] = new Complex(2.0, 2.0);
      vec[2] = new Complex(3.0, 3.0);
      Console.WriteLine("\nExample complex vector: ");
      foreach (Complex c in vec)
        Console.Write(AsString(c, 2) + " ");
      Console.WriteLine("");
 
      Console.WriteLine("\nEnd demo ");
      Console.ReadLine();

    } // Main()

    static string AsString(Complex c, int decimals)
    {
      return "{" + c.Real.ToString("F" + decimals) +
        "; " + c.Imaginary.ToString("F" + decimals) +
        "i" + "}";
    }

  } // class Program

} // ns
This entry was posted in Machine Learning. Bookmark the permalink.

Leave a Reply