DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

complex(C++std)


abs , arg , complex , complex<double> , complex<float> , complex<long double> , conj , cos , cosh , exp , imag , log , log10 , norm , operator!= , operator* , operator+ , operator- , operator/ , operator<< , operator== , operator>> , polar , pow , real , sin , sinh , sqrt , tan , tanh , __STD_COMPLEX - defines a template class that supports complex arithmetic

Synopsis

   namespace std {
   #define __STD_COMPLEX

           // TEMPLATE CLASSES
   template<class T>
       class complex;
   template<>
       class complex<float>;
   template<>
       class complex<double>;
   template<>
       class complex<long double>;

           // TEMPLATE FUNCTIONS
   template<class T>
       complex<T> operator+(const complex<T>& lhs,
           const complex<T>& rhs);
   template<class T>
       complex<T> operator+(const complex<T>& lhs,
           const T& rhs);
   template<class T>
       complex<T> operator+(const T& lhs,
           const complex<T>& rhs);
   template<class T>
       complex<T> operator-(const complex<T>& lhs,
           const complex<T>& rhs);
   template<class T>
       complex<T> operator-(const complex<T>& lhs,
           const T& rhs);
   template<class T>
       complex<T> operator-(const T& lhs,
           const complex<T>& rhs);
   template<class T>
       complex<T> operator*(const complex<T>& lhs,
           const complex<T>& rhs);
   template<class T>
       complex<T> operator*(const complex<T>& lhs,
           const T& rhs);
   template<class T>
       complex<T> operator*(const T& lhs,
           const complex<T>& rhs);
   template<class T>
       complex<T> operator/(const complex<T>& lhs,
           const complex<T>& rhs);
   template<class T>
       complex<T> operator/(const complex<T>& lhs,
           const T& rhs);
   template<class T>
       complex<T> operator/(const T& lhs,
           const complex<T>& rhs);
   template<class T>
       complex<T> operator+(const complex<T>& lhs);
   template<class T>
       complex<T> operator-(const complex<T>& lhs);
   template<class T>
       bool operator==(const complex<T>& lhs,
           const complex<T>& rhs);
   template<class T>
       bool operator==(const complex<T>& lhs,
           const T& rhs);
   template<class T>
       bool operator==(const T& lhs,
           const complex<T>& rhs);
   template<class T>
       bool operator!=(const complex<T>& lhs,
           const complex<T>& rhs);
   template<class T>
       bool operator!=(const complex<T>& lhs,
           const T& rhs);
   template<class T>
       bool operator!=(const T& lhs,
           const complex<T>& rhs);
   template<class U, class E, class T>
       basic_istream<E, T>&
           operator>>(basic_istream<E, T>& is,
               complex<U>& x);
   template<class U, class E, class T>
       basic_ostream<E, T>&
           operator<<(basic_ostream<E, T>& os,
               const complex<U>& x);
   template<class T>
       T real(const complex<T>& x);
   template<class T>
       T imag(const complex<T>& x);
   template<class T>
       T abs(const complex<T>& x);
   template<class T>
       T arg(const complex<T>& x);
   template<class T>
       T norm(const complex<T>& x);
   template<class T>
       complex<T> conj(const complex<T>& x);
   template<class T>
       complex<T> polar(const T& rho, const T& theta = 0);
   template<class T>
       complex<T> cos(const complex<T>& x);
   template<class T>
       complex<T> cosh(const complex<T>& x);
   template<class T>
       complex<T> exp(const complex<T>& x);
   template<class T>
       complex<T> log(const complex<T>& x);
   template<class T>
       complex<T> log10(const complex<T>& x);
   template<class T>
       complex<T> pow(const complex<T>& x, int y);
   template<class T>
       complex<T> pow(const complex<T>& x, const T& y);
   template<class T>
       complex<T> pow(const complex<T>& x,
           const complex<T>& y);
   template<class T>
       complex<T> pow(const T& x, const complex<T>& y);
   template<class T>
       complex<T> sin(const complex<T>& x);
   template<class T>
       complex<T> sinh(const complex<T>& x);
   template<class T>
       complex<T> sqrt(const complex<T>& x);
       };

Description

Include the standard header <complex> to define template class complex and a host of supporting template functions. Unless otherwise specified, functions that can return multiple values return an imaginary part in the half-open interval (-pi, pi].

abs

   template<class T>
       T abs(const complex<T>& x);

The function returns the magnitude of x.

arg

   template<class T>
       T arg(const complex<T>& x);

The function returns the phase angle of x.

complex


