home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-bin.lha / lib / g++-include / std / complext.h < prev    next >
C/C++ Source or Header  |  1996-10-12  |  11KB  |  318 lines

  1. // The template and inlines for the -*- C++ -*- complex number classes.
  2. // Copyright (C) 1994 Free Software Foundation
  3.  
  4. // This file is part of the GNU ANSI C++ Library.  This library is free
  5. // software; you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software
  7. // Foundation; either version 2, or (at your option) any later version.
  8.  
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. // GNU General Public License for more details.
  13.  
  14. // You should have received a copy of the GNU General Public License
  15. // along with this library; see the file COPYING.  If not, write to the Free
  16. // Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  
  18. // As a special exception, if you link this library with files compiled
  19. // with a GNU compiler to produce an executable, this does not cause the
  20. // resulting executable to be covered by the GNU General Public License.
  21. // This exception does not however invalidate any other reasons why the
  22. // executable file might be covered by the GNU General Public License.
  23.  
  24. // Written by Jason Merrill based upon the specification in the 27 May 1994
  25. // C++ working paper, ANSI document X3J16/94-0098.
  26.  
  27. #ifndef __COMPLEXT__
  28. #define __COMPLEXT__
  29.  
  30. #ifdef __GNUG__
  31. #pragma interface
  32. #endif
  33.  
  34. #include <std/cmath.h>
  35.  
  36. #if ! defined (__GNUG__) && ! defined (__attribute__)
  37. #define __attribute__ (foo) /* Ignore.  */
  38. #endif
  39.  
  40. extern "C++" {
  41. template <class FLOAT>
  42. class complex
  43. {
  44. public:
  45.   complex (FLOAT r = 0, FLOAT i = 0): re (r), im (i) { }
  46.   complex& operator += (const complex&);
  47.   complex& operator -= (const complex&);
  48.   complex& operator *= (const complex&);
  49.   complex& operator /= (const complex&);
  50.   FLOAT real () const { return re; }
  51.   FLOAT imag () const { return im; }
  52. private:
  53.   FLOAT re, im;
  54.  
  55.   // These functions are specified as friends for purposes of name injection;
  56.   // they do not actually reference private members.
  57.   friend FLOAT real (const complex&) __attribute__ ((const));
  58.   friend FLOAT imag (const complex&) __attribute__ ((const));
  59.   friend complex operator + (const complex&, const complex&) __attribute__ ((const));
  60.   friend complex operator + (const complex&, FLOAT) __attribute__ ((const));
  61.   friend complex operator + (FLOAT, const complex&) __attribute__ ((const));
  62.   friend complex operator - (const complex&, const complex&) __attribute__ ((const));
  63.   friend complex operator - (const complex&, FLOAT) __attribute__ ((const));
  64.   friend complex operator - (FLOAT, const complex&) __attribute__ ((const));
  65.   friend complex operator * (const complex&, const complex&) __attribute__ ((const));
  66.   friend complex operator * (const complex&, FLOAT) __attribute__ ((const));
  67.   friend complex operator * (FLOAT, const complex&) __attribute__ ((const));
  68.   friend complex operator / (const complex&, const complex&) __attribute__ ((const));
  69.   friend complex operator / (const complex&, FLOAT) __attribute__ ((const));
  70.   friend complex operator / (FLOAT, const complex&) __attribute__ ((const));
  71.   friend bool operator == (const complex&, const complex&) __attribute__ ((const));
  72.   friend bool operator == (const complex&, FLOAT) __attribute__ ((const));
  73.   friend bool operator == (FLOAT, const complex&) __attribute__ ((const));
  74.   friend bool operator != (const complex&, const complex&) __attribute__ ((const));
  75.   friend bool operator != (const complex&, FLOAT) __attribute__ ((const));
  76.   friend bool operator != (FLOAT, const complex&) __attribute__ ((const));
  77.   friend complex polar (FLOAT, FLOAT) __attribute__ ((const));
  78.   friend complex pow (const complex&, const complex&) __attribute__ ((const));
  79.   friend complex pow (const complex&, FLOAT) __attribute__ ((const));
  80.   friend complex pow (const complex&, int) __attribute__ ((const));
  81.   friend complex pow (FLOAT, const complex&) __attribute__ ((const));
  82.   friend istream& operator>> (istream&, complex&);
  83.   friend ostream& operator<< (ostream&, const complex&);
  84. };
  85.  
  86. // Declare specializations.
  87. class complex<float>;
  88. class complex<double>;
  89. class complex<long double>;
  90.  
  91. template <class FLOAT>
  92. inline complex<FLOAT>&
  93. complex<FLOAT>::operator += (const complex<FLOAT>& r)
  94. {
  95.   re += r.re;
  96.   im += r.im;
  97.   return *this;
  98. }
  99.  
  100. template <class FLOAT>
  101. inline complex<FLOAT>&
  102. complex<FLOAT>::operator -= (const complex<FLOAT>& r)
  103. {
  104.   re -= r.re;
  105.   im -= r.im;
  106.   return *this;
  107. }
  108.  
  109. template <class FLOAT>
  110. inline complex<FLOAT>&
  111. complex<FLOAT>::operator *= (const complex<FLOAT>& r)
  112. {
  113.   FLOAT f = re * r.re - im * r.im;
  114.   im = re * r.im + im * r.re;
  115.   re = f;
  116.   return *this;
  117. }
  118.  
  119. template <class FLOAT> inline FLOAT
  120. imag (const complex<FLOAT>& x) __attribute__ ((const))
  121. {
  122.   return x.imag ();
  123. }
  124.  
  125. template <class FLOAT> inline FLOAT
  126. real (const complex<FLOAT>& x) __attribute__ ((const))
  127. {
  128.   return x.real ();
  129. }
  130.  
  131. template <class FLOAT> inline complex<FLOAT>
  132. operator + (const complex<FLOAT>& x, const complex<FLOAT>& y) __attribute__ ((const))
  133. {
  134.   return complex<FLOAT> (real (x) + real (y), imag (x) + imag (y));
  135. }
  136.  
  137. template <class FLOAT> inline complex<FLOAT>
  138. operator + (const complex<FLOAT>& x, FLOAT y) __attribute__ ((const))
  139. {
  140.   return complex<FLOAT> (real (x) + y, imag (x));
  141. }
  142.  
  143. template <class FLOAT> inline complex<FLOAT>
  144. operator + (FLOAT x, const complex<FLOAT>& y) __attribute__ ((const))
  145. {
  146.   return complex<FLOAT> (x + real (y), imag (y));
  147. }
  148.  
  149. template <class FLOAT> inline complex<FLOAT>
  150. operator - (const complex<FLOAT>& x, const complex<FLOAT>& y) __attribute__ ((const))
  151. {
  152.   return complex<FLOAT> (real (x) - real (y), imag (x) - imag (y));
  153. }
  154.  
  155. template <class FLOAT> inline complex<FLOAT>
  156. operator - (const complex<FLOAT>& x, FLOAT y) __attribute__ ((const))
  157. {
  158.   return complex<FLOAT> (real (x) - y, imag (x));
  159. }
  160.  
  161. template <class FLOAT> inline complex<FLOAT>
  162. operator - (FLOAT x, const complex<FLOAT>& y) __attribute__ ((const))
  163. {
  164.   return complex<FLOAT> (x - real (y), - imag (y));
  165. }
  166.  
  167. template <class FLOAT> inline complex<FLOAT>
  168. operator * (const complex<FLOAT>& x, const complex<FLOAT>& y) __attribute__ ((const))
  169. {
  170.   return complex<FLOAT> (real (x) * real (y) - imag (x) * imag (y),
  171.                real (x) * imag (y) + imag (x) * real (y));
  172. }
  173.  
  174. template <class FLOAT> inline complex<FLOAT>
  175. operator * (const complex<FLOAT>& x, FLOAT y) __attribute__ ((const))
  176. {
  177.   return complex<FLOAT> (real (x) * y, imag (x) * y);
  178. }
  179.  
  180. template <class FLOAT> inline complex<FLOAT>
  181. operator * (FLOAT x, const complex<FLOAT>& y) __attribute__ ((const))
  182. {
  183.   return complex<FLOAT> (x * real (y), x * imag (y));
  184. }
  185.  
  186. template <class FLOAT> complex<FLOAT>
  187. operator / (const complex<FLOAT>& x, FLOAT y) __attribute__ ((const))
  188. {
  189.   return complex<FLOAT> (real (x) / y, imag (x) / y);
  190. }
  191.  
  192. template <class FLOAT> inline complex<FLOAT>
  193. operator + (const complex<FLOAT>& x) __attribute__ ((const))
  194. {
  195.   return x;
  196. }
  197.  
  198. template <class FLOAT> inline complex<FLOAT>
  199. operator - (const complex<FLOAT>& x) __attribute__ ((const))
  200. {
  201.   return complex<FLOAT> (-real (x), -imag (x));
  202. }
  203.  
  204. template <class FLOAT> inline bool
  205. operator == (const complex<FLOAT>& x, const complex<FLOAT>& y) __attribute__ ((const))
  206. {
  207.   return real (x) == real (y) && imag (x) == imag (y);
  208. }
  209.  
  210. template <class FLOAT> inline bool
  211. operator == (const complex<FLOAT>& x, FLOAT y) __attribute__ ((const))
  212. {
  213.   return real (x) == y && imag (x) == 0;
  214. }
  215.  
  216. template <class FLOAT> inline bool
  217. operator == (FLOAT x, const complex<FLOAT>& y) __attribute__ ((const))
  218. {
  219.   return x == real (y) && imag (y) == 0;
  220. }
  221.  
  222. template <class FLOAT> inline bool
  223. operator != (const complex<FLOAT>& x, const complex<FLOAT>& y) __attribute__ ((const))
  224. {
  225.   return real (x) != real (y) || imag (x) != imag (y);
  226. }
  227.  
  228. template <class FLOAT> inline bool
  229. operator != (const complex<FLOAT>& x, FLOAT y) __attribute__ ((const))
  230. {
  231.   return real (x) != y || imag (x) != 0;
  232. }
  233.  
  234. template <class FLOAT> inline bool
  235. operator != (FLOAT x, const complex<FLOAT>& y) __attribute__ ((const))
  236. {
  237.   return x != real (y) || imag (y) != 0;
  238. }
  239.  
  240. // Some targets don't provide a prototype for hypot when -ansi.
  241. extern "C" double hypot (double, double) __attribute__ ((const));
  242.  
  243. template <class FLOAT> inline FLOAT
  244. abs (const complex<FLOAT>& x) __attribute__ ((const))
  245. {
  246.   return hypot (real (x), imag (x));
  247. }
  248.  
  249. template <class FLOAT> inline FLOAT
  250. arg (const complex<FLOAT>& x) __attribute__ ((const))
  251. {
  252.   return atan2 (imag (x), real (x));
  253. }
  254.  
  255. template <class FLOAT> inline complex<FLOAT>
  256. polar (FLOAT r, FLOAT t) __attribute__ ((const))
  257. {
  258.   return complex<FLOAT> (r * cos (t), r * sin (t));
  259. }
  260.  
  261. template <class FLOAT> inline complex<FLOAT>
  262. conj (const complex<FLOAT>& x)  __attribute__ ((const))
  263. {
  264.   return complex<FLOAT> (real (x), -imag (x));
  265. }
  266.  
  267. template <class FLOAT> inline FLOAT
  268. norm (const complex<FLOAT>& x) __attribute__ ((const))
  269. {
  270.   return real (x) * real (x) + imag (x) * imag (x);
  271. }
  272.  
  273. // Declarations of templates in complext.ccI
  274.  
  275. template <class FLOAT> complex<FLOAT>
  276.   operator / (const complex<FLOAT>&, const complex<FLOAT>&) __attribute__ ((const));
  277. template <class FLOAT> complex<FLOAT>
  278.   operator / (FLOAT, const complex<FLOAT>&) __attribute__ ((const));
  279. template <class FLOAT> complex<FLOAT>
  280.   cos (const complex<FLOAT>&) __attribute__ ((const));
  281. template <class FLOAT> complex<FLOAT>
  282.   cosh (const complex<FLOAT>&) __attribute__ ((const));
  283. template <class FLOAT> complex<FLOAT>
  284.   exp (const complex<FLOAT>&) __attribute__ ((const));
  285. template <class FLOAT> complex<FLOAT>
  286.   log (const complex<FLOAT>&) __attribute__ ((const));
  287. template <class FLOAT> complex<FLOAT>
  288.   pow (const complex<FLOAT>&, const complex<FLOAT>&) __attribute__ ((const));
  289. template <class FLOAT> complex<FLOAT>
  290.   pow (const complex<FLOAT>&, FLOAT) __attribute__ ((const));
  291. template <class FLOAT> complex<FLOAT>
  292.   pow (const complex<FLOAT>&, int) __attribute__ ((const));
  293. template <class FLOAT> complex<FLOAT>
  294.   pow (FLOAT, const complex<FLOAT>&) __attribute__ ((const));
  295. template <class FLOAT> complex<FLOAT>
  296.   sin (const complex<FLOAT>&) __attribute__ ((const));
  297. template <class FLOAT> complex<FLOAT>
  298.   sinh (const complex<FLOAT>&) __attribute__ ((const));
  299. template <class FLOAT> complex<FLOAT>
  300.   sqrt (const complex<FLOAT>&) __attribute__ ((const));
  301.  
  302. class istream;
  303. class ostream;
  304. template <class FLOAT> istream& operator >> (istream&, complex<FLOAT>&);
  305. template <class FLOAT> ostream& operator << (ostream&, const complex<FLOAT>&);
  306. } // extern "C++"
  307.  
  308. // Specializations and such
  309.  
  310. #include <std/fcomplex.h>
  311. #include <std/dcomplex.h>
  312. #include <std/ldcomplex.h>
  313.  
  314. // Declare the instantiations.
  315. #include <std/cinst.h>
  316.  
  317. #endif
  318.