Introduction

As of .NET 4.0, the native library does offer a complex class in the namespace System.Numerics. The ideal situation had been to write the formulas in a way that they are written in for instance Matlab or Matematica.

The solution I have tried is to use Regular Expressions, and write the formula as a simple string, convert the string to a series of complex numbers, perform calculations and give out the calculated result.

The evaluator is more general than to just do calculations on complex numbers however. It can also function as a normal calculator with just real numbers.

RegEx Pattern for Complex and Real Numbers

The pattern to recognize the use input is always on the form a+bi and is defined as a complex pattern which we need to recognize. There are basically three main ways to write a complex number:

a+bi Containing both a real part and an imaginary part

a Only a real part

bi Only a real imaginary

Both a and b are real numbers (in native .NET, they can have the form of Double, Decimal or Integer). The Regular Expression for a double number can be found on the web, but they rarely show a general RegEx for finding a general computer number in succession, separated by mathematical operators. A simple example of an input gives an idea of what is needed in recognizing the main pattern of numbers of computer format:

3.567E-10+4.89E+5i-.1

The correct interpretation of this input should be:

0.00000000003567

489000i

-0.1

The general expression for a number that can either be a real or Imaginary can be written as follows:

The two regular expressions can be broken down with the following assumptions:

A mathematical operator (+*/^) will preside and operators (-+*/^) follow the actual number.

A number can start with the operator "–" (we don't need the "+" operator as a number is positive by default

It is not a standalone number if it is preceded with the letter E or if it is immediately followed by E.

To separate the Real and imaginary numbers is just a small difference at the end. It is Real if the number is not preceded with the letter “i” and imaginary if it is. All the other code is taken in to force the regular expression to include the full number.

Complex numbers on the other hand usually come in pairs of real and imaginary, so we need to write a RegEx that understands this, and only parses the number as a standalone real or imaginary if it can’t find the pairs. The match will occur in the following pair with matches would be returned in this order:

Evaluator

The original evaluator is written by Francesco Balena in the book “Programming Microsoft Visual Basic .NET” (2003) Pages 505 – 509. It was basically a calculator that dealt with real numbers, and this code is altered to take complex numbers. The architecture is basically the same.

There are two different kinds, as we might encounter numbers that are written in the following way: 5+8i^2. This means that the NumType will read it as (5+8i)^2 with is wrong, therefore the need for NumTypeSingle.

Next, we define all the functions and operators that we will support with this evaluator:

There are two things in the code that I did not mention yet. We need to cast the string to an actual complex number, and by default the System.Numerics.Complex.ToString returns (Real,Imaginary), and we don’t want it in that form. And second, we actually have to cast the matched string as a Complex number.