Extreme Optimization > User's Guide > Mathematics Library > Complex Numbers

Extreme Optimization User's Guide

User's Guide

Up: Mathematics Library Next: Curves Previous: Numerical Algorithms Support Classes Contents

Complex Numbers

Complex numbers arise in algebra in the solution of quadratic equations. The equation x2 = 1 does not have any real solutions. However, if we define a new number, i as the square root of -1, then we have two solutions: i and -i. This, in turn, gives rise to an entirely new class of numbers of the form a + ib with a and b real, and i defined as above. These are the complex numbers.

The Extreme Optimization Mathematics Library for .NET provides value types for single and double precision complex numbers. The DoubleComplex value type represents double-precision complex numbers. Value types are implemented using structures.

Constructing complex numbers

The DoubleComplex structure has three constructors.

The first constructor takes two arguments: the real and imaginary parts of the complex number.

C# CopyCode imageCopy Code
DoubleComplex a = new DoubleComplex(2, 4);
Visual Basic CopyCode imageCopy Code
Dim a As DoubleComplex = New DoubleComplex(2, 4)

The second constructor takes one real argument. It constructs a complex number whose imaginary part is zero.

C# CopyCode imageCopy Code
DoubleComplex b = new DoubleComplex(-3);
Visual Basic CopyCode imageCopy Code
Dim b As DoubleComplex = New DoubleComplex(-3)

The third constructor takes three arguments: two real numbers and a boolean parameter named polar. If polar is true, the first two arguments are treated as the modulus and argument of the complex number. If polar is false, the first two arguments are treated as the real and imaginary part of the complex number.

C# CopyCode imageCopy Code
DoubleComplex c = new DoubleComplex(2, Constants.Pi/3, true);
Visual Basic CopyCode imageCopy Code
Dim c As DoubleComplex = New DoubleComplex(2, Constants.Pi/3, True)

Finally, the static RootOfUnity method constructs a complex number that is one of the solutions to the equation xn = 1. The solutions to this equation are n complex numbers spaced equally on the unit circle. The first parameter of the method is the degree, n, of the root. The second parameter is the index of the root in the series, counting counter-clockwise on the unit circle. An index of 0 corresponds to the root x = 1.

DoubleComplex constants

The complex structure provides several constants for commonly used and special complex numbers. These are listed in the following table:

Field Description
I The number i, the square root of 1.
Zero DoubleComplex zero: 0 + 0i.
One DoubleComplex one: 1 + 0i.
Infinity DoubleComplex infinity.
NaN DoubleComplex Not-a-Number.
Table 1. Complex number constants.

Two values in the above list deserve special attention. The Infinity field represents complex infinity. It is the result of dividing any non-zero complex number by zero. DoubleComplex infinity does not have a sign. Because the complex numbers do not have a natural ordering, is does not make sense to speak of positive or negative numbers. Directed infinities, where the modulus of the complex number is infinite, but its argument is not, are not supported. The static IsInfinity method verifies whether a complex number equals DoubleComplex.Infinity .

The NaN field represents complex Not-a-Number. It is a special value that is the result of dividing zero by zero.The static IsNaN method verifies whether a complex number equals DoubleComplex.NaN. You can not use the equality operator for this purpose, as it always returns false.

Working with complex numbers

Working with complex numbers is easy. The Re property gets the real component of the complex number, while Im gets the imaginary part. The Modulus property returns the square root of the sum of the squares of the real and imaginary components. The Argument property returns the angle between the positive real axis and a line from the origin to the complex number, measured counter-clockwise.

The modulus is computed every time the Modulus property is called. The same is true for the Argument property. If you use these values multiple times, you should consider caching them in a variable.

Details of complex arithmetic

When performing binary operations, if one of the operands is a DoubleComplex, then the other operand is required to be an integral type or a floating-point type (Double or Single) or a complex type (DoubleComplex). Prior to performing the operation, if the other operand is not a DoubleComplex, it is converted to DoubleComplex, and the operation is performed using at least DoubleComplex range and precision. If the operation produces a numerical result, the type of the result is DoubleComplex. Exceptions to this are methods that return a real property of a number: Argument, Modulus, ModulusSquared and Abs. In these cases, the return type is Double.