complex::complex , complex::imag , complex::operator*= , complex::operator+= , complex::operator-= , complex::operator/= , complex::operator= , complex::real , complex::value_type

   template<class T>
       class complex {
   public:
       typedef T value_type;
       T real() const;
       T imag() const;
       complex(const T& re = 0, const T& im = 0);
       template<class U>
           complex(const complex<U>& x);
       template<class U>
           complex& operator=(const complex<U>& rhs);
       template<class U>
           complex& operator+=(const complex<U>& rhs);
       template<class U>
           complex& operator-=(const complex<U>& rhs);
       template<class U>
           complex& operator*=(const complex<U>& rhs);
       template<class U>
           complex& operator/=(const complex<U>& rhs);
       complex& operator=(const T& rhs);
       complex& operator+=(const T& rhs);
       complex& operator-=(const T& rhs);
       complex& operator*=(const T& rhs);
       complex& operator/=(const T& rhs);
       friend complex<T>
           operator+(const complex<T>& lhs, const T& rhs);
       friend complex<T>
           operator+(const T& lhs, const complex<T>& rhs);
       friend complex<T>
           operator-(const complex<T>& lhs, const T& rhs);
       friend complex<T>
           operator-(const T& lhs, const complex<T>& rhs);
       friend complex<T>
           operator*(const complex<T>& lhs, const T& rhs);
       friend complex<T>
           operator*(const T& lhs, const complex<T>& rhs);
       friend complex<T>
           operator/(const complex<T>& lhs, const T& rhs);
       friend complex<T>
           operator/(const T& lhs, const complex<T>& rhs);
       friend bool
           operator==(const complex<T>& lhs, const T& rhs);
       friend bool
           operator==(const T& lhs, const complex<T>& rhs);
       friend bool
           operator!=(const complex<T>& lhs, const T& rhs);
       friend bool
           operator!=(const T& lhs, const complex<T>& rhs);
       };

The template class describes an object that stores two objects of type T, one that represents the real part of a complex number and one that represents the imaginary part. An object of class T:

In particular, no subtle differences may exist between copy construction and default construction followed by assignment. And none of the operations on objects of class T may throw exceptions.

Explicit specializations of template class complex exist for the three floating-point types. In this implementation, a value of any other type T is type cast to double for actual calculations, with the double result assigned back to the stored object of type T.

complex::complex

   complex(const T& re = 0, const T& im = 0);
   template<class U>
       complex(const complex<U>& x);

The first constructor initializes the stored real part to re and the stored imaginary part to im. The second constructor initializes the stored real part to x.real() and the stored imaginary part to x.imag().

In this implementation, if a translator does not support member template functions, the template:

   template<class U>
       complex(const complex<U>& x);

is replaced by:

   complex(const complex& x);

which is the copy constructor.

complex::imag

   T imag() const;

The member function returns the stored imaginary part.

complex::operator*=

   template<class U>
       complex& operator*=(const complex<U>& rhs);
   complex& operator*=(const T& rhs);

The first member function replaces the stored real and imaginary parts with those corresponding to the complex product of *this and rhs. It then returns *this.

The second member function multiplies both the stored real part and the stored imaginary part with rhs. It then returns *this.

In this implementation, if a translator does not support member template functions, the template:

   template<class U>
       complex& operator*=(const complex<U>& rhs);

is replaced by:

   complex& operator*=(const complex& rhs);

complex::operator+=

   template<class U>
       complex& operator+=(const complex<U>& rhs);
   complex& operator+=(const T& rhs);

The first member function replaces the stored real and imaginary parts with those corresponding to the complex sum of *this and rhs. It then returns *this.

The second member function adds rhs to the stored real part. It then returns *this.

In this implementation, if a translator does not support member template functions, the template:

   template<class U>
       complex& operator+=(const complex<U>& rhs);

is replaced by:

   complex& operator+=(const complex& rhs);

complex::operator-=

   template<class U>
       complex& operator-=(const complex<U>& rhs);
   complex& operator-=(const T& rhs);

The first member function replaces the stored real and imaginary parts with those corresponding to the complex difference of *this and rhs. It then returns *this.

The second member function subtracts rhs from the stored real part. It then returns *this.

In this implementation, if a translator does not support member template functions, the template:

   template<class U>
       complex& operator-=(const complex<U>& rhs);

is replaced by:

   complex& operator-=(const complex& rhs);

complex::operator/=

   template<class U>
       complex& operator/=(const complex<U>& rhs);
   complex& operator/=(const T& rhs);

The first member function replaces the stored real and imaginary parts with those corresponding to the complex quotient of *this and rhs. It then returns *this.

