\File{complex\-class.h},{12:34},{Jan 12 1998}
\L{\LB{\C{}\/* \$Id: complex\-class.h,v 1.1 1997\/12\/28 05:03:56 bert Exp \$}}
\L{\LB{ *}}
\L{\LB{ * making complex numbers work with C++}}
\L{\LB{ *}}
\L{\LB{ * The following code is loosely based on the standard \"complex\" class}}
\L{\LB{ * interface, but without some of the brain damage.  If you don\'t know}}
\L{\LB{ * C++, please ignore this file, and all the other code will look like}}
\L{\LB{ * ordinary C to you. =)}}
\L{\LB{ *\/\CE{}}}
\L{\LB{}}
\L{\LB{\K{\#ifndef} \_COMPLEX\_CLASS\_H}}
\L{\LB{\K{\#define} \_COMPLEX\_CLASS\_H}}
\L{\LB{}}
\L{\LB{\C{}\/* general C++ kludge; I don\'t really have anywhere else to put this *\/\CE{}}}
\L{\LB{\K{\#ifndef} NULL}}
\L{\LB{\K{\#define} NULL 0L}}
\L{\LB{\K{\#endif}}}
\L{\LB{}}
\L{\LB{\K{\#include} \<math.h\>}}
\L{\LB{ }}
\L{\LB{\K{\#define} \K{real} \K{double}}}
\L{\LB{\K{\#define} LFMT}}
\L{\LB{}}
\L{\LB{class \K{complex} \{}}
\L{\LB{  \/\/ structure: \K{real} and imaginary parts, freely accessible}}
\L{\LB{ public:}}
\L{\LB{  \K{double} re, im;}}
\L{\LB{}}
\L{\LB{  \/\/ initializers}}
\L{\LB{  \K{complex} ()                             \{\};}}
\L{\LB{  \K{complex} (\K{double} rr, \K{double} ii = 0.0)   \{ re = rr;  im = ii; \};}}
\L{\LB{}}
\L{\LB{  \/\/ member operators}}
\L{\LB{  inline \K{complex}\& operator+= (\K{complex} a) \{ re+=a.re; im+=a.im; \K{return} *this; \};}}
\L{\LB{  inline \K{complex}\& operator\-= (\K{complex} a) \{ re\-=a.re; im\-=a.im; \K{return} *this; \};}}
\L{\LB{  inline \K{complex}\& operator*= (\K{complex});}}
\L{\LB{  inline \K{complex}\& operator\/= (\K{complex});}}
\L{\LB{}}
\L{\LB{  \/\/ member operators \K{for} \K{complex}\-and\-\K{double} operations}}
\L{\LB{  inline \K{complex}\& operator+= (\K{double} a)  \{ re += a; \K{return} *this; \};}}
\L{\LB{  inline \K{complex}\& operator\-= (\K{double} a)  \{ re \-= a; \K{return} *this; \};}}
\L{\LB{  inline \K{complex}\& operator*= (\K{double} a)  \{ re *= a; im *= a; \K{return} *this; \};}}
\L{\LB{  inline \K{complex}\& operator\/= (\K{double} a)  \{ re \/= a; im \/= a; \K{return} *this; \};}}
\L{\LB{}}
\L{\LB{  \/\/ friend operators}}
\L{\LB{  friend inline \K{complex} operator+ (\K{complex}, \K{complex});}}
\L{\LB{  friend inline \K{complex} operator\- (\K{complex}, \K{complex});}}
\L{\LB{  friend inline \K{complex} operator* (\K{complex}, \K{complex});}}
\L{\LB{  friend inline \K{complex} operator\/ (\K{complex}, \K{complex});}}
\L{\LB{  friend inline \K{complex} operator\- (\K{complex});}}
\L{\LB{  friend inline \K{complex} operator\~ (\K{complex});}}
\L{\LB{}}
\L{\LB{  friend inline \K{complex} conj (\K{complex});}}
\L{\LB{}}
\L{\LB{  \/\/ friend operators \K{for} \K{complex}\-and\-\K{double} operations}}
\L{\LB{  friend inline \K{complex} operator+ (\K{double},  \K{complex});}}
\L{\LB{  friend inline \K{complex} operator+ (\K{complex}, \K{double});}}
\L{\LB{  friend inline \K{complex} operator\- (\K{double},  \K{complex});}}
\L{\LB{  friend inline \K{complex} operator\- (\K{complex}, \K{double});}}
\L{\LB{  friend inline \K{complex} operator* (\K{double},  \K{complex});}}
\L{\LB{  friend inline \K{complex} operator* (\K{complex}, \K{double});}}
\L{\LB{  friend inline \K{complex} operator\/ (\K{complex}, \K{double});}}
\L{\LB{\};}}
\L{\LB{}}
\L{\LB{\K{\#define} Re(z)    ((z).re)}}
\L{\LB{\K{\#define} Im(z)    ((z).im)}}
\L{\LB{}}
\L{\LB{const \K{complex} I (0,1);}}
\L{\LB{}}
\L{\LB{\/\/\/\/\/\/\/\/ basic arithmetic operations on C}}
\L{\LB{}}
\L{\LB{\/\/ binary plus (addition)}}
\L{\LB{inline \K{complex} operator+ (\K{complex} a, \K{complex} b)}}
\L{\LB{\{}}
\L{\LB{  \K{return} \K{complex} (a.re + b.re, a.im + b.im);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\/\/ binary minus (subtraction)}}
\L{\LB{inline \K{complex} operator\- (\K{complex} a, \K{complex} b) }}
\L{\LB{\{}}
\L{\LB{  \K{return} \K{complex} (a.re \- b.re, a.im \- b.im);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\/\/ multiplication of \K{complex} numbers}}
\L{\LB{inline \K{complex} operator* (\K{complex} a, \K{complex} b)}}
\L{\LB{\{}}
\L{\LB{  \K{return} \K{complex} (a.re * b.re \- a.im * b.im, a.re * b.im + a.im * b.re);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\/\/ division of \K{complex} numbers}}
\L{\LB{inline \K{complex} operator\/ (\K{complex} a, \K{complex} b)}}
\L{\LB{\{}}
\L{\LB{  \K{double} denom = ((b.re * b.re) + (b.im * b.im));}}
\L{\LB{  \K{return} \K{complex} ((a.re * b.re + a.im * b.im) \/ denom,}}
\L{\LB{}\Tab{16}{  (a.im * b.re \- a.re * b.im) \/ denom);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\/\/ unary minus (negation)}}
\L{\LB{inline \K{complex} operator\- (\K{complex} a) }}
\L{\LB{\{}}
\L{\LB{  \K{return} \K{complex} (\-a.re, \-a.im);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\/\/ \K{complex} conjugate (written either as \~z or conj(z))}}
\L{\LB{inline \K{complex} operator\~ (\K{complex} a)}}
\L{\LB{\{}}
\L{\LB{  \K{return} \K{complex} (a.re, \-a.im);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{inline \K{complex} conj (\K{complex} a)}}
\L{\LB{\{}}
\L{\LB{  \K{return} \K{complex} (a.re, \-a.im);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\/\/\/\/\/\/\/\/ basic arithmetic operations on CxR}}
\L{\LB{}}
\L{\LB{\/\/ binary plus (addition)}}
\L{\LB{inline \K{complex} operator+ (\K{complex} a, \K{double} r)}}
\L{\LB{\{}}
\L{\LB{  \K{return} \K{complex} (a.re + r, a.im);}}
\L{\LB{\}}}
\L{\LB{inline \K{complex} operator+ (\K{double} r, \K{complex} a)}}
\L{\LB{\{}}
\L{\LB{  \K{return} \K{complex} (a.re + r, a.im);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\/\/ binary minus (subtraction)}}
\L{\LB{inline \K{complex} operator\- (\K{complex} a, \K{double} r) }}
\L{\LB{\{}}
\L{\LB{  \K{return} \K{complex} (a.re \- r, a.im);}}
\L{\LB{\}}}
\L{\LB{inline \K{complex} operator\- (\K{double} r, \K{complex} a)}}
\L{\LB{\{}}
\L{\LB{  \K{return} \K{complex} (r \- a.re, \-a.im);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\/\/ multiplication of \K{complex} numbers by reals}}
\L{\LB{inline \K{complex} operator* (\K{complex} a, \K{double} r)}}
\L{\LB{\{}}
\L{\LB{  \K{return} \K{complex} (a.re * r, a.im * r);}}
\L{\LB{\}}}
\L{\LB{inline \K{complex} operator* (\K{double} r, \K{complex} a)}}
\L{\LB{\{}}
\L{\LB{  \K{return} \K{complex} (a.re * r, a.im * r);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\/\/ division of \K{complex} numbers}}
\L{\LB{inline \K{complex} operator\/ (\K{complex} a, \K{double} r)}}
\L{\LB{\{}}
\L{\LB{  \K{return} \K{complex} (a.re \/ r, a.im \/ r);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\/\/\/\/\/\/\/\/ class\-member assignment operators}}
\L{\LB{}}
\L{\LB{\/\/ multiplication by a \K{complex} number}}
\L{\LB{inline \K{complex}\& \K{complex}::operator*= (\K{complex} b)}}
\L{\LB{\{}}
\L{\LB{  \K{double} temp = re * b.re \- im * b.im;}}
\L{\LB{  im = re * b.im + im * b.re;}}
\L{\LB{  re = temp;}}
\L{\LB{  \K{return} *this;}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\/\/ division by a \K{complex} number}}
\L{\LB{inline \K{complex}\& \K{complex}::operator\/= (\K{complex} b)}}
\L{\LB{\{}}
\L{\LB{  \K{double} denom = ((b.re * b.re) + (b.im * b.im));}}
\L{\LB{  \K{double} temp = (re * b.re + im * b.im) \/ denom;}}
\L{\LB{  im = (im * b.re \- re * b.im) \/ denom;}}
\L{\LB{  re = temp;}}
\L{\LB{  \K{return} *this;}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\/\/\/\/\/\/\/\/ other useful functions}}
\L{\LB{}}
\L{\LB{\/\/ exponential of a \K{complex} number.}}
\L{\LB{inline \K{complex} Cexp(\K{complex} z)}}
\L{\LB{\{}}
\L{\LB{  \K{return} exp(Re(z)) * \K{complex} (cos(Im(z)), sin(Im(z)));}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\/\/ exponential of an imaginary number.}}
\L{\LB{inline \K{complex} CexpI(\K{double} im)}}
\L{\LB{\{}}
\L{\LB{  \K{return} \K{complex} (cos(im), sin(im));}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\/\/ \K{return} (\K{real}) absolute value of a \K{complex} number.}}
\L{\LB{inline \K{double} Cabs(\K{complex} z)}}
\L{\LB{\{}}
\L{\LB{  \C{}\/* C has no power operator, and it\'s silly to call pow() for squaring... *\/\CE{}}}
\L{\LB{  \K{return} sqrt((Re(z)*Re(z)) + (Im(z)*Im(z)));}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\/\/ \K{return} square of (\K{real}) absolute value of a \K{complex} number.}}
\L{\LB{inline \K{double} Csqabs(\K{complex} z)}}
\L{\LB{\{}}
\L{\LB{  \C{}\/* C has no power operator, and it\'s silly to call pow() for squaring... *\/\CE{}}}
\L{\LB{  \K{return} ((Re(z)*Re(z)) + (Im(z)*Im(z)));}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\/\/ \K{return} one of two square roots of a \K{complex} number.}}
\L{\LB{\/\/ we choose to \K{return} the value with positive \K{real} part.}}
\L{\LB{inline \K{complex} Csqrt(\K{complex} z)}}
\L{\LB{\{}}
\L{\LB{  \K{double} abq = sqrt((Re(z)*Re(z)) + (Im(z)*Im(z)));   \C{}\/* see above *\/\CE{}}}
\L{\LB{}}
\L{\LB{  \K{if} (Im(z)\>=0) \{}}
\L{\LB{    \K{return} \K{complex} ( sqrt((abq+Re(z))\/2),  sqrt((abq\-Re(z))\/2) );}}
\L{\LB{  \} \K{else} \{}}
\L{\LB{    \K{return} \K{complex} ( sqrt((abq+Re(z))\/2), \-sqrt((abq\-Re(z))\/2) );}}
\L{\LB{  \}}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\/\/ \K{return} the argument (ie, \S{}\"angle\"\SE{}) of a \K{complex} number.}}
\L{\LB{inline \K{double} Carg(\K{complex} z)}}
\L{\LB{\{}}
\L{\LB{  \K{return} atan2(Im(z), Re(z));}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\K{\#endif} \C{}\/* \_COMPLEX\_CLASS\_H *\/\CE{}}}
