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