complex(3C++) - complex(3C++)
Standard C++ Library Copyright 1998, Rogue Wave Software, Inc.
NAMEcomplex
- C++ complex number library
SPECIALIZATIONScomplex <float>
complex <double>
complex <long double>
SYNOPSIS
#include <complex>
template <class T>
class complex;
class complex<float>;
class complex<double>;
class complex<long double>;
DESCRIPTION
complex<T> 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
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&);
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>&);
};
// Non-member Operators
template<class T>
complex<T> operator+ (const complex<T>&,
const complex<T>&);
template<class T>
complex<T> operator+ (const complex<T>&, T&);
template<class T>
complex<T> operator+ (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>&, T&);
template<classT>
complex<T> operator- (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>&, T&);
template<class T>
complex<T> operator* (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>&, T&);
template<class T>
complex<T> operator/ (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>&, T&);
template<class T>
bool operator== (T, const complex<T>&);
template<class T>
bool operator!= (const complex<T>&, const complex<T>&);
template<class T>
bool operator!= (const complex<T>&, T&);
template<class T>
bool operator!= (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>&, 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>&);
CONSTRUCTORScomplex
(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.
template <class X> complex
(const complex<X>&);
Constructs a complex number from another complex number.
ASSIGNMENT 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.
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.
Treal() const;
Returns the real part of the complex number.
NON-MEMBER 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() << ")".
NON-MEMBER 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 nega- tive
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>
#include <iostream>
using namespace std;
int main()
{
complex<double> a(1.2, 3.4);
complex<double> b(-9.8, -7.6);
a += b;
a /= sin(b) * cos(a);
b *= log(a) + pow(b, a);
cout << "a = " << a << ", b = " << b << endl;
return 0;
}
Program Outputa = (1.42804e-06,-0.0002873), b = (58.2199,69.7354)
WARNINGS
On compilers that don't support member function templates, the arithâ
metic 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 non-converting constructors permit unsafe
downcasts (for example, long double to double, double to float, long
double to float).
If your compiler does not support namespaces, then you do not need the
using declaration for std.
Rogue Wave Software 02 Apr 1998 complex(3C++)