Apache C++ Standard Library Reference Guide

## complex

Library:  Numerics

`Does not inherit`

### Summary

Class that supports the complex numbers

### Synopsis

```#include <complex>

namespace std {
template <class T>
class complex;
template<> class complex<float>;
template<> class complex<double>;
template<> class complex<long double>;
}
```

### Specializations

```namespace std {
template<> complex <float>
template<> complex <double>
template<> complex <long double>
}
```

### Description

complex is a class that supports complex numbers. A complex number has a real part and an imaginary part. The complex class supports equality, comparison, and basic arithmetic operations. In addition, mathematical functions such as exponents, logarithms, powers, and square roots are also available.

### Interface

```namespace std {
template <class T>
class complex {

public:
typedef T value_type;

complex(const T& re = T(), const T& im = T());
complex(const complex&);
template <class X> complex(const complex<X>&);

T real() const;
T imag() const;

complex<T>& operator=(const T&);
complex<T>& operator+=(const T&);
complex<T>& operator-=(const T&);
complex<T>& operator*=(const T&);
complex<T>& operator/=(const T&);
complex& operator=(const complex&);

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

template <class X>
complex<T>& operator+=(const complex<X>&);
template <class X>
complex<T>& operator-=(const complex<X>&);
template <class X>
complex<T>& operator*=(const complex<X>&);
template <class X>
complex<T>& operator/=(const complex<X>&);
};

// Nonmember Operators

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

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

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

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

template<class T>
complex<T> operator+(const complex<T>&);
template<class T>
complex<T> operator-(const complex<T>&);

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

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

template <class T, class charT, class traits>
basic_istream<charT, traits>& operator>>
(istream&, complex<T>&);
template <class T, class charT, class traits>
basic_ostream<charT, traits>& operator<<
(ostream&, const complex<T>&);

// Values
template<class T> T real(const complex<T>&);
template<class T> T imag(const complex<T>&);

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

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

// Transcendentals
template<class T> complex<T> cos(const complex<T>&);
template<class T> complex<T> cosh(const complex<T>&);
template<class T> complex<T> exp(const complex<T>&);
template<class T> complex<T> log(const complex<T>&);

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

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

template<class T> complex<T> sin(const complex<T>&);
template<class T> complex<T> sinh(const complex<T>&);
template<class T> complex<T> sqrt(const complex<T>&);
template<class T> complex<T> tan(const complex<T>&);
template<class T> complex<T> tanh(const complex<T>&);
}
```

### Constructors

`complex(const T& re_arg = T(), const T& im_arg = T());`

Constructs an object of class complex, initializing re_arg to the real part and im_arg to the imaginary part.

`complex(const complex&);`

Constructs a complex number from another complex number.

```template <class X>
complex(const complex<X>&);```

Constructs a complex number from another complex number.

### Member Operators

```complex<T>&
operator=(const T& v); ```

Assigns v to the real part of itself, setting the imaginary part to 0.

```complex<T>&
operator+=(const T& v); ```

Adds v to the real part of itself, then returns the result.

```complex<T>&
operator-=(const T& v); ```

Subtracts v from the real part of itself, then returns the result.

```complex<T>&
operator*=(const T& v); ```

Multiplies v by the real part of itself, then returns the result.

```complex<T>&
operator/=(const T& v); ```

Divides v by the real part of itself, then returns the result.

```complex&
operator=(const complex& c);```

Assigns c to itself.

```template <class X>
complex<T>
operator=(const complex<X>& c);```

Assigns c to itself.

```template <class X>
complex<T>
operator+=(const complex<X>& c);```

Adds c to itself, then returns the result.

```template <class X>
complex<T>
operator-=(const complex<X>& c);```

Subtracts c from itself, then returns the result.

```template <class X>
complex<T>
operator*=(const complex<X>& c);```

Multiplies itself by c, then returns the result.

```template <class X>
complex<T>
operator/=(const complex<X>& c);```

Divides itself by c, then returns the result.

### Member Functions

```T
imag() const;```

Returns the imaginary part of the complex number.

```T
real() const;```

Returns the real part of the complex number.

