home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / INC.PAK / COMPLEX.H < prev    next >
C/C++ Source or Header  |  1995-08-29  |  9KB  |  308 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 6.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. #if !defined(_RTL_ALLOW_po) && !defined(__FLAT__)
  52. #pragma option -po-     // disable Object data calling convention
  53. #endif
  54. #endif
  55.  
  56. #pragma option -Vo-
  57.  
  58. #if defined(__STDC__)
  59. #pragma warn -nak
  60. #endif
  61.  
  62. #endif  /* !RC_INVOKED */
  63.  
  64.  
  65. class _EXPCLASS complex {
  66.  
  67. public:
  68.     // constructors
  69.     _RTLENTRY complex(double __re_val, double __im_val=0);
  70.     _RTLENTRY complex();
  71.  
  72.     // complex manipulations
  73.     friend double _RTLENTRY _EXPFUNC real(complex _FAR &);   // the real part
  74.     friend double _RTLENTRY _EXPFUNC imag(complex _FAR &);   // the imaginary part
  75.     friend complex _RTLENTRY _EXPFUNC conj(complex _FAR &);  // the complex conjugate
  76.     friend double _RTLENTRY _EXPFUNC norm(complex _FAR &);   // the square of the magnitude
  77.     friend double _RTLENTRY _EXPFUNC arg(complex _FAR &);    // the angle in the plane
  78.  
  79.     // Create a complex object given polar coordinates
  80.     friend complex _RTLENTRY _EXPFUNC polar(double __mag, double __angle=0);
  81.  
  82.     // Overloaded ANSI C math functions
  83.     friend double  _RTLENTRY _EXPFUNC abs(complex _FAR &);
  84.     friend complex _RTLENTRY _EXPFUNC acos(complex _FAR &);
  85.     friend complex _RTLENTRY _EXPFUNC asin(complex _FAR &);
  86.     friend complex _RTLENTRY _EXPFUNC atan(complex _FAR &);
  87.     friend complex _RTLENTRY _EXPFUNC cos(complex _FAR &);
  88.     friend complex _RTLENTRY _EXPFUNC cosh(complex _FAR &);
  89.     friend complex _RTLENTRY _EXPFUNC exp(complex _FAR &);
  90.     friend complex _RTLENTRY _EXPFUNC log(complex _FAR &);
  91.     friend complex _RTLENTRY _EXPFUNC log10(complex _FAR &);
  92.     friend complex _RTLENTRY _EXPFUNC pow(complex _FAR & __base, double __expon);
  93.     friend complex _RTLENTRY _EXPFUNC pow(double __base, complex _FAR & __expon);
  94.     friend complex _RTLENTRY _EXPFUNC pow(complex _FAR & __base, complex _FAR & __expon);
  95.     friend complex _RTLENTRY _EXPFUNC sin(complex _FAR &);
  96.     friend complex _RTLENTRY _EXPFUNC sinh(complex _FAR &);
  97.     friend complex _RTLENTRY _EXPFUNC sqrt(complex _FAR &);
  98.     friend complex _RTLENTRY _EXPFUNC tan(complex _FAR &);
  99.     friend complex _RTLENTRY _EXPFUNC tanh(complex _FAR &);
  100.  
  101.     // Binary Operator Functions
  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-(double, complex _FAR &);
  107.     friend complex _RTLENTRY _EXPFUNC operator-(complex _FAR &, double);
  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 complex _RTLENTRY _EXPFUNC operator/(complex _FAR &, complex _FAR &);
  112.     friend complex _RTLENTRY _EXPFUNC operator/(complex _FAR &, double);
  113.     friend complex _RTLENTRY _EXPFUNC operator/(double, complex _FAR &);
  114.     friend int _RTLENTRY _EXPFUNC operator==(complex _FAR &, complex _FAR &);
  115.     friend int _RTLENTRY _EXPFUNC operator!=(complex _FAR &, complex _FAR &);
  116.     complex _FAR & _RTLENTRY operator+=(complex _FAR &);
  117.     complex _FAR & _RTLENTRY operator+=(double);
  118.     complex _FAR & _RTLENTRY operator-=(complex _FAR &);
  119.     complex _FAR & _RTLENTRY operator-=(double);
  120.     complex _FAR & _RTLENTRY operator*=(complex _FAR &);
  121.     complex _FAR & _RTLENTRY operator*=(double);
  122.     complex _FAR & _RTLENTRY operator/=(complex _FAR &);
  123.     complex _FAR & _RTLENTRY operator/=(double);
  124.     complex _RTLENTRY operator+();
  125.     complex _RTLENTRY operator-();
  126.  
  127. // Implementation
  128. private:
  129.         double re, im;
  130. };
  131.  
  132.  
  133. // Inline complex functions
  134.  
  135. inline _RTLENTRY complex::complex(double __re_val, double __im_val)
  136. {
  137.     re = __re_val;
  138.     im = __im_val;
  139. }
  140.  
  141. inline _RTLENTRY complex::complex()
  142. {
  143. /* if you want your complex numbers initialized ...
  144.     re = im = 0;
  145. */
  146. }
  147.  
  148. inline complex _RTLENTRY complex::operator+()
  149. {
  150.     return *this;
  151. }
  152.  
  153. inline complex _RTLENTRY complex::operator-()
  154. {
  155.     return complex(-re, -im);
  156. }
  157.  
  158.  
  159. // Definitions of compound-assignment operator member functions
  160.  
  161. inline complex _FAR & _RTLENTRY complex::operator+=(complex _FAR & __z2)
  162. {
  163.     re += __z2.re;
  164.     im += __z2.im;
  165.     return *this;
  166. }
  167.  
  168. inline complex _FAR & _RTLENTRY complex::operator+=(double __re_val2)
  169. {
  170.     re += __re_val2;
  171.     return *this;
  172. }
  173.  
  174. inline complex _FAR & _RTLENTRY complex::operator-=(complex _FAR & __z2)
  175. {
  176.     re -= __z2.re;
  177.     im -= __z2.im;
  178.     return *this;
  179. }
  180.  
  181. inline complex _FAR & _RTLENTRY complex::operator-=(double __re_val2)
  182. {
  183.     re -= __re_val2;
  184.     return *this;
  185. }
  186.  
  187. inline complex _FAR & _RTLENTRY complex::operator*=(double __re_val2)
  188. {
  189.     re *= __re_val2;
  190.     im *= __re_val2;
  191.     return *this;
  192. }
  193.  
  194. inline complex _FAR & _RTLENTRY complex::operator/=(double __re_val2)
  195. {
  196.     re /= __re_val2;
  197.     im /= __re_val2;
  198.     return *this;
  199. }
  200.  
  201.  
  202. // Definitions of non-member complex functions
  203.  
  204. inline double _RTLENTRY real(complex _FAR & __z)
  205. {
  206.     return __z.re;
  207. }
  208.  
  209. inline double _RTLENTRY imag(complex _FAR & __z)
  210. {
  211.     return __z.im;
  212. }
  213.  
  214. inline complex _RTLENTRY conj(complex _FAR & __z)
  215. {
  216.     return complex(__z.re, -__z.im);
  217. }
  218.  
  219. inline complex _RTLENTRY polar(double __mag, double __angle)
  220. {
  221.     return complex(__mag*cos(__angle), __mag*sin(__angle));
  222. }
  223.  
  224.  
  225. // Definitions of non-member binary operator functions
  226.  
  227. inline complex _RTLENTRY operator+(complex _FAR & __z1, complex _FAR & __z2)
  228. {
  229.     return complex(__z1.re + __z2.re, __z1.im + __z2.im);
  230. }
  231.  
  232. inline complex _RTLENTRY operator+(double __re_val1, complex _FAR & __z2)
  233. {
  234.     return complex(__re_val1 + __z2.re, __z2.im);
  235. }
  236.  
  237. inline complex _RTLENTRY operator+(complex _FAR & __z1, double __re_val2)
  238. {
  239.     return complex(__z1.re + __re_val2, __z1.im);
  240. }
  241.  
  242. inline complex _RTLENTRY operator-(complex _FAR & __z1, complex _FAR & __z2)
  243. {
  244.     return complex(__z1.re - __z2.re, __z1.im - __z2.im);
  245. }
  246.  
  247. inline complex _RTLENTRY operator-(double __re_val1, complex _FAR & __z2)
  248. {
  249.     return complex(__re_val1 - __z2.re, -__z2.im);
  250. }
  251.  
  252. inline complex _RTLENTRY operator-(complex _FAR & __z1, double __re_val2)
  253. {
  254.     return complex(__z1.re - __re_val2, __z1.im);
  255. }
  256.  
  257. inline complex _RTLENTRY operator*(complex _FAR & __z1, double __re_val2)
  258. {
  259.     return complex(__z1.re*__re_val2, __z1.im*__re_val2);
  260. }
  261.  
  262. inline complex _RTLENTRY operator*(double __re_val1, complex _FAR & __z2)
  263. {
  264.     return complex(__z2.re*__re_val1, __z2.im*__re_val1);
  265. }
  266.  
  267. inline complex _RTLENTRY operator/(complex _FAR & __z1, double __re_val2)
  268. {
  269.     return complex(__z1.re/__re_val2, __z1.im/__re_val2);
  270. }
  271.  
  272. inline int _RTLENTRY operator==(complex _FAR & __z1, complex _FAR & __z2)
  273. {
  274.     return __z1.re == __z2.re && __z1.im == __z2.im;
  275. }
  276.  
  277. inline int _RTLENTRY operator!=(complex _FAR & __z1, complex _FAR & __z2)
  278. {
  279.     return __z1.re != __z2.re || __z1.im != __z2.im;
  280. }
  281.  
  282.  
  283. // Complex stream I/O
  284.  
  285. ostream _FAR & _RTLENTRY _EXPFUNC operator<<(ostream _FAR &, complex _FAR &);
  286. istream _FAR & _RTLENTRY _EXPFUNC operator>>(istream _FAR &, complex _FAR &);
  287.  
  288.  
  289. #if !defined(RC_INVOKED)
  290.  
  291. #if defined(__STDC__)
  292. #pragma warn .nak
  293. #endif
  294.  
  295. #pragma option -Vo.
  296.  
  297. #if defined(__BCOPT__)
  298. #if !defined(_RTL_ALLOW_po) && !defined(__FLAT__)
  299. #pragma option -po.     // restore Object data calling convention
  300. #endif
  301. #endif
  302.  
  303. #endif  /* !RC_INVOKED */
  304.  
  305.  
  306. #endif  // __COMPLEX_H
  307.  
  308.