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 <cmath>
35
36#if ! defined (__GNUG__) && ! defined (__attribute__)
37#define __attribute__(foo) /* Ignore.  */
38#endif
39
40class istream;
41class ostream;
42
43extern "C++" {
44template <class _FLT> class complex;
45template <class _FLT> complex<_FLT>&
46  __doapl (complex<_FLT>* ths, const complex<_FLT>& r);
47template <class _FLT> complex<_FLT>&
48  __doami (complex<_FLT>* ths, const complex<_FLT>& r);
49template <class _FLT> complex<_FLT>&
50  __doaml (complex<_FLT>* ths, const complex<_FLT>& r);
51template <class _FLT> complex<_FLT>&
52  __doadv (complex<_FLT>* ths, const complex<_FLT>& r);
53
54template <class _FLT>
55class complex
56{
57public:
58  complex (_FLT r = 0, _FLT i = 0): re (r), im (i) { }
59  complex& operator += (const complex&);
60  complex& operator -= (const complex&);
61  complex& operator *= (const complex&);
62  complex& operator /= (const complex&);
63  _FLT real () const { return re; }
64  _FLT imag () const { return im; }
65private:
66  _FLT re, im;
67
68  friend complex& __doapl<> (complex *, const complex&);
69  friend complex& __doami<> (complex *, const complex&);
70  friend complex& __doaml<> (complex *, const complex&);
71  friend complex& __doadv<> (complex *, const complex&);
72};
73
74// Declare specializations.
75class complex<float>;
76class complex<double>;
77class complex<long double>;
78
79template <class _FLT>
80inline complex<_FLT>&
81__doapl (complex<_FLT>* ths, const complex<_FLT>& r)
82{
83  ths->re += r.re;
84  ths->im += r.im;
85  return *ths;
86}
87template <class _FLT>
88inline complex<_FLT>&
89complex<_FLT>::operator += (const complex<_FLT>& r)
90{
91  return __doapl (this, r);
92}
93
94template <class _FLT>
95inline complex<_FLT>&
96__doami (complex<_FLT>* ths, const complex<_FLT>& r)
97{
98  ths->re -= r.re;
99  ths->im -= r.im;
100  return *ths;
101}
102template <class _FLT>
103inline complex<_FLT>&
104complex<_FLT>::operator -= (const complex<_FLT>& r)
105{
106  return __doami (this, r);
107}
108
109template <class _FLT>
110inline complex<_FLT>&
111__doaml (complex<_FLT>* ths, const complex<_FLT>& r)
112{
113  _FLT f = ths->re * r.re - ths->im * r.im;
114  ths->im = ths->re * r.im + ths->im * r.re;
115  ths->re = f;
116  return *ths;
117}
118template <class _FLT>
119inline complex<_FLT>&
120complex<_FLT>::operator *= (const complex<_FLT>& r)
121{
122  return __doaml (this, r);
123}
124
125template <class _FLT>
126inline complex<_FLT>&
127complex<_FLT>::operator /= (const complex<_FLT>& r)
128{
129  return __doadv (this, r);
130}
131
132template <class _FLT> inline _FLT
133imag (const complex<_FLT>& x) __attribute__ ((const));
134
135template <class _FLT> inline _FLT
136imag (const complex<_FLT>& x)
137{
138  return x.imag ();
139}
140
141template <class _FLT> inline _FLT
142real (const complex<_FLT>& x) __attribute__ ((const));
143
144template <class _FLT> inline _FLT
145real (const complex<_FLT>& x)
146{
147  return x.real ();
148}
149
150template <class _FLT> inline complex<_FLT>
151operator + (const complex<_FLT>& x, const complex<_FLT>& y) __attribute__ ((const));
152
153template <class _FLT> inline complex<_FLT>
154operator + (const complex<_FLT>& x, const complex<_FLT>& y)
155{
156  return complex<_FLT> (real (x) + real (y), imag (x) + imag (y));
157}
158
159template <class _FLT> inline complex<_FLT>
160operator + (const complex<_FLT>& x, _FLT y) __attribute__ ((const));
161
162template <class _FLT> inline complex<_FLT>
163operator + (const complex<_FLT>& x, _FLT y)
164{
165  return complex<_FLT> (real (x) + y, imag (x));
166}
167
168template <class _FLT> inline complex<_FLT>
169operator + (_FLT x, const complex<_FLT>& y) __attribute__ ((const));
170
171template <class _FLT> inline complex<_FLT>
172operator + (_FLT x, const complex<_FLT>& y)
173{
174  return complex<_FLT> (x + real (y), imag (y));
175}
176
177template <class _FLT> inline complex<_FLT>
178operator - (const complex<_FLT>& x, const complex<_FLT>& y) __attribute__ ((const));
179
180template <class _FLT> inline complex<_FLT>
181operator - (const complex<_FLT>& x, const complex<_FLT>& y)
182{
183  return complex<_FLT> (real (x) - real (y), imag (x) - imag (y));
184}
185
186template <class _FLT> inline complex<_FLT>
187operator - (const complex<_FLT>& x, _FLT y) __attribute__ ((const));
188
189template <class _FLT> inline complex<_FLT>
190operator - (const complex<_FLT>& x, _FLT y)
191{
192  return complex<_FLT> (real (x) - y, imag (x));
193}
194
195template <class _FLT> inline complex<_FLT>
196operator - (_FLT x, const complex<_FLT>& y) __attribute__ ((const));
197
198template <class _FLT> inline complex<_FLT>
199operator - (_FLT x, const complex<_FLT>& y)
200{
201  return complex<_FLT> (x - real (y), - imag (y));
202}
203
204template <class _FLT> inline complex<_FLT>
205operator * (const complex<_FLT>& x, const complex<_FLT>& y) __attribute__ ((const));
206
207template <class _FLT> inline complex<_FLT>
208operator * (const complex<_FLT>& x, const complex<_FLT>& y)
209{
210  return complex<_FLT> (real (x) * real (y) - imag (x) * imag (y),
211			   real (x) * imag (y) + imag (x) * real (y));
212}
213
214template <class _FLT> inline complex<_FLT>
215operator * (const complex<_FLT>& x, _FLT y) __attribute__ ((const));
216
217template <class _FLT> inline complex<_FLT>
218operator * (const complex<_FLT>& x, _FLT y)
219{
220  return complex<_FLT> (real (x) * y, imag (x) * y);
221}
222
223template <class _FLT> inline complex<_FLT>
224operator * (_FLT x, const complex<_FLT>& y) __attribute__ ((const));
225
226template <class _FLT> inline complex<_FLT>
227operator * (_FLT x, const complex<_FLT>& y)
228{
229  return complex<_FLT> (x * real (y), x * imag (y));
230}
231
232template <class _FLT> complex<_FLT>
233operator / (const complex<_FLT>& x, _FLT y) __attribute__ ((const));
234
235template <class _FLT> complex<_FLT>
236operator / (const complex<_FLT>& x, _FLT y)
237{
238  return complex<_FLT> (real (x) / y, imag (x) / y);
239}
240
241template <class _FLT> inline complex<_FLT>
242operator + (const complex<_FLT>& x) __attribute__ ((const));
243
244template <class _FLT> inline complex<_FLT>
245operator + (const complex<_FLT>& x)
246{
247  return x;
248}
249
250template <class _FLT> inline complex<_FLT>
251operator - (const complex<_FLT>& x) __attribute__ ((const));
252
253template <class _FLT> inline complex<_FLT>
254operator - (const complex<_FLT>& x)
255{
256  return complex<_FLT> (-real (x), -imag (x));
257}
258
259template <class _FLT> inline bool
260operator == (const complex<_FLT>& x, const complex<_FLT>& y) __attribute__ ((const));
261
262template <class _FLT> inline bool
263operator == (const complex<_FLT>& x, const complex<_FLT>& y)
264{
265  return real (x) == real (y) && imag (x) == imag (y);
266}
267
268template <class _FLT> inline bool
269operator == (const complex<_FLT>& x, _FLT y) __attribute__ ((const));
270
271template <class _FLT> inline bool
272operator == (const complex<_FLT>& x, _FLT y)
273{
274  return real (x) == y && imag (x) == 0;
275}
276
277template <class _FLT> inline bool
278operator == (_FLT x, const complex<_FLT>& y) __attribute__ ((const));
279
280template <class _FLT> inline bool
281operator == (_FLT x, const complex<_FLT>& y)
282{
283  return x == real (y) && imag (y) == 0;
284}
285
286template <class _FLT> inline bool
287operator != (const complex<_FLT>& x, const complex<_FLT>& y) __attribute__ ((const));
288
289template <class _FLT> inline bool
290operator != (const complex<_FLT>& x, const complex<_FLT>& y)
291{
292  return real (x) != real (y) || imag (x) != imag (y);
293}
294
295template <class _FLT> inline bool
296operator != (const complex<_FLT>& x, _FLT y) __attribute__ ((const));
297
298template <class _FLT> inline bool
299operator != (const complex<_FLT>& x, _FLT y)
300{
301  return real (x) != y || imag (x) != 0;
302}
303
304template <class _FLT> inline bool
305operator != (_FLT x, const complex<_FLT>& y) __attribute__ ((const));
306
307template <class _FLT> inline bool
308operator != (_FLT x, const complex<_FLT>& y)
309{
310  return x != real (y) || imag (y) != 0;
311}
312
313// Some targets don't provide a prototype for hypot when -ansi.
314//extern "C" double hypot (double, double) __attribute__ ((const));
315
316template <class _FLT> inline _FLT
317abs (const complex<_FLT>& x) __attribute__ ((const));
318
319template <class _FLT> inline _FLT
320abs (const complex<_FLT>& x)
321{
322  return hypot (real (x), imag (x));
323}
324
325template <class _FLT> inline _FLT
326arg (const complex<_FLT>& x) __attribute__ ((const));
327
328template <class _FLT> inline _FLT
329arg (const complex<_FLT>& x)
330{
331  return atan2 (imag (x), real (x));
332}
333
334template <class _FLT> inline complex<_FLT>
335polar (_FLT r, _FLT t) __attribute__ ((const));
336
337template <class _FLT> inline complex<_FLT>
338polar (_FLT r, _FLT t)
339{
340  return complex<_FLT> (r * cos (t), r * sin (t));
341}
342
343template <class _FLT> inline complex<_FLT>
344conj (const complex<_FLT>& x)  __attribute__ ((const));
345
346template <class _FLT> inline complex<_FLT>
347conj (const complex<_FLT>& x)
348{
349  return complex<_FLT> (real (x), -imag (x));
350}
351
352template <class _FLT> inline _FLT
353norm (const complex<_FLT>& x) __attribute__ ((const));
354
355template <class _FLT> inline _FLT
356norm (const complex<_FLT>& x)
357{
358  return real (x) * real (x) + imag (x) * imag (x);
359}
360
361// Declarations of templates in complext.ccI
362
363template <class _FLT> complex<_FLT>
364  operator / (const complex<_FLT>&, const complex<_FLT>&) __attribute__ ((const));
365template <class _FLT> complex<_FLT>
366  operator / (_FLT, const complex<_FLT>&) __attribute__ ((const));
367template <class _FLT> complex<_FLT>
368  cos (const complex<_FLT>&) __attribute__ ((const));
369template <class _FLT> complex<_FLT>
370  cosh (const complex<_FLT>&) __attribute__ ((const));
371template <class _FLT> complex<_FLT>
372  exp (const complex<_FLT>&) __attribute__ ((const));
373template <class _FLT> complex<_FLT>
374  log (const complex<_FLT>&) __attribute__ ((const));
375template <class _FLT> complex<_FLT>
376  pow (const complex<_FLT>&, const complex<_FLT>&) __attribute__ ((const));
377template <class _FLT> complex<_FLT>
378  pow (const complex<_FLT>&, _FLT) __attribute__ ((const));
379template <class _FLT> complex<_FLT>
380  pow (const complex<_FLT>&, int) __attribute__ ((const));
381template <class _FLT> complex<_FLT>
382  pow (_FLT, const complex<_FLT>&) __attribute__ ((const));
383template <class _FLT> complex<_FLT>
384  sin (const complex<_FLT>&) __attribute__ ((const));
385template <class _FLT> complex<_FLT>
386  sinh (const complex<_FLT>&) __attribute__ ((const));
387template <class _FLT> complex<_FLT>
388  sqrt (const complex<_FLT>&) __attribute__ ((const));
389
390template <class _FLT> inline complex<_FLT>
391tan (const complex<_FLT>& x)
392{
393  return sin (x) / cos (x);
394}
395template <class _FLT> inline complex<_FLT>
396tanh (const complex<_FLT>& x)
397{
398  return sinh (x) / cosh (x);
399}
400template <class _FLT> inline complex<_FLT>
401log10 (const complex<_FLT>& x)
402{
403  return log (x) / log (10.0);
404}
405
406template <class _FLT> istream& operator >> (istream&, complex<_FLT>&);
407template <class _FLT> ostream& operator << (ostream&, const complex<_FLT>&);
408} // extern "C++"
409
410// Specializations and such
411
412#include <std/fcomplex.h>
413#include <std/dcomplex.h>
414#include <std/ldcomplex.h>
415
416#endif
417