Complex Class

Declared

complex.h

The Complex class is used for the storage and manipulation of complex numbers, which are often represented by real and imaginary components (Cartesian coordinates), or by magnitude and angle (polar coordinates). Each Complex object stores exactly one complex number. A Complex object may be used in expressions in the same manner as floating-point values.

Public Member Functions

The following constructors and destructors are declared:

Complex;
Complex( Complex const & );
Complex( double, double = 0.0 );
~Complex;

The following arithmetic member functions are declared:

Complex &operator =( Complex const & );
Complex &operator =( double );
Complex &operator +=( Complex const & );
Complex &operator +=( double );
Complex &operator -=( Complex const & );
Complex &operator -=( double );
Complex &operator *=( Complex const & );
Complex &operator *=( double );
Complex &operator /=( Complex const & );
Complex &operator /=( double );
Complex  operator  +() const;
Complex  operator  -() const;
double imag() const;
double real() const;

Friend Functions

The following I/O Stream inserter and extractor friend functions are declared:

friend istream &operator >>( istream &,
                             Complex & );
friend ostream &operator <<( ostream &,
                             Complex const & );

Related Operators

The following operators are declared:

Complex operator  +( Complex const &, 
                    Complex const & );
Complex operator  +( Complex const &, double );
Complex operator  +( double, Complex const & );
Complex operator  -( Complex const &,
                    Complex const & );
Complex operator  -( Complex const &, double );
Complex operator  -( double, Complex const & );
Complex operator  *( Complex const &,
                    Complex const & );
Complex operator  *( Complex const &, double );
Complex operator  *( double, Complex const & );
Complex operator  /( Complex const &,
                    Complex const & );
Complex operator  /( Complex const &, double );
Complex operator  /( double, Complex const & );
int  operator ==( Complex const &,
                    Complex const & );
int  operator ==( Complex const &, double );
int  operator ==( double, Complex const & );
int  operator !=( Complex const &,
                    Complex const & );
int  operator !=( Complex const &, double );
int  operator !=( double, Complex const & );

Related Functions

The following related functions are declared:

double  abs ( Complex const & );
Complex acos ( Complex const & );
Complex acosh( Complex const & );
double  arg ( Complex const & );
Complex asin ( Complex const & );
Complex asinh( Complex const & );
Complex atan ( Complex const & );
Complex atanh( Complex const & );
Complex conj ( Complex const & );
Complex cos ( Complex const & );
Complex cosh ( Complex const & );
Complex exp ( Complex const & );
double  imag ( Complex const & );
Complex log ( Complex const & );
Complex log10( Complex const & );
double  norm ( Complex const & );
Complex polar( double  , double = 0 );
Complex pow ( Complex const &,
               Complex const & );
Complex pow ( Complex const &, double );
Complex pow ( double  , Complex const & );
Complex pow ( Complex const &, int );
double  real ( Complex const & );
Complex sin ( Complex const & );
Complex sinh ( Complex const & );
Complex sqrt ( Complex const & );
Complex tan ( Complex const & );
Complex tanh ( Complex const & );

Complex abs()

compute the magnitude of a complex number

Synopsis:

#include <complex.h>
double abs( Complex const &num );

Semantics:

The abs() function computes the magnitude of num, which is equivalent to the length (magnitude) of the vector when the num is represented in polar coordinates.

Results:

This function returns the magnitude of num.

See also:

arg(), norm(), polar()

Complex acos()

compute the arccosine of a complex number

Synopsis:

#include <complex.h>
Complex acos( Complex const &num );

Semantics:

The acos() function computes the arccosine of num.

Results:

This function returns the arccosine of num.

See also:

asin(), atan(), cos()

Complex acosh()

compute the inverse hyperbolic cosine of a complex number

Synopsis:

#include <complex.h>
Complex acosh( Complex const &num );

Semantics:

The acosh() function computes the inverse hyperbolic cosine of num.

Results:

This function returns the inverse hyperbolic cosine of num.

See also:

asinh(), atanh(), cosh()

Complex arg()

compute the angle for polar coordinates

Synopsis:

#include <complex.h>
double arg( Complex const &num );

Semantics:

The arg() function computes the angle of the vector when the num is represented in polar coordinates. The angle has the same sign as the real component of the num. It's positive in the 1st and 2nd quadrants, and negative in the 3rd and 4th quadrants.

Results:

This function returns the angle of the vector when the num is represented in polar coordinates.

See also:

abs(), norm(), polar()

Complex asin()

compute the arcsine of a complex number

Synopsis:

