home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / h.z / COMPLEX.H < prev    next >
C/C++ Source or Header  |  1996-07-24  |  10KB  |  317 lines

  1. //
  2. //  complex.h    Complex Numbers
  3. //
  4. //  Copyright by WATCOM International Corp. 1988-1996.  All rights reserved.
  5. //
  6. #ifndef _COMPLEX_H_INCLUDED
  7. #define _COMPLEX_H_INCLUDED
  8.  
  9. #ifndef __cplusplus
  10. #error complex.h is for use with C++
  11. #endif
  12.  
  13. #if defined( __AXP__) || defined( __PPC__ )
  14. #pragma library( "cplx.lib" )
  15. #else
  16. #if defined(__SW_FPC)
  17. #if defined(__386__)
  18. #if defined(__3S__)
  19. #pragma library ( "cplx3s.lib" )
  20. #else
  21. #pragma library ( "cplx3r.lib" )
  22. #endif
  23. #else
  24. #if defined(__MEDIUM__)
  25. #pragma library ( "cplxm.lib" )
  26. #elif defined(__COMPACT__)
  27. #pragma library ( "cplxc.lib" )
  28. #elif defined(__LARGE__)
  29. #pragma library ( "cplxl.lib" )
  30. #elif defined(__HUGE__)
  31. #pragma library ( "cplxh.lib" )
  32. #else
  33. #pragma library ( "cplxs.lib" )
  34. #endif
  35. #endif
  36. #else
  37. #if defined(__386__)
  38. #if defined(__3S__)
  39. #pragma library ( "cplx73s.lib" )
  40. #else
  41. #pragma library ( "cplx73r.lib" )
  42. #endif
  43. #else
  44. #if defined(__MEDIUM__)
  45. #pragma library ( "cplx7m.lib" )
  46. #elif defined(__COMPACT__)
  47. #pragma library ( "cplx7c.lib" )
  48. #elif defined(__LARGE__)
  49. #pragma library ( "cplx7l.lib" )
  50. #elif defined(__HUGE__)
  51. #pragma library ( "cplx7h.lib" )
  52. #else
  53. #pragma library ( "cplx7s.lib" )
  54. #endif
  55. #endif
  56. #endif
  57. #endif
  58.  
  59. #ifndef _COMDEF_H_INCLUDED
  60.  #include <_comdef.h>
  61. #endif
  62. #ifndef _IOSTREAM_H_INCLUDED
  63.  #include <iostream.h>
  64. #endif
  65. #if defined(_M_IX86)
  66.   #pragma pack(__push,1);
  67. #else
  68.   #pragma pack(__push,8);
  69. #endif
  70.  
  71. class _WPRTLINK Complex {
  72.     double  __r;
  73.     double  __i;
  74.  
  75. public:
  76.     Complex();
  77.     Complex( Complex const & );
  78.     Complex( double __real, double __imag = 0.0 );
  79.  
  80.     Complex &operator = ( Complex const & );
  81.     Complex &operator = ( double );
  82.     Complex &operator += ( Complex const & );
  83.     Complex &operator += ( double );
  84.     Complex &operator -= ( Complex const & );
  85.     Complex &operator -= ( double );
  86.     Complex &operator *= ( Complex const & );
  87.     Complex &operator *= ( double );
  88.     Complex &operator /= ( Complex const & );
  89.     Complex &operator /= ( double );
  90.  
  91.     Complex operator + () const;
  92.     Complex operator - () const;
  93.  
  94.     double imag () const;
  95.     double real () const;
  96.  
  97.     friend _WPRTLINK istream &operator >> ( istream &, Complex & );
  98.     friend _WPRTLINK ostream &operator << ( ostream &, Complex const & );
  99. };
  100. #pragma pack(__pop);
  101.  
  102. typedef Complex complex;    // for compatibility
  103.  
  104. _WPRTLINK extern Complex operator + ( Complex const &, Complex const & );
  105. _WPRTLINK extern Complex operator + ( Complex const &, double );
  106. _WPRTLINK extern Complex operator + ( double,          Complex const & );
  107. _WPRTLINK extern Complex operator - ( Complex const &, Complex const & );
  108. _WPRTLINK extern Complex operator - ( Complex const &, double );
  109. _WPRTLINK extern Complex operator - ( double,          Complex const & );
  110. _WPRTLINK extern Complex operator * ( Complex const &, Complex const & );
  111. _WPRTLINK extern Complex operator * ( Complex const &, double );
  112. _WPRTLINK extern Complex operator * ( double,          Complex const & );
  113. _WPRTLINK extern Complex operator / ( Complex const &, Complex const & );
  114. _WPRTLINK extern Complex operator / ( Complex const &, double );
  115. _WPRTLINK extern Complex operator / ( double,          Complex const & );
  116.  
  117. _WPRTLINK extern int operator == ( Complex const &, Complex const & );
  118. _WPRTLINK extern int operator == ( Complex const &, double );
  119. _WPRTLINK extern int operator == ( double,       Complex const & );
  120. _WPRTLINK extern int operator != ( Complex const &, Complex const & );
  121. _WPRTLINK extern int operator != ( Complex const &, double );
  122. _WPRTLINK extern int operator != ( double,       Complex const & );
  123.  
  124. _WPRTLINK extern double  abs  ( Complex const & );    // magnitude of vector
  125. _WPRTLINK extern Complex acos ( Complex const & );    // arccosine
  126. _WPRTLINK extern Complex acosh( Complex const & );    // hyperbolic arccosine
  127. _WPRTLINK extern double  arg  ( Complex const & );    // angle of vector
  128. _WPRTLINK extern Complex asin ( Complex const & );    // arcsin
  129. _WPRTLINK extern Complex asinh( Complex const & );    // hyperbolic arcsin
  130. _WPRTLINK extern Complex atan ( Complex const & );    // arctangent
  131. _WPRTLINK extern Complex atanh( Complex const & );    // hyperbolic arctangent
  132. _WPRTLINK extern Complex conj ( Complex const & );    // conjugate
  133. _WPRTLINK extern Complex cos  ( Complex const & );    // cosine
  134. _WPRTLINK extern Complex cosh ( Complex const & );    // hyperbolic cosine
  135. _WPRTLINK extern Complex exp  ( Complex const & );    // e raised to a power
  136. _WPRTLINK extern double  imag ( Complex const & );    // imaginary part
  137. _WPRTLINK extern Complex log  ( Complex const & );    // log base e
  138. _WPRTLINK extern Complex log10( Complex const & );    // log base 10
  139. _WPRTLINK extern double  norm ( Complex const & );    // square of magnitude
  140. _WPRTLINK extern Complex polar( double __mag,         // polar to Complex
  141.                 double __angle = 0 );
  142. _WPRTLINK extern Complex pow  ( Complex const &__base,    // Complex ** Complex
  143.                 Complex const &__power );
  144. _WPRTLINK extern Complex pow  ( Complex const &__base,    // Complex ** double
  145.                 double         __power );
  146. _WPRTLINK extern Complex pow  ( double         __base,    // double ** Complex
  147.                 Complex const &__power );
  148. _WPRTLINK extern Complex pow  ( Complex const &__base,    // Complex ** int
  149.                 int            __power );
  150. _WPRTLINK extern double  real ( Complex const & );    // real part
  151. _WPRTLINK extern Complex sin  ( Complex const & );    // sin
  152. _WPRTLINK extern Complex sinh ( Complex const & );    // hyperbolic sin
  153. _WPRTLINK extern Complex sqrt ( Complex const & );    // square root
  154. _WPRTLINK extern Complex tan  ( Complex const & );    // tan
  155. _WPRTLINK extern Complex tanh ( Complex const & );    // hyperbolic tangent
  156.  
  157. inline Complex::Complex() {
  158.     __r = 0.0;
  159.     __i = 0.0;
  160. }
  161.  
  162. inline Complex::Complex( Complex const &__cv ) {
  163.     __r = __cv.__r;
  164.     __i = __cv.__i;
  165. }
  166.  
  167. inline Complex::Complex( double __real, double __imag ) {
  168.     __r = __real;
  169.     __i = __imag;
  170. }
  171.  
  172. inline Complex &Complex::operator = ( Complex const &__cv ) {
  173.     __r = __cv.__r;
  174.     __i = __cv.__i;
  175.     return( *this );
  176. }
  177.  
  178. inline Complex &Complex::operator = ( double __dv ) {
  179.     __r = __dv;
  180.     __i = 0.0;
  181.     return( *this );
  182. }
  183.  
  184. inline Complex &Complex::operator += ( Complex const &__cv ) {
  185.     __r += __cv.__r;
  186.     __i += __cv.__i;
  187.     return( *this );
  188. }
  189.  
  190. inline Complex &Complex::operator += ( double __dv ) {
  191.     __r += __dv;
  192.     return( *this );
  193. }
  194.  
  195. inline Complex &Complex::operator -= ( Complex const &__cv ) {
  196.     __r -= __cv.__r;
  197.     __i -= __cv.__i;
  198.     return( *this );
  199. }
  200.  
  201. inline Complex &Complex::operator -= ( double __dv ) {
  202.     __r -= __dv;
  203.     return( *this );
  204. }
  205.  
  206. inline Complex &Complex::operator *= ( double __dv ) {
  207.     __r *= __dv;
  208.     __i *= __dv;
  209.     return( *this );
  210. }
  211.  
  212. inline Complex &Complex::operator /= ( double __dv ) {
  213.     __r /= __dv;
  214.     __i /= __dv;
  215.     return( *this );
  216. }
  217.  
  218. inline Complex operator + ( Complex const &__cv1, Complex const &__cv2 ) {
  219.     return Complex( __cv1.real() + __cv2.real(), __cv1.imag() + __cv2.imag() );
  220. }
  221.  
  222. inline Complex operator + ( Complex const &__cv, double __dv ) {
  223.     return Complex( __cv.real() + __dv, __cv.imag() );
  224. }
  225.  
  226. inline Complex operator + ( double __dv, Complex const &__cv ) {
  227.     return Complex( __dv + __cv.real(), __cv.imag() );
  228. }
  229.  
  230. inline Complex operator - ( Complex const &__cv1, Complex const &__cv2 ) {
  231.     return Complex( __cv1.real() - __cv2.real(), __cv1.imag() - __cv2.imag() );
  232. }
  233.  
  234. inline Complex operator - ( Complex const &__cv, double __dv ) {
  235.     return Complex( __cv.real() - __dv, __cv.imag() );
  236. }
  237.  
  238. inline Complex operator - ( double __dv, Complex const &__cv ) {
  239.     return Complex( __dv - __cv.real(), - __cv.imag() );
  240. }
  241.  
  242. inline Complex operator * ( Complex const &__cv1, Complex const &__cv2 ) {
  243.     return Complex( __cv1.real()*__cv2.real() - __cv1.imag()*__cv2.imag(),
  244.             __cv1.imag()*__cv2.real() + __cv1.real()*__cv2.imag() );
  245. }
  246.  
  247. inline Complex operator * ( Complex const &__cv, double __dv ) {
  248.     return Complex( __cv.real() * __dv, __cv.imag() * __dv );
  249. }
  250.  
  251. inline Complex operator * ( double __dv, Complex const &__cv ) {
  252.     return Complex( __dv * __cv.real(), __dv * __cv.imag() );
  253. }
  254.  
  255. inline Complex operator / ( Complex const &__cv, double __dv ) {
  256.     return Complex( __cv.real() / __dv, __cv.imag() / __dv );
  257. }
  258.  
  259. inline Complex Complex::operator + () const {
  260.     return( *this );
  261. }
  262.  
  263. inline Complex Complex::operator - () const {
  264.     return Complex( -__r, -__i );
  265. }
  266.  
  267. inline int operator == ( Complex const &__cv1, Complex const &__cv2 ) {
  268.     return( __cv1.real() == __cv2.real()  &&  __cv1.imag() == __cv2.imag() );
  269. }
  270.  
  271. inline int operator == ( Complex const &__cv, double __dv ) {
  272.     return( __cv.imag() == 0.0  &&  __cv.real() == __dv );
  273. }
  274.  
  275. inline int operator == ( double __dv, Complex const &__cv ) {
  276.     return( __cv.imag() == 0.0  &&  __dv == __cv.real() );
  277. }
  278.  
  279. inline int operator != ( Complex const &__cv1, Complex const &__cv2 ) {
  280.     return( __cv1.real() != __cv2.real()  ||  __cv1.imag() != __cv2.imag() );
  281. }
  282.  
  283. inline int operator != ( Complex const &__cv, double __dv ) {
  284.     return( __cv.imag() != 0.0  ||  __cv.real() != __dv );
  285. }
  286.  
  287. inline int operator != ( double __dv, Complex const &__cv ) {
  288.     return( __cv.imag() != 0.0  ||  __dv != __cv.real() );
  289. }
  290.  
  291. inline Complex conj( Complex const &__cv ) {
  292.     return Complex( __cv.real(), -__cv.imag() );
  293. }
  294.  
  295. inline double Complex::real() const {
  296.     return( __r );
  297. }
  298.  
  299. inline double real( Complex const &__cv ) {
  300.     return( __cv.real() );
  301. }
  302.  
  303. inline double Complex::imag() const {
  304.     return( __i );
  305. }
  306.  
  307. inline double imag( Complex const &__cv ) {
  308.     return( __cv.imag() );
  309. }
  310.  
  311. inline double norm( Complex const &__cv ) {
  312.     return( __cv.real() * __cv.real() + __cv.imag() * __cv.imag() );
  313. }
  314.  
  315.  
  316. #endif
  317.