home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__lib / complex.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-23  |  6.1 KB  |  271 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. #pragma once
  26. #define _Complex_h 1
  27.  
  28.  
  29. #include <stream.h>
  30. #include <math.h>
  31.  
  32. class Complex
  33. {
  34. protected:
  35.   double           re;
  36.   double           im;
  37.  
  38. public:
  39.                    Complex();
  40.                    Complex(Complex& c);
  41.                    Complex(double r, double i = 0.0);
  42.  
  43.                    ~Complex();
  44.  
  45.   Complex&         operator =  (Complex& y);
  46.  
  47.   friend int       operator == (Complex& x, Complex& y);
  48.   friend int       operator == (Complex& x, double y);
  49.  
  50.   friend int       operator != (Complex& x, Complex& y);
  51.   friend int       operator != (Complex& x, double y);
  52.  
  53.   Complex          operator -  ();
  54.  
  55.   friend Complex   operator +  (Complex& x, Complex& y);
  56.   friend Complex   operator +  (Complex& x, double y);
  57.   friend Complex   operator +  (double   x, Complex& y);
  58.   friend Complex   operator -  (Complex& x, Complex& y);
  59.   friend Complex   operator -  (Complex& x, double y);
  60.   friend Complex   operator -  (double   x, Complex& y);
  61.   friend Complex   operator *  (Complex& x, Complex& y);
  62.   friend Complex   operator *  (Complex& x, double y);
  63.   friend Complex   operator *  (double   x, Complex& y);
  64.   friend Complex   operator /  (Complex& x, Complex& y);
  65.   friend Complex   operator /  (Complex& x, double y);
  66.   friend Complex   operator /  (double   x, Complex& y);
  67.  
  68.   Complex&         operator += (Complex& y); 
  69.   Complex&         operator += (double y); 
  70.   Complex&         operator -= (Complex& y); 
  71.   Complex&         operator -= (double y); 
  72.   Complex&         operator *= (Complex& y); 
  73.   Complex&         operator *= (double y); 
  74.   Complex&         operator /= (Complex& y); 
  75.   Complex&         operator /= (double y); 
  76.  
  77.   friend double    real(Complex& x);
  78.   friend double    imag(Complex& x);
  79.   friend double    abs(Complex& x);
  80.   friend double    norm(Complex& x);
  81.   friend double    arg(Complex& x);
  82.  
  83.   friend Complex   polar(double r, double t = 0.0);
  84.   friend Complex   conj(Complex& x);
  85.  
  86.   friend Complex   cos(Complex& x);
  87.   friend Complex   sin(Complex& x);
  88.  
  89.   friend Complex   cosh(Complex& x);
  90.   friend Complex   sinh(Complex& x);
  91.  
  92.   friend Complex   exp(Complex& x);
  93.   friend Complex   log(Complex& x);
  94.  
  95.   friend Complex   pow(Complex& x, long p);
  96.   friend Complex   pow(Complex& x, Complex& p);
  97.   friend Complex   sqrt(Complex& x);
  98.    
  99.   friend istream&  operator >> (istream& s, Complex& x);
  100.   friend ostream&  operator << (ostream& s, Complex& x);
  101.  
  102.   void             error(char* msg);
  103. };
  104.  
  105.  
  106. // error handlers
  107.  
  108. extern  void default_Complex_error_handler(char*);
  109. extern  one_arg_error_handler_t Complex_error_handler;
  110.  
  111. extern  one_arg_error_handler_t 
  112.         set_Complex_error_handler(one_arg_error_handler_t f);
  113.  
  114.  
  115. //#ifdef __OPTIMIZE__
  116.  
  117. inline Complex:: Complex() {}
  118. inline Complex::~Complex() {}
  119.  
  120. inline Complex::Complex(double r, double i = 0.0)
  121. {
  122.   re = r; im = i;
  123. }
  124.  
  125. inline Complex::Complex(Complex& x)
  126. {
  127.   re = x.re; im = x.im;
  128. }
  129.  
  130. inline Complex& Complex::operator = (Complex& x)
  131. {
  132.   re = x.re; im = x.im; return *this;
  133. }
  134.  
  135. inline int operator == (Complex& x, Complex& y)
  136. {
  137.   return x.re == y.re && x.im == y.im;
  138. }
  139.  
  140. inline int operator == (Complex& x, double y)
  141. {
  142.   return x.im == 0.0 && x.re == y;
  143. }
  144.  
  145. inline int operator != (Complex& x, Complex& y)
  146. {
  147.   return x.re != y.re || x.im != y.im;
  148. }
  149.  
  150. inline int operator != (Complex& x, double y)
  151. {
  152.   return x.im != 0.0 || x.re != y;
  153. }
  154.  
  155. inline Complex Complex::operator - ()
  156. {
  157.   return Complex(-re, -im);
  158. }
  159.  
  160. inline Complex conj(Complex& x)
  161. {
  162.   return Complex(x.re, -x.im);
  163. }
  164.  
  165. inline Complex operator + (Complex& x, Complex& y)
  166. {
  167.   return Complex(x.re + y.re, x.im + y.im);
  168. }
  169.  
  170. inline Complex operator + (Complex& x, double y)
  171. {
  172.   return Complex(x.re + y, x.im);
  173. }
  174.  
  175. inline Complex operator + (double x, Complex& y)
  176. {
  177.   return Complex(x + y.re, y.im);
  178. }
  179.  
  180. inline Complex operator - (Complex& x, Complex& y)
  181. {
  182.   return Complex(x.re - y.re, x.im - y.im);
  183. }
  184.  
  185. inline Complex operator - (Complex& x, double y)
  186. {
  187.   return Complex(x.re - y, x.im);
  188. }
  189.  
  190. inline Complex operator - (double x, Complex& y)
  191. {
  192.   return Complex(x - y.re, -y.im);
  193. }
  194.  
  195. inline Complex operator * (Complex& x, Complex& y)
  196. {
  197.   return Complex(x.re * y.re - x.im * y.im, x.re * y.im + x.im * y.re);
  198. }
  199.  
  200. inline Complex operator * (Complex& x, double y)
  201. {
  202.   return Complex(x.re * y, x.im * y);
  203. }
  204.  
  205. inline Complex operator * (double x, Complex& y)
  206. {
  207.   return Complex(x * y.re, x * y.im);
  208. }
  209.  
  210. inline Complex& Complex::operator += (Complex& y)
  211. {
  212.   re += y.re;  im += y.im;  return *this;
  213. }
  214.  
  215. inline Complex& Complex::operator += (double y)
  216. {
  217.   re += y;  return *this;
  218. }
  219.  
  220. inline Complex& Complex::operator -= (Complex& y)
  221. {
  222.   re -= y.re; im -= y.im; return *this;
  223. }
  224.  
  225. inline Complex& Complex::operator -= (double y)
  226. {
  227.   re -= y; return *this;
  228. }
  229.  
  230. inline Complex& Complex::operator *= (Complex& y)
  231. {
  232.   double r = re * y.re - im * y.im;
  233.   im = re * y.im + im * y.re;
  234.   re = r;
  235.   return *this;
  236. }
  237.  
  238. inline Complex& Complex::operator *= (double y)
  239. {
  240.   re *=  y; im *=  y; return *this;
  241. }
  242.  
  243. inline double real(Complex& x)
  244. {
  245.   return x.re;
  246. }
  247.  
  248. inline double imag(Complex& x)
  249. {
  250.   return x.im;
  251. }
  252.  
  253. inline double abs(Complex& x)
  254. {
  255.   return hypot(x.re, x.im);
  256. }
  257.  
  258. inline double norm(Complex& x)
  259. {
  260.   return (x.re * x.re + x.im * x.im);
  261. }
  262.  
  263. inline double arg(Complex& x)
  264. {
  265.   return atan2(x.im, x.re);
  266. }
  267.  
  268. //#endif
  269.  
  270. #endif
  271.