home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 14 / CDACTUAL.iso / cdactual / demobin / share / program / c / ZPP120.ZIP / COMPLEX.H < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-16  |  6.0 KB  |  191 lines

  1. /* -------------------------------------------------------------------- */
  2. /* Z++ Version 1.20                               Last revised 02/15/93 */
  3. /*                                                                      */
  4. /* Complex number class for Turbo C++/Borland C++.                      */
  5. /* Copyright 1992, 1993 by Carl W. Moreland                             */
  6. /*                                                                      */
  7. /* complex.h                                                            */
  8. /* -------------------------------------------------------------------- */
  9.  
  10. #ifndef _COMPLEX_H
  11. #define _COMPLEX_H
  12.  
  13. #include <math.h>
  14. #include <iostream.h>
  15.  
  16. const unsigned char Z_RADIANS = 0;
  17. const unsigned char Z_DEGREES = 1;
  18. const unsigned char Z_COMMA   = 0;    // (x, y)
  19. const unsigned char Z_LETTER  = 1;    // x + iy
  20.  
  21. class complex
  22. {
  23. public:
  24.   double re, im;
  25.  
  26. private:
  27.   static unsigned char zArgMode;
  28.   static unsigned char zPrintMode;
  29.   static unsigned char zLetter;
  30.  
  31. public:
  32.   complex(void): re(0), im(0) {}
  33.   complex(const double real, const double imag=0): re(real), im(imag) {}
  34.   complex(const complex& z): re(z.re), im(z.im) {}
  35.  
  36.   friend double    re(const complex& z) {    // real part
  37.     return z.re;
  38.   }
  39.   friend double    im(const complex& z) {    // imaginary part
  40.     return z.im;
  41.   }
  42.   friend double  real(const complex& z) {    // real part
  43.     return z.re;
  44.   }
  45.   friend double  imag(const complex& z) {    // imaginary part
  46.     return z.im;
  47.   }
  48.   friend double   mag(const complex& z) {    // magnitude |z|
  49.     return sqrt(z.re*z.re + z.im*z.im);
  50.   }
  51.   friend double   arg(const complex& z);    // argument
  52.   friend double   ang(const complex& z) {    // angle
  53.     return arg(z);
  54.   }
  55.   friend double    ph(const complex& z) {    // phase
  56.     return arg(z);
  57.   }
  58.   friend complex conj(const complex& z) {    // complex conjugate
  59.     return complex(z.re, -z.im);
  60.   }
  61.   friend double  norm(const complex& z) {    // norm
  62.     return z.re*z.re + z.im*z.im;
  63.   }
  64.   operator double() {                // cast operator
  65.     return sqrt(re*re + im*im);
  66.   }
  67.  
  68.   friend complex rtop(double x,   double y=0);
  69.   friend complex ptor(double mag, double angle=0);
  70.   complex& topolar(void);
  71.   complex& torect(void);
  72.  
  73.   void operator = (const complex& z) {        // z1 = z2
  74.     re = z.re;
  75.     im = z.im;
  76.   }
  77.   complex& operator += (const complex& z) {    // z1 += z2
  78.     re += z.re;
  79.     im += z.im;
  80.     return *this;
  81.   }
  82.   complex& operator -= (const complex& z) {    // z1 -= z2
  83.     re -= z.re;
  84.     im -= z.im;
  85.     return *this;
  86.   }
  87.   complex& operator *= (const complex& z) {    // z1 *= z2
  88.     *this = *this * z;
  89.     return *this;
  90.   }
  91.   complex& operator /= (const complex& z) {    // z1 /= z2
  92.     *this = *this / z;
  93.     return *this;
  94.   }
  95.   complex operator + (void) const {        // +z1
  96.     return *this;
  97.   }
  98.   complex operator - (void) const {        // -z1
  99.     return complex(-re, -im);
  100.   }
  101.  
  102.   friend complex operator + (const complex& z1, const complex& z2) {
  103.     return complex(z1.re + z2.re, z1.im + z2.im);
  104.   }
  105.   friend complex operator + (const complex& z, const double x) {
  106.     return complex(z.re+x, z.im);
  107.   }
  108.   friend complex operator + (const double x, const complex& z) {
  109.     return complex(z.re+x, z.im);
  110.   }
  111.   friend complex operator - (const complex& z1, const complex& z2) {
  112.     return complex(z1.re - z2.re, z1.im - z2.im);
  113.   }
  114.   friend complex operator - (const complex&, const double);
  115.   friend complex operator - (const double x, const complex& z) {
  116.     return complex(x-z.re, -z.im);
  117.   }
  118.   friend complex operator * (const complex& z1, const complex& z2) {
  119.     double re = z1.re*z2.re - z1.im*z2.im;
  120.     double im = z1.re*z2.im + z1.im*z2.re;
  121.     return complex(re, im);
  122.   }
  123.   friend complex operator * (const complex& z, const double x) {
  124.     return complex(z.re*x, z.im*x);
  125.   }
  126.   friend complex operator * (const double x, const complex& z) {
  127.     return complex(z.re*x, z.im*x);
  128.   }
  129.   friend complex operator / (const complex&, const complex&);
  130.   friend complex operator / (const complex& z, const double x) {
  131.     return complex(z.re/x, z.im/x);
  132.   }
  133.   friend complex operator / (const double, const complex&);
  134.   friend complex operator ^ (const complex& z1, const complex& z2) {
  135.     return pow(z1, z2);
  136.   }
  137.  
  138.   friend int operator == (const complex& z1, const complex& z2) {
  139.     return (z1.re == z2.re) && (z1.im == z2.im);
  140.   }
  141.   friend int operator != (const complex& z1, const complex& z2) {
  142.     return (z1.re != z2.re) || (z1.im != z2.im);
  143.   }
  144.  
  145.   friend double   abs(const complex& z);
  146.   friend complex sqrt(const complex& z);
  147.   friend complex pow(const complex& base, const complex& exp);
  148.   friend complex pow(const complex& base, const double   exp);
  149.   friend complex pow(const double   base, const complex& exp);
  150.  
  151.   friend complex   exp(const complex& z);
  152.   friend complex   log(const complex& z);
  153.   friend complex    ln(const complex& z);
  154.   friend complex log10(const complex& z);
  155.  
  156.   friend complex  cos(const complex& z);
  157.   friend complex  sin(const complex& z);
  158.   friend complex  tan(const complex& z);
  159.  
  160.   friend complex asin(const complex& z);
  161.   friend complex acos(const complex& z);
  162.   friend complex atan(const complex& z);
  163.  
  164.   friend complex sinh(const complex& z);
  165.   friend complex cosh(const complex& z);
  166.   friend complex tanh(const complex& z);
  167.  
  168.   void SetArgMode(unsigned char mode) const {
  169.     if(mode == Z_RADIANS || mode == Z_DEGREES)
  170.       zArgMode = mode;
  171.   }
  172.   void SetPrintMode(unsigned char mode) const {
  173.     if(mode == Z_COMMA || mode == Z_LETTER)
  174.       zPrintMode = mode;
  175.   }
  176.   void SetLetter(unsigned char letter) const {
  177.     zLetter = letter;
  178.   }
  179.  
  180.   friend ostream& operator<<(ostream&, const complex&);
  181.   friend istream& operator>>(istream&, complex&);
  182. };
  183.  
  184. static const complex Z0(0, 0);        // complex number 0
  185. static const complex Z1(1, 0);        // complex number 1
  186. static const complex Zi(0, 1);        // complex number i
  187. static const complex Zinf(HUGE_VAL, HUGE_VAL); // complex number infinity
  188. static const complex Complex;
  189.  
  190. #endif
  191.