#include <complex.h>
Complex asin( Complex const &num );

Semantics:

The asin() function computes the arcsine of num.

Results:

This function returns the arcsine of num.

See also:

acos(), atan(), sin()

Complex asinh()

compute the inverse hyperbolic sine of a complex number

Synopsis:

#include <complex.h>
Complex asinh( Complex const &num );

Semantics:

The asinh() function computes the inverse hyperbolic sine of num.

Results:

This function returns the inverse hyperbolic sine of num.

See also:

acosh(), atanh(), sinh()

Complex atan()

compute the arctangent of a complex number

Synopsis:

#include <complex.h>
Complex atan( Complex const &num );

Semantics:

The atan() function computes the arctangent of num.

Results:

This function returns the arctangent of num.

See also:

acos(), asin(), tan()

Complex atanh()

compute the inverse hyperbolic tangent of a complex number

Synopsis:

#include <complex.h>
Complex atanh( Complex const &num );

Semantics:

The atanh() function computes the inverse hyperbolic tangent of num.

Results:

This function returns the inverse hyperbolic tangent of num.

See also:

acosh(), asinh(), tanh()

Complex::Complex()

create a Complex object

Synopsis:

#include <complex.h>
public:
Complex::Complex();
Complex::Complex( Complex const &num );
Complex::Complex( double real, double imag = 0.0 );

Semantics:

The public Complex constructor creates a Complex object:

  • The first form creates a default Complex object with value zero for both the real and imaginary components
  • The second form creates a copy of an existing Complex object, num.
  • The third form sets the real component set to real, and the imaginary component to imag. If no imaginary component is specified, imag is set to 0.

Results:

A Complex object is created.

See also:

Complex::~Complex(), real(), imag()

Complex::~Complex()

destroy a Complex object

Synopsis:

#include <complex.h>
public:
Complex::~Complex();

Semantics:

This function destroys the Complex object. The call to the public Complex destructor is inserted implicitly by the compiler at the point where the Complex object goes out of scope.

Results:

The Complex object is destroyed.

See also:

Complex::Complex()

Complex conj()

compute the conjugate of a complex number

Synopsis:

#include <complex.h>
Complex conj( Complex const &num );

Semantics:

The conj() function computes the conjugate of num. The conjugate consists of the unchanged real component, and the negative of the imaginary component.

Results:

This function returns the conjugate of num.

Complex cos()

compute the cosine of a complex number

Synopsis:

#include <complex.h>
Complex cos( Complex const &num );

Semantics:

The cos() function computes the cosine of num.

Results:

This function returns the cosine of num.

See also:

acos(), sin(), tan()

Complex cosh()

compute the hyperbolic cosine of a complex number

Synopsis:

#include <complex.h>
Complex cosh( Complex const &num );

Semantics:

This function computes the hyperbolic cosine of num.

Results:

The cosh() function returns the hyperbolic cosine of num.

See also:

acosh(), sinh(), tanh()

Complex exp()

raise a complex number to a given power

Synopsis:

#include <complex.h>
Complex exp( Complex const &num );

Semantics:

This function computes the value of e raised to the power num.

Results:

The exp() function returns the value of e raised to the power num.

See also:

log(), log10(), pow(), sqrt()

Complex::imag()

extract the imaginary component of a complex number

Synopsis:

#include <complex.h>
public:
double Complex::imag();

Semantics:

This function extracts the imaginary component of the Complex object.

Results:

The imag() public member returns the imaginary component of the Complex object.

See also:

imag(), real(), Complex::real()

Complex imag()

extract the imaginary component of a complex number

Synopsis:

#include <complex.h>
double imag( Complex const &num );

Semantics:

This function extracts the imaginary component of num.

Results:

The imag() function returns the imaginary component of num.

See also:

real(), Complex::imag(), Complex::real()

Complex log()

compute the natural log of a complex number

Synopsis:

#include <complex.h>
Complex log( Complex const &num );

Semantics:

This function computes the natural, or base e, logarithm of num.

Results:

The log() function returns the natural, or base e, logarithm of num.

See also:

exp(), log10(), pow(), sqrt()

Complex log10()

compute the base 10 logarithm of a complex number

Synopsis:

#include <complex.h>
Complex log10( Complex const &num );

Semantics:

This function computes the base 10 logarithm of num.

Results:

The log10() function returns the base 10 logarithm of num.

See also:

exp(), log(), pow(), sqrt()

Complex norm()

normalize a complex number

Synopsis:

#include <complex.h>
double norm( Complex const &num );

Semantics:

