Click here to Skip to main content
13,189,324 members (66,061 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 26 Oct 2007

A C# Class to Perform Arithmetic on Complex Numbers

, 14 Nov 2007
Rate this:
Please Sign up or sign in to vote.
A C# class to perform arithmetic on complex numbers


Although the C# language has been used quite successfully in many kinds of projects, it still remains to be proven proficient for scientific computing. Questions remain as to whether or not C# can measure up to the likes of FORTRAN and C++ for scientific and mathematical projects.

Complex Arithmetic

Arithmetic and algebra on complex numbers is important in many fields of science and engineering. Even though C# does not support an intrinsic complex number data type, it doesn't stop you from creating your own. You can do so using a struct, which has value semantics. The purpose of this article is to present my attempt to fashion a complex arithmetic class for C#.

Using the Code

The struct Complex is completely contained within the namespace ComplexMath in the file Complex.cs. There are three separate program files included in the download as well as a documented help file for the project. You can run the program either by using the ComplexMath.dll reference, or by using the ComplexMath library code directly by simply adding the Complex.cs file to a project. You must, in either case, add the header using ComplexMath; to the main project file. If you decide to go the DLL route, first build the library (DLL) by running the CompexMath.sln file from which you can generate two ComplexMath.dll files, one each in bin/Release and bin/Debug.

Steps to Use the Library as a DLL

  1. Start a new C# console application.
  2. Build both the Debug and Release versions.
  3. Place the Debug and Release versions of the ComplexMath.dll in their respective bin folders.
  4. From Project/Add Reference menus, find the ComplexMath.dll for the version (debug or release) and add it to the project.
  5. Add the header: using ComplexMath; to your program file.
  6. Use the accompanying TestComplexMath.cs file as a prototype for your own program.

Your test program should look something like the one below which briefly demonstrates many of the capabilities of the library:

using System.Collections.Generic;
using System.Text;
using ComplexMath;

namespace TestLib
    class Program
        static void Main(string[] args)
            Complex z1, z2, z3;            // instantiation
            z1 = new Complex(1.0, -2.0);
            z2 = new Complex(-3.0, -5.0);
            z3 = new Complex(0.292342, -0.394875);            
            Console.WriteLine("instantiated complex numbers:");
            Console.WriteLine(string.Format("z1 = {0}", z1));
            Console.WriteLine(string.Format("z2 = {0}", z2));
            Console.WriteLine(string.Format("z3 = {0}", z3));            
            // random complex numbers
            z1 = Complex.Random();
            z2 = Complex.Random();
            z3 = Complex.Random();            
            Console.WriteLine("random generated:");
            Console.WriteLine(string.Format("z1 = {0}", z1));
            Console.WriteLine(string.Format("z2 = {0}", z2));
            Console.WriteLine(string.Format("z3 = {0}", z3));            
            Console.WriteLine("basic properties:");
		("z1.real = {0}  z1.imag = {1}", z1.real, z1.imag));
		("complex conjugate of z1 = {0}.", z1.Conjugate));
		("complex modulus of z1 = {0}.", z1.Modulus));
		("complex argument of z1 = {0}.", z1.Argument));   // operators
            z3 = z1 + z2; Console.WriteLine(string.Format("z1 + z2 = {0}", z3));
            z3 = z1 - z2; Console.WriteLine(string.Format("z1 - z2 = {0}", z3));
            z3 = z1 * z2; Console.WriteLine(string.Format("z1 * z2 = {0}", z3));
            z3 = z1 / z2; Console.WriteLine(string.Format("z1 / z2 = {0}", z3));
            // transcendental functions
            Console.WriteLine("transcendental functions:");
            z3 = Complex.Sin(z1); Console.WriteLine(string.Format("Sin(z1) = {0}", z3));
            z3 = Complex.Cos(z1); Console.WriteLine(string.Format("Cos(z1) = {0}", z3));
            z3 = Complex.Tan(z1); Console.WriteLine(string.Format("Tan(z1) = {0}", z3));
            z3 = Complex.Sinh(z1); 
	   Console.WriteLine(string.Format("Sinh(z1) = {0}", z3));
            z3 = Complex.Cosh(z1); 
	   Console.WriteLine(string.Format("Cosh(z1) = {0}", z3));
            z3 = Complex.Tanh(z1); 
	   Console.WriteLine(string.Format("Tanh(z1) = {0}", z3));
            z3 = Complex.Exp(z1); Console.WriteLine(string.Format("Exp(z1) = {0}", z3));
            z3 = Complex.Log(z1); Console.WriteLine(string.Format("Log(z1) = {0}", z3));
            z3 = Complex.Log10(z1); 
	   Console.WriteLine(string.Format("Log10(z1) = {0}", z3));     // roots
            z3 = Complex.Sqrt(z1); 
	   Console.WriteLine(string.Format("Sqrt(z1) = {0}", z3));            
	   Console.WriteLine("k 4th roots:");
            for (long k = 0; k < 4; k++)
                z3 = Complex.NthRoot(z1, 4, k);
                Console.WriteLine(string.Format("NthRoot(z1, 4, {0}) = {1}",k, z3));
            }            Console.WriteLine("the five 5th roots of unity:");
            Complex z = new Complex(1.0, 0.0);
            for (long k = 0; k < 5; k++)
                z3 = Complex.NthRoot(z, 5, k);
			(string.Format("{0}th 5th root of unity = {1}", k, z3));
            // powers
            int n = 7;
            z3 = Complex.Pow(z1, z2); 
            Console.WriteLine(string.Format("Complex.Pow(z1, z2) = {0}", z3));
            z3 = Complex.Pow(z1, n); 
            Console.WriteLine(string.Format("Complex.Pow(z1, {0}) = {1}", n, z3));
            // display
            Console.WriteLine(string.Format("polar form of z1 = {0}.", z1.Polar()));
            // debug tracing
    }// Program
}// TestLib

