Personal tools
complex
Click on the banner to return to the class reference home page.
complex
Complex Number Library
- Summary
- Data Type and Member Function Indexes
- Specializations
- Synopsis
- Description
- Interface
- Constructors
- Assignment Operators
- Member Functions
- Non-member Operators
- Non-member Functions
- Example
- Warnings
Summary
C++ complex number library.
Data Type and Member Function Indexes
(exclusive of constructors and destructors)
Specializations
complex <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 (T = 0 , T = 0); 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 X> istream& operator>> (istream&, complex<X>&); template <class X> ostream& operator<< (ostream&, const complex<X>&); // 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 (T, 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 (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 = 0, const T& im_arg = 0);
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>&);
Copy constructor. 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.
T real() 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, T rhs); template<class T> complex<T> operator+(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, T rhs); operator-(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, T rhs); template<class T> complex<T> operator* (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, T rhs); template<class T> complex<T> operator/(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, 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==(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, 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!=(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 X> istream& operator>>(istream& is, complex<X>& 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, the ios::badbit flag is set.
template <class X> ostream& operator<<(ostream& os, const complex<X>& 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> 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 natural logarithm of x. This function returns the complex value whose phase angle is greater than -pi and less than pi.
template<class T> complex<T> log10(const complex<T>& x);
Returns the logarithm base 10 of x.
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);
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, T y); template<class T> complex<T> pow(const complex<T>& x, const complex<T>& y); template<class T> complex<T> pow(T x, const complex<T>& y);
Returns x raised to the y power.
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 square root of x. This function returns the complex value whose phase angle is greater than -pi/2 and less than or equal to
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.h> 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; } Output : a = (1.42804e-06,-0.0002873), b = (58.2199,69.7354)
Warnings
On compilers that don't support member function templates, the arithmetic operators will not work on any arbitrary type. (They will work only on float, double and long doubles.) You also will only be able to perform binary arithmetic on types that are the same.
Compilers that don't support non-converting constructors will permit unsafe downcasts (i.e., long double to double, double to float, long double to float).
©Copyright 1996, Rogue Wave Software, Inc.