home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / c / djgpp / include / complex.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-03  |  7.0 KB  |  294 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY.  No author or distributor
  10. accepts responsibility to anyone for the consequences of using it
  11. or for whether it serves any particular purpose or works at all,
  12. unless he says so in writing.  Refer to the GNU CC General Public
  13. License for full details.
  14.  
  15. Everyone is granted permission to copy, modify and redistribute
  16. GNU CC, but only under the conditions described in the
  17. GNU CC General Public License.   A copy of this license is
  18. supposed to have been given to you along with GNU CC so you
  19. can know your rights and responsibilities.  It should be in a
  20. file named COPYING.  Among other things, the copyright notice
  21. and this notice must be preserved on all copies.  
  22. */
  23.  
  24. #ifndef _Complex_h
  25. #ifdef __GNUG__
  26. #pragma once
  27. #pragma interface
  28. #endif
  29. #define _Complex_h 1
  30.  
  31.  
  32. #include <stream.h>
  33. #include <math.h>
  34.  
  35. class Complex
  36. {
  37. #ifdef __ATT_complex__
  38. public:
  39. #else
  40. protected:
  41. #endif
  42.  
  43.   double           re;
  44.   double           im;
  45.  
  46. public:
  47.  
  48.   double           real() const;
  49.   double           imag() const;
  50.  
  51.                    Complex();
  52.                    Complex(const Complex& y);
  53.                    Complex(double r, double i=0);
  54.  
  55.                   ~Complex();
  56.  
  57.   Complex&         operator =  (const Complex& y);
  58.  
  59.   Complex&         operator += (const Complex& y);
  60.   Complex&         operator += (double y);
  61.   Complex&         operator -= (const Complex& y);
  62.   Complex&         operator -= (double y);
  63.   Complex&         operator *= (const Complex& y);
  64.   Complex&         operator *= (double y);
  65.  
  66.   Complex&         operator /= (const Complex& y); 
  67.   Complex&         operator /= (double y); 
  68.  
  69.   void             error(const char* msg) const;
  70. };
  71.  
  72.  
  73. // error handlers
  74.  
  75. extern  void default_Complex_error_handler(const char*);
  76. extern  one_arg_error_handler_t Complex_error_handler;
  77.  
  78. extern  one_arg_error_handler_t 
  79.         set_Complex_error_handler(one_arg_error_handler_t f);
  80.  
  81.  
  82. // non-inline functions
  83.  
  84. Complex   operator /  (const Complex& x, const Complex& y);
  85. Complex   operator /  (const Complex& x, double y);
  86. Complex   operator /  (double   x, const Complex& y);
  87.  
  88. Complex   cos(const Complex& x);
  89. Complex   sin(const Complex& x);
  90.  
  91. Complex   cosh(const Complex& x);
  92. Complex   sinh(const Complex& x);
  93.  
  94. Complex   exp(const Complex& x);
  95. Complex   log(const Complex& x);
  96.  
  97. Complex   pow(const Complex& x, long p);
  98. Complex   pow(const Complex& x, const Complex& p);
  99. Complex   pow(const Complex& x, double y);
  100. Complex   sqrt(const Complex& x);
  101.    
  102. istream&  operator >> (istream& s, Complex& x);
  103. ostream&  operator << (ostream& s, const Complex& x);
  104.  
  105. // other functions defined as inlines
  106.  
  107. int  operator == (const Complex& x, const Complex& y);
  108. int  operator == (const Complex& x, double y);
  109. int  operator != (const Complex& x, const Complex& y);
  110. int  operator != (const Complex& x, double y);
  111.  
  112. Complex  operator - (const Complex& x);
  113. Complex  conj(const Complex& x);
  114. Complex  operator + (const Complex& x, const Complex& y);
  115. Complex  operator + (const Complex& x, double y);
  116. Complex  operator + (double x, const Complex& y);
  117. Complex  operator - (const Complex& x, const Complex& y);
  118. Complex  operator - (const Complex& x, double y);
  119. Complex  operator - (double x, const Complex& y);
  120. Complex  operator * (const Complex& x, const Complex& y);
  121. Complex  operator * (const Complex& x, double y);
  122. Complex  operator * (double x, const Complex& y);
  123.  
  124. double  real(const Complex& x);
  125. double  imag(const Complex& x);
  126. double  abs(const Complex& x);
  127. double  norm(const Complex& x);
  128. double  arg(const Complex& x);
  129.  
  130. Complex  polar(double r, double t = 0.0);
  131.  
  132. #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
  133.  
  134. // inline members
  135.  
  136. inline double  Complex::real() const { return re; }
  137. inline double  Complex::imag() const { return im; }
  138.  
  139. inline Complex::Complex() {}
  140. inline Complex::Complex(const Complex& y) :re(y.real()), im(y.imag()) {}
  141. inline Complex::Complex(double r, double i) :re(r), im(i) {}
  142.  
  143. inline Complex::~Complex() {}
  144.  
  145. inline Complex&  Complex::operator =  (const Complex& y) 
  146.   re = y.real(); im = y.imag(); return *this; 
  147.  
  148. inline Complex&  Complex::operator += (const Complex& y)
  149.   re += y.real();  im += y.imag(); return *this; 
  150. }
  151.  
  152. inline Complex&  Complex::operator += (double y)
  153.   re += y; return *this; 
  154. }
  155.  
  156. inline Complex&  Complex::operator -= (const Complex& y)
  157.   re -= y.real();  im -= y.imag(); return *this; 
  158. }
  159.  
  160. inline Complex&  Complex::operator -= (double y)
  161.   re -= y; return *this; 
  162. }
  163.  
  164. inline Complex&  Complex::operator *= (const Complex& y)
  165. {  
  166.   double r = re * y.real() - im * y.imag();
  167.   im = re * y.imag() + im * y.real(); 
  168.   re = r; 
  169.   return *this; 
  170. }
  171.  
  172. inline Complex&  Complex::operator *= (double y)
  173. {  
  174.   re *=  y; im *=  y; return *this; 
  175. }
  176.  
  177.  
  178. //  functions
  179.  
  180. inline int  operator == (const Complex& x, const Complex& y)
  181. {
  182.   return x.real() == y.real() && x.imag() == y.imag();
  183. }
  184.  
  185. inline int  operator == (const Complex& x, double y)
  186. {
  187.   return x.imag() == 0.0 && x.real() == y;
  188. }
  189.  
  190. inline int  operator != (const Complex& x, const Complex& y)
  191. {
  192.   return x.real() != y.real() || x.imag() != y.imag();
  193. }
  194.  
  195. inline int  operator != (const Complex& x, double y)
  196. {
  197.   return x.imag() != 0.0 || x.real() != y;
  198. }
  199.  
  200. inline Complex  operator - (const Complex& x)
  201. {
  202.   return Complex(-x.real(), -x.imag());
  203. }
  204.  
  205. inline Complex  conj(const Complex& x)
  206. {
  207.   return Complex(x.real(), -x.imag());
  208. }
  209.  
  210. inline Complex  operator + (const Complex& x, const Complex& y)
  211. {
  212.   return Complex(x.real() + y.real(), x.imag() + y.imag());
  213. }
  214.  
  215. inline Complex  operator + (const Complex& x, double y)
  216. {
  217.   return Complex(x.real() + y, x.imag());
  218. }
  219.  
  220. inline Complex  operator + (double x, const Complex& y)
  221. {
  222.   return Complex(x + y.real(), y.imag());
  223. }
  224.  
  225. inline Complex  operator - (const Complex& x, const Complex& y)
  226. {
  227.   return Complex(x.real() - y.real(), x.imag() - y.imag());
  228. }
  229.  
  230. inline Complex  operator - (const Complex& x, double y)
  231. {
  232.   return Complex(x.real() - y, x.imag());
  233. }
  234.  
  235. inline Complex  operator - (double x, const Complex& y)
  236. {
  237.   return Complex(x - y.real(), -y.imag());
  238. }
  239.  
  240. inline Complex  operator * (const Complex& x, const Complex& y)
  241. {
  242.   return Complex(x.real() * y.real() - x.imag() * y.imag(), 
  243.                  x.real() * y.imag() + x.imag() * y.real());
  244. }
  245.  
  246. inline Complex  operator * (const Complex& x, double y)
  247. {
  248.   return Complex(x.real() * y, x.imag() * y);
  249. }
  250.  
  251. inline Complex  operator * (double x, const Complex& y)
  252. {
  253.   return Complex(x * y.real(), x * y.imag());
  254. }
  255.  
  256. inline double  real(const Complex& x)
  257. {
  258.   return x.real();
  259. }
  260.  
  261. inline double  imag(const Complex& x)
  262. {
  263.   return x.imag();
  264. }
  265.  
  266. inline double  abs(const Complex& x)
  267. {
  268.   return hypot(x.real(), x.imag());
  269. }
  270.  
  271. inline double  norm(const Complex& x)
  272. {
  273.   return (x.real() * x.real() + x.imag() * x.imag());
  274. }
  275.  
  276. inline double  arg(const Complex& x)
  277. {
  278.   return atan2(x.imag(), x.real());
  279. }
  280.  
  281. inline Complex  polar(double r, double t)
  282. {
  283.   return Complex(r * cos(t), r * sin(t));
  284. }
  285.  
  286. #endif __OPTIMIZE__
  287. #endif
  288.