home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / lib / g++-include / complext.cci < prev    next >
Text File  |  1994-12-22  |  6KB  |  240 lines

  1. // Member templates 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
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 2, or (at your option)
  8. // any later version.
  9.  
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14.  
  15. // You should have received a copy of the GNU General Public License
  16. // along with GNU CC; see the file COPYING.  If not, write to
  17. // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. // As a special exception, if you link this library with files
  20. // compiled with a GNU compiler to produce an executable, this does not cause
  21. // the resulting executable to be covered by the GNU General Public License.
  22. // This exception does not however invalidate any other reasons why
  23. // the executable file might be covered by the GNU General Public License.
  24.  
  25. // Written by Jason Merrill based upon the specification in the 27 May 1994
  26. // C++ working paper, ANSI document X3J16/94-0098.
  27.  
  28. #include <complex>
  29.  
  30. template <class FLOAT> __complex<FLOAT>
  31. operator / (FLOAT x, __complex<FLOAT> y)
  32. {
  33.   FLOAT d = norm (y);
  34.   return __complex<FLOAT> ((x * real (y)) / d, (- x * imag (y)) / d);
  35. }
  36.  
  37. template <class FLOAT> __complex<FLOAT>
  38. cos (__complex<FLOAT> x)
  39. {
  40.   return __complex<FLOAT> (cos (real (x)) * cosh (imag (x)),
  41.                - sin (real (x)) * sinh (imag (x)));
  42. }
  43.  
  44. template <class FLOAT> __complex<FLOAT>
  45. cosh (__complex<FLOAT> x)
  46. {
  47.   return __complex<FLOAT> (cosh (real (x)) * cos (imag (x)),
  48.                sinh (real (x)) * sin (imag (x)));
  49. }
  50.  
  51. template <class FLOAT> __complex<FLOAT>
  52. exp (__complex<FLOAT> x)
  53. {
  54.   return polar (FLOAT (exp (real (x))), imag (x));
  55. }
  56.  
  57. template <class FLOAT> __complex<FLOAT>
  58. log (__complex<FLOAT> x)
  59. {
  60.   return __complex<FLOAT> (log (abs (x)), arg (x));
  61. }
  62.  
  63. template <class FLOAT> __complex<FLOAT>
  64. pow (__complex<FLOAT> x, __complex<FLOAT> y)
  65. {
  66.   FLOAT logr = log (abs (x));
  67.   FLOAT t = arg (x);
  68.  
  69.   return polar (FLOAT (exp (logr * real (y) - imag (y) * t)),
  70.         FLOAT (imag (y) * logr + real (y) * t));
  71. }
  72.  
  73. template <class FLOAT> __complex<FLOAT>
  74. pow (__complex<FLOAT> x, FLOAT y)
  75. {
  76.   return exp (FLOAT (y) * log (x));
  77. }
  78.  
  79. template <class FLOAT> __complex<FLOAT>
  80. pow (FLOAT x, __complex<FLOAT> y)
  81. {
  82.   return exp (y * FLOAT (log (x)));
  83. }
  84.  
  85. template <class FLOAT> __complex<FLOAT>
  86. sin (__complex<FLOAT> x)
  87. {
  88.   return __complex<FLOAT> (sin (real (x)) * cosh (imag (x)),
  89.                cos (real (x)) * sinh (imag (x)));
  90. }
  91.  
  92. template <class FLOAT> __complex<FLOAT>
  93. sinh (__complex<FLOAT> x)
  94. {
  95.   return __complex<FLOAT> (sinh (real (x)) * cos (imag (x)),
  96.                cosh (real (x)) * sin (imag (x)));
  97. }
  98.  
  99. #include <iostream.h>
  100.  
  101. template <class FLOAT> istream&
  102. operator >> (istream& is, __complex<FLOAT>& x)
  103. {
  104.   char ch;
  105.   FLOAT re, im;
  106.  
  107.   if (is >> ch && ch == '('
  108.       && is >> re >> ch && ch == ','
  109.       && is >> im >> ch && ch == ')')
  110.     x = __complex<FLOAT> (re, im);
  111.  
  112.   return is;
  113. }
  114.  
  115. template <class FLOAT> ostream&
  116. operator << (ostream& os, __complex<FLOAT> x)
  117. {
  118.   return os << '(' << real (x) << ',' << imag (x) << ')';
  119. }
  120.  
  121. // The code below is adapted from f2c's libF77, and is subject to this
  122. // copyright:
  123.  
  124. /****************************************************************
  125. Copyright 1990, 1991, 1992, 1993 by AT&T Bell Laboratories and Bellcore.
  126.  
  127. Permission to use, copy, modify, and distribute this software
  128. and its documentation for any purpose and without fee is hereby
  129. granted, provided that the above copyright notice appear in all
  130. copies and that both that the copyright notice and this
  131. permission notice and warranty disclaimer appear in supporting
  132. documentation, and that the names of AT&T Bell Laboratories or
  133. Bellcore or any of their entities not be used in advertising or
  134. publicity pertaining to distribution of the software without
  135. specific, written prior permission.
  136.  
  137. AT&T and Bellcore disclaim all warranties with regard to this
  138. software, including all implied warranties of merchantability
  139. and fitness.  In no event shall AT&T or Bellcore be liable for
  140. any special, indirect or consequential damages or any damages
  141. whatsoever resulting from loss of use, data or profits, whether
  142. in an action of contract, negligence or other tortious action,
  143. arising out of or in connection with the use or performance of
  144. this software.
  145. ****************************************************************/
  146.  
  147. template <class FLOAT> __complex<FLOAT>& __complex<FLOAT>::
  148. operator /= (__complex y)
  149. {
  150.   FLOAT ar = fabs (y.re);
  151.   FLOAT ai = fabs (y.im);
  152.   FLOAT nr, ni;
  153.   FLOAT t, d;
  154.   if (ar <= ai)
  155.     {
  156.       t = y.re / y.im;
  157.       d = y.im * (1 + t*t);
  158.       nr = (re * t + im) / d;
  159.       ni = (im * t - re) / d;
  160.     }
  161.   else
  162.     {
  163.       t = y.im / y.re;
  164.       d = y.re * (1 + t*t);
  165.       nr = (re + im * t) / d;
  166.       ni = (im - re * t) / d;
  167.     }
  168.   re = nr;
  169.   im = ni;
  170.   return *this;
  171. }
  172.  
  173. template <class FLOAT> __complex<FLOAT>
  174. operator / (__complex<FLOAT> x, __complex<FLOAT> y)
  175. {
  176.   FLOAT ar = fabs (real (y));
  177.   FLOAT ai = fabs (imag (y));
  178.   FLOAT nr, ni;
  179.   FLOAT t, d;
  180.   if (ar <= ai)
  181.     {
  182.       t = real (y) / imag (y);
  183.       d = imag (y) * (1 + t*t);
  184.       nr = (real (x) * t + imag (x)) / d;
  185.       ni = (imag (x) * t - real (x)) / d;
  186.     }
  187.   else
  188.     {
  189.       t = imag (y) / real (y);
  190.       d = real (y) * (1 + t*t);
  191.       nr = (real (x) + imag (x) * t) / d;
  192.       ni = (imag (x) - real (x) * t) / d;
  193.     }
  194.   return __complex<FLOAT> (nr, ni);
  195. }
  196.  
  197. template <class FLOAT> __complex<FLOAT>
  198. pow (__complex<FLOAT> x, int y)
  199. {
  200.   if (y == 0)
  201.     return __complex<FLOAT> (1.0);
  202.   __complex<FLOAT> r (1.0);
  203.   if (y < 0)
  204.     {
  205.       y = -y;
  206.       x = 1/x;
  207.     }
  208.   for (;;)
  209.     {
  210.       if (y & 1)
  211.     r *= x;
  212.       if (y >>= 1)
  213.     x *= x;
  214.       else
  215.     return r;
  216.     }
  217. }
  218.  
  219. template <class FLOAT> __complex<FLOAT>
  220. sqrt (__complex<FLOAT> x)
  221. {
  222.   FLOAT r = abs (x);
  223.   FLOAT nr, ni;
  224.   if (r == 0.0)
  225.     nr = ni = r;
  226.   else if (real (x) > 0)
  227.     {
  228.       nr = sqrt (0.5 * (r + real (x)));
  229.       ni = imag (x) / nr / 2;
  230.     }
  231.   else
  232.     {
  233.       ni = sqrt (0.5 * (r - real (x)));
  234.       if (imag (x) < 0)
  235.     ni = - ni;
  236.       nr = imag (x) / ni / 2;
  237.     }
  238.   return __complex<FLOAT> (nr, ni); 
  239. }
  240.