This function computes the square of the magnitude of num, which is equivalent to the square of the length (magnitude) of the vector when num is represented in polar coordinates.

Results:

The norm() function returns the square of the magnitude of num.

See also:

arg(), polar()

Complex operator !=()

compare two complex numbers for inequality

Synopsis:

#include <complex.h>
int operator !=( Complex const &num1,
                 Complex const &num2 );
int operator !=( Complex const &num1,
                 double num2 );
int operator !=( double num1,
                 Complex const &num2 );

Semantics:

This function compares num1 and num2 for inequality. At least one of the parameters must be a Complex object for this function to be called.

Two Complex objects are not equal if their corresponding real or imaginary components are not equal.

If the operator !=() function is used with a Complex and an object of any other built-in numeric type, the non-Complex is converted to a double, and the second or third form of the operator !=() function is used.

Results:

The operator !=() function returns a non-zero value if num1 is not equal to num2, otherwise zero is returned.

See also:

operator ==()

Complex operator *()

multiply two complex numbers

Synopsis:

#include <complex.h>
Complex operator *( Complex const &num1,
                    Complex const &num2 );
Complex operator *( Complex const &num1,
                    double num2 );
Complex operator *( double num1,
                    Complex const &num2 );

Semantics:

This function is used to multiply num1 by num2, yielding a Complex object.

The first form of this function multiplies two Complex objects.

The second form multiplies a Complex object and a floating-point value.

The third form multiplies a floating-point value and a Complex object.

For the last two forms, in effect, the real and imaginary components of the Complex object are multiplied by the floating-point value.

If the operator *() is used with a Complex and an object of any other built-in numeric type, the non-Complex is converted to a double, and the second or third form of this function is used.

Results:

The operator *() returns a Complex object that is the product of num1 and num2.

See also:

operator +(), operator -(), operator /(), Complex::operator *=()

Complex::operator *=()

multiply a complex number by a number

Synopsis:

#include <complex.h>
public:
Complex &Complex::operator *=( 
    Complex const &num );
Complex &Complex::operator *=( double num );

Semantics:

This function is used to multiply the num argument into the Complex object.

The first form of this function multiplies the Complex object by the Complex num parameter.

The second form multiplies the real and imaginary components of the Complex object by num.

A call to this function where num is any of the other built-in numeric types, causes num to be promoted to double, and the second form of this function to be used.

Results:

The operator *=() public function returns a reference to the target of the assignment.

See also:

operator *(), Complex::operator +=(), Complex::operator -=(), Complex::operator /=(), Complex::operator =()

Complex::operator +()

do nothing to a complex number

Synopsis:

#include <complex.h>
public:
Complex Complex::operator +();

Semantics:

The unary operator +() public member function is the opposite of the unary Complex::operator -(). It's provided for completeness. It performs no operation on the Complex object.

Results:

This function returns a Complex object with the same value as the original Complex object.

See also:

operator +(), Complex::operator +=(), Complex::operator -()

Complex operator +()

add two complex numbers

Synopsis:

#include <complex.h>
Complex operator +( Complex const &num1,
                    Complex const &num2 );
Complex operator +( Complex const &num1,
                    double num2 );
Complex operator +( double num1,
                    Complex const &num2 );

Semantics:

This function is used to add num1 to num2, yielding a Complex object.

The first form of this function adds two Complex objects.

The second form adds a Complex object and a floating-point value.

The third form adds a floating-point value and a Complex object.

For the last two forms, in effect, the floating-point value is added to the real component of the Complex object.

If the operator+() is used with a Complex object and an object of any other built-in numeric type, the non-Complex object is converted to a double, and the second or third form of this function is used.

Results:

This function returns a Complex object that is the sum of num1 and num2.

See also:

operator *(), operator -(), operator /(), Complex::operator +(), Complex::operator +=()

Complex operator +=()

add a number to a complex number

Synopsis:

#include <complex.h>
public:
Complex &Complex::operator +=( 
    Complex const &num );
Complex &Complex::operator +=( double num );

Semantics:

This public member function is used to add num to the value of the Complex object. The second form of the function adds num to the real component of the Complex object.

A call to this function where num is any of the other built-in numeric types, causes num to be promoted to double and the second form of the function to be used.

Results:

The operator +=() public member function returns a reference to the target of the assignment.

See also:

operator +(), Complex::operator *=(), Complex::operator +(), Complex::operator /=(), Complex::operator -=(), Complex::operator =()

Complex::operator -()

negate a complex number

Synopsis:

#include <complex.h>
public:
Complex Complex::operator -();

