home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 14 / CDACTUAL.iso / cdactual / demobin / share / program / c / ZCOMPLEX.ZIP / COMPLEX.HPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-17  |  7.0 KB  |  420 lines

  1.  
  2. //    Header:        Complex
  3. //    Version:    2.00  28-Oct-1989
  4. //    Language:    C++ 2.0;  Environ: Any;  Compilers: Zortech C++ 2.01
  5. //    Purpose:    Provides the class "Complex" for C++ programs.  The
  6. //            majority of the class is implemented inline for
  7. //            efficiency.  Only the division, power, and i/o methods
  8. //            are actual functions.
  9. //    Written by:    Scott Robert Ladd, 705 West Virginia, Gunnison CO 81230
  10. //            BBS (303)641-6438; FidoNet 1:104/708
  11.  
  12. #ifndef COMPLEX_HPP
  13. #define COMPLEX_HPP
  14.  
  15. #include <math.h>
  16. #include <stream.hpp>
  17.  
  18. class Complex {
  19. private:
  20.     double        re;
  21.     double        im;
  22.     static void    ( * errorHandler) ();
  23. protected:
  24. public:
  25.             Complex ( void);
  26.             Complex ( const Complex &);
  27.             Complex ( double &, double &);
  28.             Complex ( double &);
  29.     static void    setErrorHandler ( void ( * userHandler) ());
  30.     friend double    real ( const Complex &);
  31.     friend double    imag ( const Complex &);
  32.     void        operator= ( const Complex &);
  33.     void        operator= ( double &);
  34.     Complex        operator- ();
  35.     friend Complex     operator+ ( const Complex &, const Complex &);
  36.     friend Complex     operator- ( const Complex &, const Complex &);
  37.     friend Complex     operator* ( const Complex &, const Complex &);
  38.     friend Complex     operator/ ( const Complex &, const Complex &);
  39.     Complex     operator+= ( const Complex &);
  40.     Complex        operator-= ( const Complex &);
  41.     Complex        operator*= ( const Complex &);
  42.     Complex        operator/= ( const Complex &);
  43.     friend int    operator== ( const Complex &, const Complex &);
  44.     friend int    operator!= ( const Complex &, const Complex &);
  45.     friend int    operator< ( const Complex &, const Complex &);
  46.     friend int    operator<= ( const Complex &, const Complex &);
  47.     friend int    operator> ( const Complex &, const Complex &);
  48.     friend int    operator>= ( const Complex &, const Complex &);
  49.     friend double    abs ( const Complex &);
  50.     friend double    norm ( const Complex &);
  51.     friend double    arg ( const Complex &);
  52.     friend Complex    polar ( double radius, double theta = 0.0);
  53.     friend Complex    conj ( const Complex &);
  54.     friend Complex    cos ( const Complex &);
  55.     friend Complex    sin ( const Complex &);
  56.     friend Complex    tan ( const Complex &);
  57.     friend Complex    cosh ( const Complex &);
  58.     friend Complex    sinh ( const Complex &);
  59.     friend Complex    tanh ( const Complex &);
  60.     friend Complex    exp ( const Complex &);
  61.     friend Complex    log ( const Complex &);
  62.     friend Complex    pow ( const Complex &, const Complex &);
  63.     friend Complex    sqrt ( const Complex &);
  64.     friend ostream &operator<< ( ostream &, const Complex &);
  65.     friend istream &operator>> ( istream &, Complex &);
  66. };
  67.  
  68. inline
  69. Complex::Complex (
  70. ) {
  71.     re = im = 0.0;
  72. }
  73.  
  74. inline
  75. Complex::Complex (
  76.     const Complex &    c
  77. ) {
  78.     re = c.re;
  79.     im = c.im;
  80. }
  81.  
  82. inline
  83. Complex::Complex (
  84.     double &    r,
  85.     double &    i
  86. ) {
  87.     re = r;
  88.     im = i;
  89. }
  90.  
  91. inline
  92. Complex::Complex (
  93.     double &    r
  94. ) {
  95.     re = r;
  96.     im = 0.0;
  97. }
  98.  
  99. inline
  100. void
  101. Complex::setErrorHandler (
  102.     void        ( * userHandler) ()
  103. ) {
  104.     errorHandler = userHandler;
  105. }
  106.  
  107. inline
  108. double
  109. real (
  110.     const Complex &    c
  111. ) {
  112.     return c.re;
  113. }
  114.  
  115. inline
  116. double
  117. imag (
  118.     const Complex &    c
  119. ) {
  120.     return c.im;
  121. }
  122.  
  123. inline
  124. void
  125. Complex::operator= (
  126.     const Complex &    c
  127. ) {
  128.     re = c.re;
  129.     im = c.im;
  130. }
  131.  
  132. inline
  133. void
  134. Complex::operator= (
  135.     double &    r
  136. ) {
  137.     re = r;
  138.     im = 0.0;
  139. }
  140.  
  141. inline
  142. Complex
  143. Complex::operator- (
  144. ) {
  145.     Complex        result;
  146.     result.re = -re;
  147.     result.im = -im;
  148.     return result;
  149. }
  150.  
  151. inline
  152. Complex
  153. operator+ (
  154.     const Complex &    c1,
  155.     const Complex &    c2
  156. ) {
  157.     Complex        result;
  158.     result.re = c1.re + c2.re;
  159.     result.im = c1.im + c2.im;
  160.     return result;
  161. }
  162.  
  163. inline
  164. Complex
  165. operator- (
  166.     const Complex &    c1,
  167.     const Complex &    c2
  168. ) {
  169.     Complex        result;
  170.     result.re = c1.re - c2.re;
  171.     result.im = c1.im - c2.im;
  172.     return result;
  173. }
  174.  
  175. inline
  176. Complex
  177. operator* (
  178.     const Complex &    c1,
  179.     const Complex &    c2
  180. ) {
  181.     Complex        result;
  182.     result.re = c1.re * c2.re - c1.im * c2.im;
  183.     result.im = c1.re * c2.im + c1.im * c2.re;
  184.     return result;
  185. }
  186.  
  187. inline
  188. Complex
  189. Complex::operator+= (
  190.     const Complex &    c
  191. ) {
  192.     re += c.re;
  193.     im += c.im;
  194.     return *this;
  195. }
  196.  
  197. inline
  198. Complex
  199. Complex::operator-= (
  200.     const Complex &    c
  201. ) {
  202.     re -= c.re;
  203.     im -= c.im;
  204.     return *this;
  205. }
  206.  
  207. inline
  208. Complex
  209. Complex::operator*= (
  210.     const Complex &    c
  211. ) {
  212.     double        oldReal;
  213.     oldReal = re;
  214.     re = re * c.re - im * c.im;
  215.     im = oldReal * c.im + im * c.re;
  216.     return *this;
  217. }
  218.  
  219. inline
  220. int
  221. operator== (
  222.     const Complex &    c1,
  223.     const Complex & c2
  224. ) {
  225.     return ( c1.re == c2.re) && ( c1.im == c2.im);
  226. }
  227.  
  228. inline
  229. int
  230. operator!= (
  231.     const Complex &    c1,
  232.     const Complex & c2
  233. ) {
  234.     return ( c1.re != c2.re) || ( c1.im != c2.im);
  235. }
  236.  
  237. inline
  238. int
  239. operator< (
  240.     const Complex &    c1,
  241.     const Complex & c2
  242. ) {
  243.     return abs ( c1) < abs ( c2);
  244. }
  245.  
  246. inline
  247. int
  248. operator<= (
  249.     const Complex & c1,
  250.     const Complex & c2
  251. ) {
  252.     return abs ( c1) <= abs ( c2);
  253. }
  254.  
  255. inline
  256. int
  257. operator> (
  258.     const Complex &    c1,
  259.     const Complex &    c2
  260. ) {
  261.     return abs ( c1) > abs ( c2);
  262. }
  263.  
  264. inline
  265. int
  266. operator>= (
  267.     const Complex &    c1,
  268.     const Complex &    c2
  269. ) {
  270.     return abs ( c1) >= abs ( c2);
  271. }
  272.  
  273. inline
  274. double
  275. abs (
  276.     const Complex &    c
  277. ) {
  278.     double        result;
  279.     result = sqrt ( c.re * c.re + c.im * c.im);
  280.     return result;
  281. }
  282.  
  283. inline
  284. double
  285. norm (
  286.     const Complex &    c
  287. ) {
  288.     double        result;
  289.     result = c.re * c.re + c.im * c.im;
  290.     return result;
  291. }
  292.  
  293. inline
  294. double
  295. arg (
  296.     const Complex &    c
  297. ) {
  298.     double        result;
  299.     result = atan2 ( c.im, c.re);
  300.     return result;
  301. }
  302.  
  303. inline
  304. Complex
  305. polar (
  306.     double        radius,
  307.     double        theta
  308. ) {
  309.     Complex        result;
  310.     result.re = radius * cos ( theta);
  311.     result.im = radius * sin ( theta);
  312.     return result;
  313. }
  314.  
  315. inline
  316. Complex
  317. conj (
  318.     const Complex &    c
  319. ) {
  320.     Complex        result;
  321.     result.re = c.re;
  322.     result.im = -c.im;
  323.     return result;
  324. }
  325.  
  326. inline
  327. Complex
  328. cos (
  329.     const Complex &    c
  330. ) {
  331.     Complex        result;
  332.     result.re = cos ( c.re) * cosh ( c.im);
  333.     result.im = cos ( c.re) * sinh ( c.im);
  334.     return result;
  335. }
  336.  
  337. inline
  338. Complex
  339. sin (
  340.     const Complex &    c
  341. ) {
  342.     Complex        result;
  343.     result.re = sin ( c.re) * cosh ( c.im);
  344.     result.im = cos ( c.re) * sinh ( c.im);
  345.     return result;
  346. }
  347.  
  348. inline
  349. Complex
  350. tan (
  351.     const Complex &    c
  352. ) {
  353.     Complex        result;
  354.     result = sin ( c) / cos ( c);
  355.     return result;
  356. }
  357.  
  358. inline
  359. Complex
  360. cosh (
  361.     const Complex &    c
  362. ) {
  363.     Complex        result;
  364.     result.re = cos ( c.im) * cosh ( c.re);
  365.     result.im = sin ( c.im) * sinh ( c.re);
  366.     return result;
  367. }
  368.  
  369. inline
  370. Complex
  371. sinh (
  372.     const Complex &     c
  373. ) {
  374.     Complex        result;
  375.     result.re = cos ( c.im) * sinh ( c.re);
  376.     result.im = sin ( c.im) * cosh ( c.re);
  377.     return result;
  378. }
  379.  
  380. inline
  381. Complex
  382. tanh (
  383.     const Complex &    c
  384. ) {
  385.     Complex        result;
  386.     result = sinh ( c) / cosh ( c);
  387.     return result;
  388. }
  389.  
  390. inline
  391. Complex
  392. exp (
  393.     const Complex &    c
  394. ) {
  395.     Complex        result;
  396.     double        x = exp ( c.re);
  397.     result.re = x * cos ( c.im);
  398.     result.im = x * sin ( c.im);
  399.     return result;
  400. }
  401.  
  402. inline
  403. Complex
  404. log (
  405.     const Complex &    c
  406. ) {
  407.     Complex        result;
  408.     double        hypot = abs ( c);
  409.     if ( hypot > 0.0) {
  410.         result.re = log ( hypot);
  411.         result.im = atan2 ( c.im, c.re);
  412.     } else
  413.         Complex::errorHandler ();
  414.     return result;
  415. }
  416.  
  417. #endif     // COMPLEX_HPP
  418.  
  419.  
  420.