## Complex Numbers

The C++ classes Complex<long double>, Complex<double>, and Complex<float> represent floating-point complex numbers of the form z = a + ib. Throughout this document, the variable z = a + ib is used to represent *this object. The template variable T represents the field of the coefficients.

# Constructors

There is one constructor

```     Complex( T a = 0, T b = 0 );
```

which creates the complex number a + ib.

# Constants

There are three static constants defined in each class:

 ZERO 0 ONE 1 I i

The constants may be accessed through the array UNITS = {ONE, I}.

# Real-Valued Functions

Each of the real-valued member functions has the prototype T f() const; and has a corresponding procedural function T f( const Complex<T> );.

 real imag_i csgn abs norm abs_imag norm_imag arg

Descriptions of each of the functions follow:

real
Return the real component ℜ(z) = a.
imag_i
Return the imaginary component ℑ(z) = b.
csgn
Return 0 if z = 0, 1 if a ≥ +0, and -1 if a ≤ -0.
abs
Return |z| = (a2 + b2)½ unless either component is infinity, in which case, it always returns ∞.
norm
Return |z|2 = a2 + b2 unless either component is infinity, in which case, it always returns ∞.
abs_imag
Return |ℑ(z)| = |b|.
norm_imag
Return |ℑ(z)|2 = b2.
arg
Return the argument of the complex number arg(z) = atan2( b, a ).

The function T operator [](int n) const returns the coefficient of the unit UNITS[n] for n = 0 and 1.

# Complex-Valued Functions

Each of these complex-valued member functions has the prototype Complex<T> f() const; and has a corresponding procedural function Complex<T> f( const Complex<T> );.

 imag conj signum

Descriptions of each of the functions follow:

imag
Returns the complex number 0 + ib.
conj
Returns the complex number aib.
signum
Returns the complex number z/|z|. If z = 0, then z is returned.

# Squares and Inverses

Each of the square, square root, and inverse member functions has the prototype Complex<T> f() const; and has a corresponding procedural function Complex<T> f( const Complex<T> );.

 sqr sqrt inverse

Descriptions of each of the functions follow:

sqr
Calculate z2.
sqrt
Calculate the square root of z (that is, z½).
inverse
Calculate the inverse of z (that is, z-1).

# Rotations

As spherical (or imaginary) rotations do not apply for complex numbers, there is no Complex<T> rotate( Complex<T> ) member function.

# Powers

The power function is overloaded with two prototypes:

 Complex pow( T ) Complex pow( Complex )
These calculate z raised to the power of the given argument. There are corresponding procedural functions Complex<T> pow( const Complex<T>, T ); and Complex<T> pow( const Complex<T>, const Complex<T> );.

# Exponential and Logarithmic Functions

Each of the exponential and logarithmic member functions has the prototype Complex<T> f() const; and has a corresponding procedural function Complex<T> f( const Complex<T> );.

 exp log log10

Descriptions of each of the functions follow:

exp
Calculate ez.
log
Calculate the natural logarithm ln(z).
log10
Calculate the base-10 logarithm log10(z).

# Trigonometric and Hyperbolic Functions (and their Inverses)

Each of the trigonometric, hyperbolic, inverse trigonometric, and inverse hyperbolic member functions has the prototype Complex<T> f() const; and has a corresponding procedural function Complex<T> f( const Complex<T> );.

 sin cos tan sec csc cot sinh cosh tanh sech csch coth asin acos atan asec acsc acot asinh acosh atanh asech acsch acoth

For example, the sin function calculates the result of zz3/3! + z5/5! − z7/7! + z9/9! − z11/11! + ⋅⋅⋅ by using the formula:

sin(z) = sin(a) cosh(b) + i cos(a) sinh(b)

# Special Functions

Bessel functions of the first kind, Jn(z) are implemented for integer values of n. The prototype of the member function is Complex<T> bessel_J( int ) const; and there is the corresponding procedural function Complex<T> bessel_J( int, const Complex<T>) const;.

# Integer-Value Functions

Each of the integer-valued member functions has the prototype Complex<T> f() const; and has a corresponding procedural function Complex<T> f( const Complex<T> );.

 floor ceil

In both cases, the floor and ceiling, respectively, are calculated for each component.

# Horner's Rule

The polynomial v0zn − 1 + v1zn − 2 + v2zn − 3 + ⋅⋅⋅ vn − 3z2 + vn − 2z + vn − 1 may be calculated efficiently using Horner's rule. The array v of n entries may be either of type Complex<T> * or T *.

```     Complex<T> horner( T * v, unsigned int n );
Complex<T> horner( Complex<T> * v, unsigned int n );
```

The Newton polynomial with offsets:       v0(z − cn − 1)(z − cn − 2)⋅⋅⋅(z − c3)(z − c2)(z − c1) +
v1(z − cn − 1)(z − cn − 2)⋅⋅⋅(z − c3)(z − c2) +
v2(z − cn − 1)(z − cn − 2)⋅⋅⋅(z − c3) + ⋅⋅⋅ +
vn − 3(z − cn − 1)(z − cn − 2) +
vn − 2(z − cn − 1) +
vn − 1 may also be calculated efficiently using Horner's rule. The arrays v and c of n entries may be either of type Complex<T> * or T *.

```     Complex<T> horner( T * v, T * c, unsigned int n );
Complex<T> horner( Complex<T> * v, T * c, unsigned int n );
Complex<T> horner( T * v, Complex<T> * c, unsigned int n );
Complex<T> horner( Complex<T> * v, Complex<T> * c, unsigned int n );
```

Corresponding to each of these functions is a procedural function which takes the variable z as a first argument.

# Binary Arithmetic Operators

All binary arithmetic operators operator ⋅ have the following prototypes:

operator ⋅ ( const Complex<T> &, const Complex<T> & ), operator ⋅ ( T, const Complex<T> & ), and operator ⋅ ( const Complex<T> &, T ). The standard operations are:

+ - * /

# Unary Arithmetic Operators

The unary arithmetic operator operator - has the prototype operator - ( const Complex<T> & ) and returns the negative of the complex number.

# Assignment Operators

The assignment operators operator ⋅ have the prototypes operator ⋅ ( const Complex<T> & ) and operator ⋅ ( T ) and appropriately modifies and returns this complex number. The operations are:

= += -= *= /=

# Auto Increment and Auto Decrement Operators

The auto increment and auto decrement operators work on the real part of the complex number.

# Binary Boolean Operators

All binary Boolean operators have the following prototypes:

operator ⋅ ( const Complex<T> &, const Complex<T> & ), operator ⋅ ( T, const Complex<T> & ), and operator ⋅ ( const Complex<T> &, T ). The standard operations are:

== !=

Descriptions of each of the functions follow:

operator ==
Returns true if both components return true under ==.
operator !=
Returns true if any components return true under !=.

# Query Functions

Each of the following member functions has the prototype bool f() const; and returns a value based on the components of this complex number.

 is_imaginary is_inf is_nan is_neg_inf is_pos_inf is_real is_real_inf is_zero

Descriptions of each of the functions follow:

is_imaginary
Returns true if this complex number has a zero real component (of the form 0 + bi) and false otherwise.
is_inf
Returns true if either component of this is one of either +∞ or -∞ and false otherwise.
is_nan
Returns true if either component of this is NaN and false otherwise.
is_neg_inf
Returns true if this complex number is -∞ + 0i and false otherwise.
is_pos_inf
Returns true if this complex number is ∞ + 0i and false otherwise.
is_real
Returns true if this complex number has a zero imaginary component (of the form a + 0i) and false otherwise.
is_real_inf
Returns true if this complex number is either +∞ + 0i or -∞ + 0i and false otherwise.
is_zero
Returns true if this complex number is 0 + 0i and false otherwise.

# Static Factory Functions

Each of the following static factory functions has the prototype static Complex<T> f() const; and returns a random complex number according to the following definitions:

random
r1 + ir2
random_real
r1 + i0
random_imag
0 + ir1

where rk is a random real value.

# Stream Operators

The stream operators have been overloaded to print and read complex numbers.

The character used to displayed by the stream operators may be set using the static functions with prototypes char use_symbol( char sym ); and char get_symbol();.