The Extreme Optimization Mathematics Library for .NET extends the IEEE-754 standard definition of NaN's for real numbers to complex numbers as follows.

The floating-point operators, including the assignment operators, do not throw exceptions. Instead, in exceptional situations, the result of a floating-point operation is zero, Infinity, or NaN, as described below:

Arithmetic operations

The Extreme Optimization Mathematics Library for .NET provides methods for all basic arithmetic operators on complex numbers. Most contain special versions for cases where one of the operands is real. Overloaded versions of the arithmetic operators are provided for languages that support them. For languages that don't support operator overloading, equivalent static (Shared in Visual Basic) methods are supplied.

For example, here is some C# code:

[C#]
DoubleComplex a = new DoubleComplex(1, 2);
DoubleComplex b = new DoubleComplex(-3, 4);
DoubleComplex c = 2 - 1 / (a + b);

and here is the equivalent Visual Basic.NET code:

[Visual Basic]
Dim a As DoubleComplex = New DoubleComplex(1, 2)
Dim b As DoubleComplex = New DoubleComplex(-3, 4)
Dim c As DoubleComplex = _
DoubleComplex.Subtract(2, DoubleComplex.Divide(1, DoubleComplex.Add(a, b)))

Specific to complex numbers is the static Conjugate method. This method returns the conjugate of a complex number, leaving the original unchanged.

Operator Static method equivalent Description
+z (no equivalent) Returns the complex number z.
-z DoubleComplex.Negate(z) Returns the negation of the complex number z.
z1 + z2 DoubleComplex.Add(z1, z2) Adds the complex numbers z1 and z2.
z + a DoubleComplex.Add(z, a) Adds the complex number z and the real number a.
a + z DoubleComplex.Add(a, z) Adds the real number a to the complex number z.
z++ (no equivalent) Increments the complex number z by one.
z1 - z2 DoubleComplex.Subtract(z1, z2) Subtracts the complex numbers z1 and z2.
z - a DoubleComplex.Subtract(z, a) Subtracts the real number a from the complex number z.
a - z DoubleComplex.Subtract(a, z) Subtracts the complex number z from the real number a.
z-- (no equivalent) Decrements the complex number z by one.
z1 * z2 DoubleComplex.Multiply(z1, z2) Multiplies the complex numbers z1 and z2.
z * a DoubleComplex.Multiply(z, a) Multiplies the complex number z and the real number a.
a * z DoubleComplex.Multiply(a, z) Multiplies the real number a and the complex number z.
z1 / z2 DoubleComplex.Divide(z1, z2) Divides the complex number z1 by z2.
z / a DoubleComplex.Divide(z, a) Divides the complex number z by the real number a.
a / z DoubleComplex.Divide(a, z) Divides the real number a by the complex number z.
~z DoubleComplex.Conjugate(z) Returns the complex conjugate of the complex number z.
Table 2. Complex number operators and their static (Shared) method equivalents.

There is one other static method that does not have an operator equivalent. The ConjugateMultiply method returns the product of the conjugate of the first argument and the second argument.

Because the complex numbers don't have a natural ordering, only equality and inequality operators are available. No other comparison operators are available.

Functions of complex numbers

The DoubleComplex type defines static methods for the most common mathematical functions of complex numbers, including: logarithmic, exponential, trigonometric and hyperbolic functions.

Some functions of real numbers have a limited domain when the result is restricted to be real, but are defined for all real numbers if the result can be complex. Examples of this are the inverse sine and cosine, which are real only for arguments between -1 and +1. The DoubleComplex type defines methods that extend these functions to return a complex result.

These functions are often discontinuous along the part of the real axis where they are complex-valued. Which of the two limit values is chosen is arbitrary. However, the most consistent choice is the one that preserves any symmetry or anti-symmetry about the origin. For example, the function Arcsin(x) satisfies Arcsin(-x) = -Arcsin(x) when x is within [-1, 1]. The Arcsin method with real argument also satisfies this identity.

Most elementary functions have been extended to cover the entire complex plane. Once again, many of them are multi-valued, and a suitable choice has to be made regarding which of the two possible limit values is returned. The choice is made easier by the fact that the discontinuities lie along the real or imaginary axis. This makes it possible to distinguish between the 'limit from above' and the 'limit from below' by using the value of negative zero for the limit from below. The situation is somewhat simplified by the fact that any analytic function satisfies the identity f(conj(z) = conj(f(z)).

The tables below summarize these methods, and their meaning. Special functions with complex argument may be available from the Extreme.Mathematics.SpecialFunctions namespace.

Method Description
DoubleComplex.Exp(z) The number E raised to the complex power z.
DoubleComplex.ExpI(a) The number E raised to I times the real number a.
DoubleComplex.RootOfUnity(n,i) The number E raised to I times TwoPi*i/n.
DoubleComplex.Sqrt(z) The first square root of the complex number number z.
DoubleComplex.Sqrt(a) The first square root of the real number number a, which may be negative.
DoubleComplex.Pow(z1,z2) The complex number z1 raised to the complex power z2.
DoubleComplex.Pow(z,a) The complex number z raised to the real power a.
DoubleComplex.Pow(z,n) The complex number z raised to the integer power n.
DoubleComplex.Log(z) Natural logarithm of the complex number z.
DoubleComplex.Log(z1,z2) Base z1 logarithm of the complex number z2.
DoubleComplex.Log10(z) Base 10 (common) logarithm of the complex number z.
Table 3. Logarithmic and exponential functions of complex numbers.

Method Description
DoubleComplex.Sin(z) Sine of the complex number z.
DoubleComplex.Cos(z) Cosine of the complex number z.
DoubleComplex.Tan(z) Tangent of the complex number z.
DoubleComplex.Asin(z) Inverse sine of the complex number z.
DoubleComplex.Asin(a) Inverse sine of the real number a.
DoubleComplex.Acos(z) Inverse cosine of the complex number z.
DoubleComplex.Acos(a) Inverse cosine of the real number a.
DoubleComplex.Atan(z) Inverse tangent of the complex number z.
Table 4. Trigonometric functions of complex numbers.

Method Description
DoubleComplex.Sinh(z) Hyperbolic sine of the complex number z.
DoubleComplex.Cosh(z) Hyperbolic cosine of the complex number z.
DoubleComplex.Tanh(z) Hyperbolic tangent of the complex number z.
DoubleComplex.Asinh(z) Inverse hyperbolic sine of the complex number z.
DoubleComplex.Acosh(z) Inverse hyperbolic cosine of the complex number z.
DoubleComplex.Atanh(z) Inverse hyperbolic tangent of the complex number z.
Table 5. Hyperbolic functions of complex numbers.

Up: Mathematics Library Next: Curves Previous: Numerical Algorithms Support Classes Contents

Overview
Introduction
Features
Documentation
QuickStart Samples
Sample Applications
Downloads
Get it now!
Download trial version
How to Buy
Information
Resources
Contact Us
Search

"The Extreme Optimization Statistics Library for .NET is a major boon for those doing statistical work in .NET. I strongly recommend this product."
- Marc Brooks

"I have made it my mission to institutionalize the value of good API design.  I strongly believe that this is key to making developers more productive and happy on our platform. It is clear that you value good API design in your work, and take to heart developer productivity and synergy with the .NET framework."
- Brad Abrams,
Lead Program Manager, Microsoft.

This is a partial list of companies who are using our libraries:
ABB Robotics
Allstate
Applied Materials
Arcam
Astra Schedule
Babson College
Canadian Council on Learning
Canyon Associates
Caxton Associates
CECity
Constellation Energy
CreditSights
DeepOcean
Duke University
Dynamotive
Elecsoft
Engelhard Corporation
Epcor
Equipoise Software
Galileo International
GAM UK
Gammex
GlaxoSmithKline
Global Matrix
The Hartford
Infinera Corporation
Intel
JDS Uniphase
LaBranche & Co.
Learning & Skills Council
Jacobs Consultancy
Litman Gregory
Lucas Systems
Malvern Instruments
Medrio
Merck & Co.
Mintera.
Monitor Software
MorningStar
NanoString Technologies
Paletta Invent
Parametric Portfolio Associates
Prosanos
RATA Associates
RiskShield
Ramboll
Standard & Poor's
Strategic Analysis Corporation
Univ. of Alicante
Univ. of South Carolina
vielife
Xerox
US Army