The accompanying TestLib program demonstrates all of the capabilities of the Complex struct. One point worth making is that complex numbers can be expressed both in a rectangular form that most high-school algebra students are familiar with and in the more sophisticated polar form which requires some more advanced understanding of trigonometry, De Moivre's Theorem and Euler's Identity. If you are interested, check them out on the Web.

Note that all of the initialization input assumes that you are using the rectangular form of the complex number, which implies that real is measured along the x-axis of the Argand plane and imaginary is measured along the y-axis of that plane. However, the program has the capacity to output the components of the polar form -- that is, the modulus |z| and the argument (theta) -- by using the ShowPolar() and Polar() methods and the Argument and Modulus properties (see above).

Since complex numbers can be added, subtracted, multiplied and divided, it is convenient to have operators to accomplish those ends. Thus, we have incorporated those operators.


There appears to be no shortage of postings offering C# code for dealing with complex numbers. These include:

Although I have been involved in scientific mathematical and statistical programming for over 10 years, this article is the first that I have attempted to post. I quickly learned that there are many smarter and better programmers out there who have done a much better job at using C#. For me, this was an excellent learning experience, both in terms of learning some of the pitfalls of scientific computing with C# and having to explain to others what my code does.


The fact that C# class assignment operators which are automatically generated and cannot be overridden create something of a problem, particularly if one is coming from a C++ background. Once an assignment is made, the assignor and the assignee are locked together by a common pointer. Change one, you change the other. In order to work around that, one would have to implement ICloneable which can be rather tedious and complicated. For that reason, I chose to use the struct rather than a class. I am still learning about C#.

I plan to update and improve this code for my own use and for any others who wish to use it, and to do a better job in the future. You may use this code in any manner that you wish. It is always polite and professional to give the original author credit. Please report any comments, criticisms or bugs to the forum below.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mike Pliam
United States United States
Born in the USA

You may also be interested in...


Comments and Discussions

SuggestionVery good class. Pin
CFSW25-Aug-14 4:46
memberCFSW25-Aug-14 4:46 
Generalcomplex atan Pin
joerghaus23-Nov-08 3:36
memberjoerghaus23-Nov-08 3:36 
GeneralRe: complex atan Pin
Michael B Pliam25-Nov-08 8:43
memberMichael B Pliam25-Nov-08 8:43 
GeneralRe: complex atan Pin
joerghaus26-Nov-08 7:11
memberjoerghaus26-Nov-08 7:11 
GeneralThanks for the code Pin
Herbert Sauro1-May-08 19:26
memberHerbert Sauro1-May-08 19:26 
GeneralRe: Thanks for the code Pin
Michael B Pliam5-May-08 10:28
memberMichael B Pliam5-May-08 10:28 
GeneralReinventing the Wheel Pin
Dmitri Nesteruk15-Nov-07 12:44
memberDmitri Nesteruk15-Nov-07 12:44 
GeneralRe: Reinventing the Wheel Pin
Mike Pliam16-Nov-07 10:51
memberMike Pliam16-Nov-07 10:51 
GeneralRe: Reinventing the Wheel Pin
tbg1010116-Nov-14 18:43
membertbg1010116-Nov-14 18:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171016.2 | Last Updated 14 Nov 2007
Article Copyright 2007 by Mike Pliam
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid