digitalmars.com                      
Last update Sat Jun 18 16:54:32 2011

The Complex Class

This chapter describes: Class complex is implemented as a set of overloaded operators and functions that perform general complex mathematics. A complex number has two data members: a real and an imaginary part. Both are of type double.

This class implements complex numbers. Using overloaded functions and operators, you can treat complex numbers like you treat reals or integers. You can use the following:

Using the Complex Class

To use complex numbers, add the library complex to your project and include file oldcomplex.h in your file, in a statement like this:
#include <oldcomplex.h>
You can declare a complex variable three ways: uninitialized, initialized with two numbers, or initialized with another complex number. For example:
complex z1;		// Uninitialized
complex z2(1.0,2.0);	// Initialized with two numbers
complex z3 = z2;	// Initialized with a complex number
To access components of a complex number, use member functions complex::real() and complex::imag(). For example:
complex z1, z2(5.0,6.0);
z1.real() = z2.imag(); 
z1.imag() = z2.real(); 
To print or read a complex number, use stream operators operator>> and operator<<. For example, this program:
#include <complex.h>
#include <iostream.h>

int main()
{   
   complex z1(1.1, 2.0), z2; 
   cout << "Enter z2: "; 
   cin >> z2; 
   cout << "z1 = " << z1 << '\n'; 
   cout << "z2 = " << z2 << '\n'; 
   return 0; 
} 
prints this:
Enter z2: 10 9.8 
z1 = (1.1, 2) 
z2 = (10,9.8) 
You can also use a complex number in the middle of a statement. For example, this line prints out the sine of (1, 1):
cout << sin(complex(1,1));
Note: To invoke the complex version of a function, one of its arguments must be complex. For example, to figure the square root of -1, you must use sqrt(complex(-1,0)).

Member Functions

The five types of class member functions are:

Constructor Function

The following function is a constructor function.


complex::complex()

Header
oldcomplex.h
Prototype
complex::complex();
complex::complex(double re, double im);
complex::complex(const complex& z);
Description
The first constructor creates an unitialized complex number. For example:
complex z;
The second constructor creates a complex number with the real part initialized to re and the imaginary part initialized to im. For example:
complex z1(1.2,3.0), z2(3,4);
The third constructor creates a copy of a complex number. It is also called the X(X&) constructor. For example:

#include <oldcomplex.h>

complex complexFunc() 
{ 
// Invokes complex(double, double)
   complex z1(2.0,4.0); 

// Invokes complex(const complex&) 
   complex z2 = z1; 

// Invokes complex(const complex&) to 
// create a temporary variable and adds 
// it to z1. 
   return complex(1.1, 2.2) + z1; 
} 

Operator Functions

The operators defined for complex are overloaded to have the same meaning as the built-in operators. For example, + is the addition of two complex variables just as it is used for the addition of two variables of type double. The operators in the complex class have the usual operator precedence.

The five types of operators defined for the complex class are:

arithmetic operator+
operator-
operator*
operator/
relational operator&&
operator||
operator!
operator==
assignment complex::operator=
complex::operator+=
complex::operator-=
complex::operator*
complex::operator/
unary complex::operator-
complex::operator!
stream operator<<
operator>>

complex::operator=

Header
oldcomplex.h
Prototype
complex& complex::operator=(const complex& z);
complex& complex::operator=(double u);
Description
Assigns a value to complex variable.

complex::operator+=

Header
oldcomplex.h
Prototype
complex& complex::operator+=(const complex& z);
complex& complex::operator+=(double u);
Description
Adds a value to a complex variable and assigns the result to the complex variable.

complex::operator-=

Header
oldcomplex.h
Prototype
complex& complex::operator-=(const complex& z);
complex& complex::operator-=(double u);
Description
Subtracts a value from a complex variable and assigns the result to the complex variable.

complex::operator*=

Header
oldcomplex.h
Prototype
complex& complex::operator*=(const complex& z);
complex& complex::operator*=(double u);
Description
Multiplies a value by a complex variable and assigns the result to the complex variable.

complex::operator/=

Header
oldcomplex.h
Prototype
complex& complex::operator/=(const complex& z);
complex& complex::operator/=(double u);
Description
Divides a complex variable by a value and assigns the result to the complex variable.

complex::operator-

Header
oldcomplex.h
Prototype
complex complex::operator-() const;
Description
Returns the negative of a complex number.

complex::operator!

Header
oldcomplex.h
Prototype
int complex::operator!() const;
Description
Performs logical negation on a complex number.

operator+

Header
oldcomplex.h
Prototype
complex operator+(const complex& z1, complex& z2);
complex operator+(double u, const complex& z);
complex operator+(const complex& z, double u);
Description
Returns the sum of a complex number and any other number.

operator-

Header
oldcomplex.h
Prototype
complex operator-(const complex& z1, const complex& z2);
complex operator-(double u, const complex& z);
complex operator-(const complex& z, double u);
Description
Returns the difference of a complex number and any other number.

operator*

Header
oldcomplex.h
Prototype
complex operator*(const complex& z1, const complex& z2);
complex operator*(double u, const complex& z);
complex operator*(const complex& z, double u);
Description
Returns the product of a complex number and any other number.

operator/

Header
oldcomplex.h
Prototype
complex operator/(const complex& z1, const complex& z2);
complex operator/(double u, const complex& z);
complex operator/(const complex& z, double u);
Description
Returns the quotient of a complex number and any other number.

operator&&

Header
oldcomplex.h
Prototype
complex operator&&(const complex& z1, const complex& z2);
complex operator&&(double u, const complex& z);
complex operator&&(const complex& z, double u);
Description
Performs logical AND. Returns 0 if both arguments are 0 or (0, 0).

operator||

Header
oldcomplex.h
Prototype
complex operator||(const complex& z1, const complex& z2);
complex operator||(double u, const complex& z);
complex operator||(const complex& z, double u);
Description
Performs logical OR. Returns 0 if both arguments are 0 or (0, 0).

operator!

Header
oldcomplex.h
Prototype
complex operator!=(const complex& z1, const complex& z2);
complex operator!=(double u, const complex& z);
complex operator!=(const complex& z, double u);
Description
Tests for inequality.

operator==

Header
oldcomplex.h
Prototype
complex ::operator==(const complex& z1, const complex& z2);
complex ::operator==(double u, const complex& z);
complex ::operator==(const complex& z, double u);
Description
Tests for equality.

operator<<

Header
oldcomplex.h
Prototype
ostream& operator<<(ostream& s, const complex& z);
Description
Reads z from the stream s, in the form(real, imaginary).

operator>>

Header
oldcomplex.h
Prototype
istream& operator>>(istream& s, const complex& z);
Description
Reads z from the stream s, in the form(real, imaginary).

Complex Functions

The complex functions are unique to the complex type and do not overload names of the ANSI C math library.

arg

Header
oldcomplex.h
Prototype
double arg(const complex& z);
Description
The arg function determines the angle, in radians, of the number in
the complex plane specified in the x argument. The positive real axis
has angle 0; the positive imaginary axis has angle pi/2.

The arg function is equivalent to:

atan2(imag(x), real(x)) 
Return Value
Returns the argument of the complex number z in radians.

complex::imag, complex::real

Header
oldcomplex.h
Prototype
double& complex::imag();
double& complex::real();
Description
complex::imag returns the imaginary portion of a complex number; complex::real returns the real portion. Either function can be on the left side or right side of an assignment statement.
Example
For complex::imag 		For complex::real 

#include <oldcomplex.h> 	#include <oldcomplex.h> 

complex z1(1.0,2.0), z2; 	complex z1(1.0,2.0), z2;
z1.imag() = z2.imag(); 		z1.real() = z2.real(); 

conj

Header
oldcomplex.h
Prototype
complex conj(const complex& z);
Description
Returns the complex conjugate of a complex number. If the complex number z is x+iy, then conj(z) is x-iy.

imag

Header
oldcomplex.h
Prototype
double imag(const complex& z);
Description
Returns the imaginary portion of a complex number. It can be on only the right side of an assignment statement.

modulus

Header
oldcomplex.h
Prototype
double modulus(const complex& z);
Description
Returns the absolute value of a complex number. The absolute value of a complex number is also called the modulus. modulus(z) is equal to abs(z), which is sqrt (x * x + y * y).

norm

Header
oldcomplex.h
Prototype
double norm(const complex& z);
Description
Returns the square of the absolute value of a complex number. For z, of form (x + iy), conj(z) = (x * x + y * y).

polar

Header
oldcomplex.h
Prototype
complex polar(double range, double theta);
Description
Returns the complex number that is the result of converting polar coordinates range and theta to x and y coordinates. This is equivalent to using the formula:
complex(range * cos(theta), range * sin(theta));

real

Header
oldcomplex.h
Prototype
double real(const complex& z);
Description
Returns the real portion of a complex number. It can be on only the right side of an assignment statement.

Trigonometric and hyperbolic functions

The trigonometric and hyperbolic functions overload the names of ANSI C library floating-point trigonometric and hyperbolic functions.


acos

Header
oldcomplex.h
Prototype
complex acos(const complex& z);
Description
Returns the arccosine of a complex number.

asin, asinh

Header
oldcomplex.h
Prototype
complex asin(const complex& z);
complex asinh(const complex& z);
Description
asin returns the arcsine of a complex number. asinh returns the hyperbolic arcsine of a complex number.

atan, atanh

Header
oldcomplex.h
Prototype
complex atan(const complex& z);
complex atanh(const complex& z);
Description
atan returns the arctangent of a complex number. atanh returns the hyperbolic arctangent of a complex number.

cos, cosh

Header
oldcomplex.h
Prototype
complex cos(const complex& z);
complex cosh(const complex& z);
Description
cos returns the cosine of a complex number. cosh returns the hyperbolic cosine of a complex number.

sin, sinh

Header
oldcomplex.h
Prototype
complex sin (const complex& z);
complex sinh(const complex& z);
Description
sin returns the sine of a complex number. sinh returns the hyperbolic sine of a complex number.

tan, tanh

Header
oldcomplex.h
Prototype
complex tan(const complex& z);
complex tanh(const complex& z);
Description
tan returns the tangent of a complex number. tanh returns the hyperbolic tangent of a complex number.

Mathematical functions

The mathematical functions overload functions contained in the ANSI C math library to provide consistent complex support.

abs

Header
oldcomplex.h
Prototype
double abs(const complex&);
Description
Returns the absolute value of a complex number.

exp

Header
oldcomplex.h
Prototype
complex exp(const complex&);
Description
Returns the value of e raised to a complex number.

log, log10

Header
oldcomplex.h
Prototype
complex log(const complex&);
complex log10(const complex&);
Description
log returns the logarithm base e of a complex number. log10 returns the logarithm base 10 of a complex number.

pow

Header
oldcomplex.h
Prototype
complex pow(const complex&, double);
complex pow(const complex&, const complex &);
complex pow(double, const complex&);
complex pow(const complex&, int);
Description
Returns any number raised to a complex number or vice versa.

sqrt

Header
oldcomplex.h
Prototype
complex sqrt(const complex&);
Description
Returns the square root of a complex number.
Home | Compiler & Tools | IDDE Reference | STL | Search | Download | Forums