In the following sections we will describe the operations used to create and manipulate complex numbers.
The template argument is used to define the types associated with the real and imaginary fields. This argument must be one of the floating point number data types available in the C++ language, either float, double, or long double.
There are several constructors associated with the class. A constructor with no arguments initializes both the real and imaginary fields to zero. A constructor with a single argument initializes the real field to the given value, and the imaginary value to zero. A constructor with two arguments initializes both real and imaginary fields. Finally, a copy constructor can be used to initialize a complex number with values derived from another complex number.
complex<double> com_one; // value 0 + 0i complex<double> com_two(3.14); // value 3.14 + 0i complex<double> com_three(1.5, 3.14) // value 1.5 + 3.14i complex<double> com_four(com_two); // value is also 3.14 + 0i
A complex number can be assigned the value of another complex number. Since the one-argument constructor is also used for a conversion operator, a complex number can also be assigned the value of a real number. The real field is changed to the right hand side, while the imaginary field is set to zero.
com_one = com_three; // becomes 1.5 + 3.14i com_three = 2.17; // becomes 2.17 + 0i
The function polar() can be used to construct a complex number with the given magnitude and phase angle.
com_four = polar(5.6, 1.8);
The conjugate of a complex number is formed using the function conj(). If a complex number represents x + iy, then the conjugate is the value x-iy.
complex<double> com_five = conj(com_four);Functions and Member Functions
The member functions real() and imag() return the real and imaginary fields of a complex number, respectively. These functions can also be invoked as ordinary functions with complex number arguments.
// the following should be the same cout << com_one.real() << "+" << com_one.imag() << "i" << endl; cout << real(com_one) << "+" << imag(com_one) << "i" << endl;
The arithmetic operators +, -, *, and / can be used to perform addition, subtraction, multiplication and division of complex numbers. All four work either with two complex numbers, or with a complex number and a real value. Assignment operators are also defined for all four.
cout << com_one + com_two << endl; cout << com_one - 3.14 << endl; cout << 2.75 * com_two << endl; com_one += com_three / 2.0;
The unary operators + and - can also be applied to complex numbers.
Two complex numbers can be compared for equality or inequality, using the operators == and !=. Two values are equal if their corresponding fields are equal. Complex numbers are not well-ordered, and thus cannot be compared using any other relational operator.
Complex numbers can be written to an output stream, or read from an input stream, using the normal stream I/O conventions. A value is written in parentheses, either as (u) or (u,v), depending upon whether or not the imaginary value is zero. A value is read as a set of parentheses surrounding two numeric values.
The function norm() returns the norm of the complex number. This is the sum of the squares of the real and imaginary parts. The function abs() returns the absolute value, which is the square root of the norm. Note that both are ordinary functions that take the complex value as an argument, not member functions.
cout << norm(com_two) << endl; cout << abs(com_two) << endl;
The directed phase angle of a complex number is yielded by the function arg().
cout << com_four << " in polar coordinates is " << arg(com_four) << " and " << norm(com_four) << endl;
The trigonometric functions defined for floating point values (namely, sin(), cos(), tan(), sinh(), cosh(), and tanh()), have all been extended to complex number arguments. Each takes a single complex number as argument and returns a complex number as result.
The transcendental functions exp(), log(), log10() and sqrt() have been extended to complex arguments. Each takes a single complex number as argument, and returns a complex number as result.
The standard library defines several variations of the exponential function pow(). Versions exist to raise a complex number to an integer power, to raise a complex number to a complex power or to a real power, or to raise a real value to a complex power.
©Copyright 1996, Rogue Wave Software, Inc.