The second member function multiplies both the stored real part and the stored imaginary part with rhs. It then returns *this.

In this implementation, if a translator does not support member template functions, the template:

   template<class U>
       complex& operator/=(const complex<U>& rhs);

is replaced by:

   complex& operator/=(const complex& rhs);

complex::operator=

   template<class U>
       complex& operator=(const complex<U>& rhs);
   complex& operator=(const T& rhs);

The first member function replaces the stored real part with rhs.real() and the stored imaginary part with rhs.imag(). It then returns *this.

The second member function replaces the stored real part with rhs and the stored imaginary part with zero. It then returns *this.

In this implementation, if a translator does not support member template functions, the template:

   template<class U>
       complex& operator=(const complex<U>& rhs);

is replaced by:

   complex& operator=(const complex& rhs);

which is the default assignment operator.

complex::real

   T real() const;

The member function returns the stored real part.

complex::value_type

   typedef T value_type;

The type is a synonym for the template parameter T.

complex<double>

   template<>
       class complex<double> {
   public:
       complex(double re = 0, double im = 0);
       complex(const complex<float>& x);
       explicit complex(const complex<long double>& x);
   // rest same as template class complex
       };

The explicitly specialized template class describes an object that stores two objects of type double, one that represents the real part of a complex number and one that represents the imaginary part. The explicit specialization differs only in the constructors it defines. The first constructor initializes the stored real part to re and the stored imaginary part to im. The remaining two constructors initialize the stored real part to x.real() and the stored imaginary part to x.imag().

complex<float>

   template<>
       class complex<float> {
   public:
       complex(float re = 0, float im = 0);
       explicit complex(const complex<double>& x);
       explicit complex(const complex<long double>& x);
   // rest same as template class complex
       };

The explicitly specialized template class describes an object that stores two objects of type float, one that represents the real part of a complex number and one that represents the imaginary part. The explicit specialization differs only in the constructors it defines. The first constructor initializes the stored real part to re and the stored imaginary part to im. The remaining two constructors initialize the stored real part to x.real() and the stored imaginary part to x.imag().

complex<long double>

   template<>
       class complex<long double> {
   public:
       complex(long double re = 0, long double im = 0);
       complex(const complex<float>& x);
       complex(const complex<double>& x);
   // rest same as template class complex
       };

The explicitly specialized template class describes an object that stores two objects of type long double, one that represents the real part of a complex number and one that represents the imaginary part. The explicit specialization differs only in the constructors it defines. The first constructor initializes the stored real part to re and the stored imaginary part to im. The remaining two constructors initialize the stored real part to x.real() and the stored imaginary part to x.imag().

conj

   template<class T>
       complex<T> conj(const complex<T>& x);

The function returns the conjugate of x.

cos

   template<class T>
       complex<T> cos(const complex<T>& x);

The function returns the cosine of x.

cosh

   template<class T>
       complex<T> cosh(const complex<T>& x);

The function returns the hyperbolic cosine of x.

exp

   template<class T>
       complex<T> exp(const complex<T>& x);

The function returns the exponential of x.

imag

   template<class T>
       T imag(const complex<T>& x);

The function returns the imaginary part of x.

log

   template<class T>
       complex<T> log(const complex<T>& x);

The function returns the logarithm of x. The branch cuts are along the negative real axis.

log10

   template<class T>
       complex<T> log10(const complex<T>& x);

The function returns the base 10 logarithm of x. The branch cuts are along the negative real axis.

norm

   template<class T>
       T norm(const complex<T>& x);

The function returns the squared magnitude of x.

operator!=

   template<class T>
       bool operator!=(const complex<T>& lhs,
           const complex<T>& rhs);
   template<class T>
       bool operator!=(const complex<T>& lhs,
           const T& rhs);
   template<class T>
       bool operator!=(const T& lhs,
           const complex<T>& rhs);

The operators each return true only if real(lhs) != real(rhs) || imag(lhs) != imag(rhs).

operator*

   template<class T>
       complex<T> operator*(const complex<T>& lhs,
           const complex<T>& rhs);
   template<class T>
       complex<T> operator*(const complex<T>& lhs,
           const T& rhs);
   template<class T>
       complex<T> operator*(const T& lhs,
           const complex<T>& rhs);

The operators each convert both operands to the return type, then return the complex product of the converted lhs and rhs.

operator+

   template<class T>
       complex<T> operator+(const complex<T>& lhs,
           const complex<T>& rhs);
   template<class T>
       complex<T> operator+(const complex<T>& lhs,
           const T& rhs);
   template<class T>
       complex<T> operator+(const T& lhs,
           const complex<T>& rhs);
   template<class T>
       complex<T> operator+(const complex<T>& lhs);

