<complex>

<complex>


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


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].

Many of the functions declared in this header have additional overloads, which behave much like the generic functions defined in the C99 header <tgmath.h>. The following functions have such additional overloads:

abs      conj     polar
acos     cos      pow
acosh    cosh     real
arg      exp      sin
asin     imag     sinh
asinh    log      sqrt
atan     log10    tan
atanh    norm     tanh

namespace std {
#define __STD_COMPLEX

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

        // TEMPLATE FUNCTIONS
template<class Ty>
    complex<Ty> operator+(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator+(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    complex<Ty> operator+(const Ty& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator-(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator-(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    complex<Ty> operator-(const Ty& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator*(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator*(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    complex<Ty> operator*(const Ty& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator/(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator/(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    complex<Ty> operator/(const Ty& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator+(const complex<Ty>& left);
template<class Ty>
    complex<Ty> operator-(const complex<Ty>& left);
template<class Ty>
    bool operator==(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    bool operator==(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    bool operator==(const Ty& left,
        const complex<Ty>& right);
template<class Ty>
    bool operator!=(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    bool operator!=(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    bool operator!=(const Ty& left,
        const complex<Ty>& right);
template<class Ty, class Elem, class Tr>
    basic_istream<Elem, Tr>&
        operator>>(basic_istream<Elem, Tr>& istr,
            complex<Ty>& right);
template<class Ty, class Elem, class Tr>
    basic_ostream<Elem, Tr>&
        operator<<(basic_ostream<Elem, Tr>& ostr,
            const complex<Ty>& right);
template<class Ty>
    Ty real(const complex<Ty>& left);
template<class Ty>
    Ty imag(const complex<Ty>& left);
template<class Ty>
    Ty abs(const complex<Ty>& left);
template<class Ty>
    Ty arg(const complex<Ty>& left);
template<class Ty>
    Ty norm(const complex<Ty>& left);
template<class Ty>
    complex<Ty> conj(const complex<Ty>& left);
template<class Ty>
    complex<Ty> polar(const Ty& rho, const Ty& theta = 0);
template<class Ty>
    complex<Ty> cos(const complex<Ty>& left);
template<class Ty>
    complex<Ty> cosh(const complex<Ty>& left);
template<class Ty>
    complex<Ty> exp(const complex<Ty>& left);
template<class Ty>
    complex<Ty> log(const complex<Ty>& left);
template<class Ty>
    complex<Ty> log10(const complex<Ty>& left);
template<class Ty>
    complex<Ty> pow(const complex<Ty>& left, int right);
template<class Ty>
    complex<Ty> pow(const complex<Ty>& left, const Ty& right);
template<class Ty>
    complex<Ty> pow(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> pow(const Ty& left, const complex<Ty>& right);
template<class Ty>
    complex<Ty> sin(const complex<Ty>& left);
template<class Ty>
    complex<Ty> sinh(const complex<Ty>& left);
template<class Ty>
    complex<Ty> sqrt(const complex<Ty>& left);
}  // namespace std

abs

template<class Ty>
    Ty abs(const complex<Ty>& left);

The function returns the magnitude of left.

arg

template<class Ty>
    Ty arg(const complex<Ty>& left);

The function returns the phase angle of left.

complex

template<class Ty>
    class complex {
public:
    typedef Ty value_type;
    Ty real() const;
    Ty imag() const;
    complex(const Ty& realval = 0, const Ty& imagval = 0);
    template<class Other>
        complex(const complex<Other>& right);
    template<class Other>
        complex& operator=(const complex<Other>& right);
    template<class Other>
        complex& operator+=(const complex<Other>& right);
    template<class Other>
        complex& operator-=(const complex<Other>& right);
    template<class Other>
        complex& operator*=(const complex<Other>& right);
    template<class Other>
        complex& operator/=(const complex<Other>& right);
    complex& operator=(const Ty& right);
    complex& operator+=(const Ty& right);
    complex& operator-=(const Ty& right);
    complex& operator*=(const Ty& right);
    complex& operator/=(const Ty& right);
    };

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

  • has a public default constructor, destructor, copy constructor, and assignment operator -- with conventional behavior
  • can be assigned integer or floating-point values, or type cast to such values -- with conventional behavior
  • defines the arithmetic operators and math functions, as needed, that are defined for the floating-point types -- with conventional behavior

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 Ty 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 Ty is type cast to double for actual calculations, with the double result assigned back to the stored object of type Ty.

complex::complex

complex(const Ty& realval = 0, const Ty& imagval = 0);
template<class Other>
    complex(const complex<Other>& right);

The first constructor initializes the stored real part to realval and the stored imaginary part to imagval. The second constructor initializes the stored real part to right.real() and the stored imaginary part to right.imag().

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

template<class Other>
    complex(const complex<Other>& right);

is replaced by:

complex(const complex& right);

which is the copy constructor.

complex::imag

Ty imag() const;

The member function returns the stored imaginary part.

complex::operator*=

template<class Other>
    complex& operator*=(const complex<Other>& right);
complex& operator*=(const Ty& right);

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

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

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

template<class Other>
    complex& operator*=(const complex<Other>& right);

is replaced by:

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

complex::operator+=

template<class Other>
    complex& operator+=(const complex<Other>& right);
complex& operator+=(const Ty& right);

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

The second member function adds right 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 Other>
    complex& operator+=(const complex<Other>& right);

is replaced by:

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

complex::operator-=

template<class Other>
    complex& operator-=(const complex<Other>& right);
complex& operator-=(const Ty& right);

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

The second member function subtracts right 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 Other>
    complex& operator-=(const complex<Other>& right);

is replaced by:

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

complex::operator/=

template<class Other>
    complex& operator/=(const complex<Other>& right);
complex& operator/=(const Ty& right);

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

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

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

template<class Other>
    complex& operator/=(const complex<Other>& right);

is replaced by:

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

complex::operator=

template<class Other>
    complex& operator=(const complex<Other>& right);
complex& operator=(const Ty& right);

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

The second member function replaces the stored real part with right 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 Other>
    complex& operator=(const complex<Other>& right);

is replaced by:

complex& operator=(const complex& right);

which is the default assignment operator.

complex::real

Ty real() const;

The member function returns the stored real part.

complex::value_type

typedef Ty value_type;

The type is a synonym for the template parameter Ty.

complex<double>

template<>
    class complex<double> {
public:
    complex(double realval = 0, double imagval = 0);
    complex(const complex<float>& right);
    explicit complex(const complex<long double>& right);
// 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 realval and the stored imaginary part to imagval. The remaining two constructors initialize the stored real part to right.real() and the stored imaginary part to right.imag().

complex<float>

template<>
    class complex<float> {
public:
    complex(float realval = 0, float imagval = 0);
    explicit complex(const complex<double>& right);
    explicit complex(const complex<long double>& right);
// 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 realval and the stored imaginary part to imagval. The remaining two constructors initialize the stored real part to right.real() and the stored imaginary part to right.imag().

complex<long double>

template<>
    class complex<long double> {
public:
    complex(long double realval = 0, long double imagval = 0);
    complex(const complex<float>& right);
    complex(const complex<double>& right);
// 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 realval and the stored imaginary part to imagval. The remaining two constructors initialize the stored real part to right.real() and the stored imaginary part to right.imag().

conj

template<class Ty>
    complex<Ty> conj(const complex<Ty>& left);

The function returns the conjugate of left.

cos

template<class Ty>
    complex<Ty> cos(const complex<Ty>& left);

The function returns the cosine of left.

cosh

template<class Ty>
    complex<Ty> cosh(const complex<Ty>& left);

The function returns the hyperbolic cosine of left.

exp

template<class Ty>
    complex<Ty> exp(const complex<Ty>& left);

The function returns the exponential of left.

imag

template<class Ty>
    Ty imag(const complex<Ty>& left);

The function returns the imaginary part of left.

log

template<class Ty>
    complex<Ty> log(const complex<Ty>& left);

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

log10

template<class Ty>
    complex<Ty> log10(const complex<Ty>& left);

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

norm

template<class Ty>
    Ty norm(const complex<Ty>& left);

The function returns the squared magnitude of left.

operator!=

template<class Ty>
    bool operator!=(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    bool operator!=(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    bool operator!=(const Ty& left,
        const complex<Ty>& right);

The operators each return true only if real(left) != real(right) || imag(left) != imag(right).

operator*

template<class Ty>
    complex<Ty> operator*(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator*(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    complex<Ty> operator*(const Ty& left,
        const complex<Ty>& right);

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

operator+

template<class Ty>
    complex<Ty> operator+(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator+(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    complex<Ty> operator+(const Ty& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator+(const complex<Ty>& left);

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

The unary operator returns left.

operator-

template<class Ty>
    complex<Ty> operator-(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator-(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    complex<Ty> operator-(const Ty& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator-(const complex<Ty>& left);

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

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

operator/

template<class Ty>
    complex<Ty> operator/(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator/(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    complex<Ty> operator/(const Ty& left,
        const complex<Ty>& right);

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

operator<<

template<class Ty, class Elem, class Tr>
    basic_ostream<Elem, Tr>&
        operator<<(basic_ostream<Elem, Tr>& ostr,
            const complex<Ty>& right);

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

basic_ostringstream<Elem, Tr> osstr;
osstr.flags(ostr.flags());
osstr.imbue(ostr.imbue());
osstr.precision(ostr.precision());
osstr << '(' << real(right) << ','
    << imag(right) << ')';
ostr << osstr.str().c_str();

Thus, if ostr.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 ostr.

operator==

template<class Ty>
    bool operator==(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    bool operator==(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    bool operator==(const Ty& left,
        const complex<Ty>& right);

The operators each return true only if real(left) == real(right) && imag(left) == imag(right).

operator>>

template<class Ty, class Elem, class Tr>
    basic_istream<Elem, Tr>&
        operator>>(basic_istream<Elem, Tr>& istr,
            complex<Ty>& right);

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

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

Here, ch is an object of type Elem, and re and im are objects of type Ty.

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

polar

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

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

pow

template<class Ty>
    complex<Ty> pow(const complex<Ty>& left, int right);
template<class Ty>
    complex<Ty> pow(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    complex<Ty> pow(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> pow(const Ty& left,
        const complex<Ty>& right);

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

real

template<class Ty>
    Ty real(const complex<Ty>& left);

The function returns the real part of left.

sin

template<class Ty>
    complex<Ty> sin(const complex<Ty>& left);

The function returns the sine of left.

sinh

template<class Ty>
    complex<Ty> sinh(const complex<Ty>& left);

The function returns the hyperbolic sine of left.

sqrt

template<class Ty>
    complex<Ty> sqrt(const complex<Ty>& left);

The function returns the square root of left, 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 Ty>
    complex<Ty> tan(const complex<Ty>& left);

The function returns the tangent of left.

tanh

template<class Ty>
    complex<Ty> tanh(const complex<Ty>& left);

The function returns the hyperbolic tangent of left.


See also the Table of Contents and the Index.

Copyright © 1992-2006 by P.J. Plauger. All rights reserved.

Last modified: 2013-12-21

comments powered by Disqus