home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / CTECHAPP.ZIP / CLASSES.ZIP / CLASSES next >
Text File  |  1990-05-24  |  13KB  |  597 lines

  1. //  Header:     Complex
  2. //  Version:    2.20
  3. //
  4. //  Language:   C++ 2.0
  5. //  Environ:    Any
  6. //
  7. //  Purpose:    Provides the class "Complex" for C++ programs. The majority
  8. //              of the class is implemented inline for efficiency. Only
  9. //              the division, power, and i/o methods are actual functions.
  10. //
  11. //  Written by: Scott Robert Ladd
  12.  
  13. #if !defined(__COMPLEX_HPP)
  14. #define __COMPLEX_HPP 1
  15.  
  16. class Complex
  17.     {
  18.     private:
  19.         double Real; // Real part
  20.         double Imag; // Imaginary part
  21.  
  22.         static void (* ErrorHandler)();
  23.  
  24.     public:
  25.         // constructors
  26.         Complex (void);
  27.         Complex (const Complex & c);
  28.         Complex (const double & r, const double & i);
  29.         Complex (const double & r);
  30.  
  31.         // method to set error handler function
  32.         static void SetErrorHandler(void (* userHandler)());
  33.  
  34.         // value extraction methods
  35.         friend double real(const Complex & c);
  36.         friend double imag(const Complex & c);
  37.  
  38.         // assignment methods
  39.         void operator = (const Complex & c);
  40.  
  41.         // utility methods
  42.         friend double abs(const Complex & c);
  43.         friend double norm(const Complex & c);
  44.         friend double arg(const Complex & c);
  45.  
  46.         // unary minus method
  47.         Complex operator - ();
  48.  
  49.         // calculation methods
  50.         friend Complex operator + (const Complex & c1, const Complex & c2);
  51.         friend Complex operator - (const Complex & c1, const Complex & c2);
  52.         friend Complex operator * (const Complex & c1, const Complex & c2);
  53.         friend Complex operator / (const Complex & c1, const Complex & c2);
  54.  
  55.         Complex operator += (const Complex & c);
  56.         Complex operator -= (const Complex & c);
  57.         Complex operator *= (const Complex & c);
  58.         Complex operator /= (const Complex & c);
  59.  
  60.         // comparison methods
  61.         friend int operator == (const Complex & c1, const Complex & c2);
  62.         friend int operator != (const Complex & c1, const Complex & c2);
  63.  
  64.         friend int operator <  (const Complex & c1, const Complex & c2);
  65.         friend int operator <= (const Complex & c1, const Complex & c2);
  66.  
  67.         friend int operator >  (const Complex & c1, const Complex & c2);
  68.         friend int operator >= (const Complex & c1, const Complex & c2);
  69.  
  70.         // polar coordinate methods
  71.         friend Complex polar(const double radius, const double theta = 0.0);
  72.         friend Complex conj(const Complex & c);
  73.  
  74.         // trigonometric methods
  75.         friend Complex cos(const Complex & c);
  76.         friend Complex sin(const Complex & c);
  77.         friend Complex tan(const Complex & c);
  78.  
  79.         friend Complex cosh(const Complex & c);
  80.         friend Complex sinh(const Complex & c);
  81.         friend Complex tanh(const Complex & c);
  82.  
  83.         // logarithmic methods
  84.         friend Complex exp(const Complex & c);
  85.         friend Complex log(const Complex & c);
  86.  
  87.         // "power" methods
  88.         friend Complex pow(const Complex & c, const Complex & power);
  89.         friend Complex sqrt(const Complex & c);
  90.  
  91.         // output method
  92.         int Print() const;
  93.     };
  94.  
  95. #endif
  96. //  Module:     Cp_asop
  97. //  Version:    2.20
  98. //
  99. //  Language:   C++ 2.0
  100. //  Environ:    Any
  101. //
  102. //  Purpose:    Assignment operators for Complex objects
  103. //
  104. //  Written by: Scott Robert Ladd
  105.  
  106. #include "Complex.hpp"
  107.  
  108. Complex Complex::operator += (const Complex & c)
  109.     {
  110.     Real += c.Real;
  111.     Imag += c.Imag;
  112.  
  113.     return *this;
  114.     }
  115.  
  116. Complex Complex::operator -= (const Complex & c)
  117.     {
  118.     Real -= c.Real;
  119.     Imag -= c.Imag;
  120.  
  121.     return *this;
  122.     }
  123.  
  124. Complex Complex::operator *= (const Complex & c)
  125.     {
  126.     double OldReal = Real; // save old Real value
  127.  
  128.     Real = (Real * c.Real) - (Imag * c.Imag);
  129.     Imag = (OldReal * c.Imag) + (Imag * c.Real);
  130.  
  131.     return *this;
  132.     }
  133.  
  134. Complex Complex::operator /= (const Complex & c)
  135.     {
  136.     double den = norm(c);
  137.  
  138.     if (den != 0.0)
  139.         {
  140.         double OldReal = Real;
  141.  
  142.         Real = (Real * c.Real + Imag * c.Imag) / den;
  143.         Imag = (Imag * c.Real - OldReal * c.Imag) / den;
  144.         }
  145.     else
  146.         Complex::ErrorHandler();
  147.  
  148.     return *this;
  149.     }
  150. //  Module:     Cp_comp
  151. //  Version:    2.20
  152. //
  153. //  Language:   C++ 2.0
  154. //  Environ:    Any
  155. //
  156. //  Purpose:    Comparison operations for Complex class
  157. //
  158. //  Written by: Scott Robert Ladd
  159.  
  160. #include "Complex.hpp"
  161.  
  162. extern "C"
  163.     {
  164.     #include "math.h"
  165.     #include "stdlib.h"
  166.     }
  167.  
  168. // comparison methods
  169. int operator == (const Complex & c1, const Complex & c2)
  170.     {
  171.     return (c1.Real == c2.Real) && (c1.Imag == c2.Imag);
  172.     }
  173.  
  174. int operator != (const Complex & c1, const Complex & c2)
  175.     {
  176.     return (c1.Real != c2.Real) || (c1.Imag != c2.Imag);
  177.     }
  178.  
  179.  
  180. int operator <  (const Complex & c1, const Complex & c2)
  181.     {
  182.     return abs(c1) < abs(c2);
  183.     }
  184.  
  185. int operator <= (const Complex & c1, const Complex & c2)
  186.     {
  187.     return abs(c1) <= abs(c2);
  188.     }
  189.  
  190.  
  191. int operator >  (const Complex & c1, const Complex & c2)
  192.     {
  193.     return abs(c1) > abs(c2);
  194.     }
  195.  
  196. int operator >= (const Complex & c1, const Complex & c2)
  197.     {
  198.     return abs(c1) >= abs(c2);
  199.     }
  200. //  Module:     Cp_log
  201. //  Version:    2.20
  202. //
  203. //  Language:   C++ 2.0
  204. //  Environ:    Any
  205. //
  206. //  Purpose:    Logarithmic functions for Complex class
  207. //
  208. //  Written by: Scott Robert Ladd
  209.  
  210. #include "Complex.hpp"
  211.  
  212. extern "C"
  213.     {
  214.     #include "math.h"
  215.     #include "stdlib.h"
  216.     }
  217.  
  218. // logarithmic methods
  219. Complex exp(const Complex & c)
  220.     {
  221.     double X = exp(c.Real);
  222.  
  223.     Complex result;
  224.  
  225.     result.Real = X * cos(c.Imag);
  226.     result.Imag = X * sin(c.Imag);
  227.  
  228.     return result;
  229.     }
  230.  
  231. Complex log(const Complex & c)
  232.     {
  233.     double hypot = abs(c);
  234.  
  235.     Complex result;
  236.  
  237.     if (hypot > 0.0)
  238.         {
  239.         result.Real = log(hypot);
  240.         result.Imag = atan2(c.Imag, c.Real);
  241.         }
  242.     else
  243.         Complex::ErrorHandler();
  244.  
  245.     return result;
  246.     }
  247. //  Module:     Cp_Misc
  248. //  Version:    2.20
  249. //
  250. //  Language:   C++ 2.0
  251. //  Environ:    Any
  252. //
  253. //  Purpose:    Miscellaneous methods for Complex class
  254. //
  255. //  Written by: Scott Robert Ladd
  256.  
  257. #include "Complex.hpp"
  258.  
  259. extern "C"
  260.     {
  261.     #include "math.h"
  262.     #include "stdio.h"
  263.     #include "stdlib.h"
  264.     }
  265.  
  266. // polar coordinate methods
  267. Complex polar(const double radius, const double theta)
  268.     {
  269.     Complex result;
  270.  
  271.     result.Real = radius * cos(theta);
  272.     result.Imag = radius * sin(theta);
  273.  
  274.     return result;
  275.     }
  276.  
  277. Complex conj(const Complex & c)
  278.     {
  279.     Complex result;
  280.  
  281.     result.Real =  c.Real;
  282.     result.Imag = -c.Imag;
  283.  
  284.     return result;
  285.     }
  286.  
  287. // output method
  288. int Complex::Print() const
  289.     {
  290.     int out_len;
  291.  
  292.     out_len = printf("(%g", Real);
  293.  
  294.     if (Imag >= 0.0)
  295.         {
  296.         ++out_len;
  297.         putchar('+');
  298.         }
  299.  
  300.     out_len += printf("%g)", Imag);
  301.  
  302.     return out_len;
  303.     }
  304. //  Module:     Cp_ops
  305. //  Version:    2.20
  306. //
  307. //  Language:   C++ 2.0
  308. //  Environ:    Any
  309. //
  310. //  Purpose:    Basic operators for Complex objects
  311. //
  312. //  Written by: Scott Robert Ladd
  313.  
  314. #include "Complex.hpp"
  315.  
  316. extern "C"
  317.     {
  318.     #include "math.h"
  319.     }
  320.  
  321. // unary minus method
  322. Complex Complex::operator - ()
  323.     {
  324.     Complex result;
  325.  
  326.     result.Real = -Real;
  327.     result.Imag = -Imag;
  328.  
  329.     return result;
  330.     }
  331.  
  332. // calculation methods
  333. Complex operator + (const Complex & c1, const Complex & c2)
  334.     {
  335.     Complex result;
  336.  
  337.     result.Real = c1.Real + c2.Real;
  338.     result.Imag = c1.Imag + c2.Imag;
  339.  
  340.     return result;
  341.     }
  342.  
  343. Complex operator - (const Complex & c1, const Complex & c2)
  344.     {
  345.     Complex result;
  346.  
  347.     result.Real = c1.Real - c2.Real;
  348.     result.Imag = c1.Imag - c2.Imag;
  349.  
  350.     return result;
  351.     }
  352.  
  353. Complex operator * (const Complex & c1, const Complex & c2)
  354.     {
  355.     Complex result;
  356.  
  357.     result.Real = (c1.Real * c2.Real) - (c1.Imag * c2.Imag);
  358.     result.Imag = (c1.Real * c2.Imag) + (c1.Imag * c2.Real);
  359.  
  360.     return result;
  361.     }
  362.  
  363. Complex operator / (const Complex & c1, const Complex & c2)
  364.     {
  365.     Complex result;
  366.     double den;
  367.  
  368.     den = norm(c2);
  369.  
  370.     if (den != 0.0)
  371.         {
  372.         result.Real = (c1.Real * c2.Real + c1.Imag * c2.Imag) / den;
  373.         result.Imag = (c1.Imag * c2.Real - c1.Real * c2.Imag) / den;
  374.         }
  375.     else
  376.         Complex::ErrorHandler();
  377.  
  378.     return result;
  379.     }
  380. //  Module:     Cp_Pow
  381. //  Version:    2.20
  382. //
  383. //  Language:   C++ 2.0
  384. //  Environ:    Any
  385. //
  386. //  Purpose:    Power function for Complex class
  387. //
  388. //  Written by: Scott Robert Ladd
  389.  
  390. #include "Complex.hpp"
  391.  
  392. extern "C"
  393.     {
  394.     #include "math.h"
  395.     #include "stdlib.h"
  396.     }
  397.  
  398. // "power" methods
  399. Complex pow(const Complex & c, const Complex & power)
  400.     {
  401.     Complex result;
  402.  
  403.     if (power.Real == 0.0 && power.Imag == 0.0)
  404.         {
  405.         result.Real = 1.0;
  406.         result.Imag = 0.0;
  407.         }
  408.     else
  409.         {
  410.         if (c.Real != 0.0 || c.Imag != 0.0)
  411.             result = exp(log(c) * power);
  412.         else
  413.             Complex::ErrorHandler();
  414.         }
  415.  
  416.     return result;
  417.     }
  418.  
  419. Complex sqrt(const Complex & c)
  420.     {
  421.     return pow(c,Complex(0.5,0.0));
  422.     }
  423. //  Module:     Cp_trig
  424. //  Version:    2.20
  425. //
  426. //  Language:   C++ 2.0
  427. //  Environ:    Any
  428. //
  429. //  Purpose:    Trignometric functions for Complex class
  430. //
  431. //  Written by: Scott Robert Ladd
  432.  
  433. #include "Complex.hpp"
  434.  
  435. extern "C"
  436.     {
  437.     #include "math.h"
  438.     #include "stdlib.h"
  439.     }
  440.  
  441. // trigonometric methods
  442. Complex cos(const Complex & c)
  443.     {
  444.     Complex result;
  445.  
  446.     result.Real =  cos(c.Real) * cosh(c.Imag);
  447.     result.Imag = -sin(c.Real) * sinh(c.Imag);
  448.  
  449.     return result;
  450.     }
  451.  
  452. Complex sin(const Complex & c)
  453.     {
  454.     Complex result;
  455.  
  456.     result.Real = sin(c.Real) * cosh(c.Imag);
  457.     result.Imag = cos(c.Real) * sinh(c.Imag);
  458.  
  459.     return result;
  460.     }
  461.  
  462. Complex tan(const Complex & c)
  463.     {
  464.     Complex result = sin(c) / cos(c);
  465.  
  466.     return result;
  467.     }
  468.  
  469. Complex cosh(const Complex & c)
  470.     {
  471.     Complex result;
  472.  
  473.     result.Real = cos(c.Imag) * cosh(c.Real);
  474.     result.Imag = sin(c.Imag) * sinh(c.Real);
  475.  
  476.     return result;
  477.     }
  478.  
  479. Complex sinh(const Complex & c)
  480.     {
  481.     Complex result;
  482.  
  483.     result.Real = cos(c.Imag) * sinh(c.Real);
  484.     result.Imag = sin(c.Imag) * cosh(c.Real);
  485.  
  486.     return result;
  487.     }
  488.  
  489. Complex tanh(const Complex & c)
  490.     {
  491.     Complex result = sinh(c) / cosh(c);
  492.  
  493.     return result;
  494.     }
  495. //  Module:     Cp_Util
  496. //  Version:    2.20
  497. //
  498. //  Language:   C++ 2.0
  499. //  Environ:    Any
  500. //
  501. //  Purpose:    Utility methods for Complex class
  502. //
  503. //  Written by: Scott Robert Ladd
  504.  
  505. #include "Complex.hpp"
  506.  
  507. extern "C"
  508.     {
  509.     #include "math.h"
  510.     #include "stdio.h"
  511.     #include "stdlib.h"
  512.     }
  513.  
  514. // prototype for default error handler
  515. static void DefaultHandler();
  516.  
  517. // assignment of default handler address to error function pointer
  518. void (* Complex::ErrorHandler)() = DefaultHandler;
  519.  
  520. // default error handler
  521. static void DefaultHandler()
  522.     {
  523.     puts("\aERROR in complex object: DIVIDE BY ZERO\n");
  524.     exit(1);
  525.     }
  526.  
  527. // constructors
  528. Complex::Complex (void)
  529.     {
  530.     Real = 0.0;
  531.     Imag = 0.0;
  532.     }
  533.  
  534. Complex::Complex (const Complex & c)
  535.     {
  536.     Real = c.Real;
  537.     Imag = c.Imag;
  538.     }
  539.  
  540. Complex::Complex (const double & r, const double & i)
  541.     {
  542.     Real = r;
  543.     Imag = i;
  544.     }
  545.  
  546. Complex::Complex (const double & r)
  547.     {
  548.     Real = r;
  549.     Imag = 0.0;
  550.     }
  551.  
  552. // method to set error handler function
  553. void Complex::SetErrorHandler(void (* userHandler)())
  554.     {
  555.     ErrorHandler = userHandler;
  556.     }
  557.  
  558. // value extraction methods
  559. double real (const Complex & c)
  560.     {
  561.     return c.Real;
  562.     }
  563.  
  564. double imag (const Complex & c)
  565.     {
  566.     return c.Imag;
  567.     }
  568.  
  569. // assignment method
  570. void Complex::operator = (const Complex & c)
  571.     {
  572.     Real = c.Real;
  573.     Imag = c.Imag;
  574.     }
  575.  
  576. // utility methods
  577. double abs(const Complex & c)
  578.     {
  579.     double result = sqrt(c.Real * c.Real + c.Imag * c.Imag);
  580.  
  581.     return result;
  582.     }
  583.  
  584. double norm(const Complex & c)
  585.     {
  586.     double result = (c.Real * c.Real) + (c.Imag * c.Imag);
  587.  
  588.     return result;
  589.     }
  590.  
  591. double arg(const Complex & c)
  592.     {
  593.     double result = atan2(c.Imag, c.Real);
  594.  
  595.     return result;
  596.     }
  597.