Index

NAME

Complex - class for complex numbers


INCLUDE

include "Complex.h"

SYNTAX

 class Complex
 {
 private:
   real             re;
   real             im;

 public:

   inline real             Re() const;
   inline real             Im() const;

                    Complex();
                    Complex(const Complex& y);
                    Complex(real   r, real   i=0);

                   ~Complex();

   Complex&         operator =  (const Complex& y);

   Complex&         operator += (const Complex& y);
   Complex&         operator += (real   y);
   Complex&         operator -= (const Complex& y);
   Complex&         operator -= (real   y);
   Complex&         operator *= (const Complex& y);
   Complex&         operator *= (real   y);

   Complex&         operator /= (const Complex& y);
   Complex&         operator /= (real   y);
 };


 // non-inline functions

 char*  oform2 (char* s, Complex c);
 real   getReal (const Complex& c);

 Complex   operator /  (const Complex& x, const Complex& y);
 Complex   operator /  (const Complex& x, real   y);
 Complex   operator /  (real     x, const Complex& y);

 Complex   cos(const Complex& x);
 Complex   sin(const Complex& x);

 Complex   cosh(const Complex& x);
 Complex   sinh(const Complex& x);
 Complex   exp(const Complex& x);
 //int   exp(const int& i);
 Complex   log(const Complex& x);

 Complex   pow(const Complex& x, int p);
 Complex   pow(const Complex& x, const Complex& p);
 Complex   pow(const Complex& x, real   y);
 Complex   sqrt(const Complex& x);

 Is&  operator >> (Is& s, Complex& x);
 Os&  operator << (Os& s, const Complex& x);

 // other functions defined as inlines

 inline int  operator == (const Complex& x, const Complex& y);
 inline int  operator == (const Complex& x, real   y);
 inline int  operator != (const Complex& x, const Complex& y);
 inline int  operator != (const Complex& x, real   y);
 inline int  operator >  (const Complex& x, const Complex& y);
 inline int  operator >= (const Complex& x, const Complex& y);
 inline int  operator <  (const Complex& x, const Complex& y);
 inline int  operator <= (const Complex& x, const Complex& y);

 inline Complex  operator - (const Complex& x);
 inline Complex  conj(const Complex& x);
 inline Complex  operator + (const Complex& x, const Complex& y);
 inline Complex  operator + (const Complex& x, real   y);
 inline Complex  operator + (real   x, const Complex& y);
 inline Complex  operator - (const Complex& x, const Complex& y);
 inline Complex  operator - (const Complex& x, real   y);
 inline Complex  operator - (real   x, const Complex& y);
        Complex  operator * (const Complex& x, const Complex& y);
 inline Complex  operator * (const Complex& x, real   y);
 inline Complex  operator * (real   x, const Complex& y);

 inline real Re(const Complex& x);
 inline real Im(const Complex& x);
 inline real abs(const Complex& x);
 inline real norm(const Complex& x);
 inline real arg(const Complex& x);

 inline Complex  polar(real   r, real   t = 0.0);


 // inline members


 inline real Complex::Re() const { return re; }
 inline real Complex::Im() const { return im; }

 inline Complex::Complex() { re = im = 0.0; }
 inline Complex::Complex(const Complex& y) :re(y.Re()), im(y.Im()) {}
 inline Complex::Complex(real   r, real   i) :re(r), im(i) {}

 inline Complex::~Complex() {}


 inline real Re(const Complex& x)
 {
   return x.Re();
 }

 inline real Im(const Complex& x)
 {
   return x.Im();
 }

 inline real abs(const Complex& x)
 {
   return sqrt(x.Re()*x.Re() + x.Im()*x.Im());
 }

 inline real norm(const Complex& x)
 {
   return (x.Re() * x.Re() + x.Im() * x.Im());
 }

 inline real arg(const Complex& x)
 {
   return atan2(x.Im(), x.Re());
 }

 inline Complex  polar(real   r, real   t)
 {
   return Complex(r * cos(t), r * sin(t));
 }

 inline int       operator >  (const Complex& x, const Complex& y)
 {
   return (norm(x) > norm(y));
 }

 inline int       operator <  (const Complex& x, const Complex& y)
 {
   return (norm(x) < norm(y));
 }

 inline int       operator >=  (const Complex& x, const Complex& y)
 {
   return (norm(x) >= norm(y));
 }

 inline int       operator <=  (const Complex& x, const Complex& y)
 {
   return (norm(x) <= norm(y));
 }


 inline Complex&  Complex::operator =  (const Complex& y)
 {
   re = y.Re(); im = y.Im(); return *this;
 }

 inline Complex&  Complex::operator += (const Complex& y)
 {
   re += y.Re();  im += y.Im(); return *this;
 }

 inline Complex&  Complex::operator += (real   y)
 {
   re += y; return *this;
 }

 inline Complex&  Complex::operator -= (const Complex& y)
 {
   re -= y.Re();  im -= y.Im(); return *this;
 }

 inline Complex&  Complex::operator -= (real   y)
 {
   re -= y; return *this;
 }

 inline Complex&  Complex::operator *= (const Complex& y)
 {
   real   r = re * y.Re() - im * y.Im();
   im = re * y.Im() + im * y.Re();
   re = r;
   return *this;
 }

 inline Complex&  Complex::operator *= (real   y)
 {
   re *=  y; im *=  y; return *this;
 }


 //  functions

 inline int  operator == (const Complex& x, const Complex& y)
 {
   return x.Re() == y.Re() && x.Im() == y.Im();
 }

 inline int  operator == (const Complex& x, real   y)
 {
   return x.Im() == 0.0 && x.Re() == y;
 }

 inline int  operator != (const Complex& x, const Complex& y)
 {
   return x.Re() != y.Re() || x.Im() != y.Im();
 }

 inline int  operator != (const Complex& x, real   y)
 {
   return x.Im() != 0.0 || x.Re() != y;
 }

 inline Complex  operator - (const Complex& x)
 {
   return Complex(-x.Re(), -x.Im());
 }

 inline Complex  conj(const Complex& x)
 {
   return Complex(x.Re(), -x.Im());
 }

 inline Complex  operator + (const Complex& x, const Complex& y)
 {
   return Complex(x.Re() + y.Re(), x.Im() + y.Im());
 }

 inline Complex  operator + (const Complex& x, real   y)
 {
   return Complex(x.Re() + y, x.Im());
 }

 inline Complex  operator + (real   x, const Complex& y)
 {
   return Complex(x + y.Re(), y.Im());
 }

 inline Complex  operator - (const Complex& x, const Complex& y)
 {
   return Complex(x.Re() - y.Re(), x.Im() - y.Im());
 }

 inline Complex  operator - (const Complex& x, real   y)
 {
   return Complex(x.Re() - y, x.Im());
 }

 inline Complex  operator - (real   x, const Complex& y)
 {
   return Complex(x - y.Re(), -y.Im());
 }

 inline Complex  operator * (const Complex& x, real   y)
 {
   return Complex(x.Re() * y, x.Im() * y);
 }

 inline Complex  operator * (real   x, const Complex& y)
 {
   return Complex(x * y.Re(), x * y.Im());
 }



 // Here are routines for Complex, where the corresponding int, float
 // and double versions are in genfc.h:

 inline Complex sqr     (Complex a)         { return a*a; }
 inline Complex pow3    (Complex a)         { return a*a*a; }
 inline Complex pow4    (Complex a)         { Complex b = a*a; return b*b; }

 extern Complex pow_int  (Complex a, int i);  // a to power i
 extern bool eq (Complex a, Complex b, real tol = comparison_tolerance);
 extern bool lt (Complex a, Complex b, real tol = comparison_tolerance);
 extern bool le (Complex a, Complex b, real tol = comparison_tolerance);

 #ifdef WIN32
 #undef min
 #undef max
 #endif

 inline Complex min(Complex arg1, Complex arg2)
 { return  (arg1 < arg2) ? arg1 : arg2; }

 inline Complex max(Complex arg1, Complex arg2)
 { return  (arg1 > arg2) ? arg1 : arg2; }




KEYWORDS

complex numbers



DESCRIPTION

The  class  is based on the Complex class in the GNU C++ library,
but the present version  contains  additional  functionality  and
some  of the syntax, names and design have been changed. We refer
to the copyright notice at the top of the header file.

Most of the class should be self-explanatory.

Only a few of the relevant Complex functions are members  of  the
class.   Most  of the functions that could be const (no modifica­
tion of the real and imaginary parts) are global  C++  functions.
These  include  cos, sin, cosh, sinh, exp. log, pow (several ver­
sions, some very efficient for integer exponents),  sqrt,  opera­
tor==,  operator!=, operator>, operator<, operator>=, operator<=,
operator-, conj (the conjugate), operator+, operator-, operator*,
Re  (real part), Im (imaginary part), abs, norm, arg, polar, sqr,
min and max.

If some functionality is not obvious from the functions name,  we
recommend  to  consult  the  implementation. It should be easy to
read.