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.
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;
The following I/O Stream inserter and extractor friend functions are declared:
friend istream &operator >>( istream &, Complex & ); friend ostream &operator <<( ostream &, Complex const & );
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 & );
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 & );
compute the magnitude of a complex number
#include <complex.h> double abs( Complex const &num );
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.
This function returns the magnitude of num.
compute the arccosine of a complex number
#include <complex.h> Complex acos( Complex const &num );
The acos() function computes the arccosine of num.
This function returns the arccosine of num.
compute the inverse hyperbolic cosine of a complex number
#include <complex.h> Complex acosh( Complex const &num );
The acosh() function computes the inverse hyperbolic cosine of num.
This function returns the inverse hyperbolic cosine of num.
compute the angle for polar coordinates
#include <complex.h> double arg( Complex const &num );
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.
This function returns the angle of the vector when the num is represented in polar coordinates.
compute the arcsine of a complex number
#include <complex.h> Complex asin( Complex const &num );
The asin() function computes the arcsine of num.
This function returns the arcsine of num.
compute the inverse hyperbolic sine of a complex number
#include <complex.h> Complex asinh( Complex const &num );
The asinh() function computes the inverse hyperbolic sine of num.
This function returns the inverse hyperbolic sine of num.
compute the arctangent of a complex number
#include <complex.h> Complex atan( Complex const &num );
The atan() function computes the arctangent of num.
This function returns the arctangent of num.
compute the inverse hyperbolic tangent of a complex number
#include <complex.h> Complex atanh( Complex const &num );
The atanh() function computes the inverse hyperbolic tangent of num.
This function returns the inverse hyperbolic tangent of num.
create a Complex object
#include <complex.h> public: Complex::Complex(); Complex::Complex( Complex const &num ); Complex::Complex( double real, double imag = 0.0 );
The public Complex constructor creates a Complex object:
A Complex object is created.
Complex::~Complex(), real(), imag()
destroy a Complex object
#include <complex.h> public: Complex::~Complex();
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.
The Complex object is destroyed.
compute the conjugate of a complex number
#include <complex.h> Complex conj( Complex const &num );
The conj() function computes the conjugate of num. The conjugate consists of the unchanged real component, and the negative of the imaginary component.
This function returns the conjugate of num.
compute the cosine of a complex number
#include <complex.h> Complex cos( Complex const &num );
The cos() function computes the cosine of num.
This function returns the cosine of num.
compute the hyperbolic cosine of a complex number
#include <complex.h> Complex cosh( Complex const &num );
This function computes the hyperbolic cosine of num.
The cosh() function returns the hyperbolic cosine of num.
raise a complex number to a given power
#include <complex.h> Complex exp( Complex const &num );
This function computes the value of e raised to the power num.
The exp() function returns the value of e raised to the power num.
extract the imaginary component of a complex number
#include <complex.h> public: double Complex::imag();
This function extracts the imaginary component of the Complex object.
The imag() public member returns the imaginary component of the Complex object.
imag(), real(), Complex::real()
extract the imaginary component of a complex number
#include <complex.h> double imag( Complex const &num );
This function extracts the imaginary component of num.
The imag() function returns the imaginary component of num.
real(), Complex::imag(), Complex::real()
compute the natural log of a complex number
#include <complex.h> Complex log( Complex const &num );
This function computes the natural, or base e, logarithm of num.
The log() function returns the natural, or base e, logarithm of num.
compute the base 10 logarithm of a complex number
#include <complex.h> Complex log10( Complex const &num );
This function computes the base 10 logarithm of num.
The log10() function returns the base 10 logarithm of num.
normalize a complex number
#include <complex.h> double norm( Complex const &num );
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.
The norm() function returns the square of the magnitude of num.
compare two complex numbers for inequality
#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 );
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.
The operator !=() function returns a non-zero value if num1 is not equal to num2, otherwise zero is returned.
multiply two complex numbers
#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 );
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.
The operator *() returns a Complex object that is the product of num1 and num2.
operator +(), operator -(), operator /(), Complex::operator *=()
multiply a complex number by a number
#include <complex.h> public: Complex &Complex::operator *=( Complex const &num ); Complex &Complex::operator *=( double num );
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.
The operator *=() public function returns a reference to the target of the assignment.
operator *(), Complex::operator +=(), Complex::operator -=(), Complex::operator /=(), Complex::operator =()
do nothing to a complex number
#include <complex.h> public: Complex Complex::operator +();
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.
This function returns a Complex object with the same value as the original Complex object.
operator +(), Complex::operator +=(), Complex::operator -()
add two complex numbers
#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 );
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.
This function returns a Complex object that is the sum of num1 and num2.
operator *(), operator -(), operator /(), Complex::operator +(), Complex::operator +=()
add a number to a complex number
#include <complex.h> public: Complex &Complex::operator +=( Complex const &num ); Complex &Complex::operator +=( double num );
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.
The operator +=() public member function returns a reference to the target of the assignment.
operator +(), Complex::operator *=(), Complex::operator +(), Complex::operator /=(), Complex::operator -=(), Complex::operator =()
negate a complex number
#include <complex.h> public: Complex Complex::operator -();
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.
This function returns a Complex object with the same magnitude as the original Complex object and with opposite sign.
operator -(), Complex::operator +(), Complex::operator -=()
subtract one complex number from another
#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 );
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.
This function returns a Complex object that is the difference between num1 and num2.
operator *(), operator +(), operator /(), Complex::operator -(), Complex::operator -=()
subtract a number from a complex number
#include <complex.h> public: Complex &Complex::operator -=( Complex const &num ); Complex &Complex::operator -=( double num );
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.
The operator -=() public member function returns a reference to the target of the assignment.
operator -(), Complex::operator *=(), Complex::operator +=(), Complex::operator -(), Complex::operator /=(), Complex::operator =()
divide a complex number
#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 );
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.
This function returns a Complex object that is the quotient of num1 divided by num2.
operator *(), operator +(), operator -(), Complex::operator /=()
divide a complex number by a number
#include <complex.h> public: Complex &Complex::operator /=( Complex const &num ); Complex &Complex::operator /=( double num );
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.
This function returns a reference to the target of the assignment.
operator /(), Complex::operator *=(), Complex::operator +=(), Complex::operator -=(), Complex::operator =()
write a complex number to an I/O stream
#include <complex.h> friend ostream &operator <<( ostream &strm, Complex &num );
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.
This function returns a reference to the strm object.
set the value of a complex number
#include <complex.h> public: Complex &Complex::operator =( Complex const &num ); Complex &Complex::operator =( double num );
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.
The operator =() public member function returns a reference to the target of the assignment.
Complex::operator *=(), Complex::operator +=(), Complex::operator -=(), Complex::operator /=()
compare two complex numbers for equality
#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 );
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.
This function returns a non-zero value if num1 is equal to num2, otherwise it returns zero.
read a complex number from an I/O stream
#include <complex.h> friend istream &operator >>( istream &strm, Complex &num );
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:
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.
This function returns a reference to strm. num contains the value read from strm on success, otherwise it's unchanged.
convert polar coordinates into a complex number
#include <complex.h> Complex polar( double mag, double angle = 0.0 );
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.
This function returns a Complex object that is mag and angle interpreted as polar coordinates.
raise a complex number to the power of another number
#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 );
The pow() function computes num raised to the power exp. The various forms are provided to minimize the amount of floating-point calculation performed.
This function returns a Complex object that is num raised to the power of a Complex object that is exp.
extract the real portion of an imaginary number
#include <complex.h> public: double Complex::real();
The real() public member function extracts the real component of the Complex object.
This function returns the real component of the Complex object.
imag(), real(), Complex::imag()
extract the real portion of an imaginary number
#include <complex.h> double real( Complex const &num );
The real() function extracts the real component of num.
This function returns the real component of num.
imag(), Complex::imag(), Complex::real()
compute the sine of a complex number
#include <complex.h> Complex sin( Complex const &num );
The sin() function computes the sine of num.
This function returns the sine of num.
compute the hyperbolic sine of a complex number
#include <complex.h> Complex sinh( Complex const &num );
This function computes the hyperbolic sine of num.
This function returns the hyperbolic sine of num.
compute the square root of a complex number
#include <complex.h> Complex sqrt( Complex const &num );
The sqrt() function computes the square root of num.
This function returns the square root of num.
compute the tangent of a complex number
#include <complex.h> Complex tan( Complex const &num );
The tan() function computes the tangent of num.
This function returns the tangent of num.
compute the hyperbolic tangent of a complex number
#include <complex.h> Complex tanh( Complex const &num );
This function computes the hyperbolic tangent of num.
This function returns the hyperbolic tangent of num.