The binary operators each convert both operands to the return type, then return the complex sum of the converted lhs and rhs.

The unary operator returns lhs.

operator-

   template<class T>
       complex<T> operator-(const complex<T>& lhs,
           const complex<T>& rhs);
   template<class T>
       complex<T> operator-(const complex<T>& lhs,
           const T& rhs);
   template<class T>
       complex<T> operator-(const T& lhs,
           const complex<T>& rhs);
   template<class T>
       complex<T> operator-(const complex<T>& lhs);

The binary operators each convert both operands to the return type, then return the complex difference of the converted lhs and rhs.

The unary operator returns a value whose real part is -real(lhs) and whose imaginary part is -imag(lhs).

operator/

   template<class T>
       complex<T> operator/(const complex<T>& lhs,
           const complex<T>& rhs);
   template<class T>
       complex<T> operator/(const complex<T>& lhs,
           const T& rhs);
   template<class T>
       complex<T> operator/(const T& lhs,
           const complex<T>& rhs);

The operators each convert both operands to the return type, then return the complex quotient of the converted lhs and rhs.

operator<<

   template<class U, class E, class T>
       basic_ostream<E, T>&
           operator<<(basic_ostream<E, T>& os,
               const complex<U>& x);

The template function inserts the complex value x in the output stream os, effectively by executing:

   basic_ostringstream<E, T> ostr;
   ostr.flags(os.flags());
   ostr.imbue(os.imbue());
   ostr.precision(os.precision());
   ostr << '(' << real(x) << ','
       << imag(x) << ')';
   os << ostr.str().c_str();

Thus, if os.width() is greater than zero, any padding occurs either before or after the parenthesized pair of values, which itself contains no padding. The function returns os.

operator==

   template<class T>
       bool operator==(const complex<T>& lhs,
           const complex<T>& rhs);
   template<class T>
       bool operator==(const complex<T>& lhs,
           const T& rhs);
   template<class T>
       bool operator==(const T& lhs,
           const complex<T>& rhs);

The operators each return true only if real(lhs) == real(rhs) && imag(lhs) == imag(rhs).

operator>>

   template<class U, class E, class T>
       basic_istream<E, T>&
           operator>>(basic_istream<E, T>& is,
               complex<U>& x);

The template function attempts to extract a complex value from the input stream is, effectively by executing:

   is >> ch && ch == '('
       && is >> re >> ch && ch == ','
       && is >> im >> ch && ch == ')'

Here, ch is an object of type E, and re and im are objects of type U.

If the result of this expression is true, the function stores re in the real part and im in the imaginary part of x. In any event, the function returns is.

polar

   template<class T>
       complex<T> polar(const T& rho,
           const T& theta = 0);

The function returns the complex value whose magnitude is rho and whose phase angle is theta.

pow

   template<class T>
       complex<T> pow(const complex<T>& x, int y);
   template<class T>
       complex<T> pow(const complex<T>& x,
           const T& y);
   template<class T>
       complex<T> pow(const complex<T>& x,
           const complex<T>& y);
   template<class T>
       complex<T> pow(const T& x,
           const complex<T>& y);

The functions each effectively convert both operands to the return type, then return the converted x to the power y. The branch cut for x is along the negative real axis.

real

   template<class T>
       T real(const complex<T>& x);

The function returns the real part of x.

sin

   template<class T>
       complex<T> sin(const complex<T>& x);

The function returns the sine of x.

sinh

   template<class T>
       complex<T> sinh(const complex<T>& x);

The function returns the hyperbolic sine of x.

sqrt

   template<class T>
       complex<T> sqrt(const complex<T>& x);

The function returns the square root of x, with phase angle in the half-open interval (-pi/2, pi/2]. The branch cuts are along the negative real axis.

__STD_COMPLEX

   #define __STD_COMPLEX

The macro is defined, with an unspecified expansion, to indicate compliance with the specifications of this header.

tan

   template<class T>
       complex<T> tan(const complex<T>& x);

The function returns the tangent of x.

tanh

   template<class T>
       complex<T> tanh(const complex<T>& x);

The function returns the hyperbolic tangent of x.

References

ios(C++std)
18 February 2000
© 2000 The Santa Cruz Operation, Inc. All rights reserved.

Copyright © 1992-1996 by P.J. Plauger. Portions derived from work copyright © 1994 by Hewlett-Packard Company. All rights reserved.