home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / borhead.zip / COMPLEX.H < prev    next >
C/C++ Source or Header  |  1994-11-09  |  9KB  |  302 lines

  1. /* complex.h
  2.  
  3.     Complex Number Library - Include File
  4.     class complex:  declarations for complex numbers.
  5.  
  6. All function names, member names, and operators have been borrowed
  7. from AT&T C++, except for the addition of:
  8.  
  9.     friend complex _RTLENTRY acos(complex _FAR &);
  10.     friend complex _RTLENTRY asin(complex _FAR &);
  11.     friend complex _RTLENTRY atan(complex _FAR &);
  12.     friend complex _RTLENTRY log10(complex _FAR &);
  13.     friend complex _RTLENTRY tan(complex _FAR &);
  14.     friend complex _RTLENTRY tanh(complex _FAR &);
  15.     complex _RTLENTRY operator+();
  16.     complex _RTLENTRY operator-();
  17. */
  18.  
  19. /*
  20.  *      C/C++ Run Time Library - Version 1.5
  21.  *
  22.  *      Copyright (c) 1990, 1994 by Borland International
  23.  *      All Rights Reserved.
  24.  *
  25.  */
  26.  
  27. #ifndef __cplusplus
  28. #error Must use C++ for the type complex.
  29. #endif
  30.  
  31. #if !defined(__COMPLEX_H)
  32. #define __COMPLEX_H
  33.  
  34.  
  35. #if !defined(___DEFS_H)
  36. #include <_defs.h>
  37. #endif
  38.  
  39. #if !defined(__MATH_H)
  40. #include <math.h>
  41. #endif
  42.  
  43. #if !defined(__IOSTREAM_H)
  44. #include <iostream.h>
  45. #endif
  46.  
  47.  
  48. #if !defined(RC_INVOKED)
  49.  
  50. #if defined(__BCOPT__)
  51. #endif
  52.  
  53. #pragma option -Vo-
  54.  
  55. #if defined(__STDC__)
  56. #pragma warn -nak
  57. #endif
  58.  
  59. #endif  /* !RC_INVOKED */
  60.  
  61.  
  62. class _EXPCLASS complex {
  63.  
  64. public:
  65.     // constructors
  66.     _RTLENTRY complex(double __re_val, double __im_val=0);
  67.     _RTLENTRY complex();
  68.  
  69.     // complex manipulations
  70.     friend double _RTLENTRY _EXPFUNC real(complex _FAR &);   // the real part
  71.     friend double _RTLENTRY _EXPFUNC imag(complex _FAR &);   // the imaginary part
  72.     friend complex _RTLENTRY _EXPFUNC conj(complex _FAR &);  // the complex conjugate
  73.     friend double _RTLENTRY _EXPFUNC norm(complex _FAR &);   // the square of the magnitude
  74.     friend double _RTLENTRY _EXPFUNC arg(complex _FAR &);    // the angle in the plane
  75.  
  76.     // Create a complex object given polar coordinates
  77.     friend complex _RTLENTRY _EXPFUNC polar(double __mag, double __angle=0);
  78.  
  79.     // Overloaded ANSI C math functions
  80.     friend double  _RTLENTRY _EXPFUNC abs(complex _FAR &);
  81.     friend complex _RTLENTRY _EXPFUNC acos(complex _FAR &);
  82.     friend complex _RTLENTRY _EXPFUNC asin(complex _FAR &);
  83.     friend complex _RTLENTRY _EXPFUNC atan(complex _FAR &);
  84.     friend complex _RTLENTRY _EXPFUNC cos(complex _FAR &);
  85.     friend complex _RTLENTRY _EXPFUNC cosh(complex _FAR &);
  86.     friend complex _RTLENTRY _EXPFUNC exp(complex _FAR &);
  87.     friend complex _RTLENTRY _EXPFUNC log(complex _FAR &);
  88.     friend complex _RTLENTRY _EXPFUNC log10(complex _FAR &);
  89.     friend complex _RTLENTRY _EXPFUNC pow(complex _FAR & __base, double __expon);
  90.     friend complex _RTLENTRY _EXPFUNC pow(double __base, complex _FAR & __expon);
  91.     friend complex _RTLENTRY _EXPFUNC pow(complex _FAR & __base, complex _FAR & __expon);
  92.     friend complex _RTLENTRY _EXPFUNC sin(complex _FAR &);
  93.     friend complex _RTLENTRY _EXPFUNC sinh(complex _FAR &);
  94.     friend complex _RTLENTRY _EXPFUNC sqrt(complex _FAR &);
  95.     friend complex _RTLENTRY _EXPFUNC tan(complex _FAR &);
  96.     friend complex _RTLENTRY _EXPFUNC tanh(complex _FAR &);
  97.  
  98.     // Binary Operator Functions
  99.     friend complex _RTLENTRY _EXPFUNC operator+(complex _FAR &, complex _FAR &);
  100.     friend complex _RTLENTRY _EXPFUNC operator+(double, complex _FAR &);
  101.     friend complex _RTLENTRY _EXPFUNC operator+(complex _FAR &, double);
  102.     friend complex _RTLENTRY _EXPFUNC operator-(complex _FAR &, complex _FAR &);
  103.     friend complex _RTLENTRY _EXPFUNC operator-(double, complex _FAR &);
  104.     friend complex _RTLENTRY _EXPFUNC operator-(complex _FAR &, double);
  105.     friend complex _RTLENTRY _EXPFUNC operator*(complex _FAR &, complex _FAR &);
  106.     friend complex _RTLENTRY _EXPFUNC operator*(complex _FAR &, double);
  107.     friend complex _RTLENTRY _EXPFUNC operator*(double, complex _FAR &);
  108.     friend complex _RTLENTRY _EXPFUNC operator/(complex _FAR &, complex _FAR &);
  109.     friend complex _RTLENTRY _EXPFUNC operator/(complex _FAR &, double);
  110.     friend complex _RTLENTRY _EXPFUNC operator/(double, complex _FAR &);
  111.     friend int _RTLENTRY _EXPFUNC operator==(complex _FAR &, complex _FAR &);
  112.     friend int _RTLENTRY _EXPFUNC operator!=(complex _FAR &, complex _FAR &);
  113.     complex _FAR & _RTLENTRY operator+=(complex _FAR &);
  114.     complex _FAR & _RTLENTRY operator+=(double);
  115.     complex _FAR & _RTLENTRY operator-=(complex _FAR &);
  116.     complex _FAR & _RTLENTRY operator-=(double);
  117.     complex _FAR & _RTLENTRY operator*=(complex _FAR &);
  118.     complex _FAR & _RTLENTRY operator*=(double);
  119.     complex _FAR & _RTLENTRY operator/=(complex _FAR &);
  120.     complex _FAR & _RTLENTRY operator/=(double);
  121.     complex _RTLENTRY operator+();
  122.     complex _RTLENTRY operator-();
  123.  
  124. // Implementation
  125. private:
  126.         double re, im;
  127. };
  128.  
  129.  
  130. // Inline complex functions
  131.  
  132. inline _RTLENTRY complex::complex(double __re_val, double __im_val)
  133. {
  134.     re = __re_val;
  135.     im = __im_val;
  136. }
  137.  
  138. inline _RTLENTRY complex::complex()
  139. {
  140. /* if you want your complex numbers initialized ...
  141.     re = im = 0;
  142. */
  143. }
  144.  
  145. inline complex _RTLENTRY complex::operator+()
  146. {
  147.     return *this;
  148. }
  149.  
  150. inline complex _RTLENTRY complex::operator-()
  151. {
  152.     return complex(-re, -im);
  153. }
  154.  
  155.  
  156. // Definitions of compound-assignment operator member functions
  157.  
  158. inline complex _FAR & _RTLENTRY complex::operator+=(complex _FAR & __z2)
  159. {
  160.     re += __z2.re;
  161.     im += __z2.im;
  162.     return *this;
  163. }
  164.  
  165. inline complex _FAR & _RTLENTRY complex::operator+=(double __re_val2)
  166. {
  167.     re += __re_val2;
  168.     return *this;
  169. }
  170.  
  171. inline complex _FAR & _RTLENTRY complex::operator-=(complex _FAR & __z2)
  172. {
  173.     re -= __z2.re;
  174.     im -= __z2.im;
  175.     return *this;
  176. }
  177.  
  178. inline complex _FAR & _RTLENTRY complex::operator-=(double __re_val2)
  179. {
  180.     re -= __re_val2;
  181.     return *this;
  182. }
  183.  
  184. inline complex _FAR & _RTLENTRY complex::operator*=(double __re_val2)
  185. {
  186.     re *= __re_val2;
  187.     im *= __re_val2;
  188.     return *this;
  189. }
  190.  
  191. inline complex _FAR & _RTLENTRY complex::operator/=(double __re_val2)
  192. {
  193.     re /= __re_val2;
  194.     im /= __re_val2;
  195.     return *this;
  196. }
  197.  
  198.  
  199. // Definitions of non-member complex functions
  200.  
  201. inline double _RTLENTRY real(complex _FAR & __z)
  202. {
  203.     return __z.re;
  204. }
  205.  
  206. inline double _RTLENTRY imag(complex _FAR & __z)
  207. {
  208.     return __z.im;
  209. }
  210.  
  211. inline complex _RTLENTRY conj(complex _FAR & __z)
  212. {
  213.     return complex(__z.re, -__z.im);
  214. }
  215.  
  216. inline complex _RTLENTRY polar(double __mag, double __angle)
  217. {
  218.     return complex(__mag*cos(__angle), __mag*sin(__angle));
  219. }
  220.  
  221.  
  222. // Definitions of non-member binary operator functions
  223.  
  224. inline complex _RTLENTRY operator+(complex _FAR & __z1, complex _FAR & __z2)
  225. {
  226.     return complex(__z1.re + __z2.re, __z1.im + __z2.im);
  227. }
  228.  
  229. inline complex _RTLENTRY operator+(double __re_val1, complex _FAR & __z2)
  230. {
  231.     return complex(__re_val1 + __z2.re, __z2.im);
  232. }
  233.  
  234. inline complex _RTLENTRY operator+(complex _FAR & __z1, double __re_val2)
  235. {
  236.     return complex(__z1.re + __re_val2, __z1.im);
  237. }
  238.  
  239. inline complex _RTLENTRY operator-(complex _FAR & __z1, complex _FAR & __z2)
  240. {
  241.     return complex(__z1.re - __z2.re, __z1.im - __z2.im);
  242. }
  243.  
  244. inline complex _RTLENTRY operator-(double __re_val1, complex _FAR & __z2)
  245. {
  246.     return complex(__re_val1 - __z2.re, -__z2.im);
  247. }
  248.  
  249. inline complex _RTLENTRY operator-(complex _FAR & __z1, double __re_val2)
  250. {
  251.     return complex(__z1.re - __re_val2, __z1.im);
  252. }
  253.  
  254. inline complex _RTLENTRY operator*(complex _FAR & __z1, double __re_val2)
  255. {
  256.     return complex(__z1.re*__re_val2, __z1.im*__re_val2);
  257. }
  258.  
  259. inline complex _RTLENTRY operator*(double __re_val1, complex _FAR & __z2)
  260. {
  261.     return complex(__z2.re*__re_val1, __z2.im*__re_val1);
  262. }
  263.  
  264. inline complex _RTLENTRY operator/(complex _FAR & __z1, double __re_val2)
  265. {
  266.     return complex(__z1.re/__re_val2, __z1.im/__re_val2);
  267. }
  268.  
  269. inline int _RTLENTRY operator==(complex _FAR & __z1, complex _FAR & __z2)
  270. {
  271.     return __z1.re == __z2.re && __z1.im == __z2.im;
  272. }
  273.  
  274. inline int _RTLENTRY operator!=(complex _FAR & __z1, complex _FAR & __z2)
  275. {
  276.     return __z1.re != __z2.re || __z1.im != __z2.im;
  277. }
  278.  
  279.  
  280. // Complex stream I/O
  281.  
  282. ostream _FAR & _RTLENTRY _EXPFUNC operator<<(ostream _FAR &, complex _FAR &);
  283. istream _FAR & _RTLENTRY _EXPFUNC operator>>(istream _FAR &, complex _FAR &);
  284.  
  285.  
  286. #if !defined(RC_INVOKED)
  287.  
  288. #if defined(__STDC__)
  289. #pragma warn .nak
  290. #endif
  291.  
  292. #pragma option -Vo.
  293.  
  294. #if defined(__BCOPT__)
  295. #endif
  296.  
  297. #endif  /* !RC_INVOKED */
  298.  
  299.  
  300. #endif  // __COMPLEX_H
  301.  
  302.