home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__inc / xcomplex.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-23  |  6.9 KB  |  289 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 the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. #ifndef _Complex_h
  20. #ifdef __GNUG__
  21. #pragma once
  22. #pragma interface
  23. #endif
  24. #define _Complex_h 1
  25.  
  26.  
  27. #include <stream.h>
  28. #include <math.h>
  29.  
  30. class Complex
  31. {
  32. #ifdef __ATT_complex__
  33. public:
  34. #else
  35. protected:
  36. #endif
  37.  
  38.   double           re;
  39.   double           im;
  40.  
  41. public:
  42.  
  43.   double           real() const;
  44.   double           imag() const;
  45.  
  46.                    Complex();
  47.                    Complex(const Complex& y);
  48.                    Complex(double r, double i=0);
  49.  
  50.                   ~Complex();
  51.  
  52.   Complex&         operator =  (const Complex& y);
  53.  
  54.   Complex&         operator += (const Complex& y);
  55.   Complex&         operator += (double y);
  56.   Complex&         operator -= (const Complex& y);
  57.   Complex&         operator -= (double y);
  58.   Complex&         operator *= (const Complex& y);
  59.   Complex&         operator *= (double y);
  60.  
  61.   Complex&         operator /= (const Complex& y); 
  62.   Complex&         operator /= (double y); 
  63.  
  64.   void             error(const char* msg) const;
  65. };
  66.  
  67.  
  68. // error handlers
  69.  
  70. extern  void default_Complex_error_handler(const char*);
  71. extern  one_arg_error_handler_t Complex_error_handler;
  72.  
  73. extern  one_arg_error_handler_t 
  74.         set_Complex_error_handler(one_arg_error_handler_t f);
  75.  
  76.  
  77. // non-inline functions
  78.  
  79. Complex   operator /  (const Complex& x, const Complex& y);
  80. Complex   operator /  (const Complex& x, double y);
  81. Complex   operator /  (double   x, const Complex& y);
  82.  
  83. Complex   cos(const Complex& x);
  84. Complex   sin(const Complex& x);
  85.  
  86. Complex   cosh(const Complex& x);
  87. Complex   sinh(const Complex& x);
  88.  
  89. Complex   exp(const Complex& x);
  90. Complex   log(const Complex& x);
  91.  
  92. Complex   pow(const Complex& x, long p);
  93. Complex   pow(const Complex& x, const Complex& p);
  94. Complex   pow(const Complex& x, double y);
  95. Complex   sqrt(const Complex& x);
  96.    
  97. istream&  operator >> (istream& s, Complex& x);
  98. ostream&  operator << (ostream& s, const Complex& x);
  99.  
  100. // other functions defined as inlines
  101.  
  102. int  operator == (const Complex& x, const Complex& y);
  103. int  operator == (const Complex& x, double y);
  104. int  operator != (const Complex& x, const Complex& y);
  105. int  operator != (const Complex& x, double y);
  106.  
  107. Complex  operator - (const Complex& x);
  108. Complex  conj(const Complex& x);
  109. Complex  operator + (const Complex& x, const Complex& y);
  110. Complex  operator + (const Complex& x, double y);
  111. Complex  operator + (double x, const Complex& y);
  112. Complex  operator - (const Complex& x, const Complex& y);
  113. Complex  operator - (const Complex& x, double y);
  114. Complex  operator - (double x, const Complex& y);
  115. Complex  operator * (const Complex& x, const Complex& y);
  116. Complex  operator * (const Complex& x, double y);
  117. Complex  operator * (double x, const Complex& y);
  118.  
  119. double  real(const Complex& x);
  120. double  imag(const Complex& x);
  121. double  abs(const Complex& x);
  122. double  norm(const Complex& x);
  123. double  arg(const Complex& x);
  124.  
  125. Complex  polar(double r, double t = 0.0);
  126.  
  127. #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
  128.  
  129. // inline members
  130.  
  131. inline double  Complex::real() const { return re; }
  132. inline double  Complex::imag() const { return im; }
  133.  
  134. inline Complex::Complex() {}
  135. inline Complex::Complex(const Complex& y) :re(y.real()), im(y.imag()) {}
  136. inline Complex::Complex(double r, double i) :re(r), im(i) {}
  137.  
  138. inline Complex::~Complex() {}
  139.  
  140. inline Complex&  Complex::operator =  (const Complex& y) 
  141.   re = y.real(); im = y.imag(); return *this; 
  142.  
  143. inline Complex&  Complex::operator += (const Complex& y)
  144.   re += y.real();  im += y.imag(); return *this; 
  145. }
  146.  
  147. inline Complex&  Complex::operator += (double y)
  148.   re += y; return *this; 
  149. }
  150.  
  151. inline Complex&  Complex::operator -= (const Complex& y)
  152.   re -= y.real();  im -= y.imag(); return *this; 
  153. }
  154.  
  155. inline Complex&  Complex::operator -= (double y)
  156.   re -= y; return *this; 
  157. }
  158.  
  159. inline Complex&  Complex::operator *= (const Complex& y)
  160. {  
  161.   double r = re * y.real() - im * y.imag();
  162.   im = re * y.imag() + im * y.real(); 
  163.   re = r; 
  164.   return *this; 
  165. }
  166.  
  167. inline Complex&  Complex::operator *= (double y)
  168. {  
  169.   re *=  y; im *=  y; return *this; 
  170. }
  171.  
  172.  
  173. //  functions
  174.  
  175. inline int  operator == (const Complex& x, const Complex& y)
  176. {
  177.   return x.real() == y.real() && x.imag() == y.imag();
  178. }
  179.  
  180. inline int  operator == (const Complex& x, double y)
  181. {
  182.   return x.imag() == 0.0 && x.real() == y;
  183. }
  184.  
  185. inline int  operator != (const Complex& x, const Complex& y)
  186. {
  187.   return x.real() != y.real() || x.imag() != y.imag();
  188. }
  189.  
  190. inline int  operator != (const Complex& x, double y)
  191. {
  192.   return x.imag() != 0.0 || x.real() != y;
  193. }
  194.  
  195. inline Complex  operator - (const Complex& x)
  196. {
  197.   return Complex(-x.real(), -x.imag());
  198. }
  199.  
  200. inline Complex  conj(const Complex& x)
  201. {
  202.   return Complex(x.real(), -x.imag());
  203. }
  204.  
  205. inline Complex  operator + (const Complex& x, const Complex& y)
  206. {
  207.   return Complex(x.real() + y.real(), x.imag() + y.imag());
  208. }
  209.  
  210. inline Complex  operator + (const Complex& x, double y)
  211. {
  212.   return Complex(x.real() + y, x.imag());
  213. }
  214.  
  215. inline Complex  operator + (double x, const Complex& y)
  216. {
  217.   return Complex(x + y.real(), y.imag());
  218. }
  219.  
  220. inline Complex  operator - (const Complex& x, const Complex& y)
  221. {
  222.   return Complex(x.real() - y.real(), x.imag() - y.imag());
  223. }
  224.  
  225. inline Complex  operator - (const Complex& x, double y)
  226. {
  227.   return Complex(x.real() - y, x.imag());
  228. }
  229.  
  230. inline Complex  operator - (double x, const Complex& y)
  231. {
  232.   return Complex(x - y.real(), -y.imag());
  233. }
  234.  
  235. inline Complex  operator * (const Complex& x, const Complex& y)
  236. {
  237.   return Complex(x.real() * y.real() - x.imag() * y.imag(), 
  238.                  x.real() * y.imag() + x.imag() * y.real());
  239. }
  240.  
  241. inline Complex  operator * (const Complex& x, double y)
  242. {
  243.   return Complex(x.real() * y, x.imag() * y);
  244. }
  245.  
  246. inline Complex  operator * (double x, const Complex& y)
  247. {
  248.   return Complex(x * y.real(), x * y.imag());
  249. }
  250.  
  251. inline double  real(const Complex& x)
  252. {
  253.   return x.real();
  254. }
  255.  
  256. inline double  imag(const Complex& x)
  257. {
  258.   return x.imag();
  259. }
  260.  
  261. inline double  abs(const Complex& x)
  262. {
  263.   return hypot(x.real(), x.imag());
  264. }
  265.  
  266. inline double  norm(const Complex& x)
  267. {
  268.   return (x.real() * x.real() + x.imag() * x.imag());
  269. }
  270.  
  271. inline double  arg(const Complex& x)
  272. {
  273.   return atan2(x.imag(), x.real());
  274. }
  275.  
  276. inline Complex  polar(double r, double t)
  277. {
  278.   return Complex(r * cos(t), r * sin(t));
  279. }
  280.  
  281. #endif __OPTIMIZE__
  282. #endif
  283.