.\" ident @(#)complex.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH complex 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2complex\fP \ - C++ complex number library .SH SPECIALIZATIONS .br complex .br complex .br complex .SH SYNOPSIS .br #include .br template .br class complex; .br class complex; .br class complex; .br class complex; .SH 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. .SH INTERFACE .br template .br class complex { .br .br public: .RE .RS 2 typedef T value_type; .RE .RS 0 .RE .RS 2 complex (const T& re = T(), const T& im = T()); .br complex (const complex&); .br template complex .RE .RS 4 (const complex&); .RE .RS 0 .RE .RS 2 T real () const; .br T imag () const; .RE .RS 0 .RE .RS 2 complex& operator= (const T&); .br complex& operator+=(const T&); .br complex& operator-=(const T&); .br complex& operator*=(const T&); .br complex& operator/=(const T&); .RE .RS 0 .RE .RS 2 template .RE .RS 3 complex& operator= (const complex&); .RE .RS 1 .RE .RS 2 template .RE .RS 3 complex& operator+= (const complex&); .RE .RS 2 template .RE .RS 3 complex& operator-= (const complex&); .RE .RS 2 template .RE .RS 3 complex& operator*= (const complex&); .RE .RS 2 template .RE .RS 3 complex& operator/= (const complex&); .RE .RS 0 }; .br // Non-member Operators .br .br template .br complex operator+ (const complex&, .RE .RS 22 const complex&); .RE .RS 0 template .br complex operator+ (const complex&, T&); .br template .br complex operator+ (T, const complex&); .br .br template .br complex operator- (const complex&, .RE .RS 22 const complex&); .RE .RS 0 template .br complex operator- (const complex&, T&); .br template .br complex operator- (T, const complex&); .br .br template .br complex operator* (const complex&, .RE .RS 22 const complex&); .RE .RS 0 template .br complex operator* (const complex&, T&); .br template .br complex operator* (T, const complex&); .br .br template .br complex operator/ (const complex&, .RE .RS 22 const complex&); .RE .RS 0 template .br complex operator/ (const complex&, T&); .br template .br complex operator/ (T, const complex&); .br .br template .br complex operator+ (const complex&); .br template .br complex operator- (const complex&); .br .br template .br bool operator== (const complex&, const complex&); .br template .br bool operator== (const complex&, T&); .br template .br bool operator== (T, const complex&); .br .br template .br bool operator!= (const complex&, const complex&); .br template .br bool operator!= (const complex&, T&); .br template .br bool operator!= (T, const complex&); .br .br template .br basic_istream& operator>> .RE .RS 14 (istream&, complex&); .RE .RS 0 template .br basic_ostream& operator<< .RE .RS 14 (ostream&, const complex&); .RE .RS 0 .br // Values .br template T real (const complex&); .br template T imag (const complex&); .br .br template T abs (const complex&); .br template T arg (const complex&); .br template T norm (const complex&); .br .br template complex conj (const complex&); .br template complex polar (const T&, const T&); .br .br // Transcendentals .br template complex cos (const complex&); .br template complex cosh (const complex&); .br template complex exp (const complex&); .br template complex log (const complex&); .br .br template complex log10 (const complex&); .br .br template complex pow (const complex&, int); .br template complex pow (const complex&, T&); .br template complex pow (const complex&, .RE .RS 33 const complex&); .RE .RS 0 template complex pow (const T&, .RE .RS 33 const complex&); .RE .RS 0 .br template complex sin (const complex&); .br template complex sinh (const complex&); .br template complex sqrt (const complex&); .br template complex tan (const complex&); .br template complex tanh (const complex&); .SH CONSTRUCTORS .br complex .br (const T& re_arg = T(), const T& im_arg = T()); .RE .RS 3 Constructs an object of class complex, initializing \f2re_arg\fP to the real part and \f2im_arg\fP to the imaginary part. .RE .br template complex .br (const complex&); .RE .RS 3 Constructs a complex number from another complex number. .RE .SH ASSIGNMENT OPERATORS .br complex& operator=(const T& v); .RE .RS 3 Assigns \f2v\fP to the real part of itself, setting the imaginary part to \f20\fP. .RE .br complex& operator+=(const T& v); .RE .RS 3 Adds \f2v\fP to the real part of itself, then returns the result. .RE .br complex& operator-=(const T& v); .RE .RS 3 Subtracts \f2v\fP from the real part of itself, then returns the result. .RE .br complex& operator*=(const T& v); .RE .RS 3 Multiplies \f2v\fP by the real part of itself, then returns the result. .RE .br complex& operator/=(const T& v); .RE .RS 3 Divides \f2v\fP by the real part of itself, then returns the result. .RE .br template .br complex .br operator=(const complex& c); .RE .RS 3 Assigns \f2c\fP to itself. .RE .br template .br complex .br operator+=(const complex& c); .RE .RS 3 Adds \f2c\fP to itself, then returns the result. .RE .br template .br complex .br operator-=(const complex& c); .RE .RS 3 Subtracts \f2c\fP from itself, then returns the result. .RE .br template .br complex .br operator*=(const complex& c); .RE .RS 3 Multiplies itself by \f2c\fP, then returns the result. .RE .br template .br complex .br operator/=(const complex& c); .RE .RS 3 Divides itself by \f2c\fP, then returns the result. .RE .SH MEMBER FUNCTIONS .br T .br imag() const; .RE .RS 3 Returns the imaginary part of the complex number. .RE .br T .br real() const; .RE .RS 3 Returns the real part of the complex number. .RE .SH NON-MEMBER OPERATORS .br template complex .br operator+(const complex& lhs,const complex& rhs); .br template complex .br operator+(const complex& lhs, const T& rhs); .br template complex .br operator+(const T& lhs, const complex& rhs); .RE .RS 3 Returns the sum of \f2lhs\fP and \f2rhs\fP. .RE .br template complex .br operator-(const complex& lhs,const complex& rhs); .br template complex .br operator-(const complex& lhs, const T& rhs); .br template complex .br operator-(const T& lhs, const complex& rhs); .RE .RS 3 Returns the difference of \f2lhs\fP and \f2rhs\fP. .RE .br template complex .br operator*(const complex& lhs,const complex& rhs); .br template complex .br operator*(const complex& lhs, const T& rhs); .br template complex .br operator* (const T& lhs, const complex& rhs); .RE .RS 3 Returns the product of \f2lhs\fP and \f2rhs\fP. .RE .br template complex .br operator/(const complex& lhs,const complex& rhs); .br template complex .br operator/(const complex& lhs, const T& rhs); .br template complex .br operator/(const T& lhs, const complex& rhs); .RE .RS 3 Returns the quotient of \f2lhs\fP divided by \f2rhs\fP. .RE .br template complex .br operator+(const complex& rhs); .RE .RS 3 Returns \f2rhs\fP. .RE .br template complex .br operator-(const complex& lhs); .RE .RS 3 Returns \f2complex(-lhs.real(), -lhs.imag())\fP. .RE .br template bool .br operator==(const complex& x, const complex& y); .RE .RS 3 Returns \f2true\fP if the real and imaginary parts of \f2x\fP and \f2y\fP are equal. .RE .br template bool .br operator==(const complex& x, const T& y); .RE .RS 3 Returns \f2true\fP if \f2y\fP is equal to the real part of \f2x\fP and the imaginary part of \f2x\fP is equal to \f20\fP. .RE .br template bool .br operator==(const T& x, const complex& y); .RE .RS 3 Returns \f2true\fP if \f2x\fP is equal to the real part of \f2y\fP and the imaginary part of \f2y\fP is equal to \f20\fP. .RE .br template bool .br operator!=(const complex& x, const complex& y); .RE .RS 3 Returns \f2true\fP if either the real or the imaginary part of \f2x\fP and \f2y\fP are not equal. .RE .br template bool .br operator!=(const complex& x, const T& y); .RE .RS 3 Returns \f2true\fP if \f2y\fP is not equal to the real part of \f2x\fP or the imaginary part of \f2x\fP is not equal to \f20\fP. .RE .br template bool .br operator!=(const T& x, const complex& y); .RE .RS 3 Returns \f2true\fP if \f2x\fP is not equal to the real part of \f2y\fP or the imaginary part of \f2y\fP is not equal to \f20\fP. .RE .br template .RE .RS 9 basic_istream& .RE .RS 0 operator>>(basic_istream& is, complex& x); .RE .RS 3 Reads a complex number \f2x\fP into the input stream \f2is\fP. \f2x\fP may be of the form \f2u\fP, \f2(u)\fP, or \f2(u,v)\fP where \f2u\fP is the real part and \f2v\fP is the imaginary part. If bad input is encountered, \f2is.setstate(ios::failbit)\fP is called. .RE .br template .RE .RS 9 basic_ostream& .RE .RS 0 operator<<(basic_ostream& os, .RE .RS 10 const complex& x); .RE .RS 3 Returns \f2os << "(" << x.real() << "," << x.imag() << ")"\fP. .RE .SH NON-MEMBER FUNCTIONS .RE .RS 0 template T .br abs(const complex& c); .RE .RS 3 Returns the absolute value or magnitude of \f2c\fP (the square root of the norm). .RE .br template T .br arg(const complex& x); .RE .RS 3 Returns the phase angle of \f2x\fP or \f2atan2(imag(x), real(x))\fP. .RE .br template complex .br conj(const complex& c); .RE .RS 3 Returns the conjugate of \f2c\fP. .RE .br template complex .br cos(const complex& c); .RE .RS 3 Returns the cosine of \f2c\fP. .RE .br template complex .br cosh(const complex& c); .RE .RS 3 Returns the hyperbolic cosine of \f2c\fP. .RE .br template complex .br exp(const complex& x); .RE .RS 3 Returns \f2e\fP raised to the \f2x\fP power. .RE .br template T .br imag(const complex& c) const; .RE .RS 3 Returns the imaginary part of \f2c\fP. .RE .br template complex .br log(const complex& x); .RE .RS 3 Returns the complex natural (base e) logarithm of \f2x\fP, 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 \f2x\fP is a nega- tive real number, \f2imag(log(x))\fP is pi. The branch cuts are along the negative real axis. .RE .br template complex .br log10(const complex& x); .RE .RS 3 Returns the complex common (base 10) logarithm of \f2x\fP, defined as log(\f2x\fP)/log(10). The branch cuts are along the negative real axis. .RE .br template T .br norm(const complex& c); .RE .RS 3 Returns the squared magnitude of \f2c\fP. (The sum of the squares of the real and imaginary parts.) .RE .br template complex .br polar(const T& m, const T& a = 0); .RE .RS 3 Returns the complex value of a complex number whose magnitude is \f2m\fP and phase angle is \f2a\fP, measured in radians. .RE .br template complex .br pow(const complex& x, int y); .br template complex .br pow(const complex& x, const T& y); .br template complex .br pow(const complex& x, const complex& y); .br template complex .br pow(const T& x, const complex& y); .RE .RS 3 Returns \f2x\fP raised to the \f2y\fP power; or, if called with \f2(0, 0)\fP, returns \f2complex (1,0)\fP. The branch cuts are along the negative real axis. .RE .br template T .br real(const complex& c); .RE .RS 3 Returns the real part of \f2c\fP. .RE .br template complex .br sin(const complex& c); .RE .RS 3 Returns the sine of \f2c\fP. .RE .br template complex .br sinh(const complex& c); .RE .RS 3 Returns the hyperbolic sine of \f2c\fP. .RE .br template complex .br sqrt(const complex& x); .RE .RS 3 Returns the complex square root of \f2x\fP, 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. .RE .br template complex .br tan(const complex& x); .RE .RS 3 Returns the tangent of \f2x\fP. .RE .br template complex .br tanh(const complex& x); .RE .RS 3 Returns the hyperbolic tangent of \f2x\fP. .RE .SH EXAMPLE .br // .br // complex.cpp .br // .RE .RS 1 #include .br #include .RE .RS 0 using namespace std; .br .br int main() .RE .RS 1 { .RE .RS 2 complex a(1.2, 3.4); .br complex b(-9.8, -7.6); .RE .RS 0 .RE .RS 2 a += b; .br a /= sin(b) * cos(a); .br b *= log(a) + pow(b, a); .RE .RS 0 .RE .RS 2 cout << "a = " << a << ", b = " << b << endl; .RE .RS 0 .RE .RS 2 return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br a = (1.42804e-06,-0.0002873), b = (58.2199,69.7354) .SH 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 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 \f2std\fP.