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