Semantics:

The unary operator -() public member function yields a Complex object with the real and imaginary components having the same magnitude as those of the original object, but with opposite sign.

Results:

This function returns a Complex object with the same magnitude as the original Complex object and with opposite sign.

See also:

operator -(), Complex::operator +(), Complex::operator -=()

Complex operator -()

subtract one complex number from another

Synopsis:

#include <complex.h>
Complex operator -( Complex const &num1,
                    Complex const &num2 );
Complex operator -( Complex const &num1,
                    double num2 );
Complex operator -( double num1,
                    Complex const &num2 );

Semantics:

This function is used to subtract num2 from num1, yielding a Complex object.

The first form of the operator -() function computes the difference between two Complex objects.

The second form computes the difference between a Complex object and a floating-point value. In effect, the floating-point value is subtracted from the real component of the Complex object.

The third form computes the difference between a floating-point value and a Complex object. In effect, the real component of the result is num1 minus the real component of num2, and the imaginary component of the result is the negative of the imaginary component of num2.

If the operator -() is used with a Complex object and an object of any other built-in numeric type, the non-Complex object is converted to a double, and the second or third form of the function is used.

Results:

This function returns a Complex object that is the difference between num1 and num2.

See also:

operator *(), operator +(), operator /(), Complex::operator -(), Complex::operator -=()

Complex::operator -=()

subtract a number from a complex number

Synopsis:

#include <complex.h>
public:
Complex &Complex::operator -=( 
    Complex const &num );
Complex &Complex::operator -=( double num );

Semantics:

The operator -=() public member function is used to subtract num from the value of the Complex object. The second form of the function subtracts num from the real component of the Complex object.

A call to this function where num is any of the other built-in numeric types, causes num to be promoted to double and the second form of the function to be used.

Results:

The operator -=() public member function returns a reference to the target of the assignment.

See also:

operator -(), Complex::operator *=(), Complex::operator +=(), Complex::operator -(), Complex::operator /=(), Complex::operator =()

Complex operator /()

divide a complex number

Synopsis:

#include <complex.h>
Complex operator /( Complex const &num1,
                    Complex const &num2 );
Complex operator /( Complex const &num1,
                    double num2 );
Complex operator /( double num1,
                    Complex const &num2 );

Semantics:

This function is used to divide num1 by num2, yielding a Complex object.

The first form of the operator /() function divides two Complex objects.

The second form divides a Complex object by a floating-point value. In effect, the real and imaginary components of the complex number are divided by the floating-point value.

The third form divides a floating-point value by a Complex object. Conceptually, the floating-point value is converted to a Complex object and then the division is done.

If the operator /() function is used with a Complex object and an object of any other built-in numeric type, the non-Complex object is converted to a double, and the second or third form of the function is used.

Results:

This function returns a Complex object that is the quotient of num1 divided by num2.

See also:

operator *(), operator +(), operator -(), Complex::operator /=()

Complex::operator /=()

divide a complex number by a number

Synopsis:

#include <complex.h>
public:
Complex &Complex::operator /=( 
    Complex const &num );
Complex &Complex::operator /=( double num );

Semantics:

The operator /=() public member function is used to divide the Complex object by num. The second form of the function divides the real and imaginary components of the Complex object by num.

A call to the operator /=() public member function where num is any of the other built-in numeric types, causes num to be promoted to double, and the second form of the function to be used.

Results:

This function returns a reference to the target of the assignment.

See also:

operator /(), Complex::operator *=(), Complex::operator +=(), Complex::operator -=(), Complex::operator =()

Complex operator <<()

write a complex number to an I/O stream

Synopsis:

#include <complex.h>
friend ostream &operator <<( ostream &strm,
                             Complex &num );

Semantics:

This function is used to write Complex objects to an I/O stream. The Complex object is always written in the form:

(real,imag)

The real and imaginary components are written using the normal rules for formatting floating-point numbers. Any formatting options specified prior to inserting the num apply to both the real and imaginary components. If the real and imaginary components are to be inserted using different formats, the real() and imag() member functions should be used to insert each component separately.

Results:

This function returns a reference to the strm object.

See also:

ostream class description

Complex::operator =()

set the value of a complex number

Synopsis:

#include <complex.h>
public:
Complex &Complex::operator =( Complex const &num );
Complex &Complex::operator =( double num );

Semantics:

The operator =() public member function is used to set the value of the Complex object to num. The first assignment operator copies the value of num into the Complex object.

The second assignment operator sets the real component of the Complex object to num, and the imaginary component to zero.