### Nonmember Operators

```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);```

Returns the sum of lhs and 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);```

Returns the difference of lhs and 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);```

Returns the product of lhs and 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);```

Returns the quotient of lhs divided by rhs.

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

Returns rhs.

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

Returns complex<T>(-lhs.real(), -lhs.imag()).

```template<class T> bool
operator==(const complex<T>& x, const complex<T>& y);```

Returns true if the real and imaginary parts of x and y are equal.

```template<class T> bool
operator==(const complex<T>& x, const T& y);```

Returns true if y is equal to the real part of x and the imaginary part of x is equal to 0.

```template<class T> bool
operator==(const T& x, const complex<T>& y);```

Returns true if x is equal to the real part of y and the imaginary part of y is equal to 0.

```template<class T> bool
operator!=(const complex<T>& x, const complex<T>& y);```

Returns true if either the real or the imaginary part of x and y are not equal.

```template<class T> bool
operator!=(const complex<T>& x, const T& y);```

Returns true if y is not equal to the real part of x or the imaginary part of x is not equal to 0.

```template<class T> bool
operator!=(const T& x, const complex<T>& y);```

Returns true if x is not equal to the real part of y or the imaginary part of y is not equal to 0.

```template <class T, class charT, class traits>
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& is, complex<T>& x);```

Reads a complex number x into the input stream is. x may be of the form u, (u), or (u,v) where u is the real part and v is the imaginary part. If bad input is encountered, is.setstate(ios::failbit) is called.

```template <class T, class charT, class traits>
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>& os,
const complex<T>& x);```

Returns os << "(" << x.real() << "," << x.imag() << ")".

### Nonmember Functions

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

Returns the absolute value or magnitude of c, the square root of the norm.

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

Returns the phase angle of x or atan2(imag(x), real(x)).

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

Returns the conjugate of c.

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

Returns the cosine of c.

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

Returns the hyperbolic cosine of c.

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

Returns e raised to the x power.

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

Returns the imaginary part of c.

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

Returns the complex natural (base e) logarithm of x, in the range of a strip mathematically unbounded along the real axis and in the interval [-i times pi, i times pi] along the imaginary axis. When x is a negative real number, imag(log(x)) is pi. The branch cuts are along the negative real axis.

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

Returns the complex common (base 10) logarithm of x, defined as log(x)/log(10). The branch cuts are along the negative real axis.

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

Returns the squared magnitude of c, the sum of the squares of the real and imaginary parts.

```template<class T> complex<T>
polar(const T& m, const T& a = 0);```

Returns the complex value of a complex number whose magnitude is m and phase angle is a, measured in radians.

```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);```

Returns x raised to the y power; or, if called with (0, 0), returns complex <T>(1,0). The branch cuts are along the negative real axis.

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

Returns the real part of c.

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

Returns the sine of c.

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

Returns the hyperbolic sine of c.

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

Returns the complex square root of x, in the range of the right half-plane. If the argument is a negative real number, the value returned lies on the positive imaginary axis. The branch cuts are along the negative real axis.

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

Returns the tangent of x.

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

Returns the hyperbolic tangent of x.

### Example

```//
// complex.cpp
//

#include <complex>    // for complex
#include <iostream>   // for cout, endl

int main ()
{
// Create two arbitrary complex numbers.
std::complex<double> a (1.2, 3.4);
std::complex<double> b (-9.8, -7.6);

// Perform some arithmetic on the numbers.
a += b;
a /= sin (b) * cos (a);
b *= log (a) + pow (b, a);

// Output result in fixed notation.
std::cout.setf (std::ios::fixed, std::ios::floatfield);
std::cout << "a = " << a << ", b = " << b << std::endl;

return 0;
}

Program Output:

a = (0.000001,-0.000287), b = (58.219883,69.735392)
```

### Warnings

On compilers that don't support member function templates, the arithmetic operators do not work on any arbitrary type; they work only on float, double and long doubles. Also, you can perform binary arithmetic only on types that are the same.

Compilers that don't support nonconverting constructors permit unsafe downcasts; for example, long double to double, double to float, long double to float.

### Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, 26.6.2