1 // Member templates for the -*- C++ -*- complex number classes.
2 // Copyright (C) 1994 Free Software Foundation
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)
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.
15 // You should have received a copy of the GNU General Public License
16 // along with this library; see the file COPYING. If not, write to the Free
17 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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.
25 // Written by Jason Merrill based upon the specification in the 27 May 1994
26 // C++ working paper, ANSI document X3J16/94-0098.
31 template <class FLOAT> complex<FLOAT>
32 cos (const complex<FLOAT>& x)
34 return complex<FLOAT> (cos (real (x)) * cosh (imag (x)),
35 - sin (real (x)) * sinh (imag (x)));
38 template <class FLOAT> complex<FLOAT>
39 cosh (const complex<FLOAT>& x)
41 return complex<FLOAT> (cosh (real (x)) * cos (imag (x)),
42 sinh (real (x)) * sin (imag (x)));
45 template <class FLOAT> complex<FLOAT>
46 exp (const complex<FLOAT>& x)
48 return polar (FLOAT (exp (real (x))), imag (x));
51 template <class FLOAT> complex<FLOAT>
52 log (const complex<FLOAT>& x)
54 return complex<FLOAT> (log (abs (x)), arg (x));
57 template <class FLOAT> complex<FLOAT>
58 pow (const complex<FLOAT>& x, const complex<FLOAT>& y)
60 FLOAT logr = log (abs (x));
63 return polar (FLOAT (exp (logr * real (y) - imag (y) * t)),
64 FLOAT (imag (y) * logr + real (y) * t));
67 template <class FLOAT> complex<FLOAT>
68 pow (const complex<FLOAT>& x, FLOAT y)
70 return exp (FLOAT (y) * log (x));
73 template <class FLOAT> complex<FLOAT>
74 pow (FLOAT x, const complex<FLOAT>& y)
76 return exp (y * FLOAT (log (x)));
79 template <class FLOAT> complex<FLOAT>
80 sin (const complex<FLOAT>& x)
82 return complex<FLOAT> (sin (real (x)) * cosh (imag (x)),
83 cos (real (x)) * sinh (imag (x)));
86 template <class FLOAT> complex<FLOAT>
87 sinh (const complex<FLOAT>& x)
89 return complex<FLOAT> (sinh (real (x)) * cos (imag (x)),
90 cosh (real (x)) * sin (imag (x)));
95 template <class FLOAT> istream&
96 operator >> (istream& is, complex<FLOAT>& x)
103 if (is.peek () == '(')
115 if (ch != 0 && ch != ')')
116 is.setstate (ios::failbit);
118 x = complex<FLOAT> (re, im);
123 template <class FLOAT> ostream&
124 operator << (ostream& os, const complex<FLOAT>& x)
126 return os << '(' << real (x) << ',' << imag (x) << ')';
129 // The code below is adapted from f2c's libF77, and is subject to this
132 /****************************************************************
133 Copyright 1990, 1991, 1992, 1993 by AT&T Bell Laboratories and Bellcore.
135 Permission to use, copy, modify, and distribute this software
136 and its documentation for any purpose and without fee is hereby
137 granted, provided that the above copyright notice appear in all
138 copies and that both that the copyright notice and this
139 permission notice and warranty disclaimer appear in supporting
140 documentation, and that the names of AT&T Bell Laboratories or
141 Bellcore or any of their entities not be used in advertising or
142 publicity pertaining to distribution of the software without
143 specific, written prior permission.
145 AT&T and Bellcore disclaim all warranties with regard to this
146 software, including all implied warranties of merchantability
147 and fitness. In no event shall AT&T or Bellcore be liable for
148 any special, indirect or consequential damages or any damages
149 whatsoever resulting from loss of use, data or profits, whether
150 in an action of contract, negligence or other tortious action,
151 arising out of or in connection with the use or performance of
153 ****************************************************************/
155 template <class FLOAT> complex<FLOAT>&
156 __doadv (complex<FLOAT>* ths, const complex<FLOAT>& y)
158 FLOAT ar = abs (y.re);
159 FLOAT ai = abs (y.im);
165 d = y.im * (1 + t*t);
166 nr = (ths->re * t + ths->im) / d;
167 ni = (ths->im * t - ths->re) / d;
172 d = y.re * (1 + t*t);
173 nr = (ths->re + ths->im * t) / d;
174 ni = (ths->im - ths->re * t) / d;
181 template <class FLOAT> complex<FLOAT>
182 operator / (const complex<FLOAT>& x, const complex<FLOAT>& y)
184 FLOAT ar = abs (real (y));
185 FLOAT ai = abs (imag (y));
190 t = real (y) / imag (y);
191 d = imag (y) * (1 + t*t);
192 nr = (real (x) * t + imag (x)) / d;
193 ni = (imag (x) * t - real (x)) / d;
197 t = imag (y) / real (y);
198 d = real (y) * (1 + t*t);
199 nr = (real (x) + imag (x) * t) / d;
200 ni = (imag (x) - real (x) * t) / d;
202 return complex<FLOAT> (nr, ni);
205 template <class FLOAT> complex<FLOAT>
206 operator / (FLOAT x, const complex<FLOAT>& y)
208 FLOAT ar = abs (real (y));
209 FLOAT ai = abs (imag (y));
214 t = real (y) / imag (y);
215 d = imag (y) * (1 + t*t);
221 t = imag (y) / real (y);
222 d = real (y) * (1 + t*t);
226 return complex<FLOAT> (nr, ni);
229 template <class FLOAT> complex<FLOAT>
230 pow (const complex<FLOAT>& xin, int y)
233 return complex<FLOAT> (1.0);
234 complex<FLOAT> r (1.0);
235 complex<FLOAT> x (xin);
252 template <class FLOAT> complex<FLOAT>
253 sqrt (const complex<FLOAT>& x)
259 else if (real (x) > 0)
261 nr = sqrt (0.5 * (r + real (x)));
262 ni = imag (x) / nr / 2;
266 ni = sqrt (0.5 * (r - real (x)));
269 nr = imag (x) / ni / 2;
271 return complex<FLOAT> (nr, ni);