A call to the operator =() public member function where num is any of the other built-in numeric types, causes num to be promoted to double, and the second form of the function to be used.

Results:

The operator =() public member function returns a reference to the target of the assignment.

See also:

Complex::operator *=(), Complex::operator +=(), Complex::operator -=(), Complex::operator /=()

Complex operator ==()

compare two complex numbers for equality

Synopsis:

#include <complex.h>
int operator ==( Complex const &num1,
                 Complex const &num2 );
int operator ==( Complex const &num1,
                 double num2 );
int operator ==( double num1,
                 Complex const &num2 );

Semantics:

The operator ==() function compares num1 and num2 for equality. At least one of the arguments must be a Complex object for this function to be called.

Two Complex objects are equal if their corresponding real and imaginary components are equal.

If the operator ==() function is used with a Complex and an object of any other built-in numeric type, the non-Complex is converted to a double, and the second or third form of the function is used.

Results:

This function returns a non-zero value if num1 is equal to num2, otherwise it returns zero.

See also:

operator !=()

Complex operator >>()

read a complex number from an I/O stream

Synopsis:

#include <complex.h>
friend istream &operator >>( istream &strm,
                             Complex &num );

Semantics:

The operator >>() function is used to read a Complex object from an I/O stream. A valid complex value is of one of the following forms:

  • (real,imag)
  • real,imag
  • (real)

If the imaginary portion is omitted, zero is assumed.

While reading a Complex object, whitespace is ignored before and between the various components of the number if the ios::skipws bit is set in ios::fmtflags.

Results:

This function returns a reference to strm. num contains the value read from strm on success, otherwise it's unchanged.

See also:

istream class description

Complex polar()

convert polar coordinates into a complex number

Synopsis:

#include <complex.h>
Complex polar( double mag, double angle = 0.0 );

Semantics:

The polar() function converts mag and angle (polar coordinates) into a complex number. The angle is optional, and defaults to zero if it isn't specified.

Results:

This function returns a Complex object that is mag and angle interpreted as polar coordinates.

See also:

abs(), arg(), norm()

Complex pow()

raise a complex number to the power of another number

Synopsis:

#include <complex.h>
Complex pow( Complex const &num,
             Complex const &exp );
Complex pow( Complex const &num,
             double exp );
Complex pow( double num, 
             Complex const &exp );
Complex pow( Complex const &num,
             int exp );

Semantics:

The pow() function computes num raised to the power exp. The various forms are provided to minimize the amount of floating-point calculation performed.

Results:

This function returns a Complex object that is num raised to the power of a Complex object that is exp.

See also:

exp(), log(), log10(), sqrt()

Complex::real()

extract the real portion of an imaginary number

Synopsis:

#include <complex.h>
public:
double Complex::real();

Semantics:

The real() public member function extracts the real component of the Complex object.

Results:

This function returns the real component of the Complex object.

See also:

imag(), real(), Complex::imag()

Complex real()

extract the real portion of an imaginary number

Synopsis:

#include <complex.h>
double real( Complex const &num );

Semantics:

The real() function extracts the real component of num.

Results:

This function returns the real component of num.

See also:

imag(), Complex::imag(), Complex::real()

Complex sin()

compute the sine of a complex number

Synopsis:

#include <complex.h>
Complex sin( Complex const &num );

Semantics:

The sin() function computes the sine of num.

Results:

This function returns the sine of num.

See also:

asin(), cos(), tan()

Complex sinh()

compute the hyperbolic sine of a complex number

Synopsis:

#include <complex.h>
Complex sinh( Complex const &num );

Semantics:

This function computes the hyperbolic sine of num.

Results:

This function returns the hyperbolic sine of num.

See also:

asinh(), cosh(), tanh()

Complex sqrt()

compute the square root of a complex number

Synopsis:

#include <complex.h>
Complex sqrt( Complex const &num );

Semantics:

The sqrt() function computes the square root of num.

Results:

This function returns the square root of num.

See also:

exp(), log(), log10(), pow()

Complex tan()

compute the tangent of a complex number

Synopsis:

#include <complex.h>
Complex tan( Complex const &num );

Semantics:

The tan() function computes the tangent of num.

Results:

This function returns the tangent of num.

See also:

atan(), cos(), sin()

Complex tanh()

compute the hyperbolic tangent of a complex number

Synopsis:

#include <complex.h>
Complex tanh( Complex const &num );

Semantics:

This function computes the hyperbolic tangent of num.

Results:

This function returns the hyperbolic tangent of num.

See also:

atanh(), cosh(), sinh()