xref: /haiku/headers/cpp/std/complext.h (revision 1deede7388b04dbeec5af85cae7164735ea9e70d)
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 
40 class istream;
41 class ostream;
42 
43 extern "C++" {
44 template <class _FLT> class complex;
45 template <class _FLT> complex<_FLT>&
46   __doapl (complex<_FLT>* ths, const complex<_FLT>& r);
47 template <class _FLT> complex<_FLT>&
48   __doami (complex<_FLT>* ths, const complex<_FLT>& r);
49 template <class _FLT> complex<_FLT>&
50   __doaml (complex<_FLT>* ths, const complex<_FLT>& r);
51 template <class _FLT> complex<_FLT>&
52   __doadv (complex<_FLT>* ths, const complex<_FLT>& r);
53 
54 template <class _FLT>
55 class complex
56 {
57 public:
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; }
65 private:
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.
75 class complex<float>;
76 class complex<double>;
77 class complex<long double>;
78 
79 template <class _FLT>
80 inline 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 }
87 template <class _FLT>
88 inline complex<_FLT>&
89 complex<_FLT>::operator += (const complex<_FLT>& r)
90 {
91   return __doapl (this, r);
92 }
93 
94 template <class _FLT>
95 inline 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 }
102 template <class _FLT>
103 inline complex<_FLT>&
104 complex<_FLT>::operator -= (const complex<_FLT>& r)
105 {
106   return __doami (this, r);
107 }
108 
109 template <class _FLT>
110 inline 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 }
118 template <class _FLT>
119 inline complex<_FLT>&
120 complex<_FLT>::operator *= (const complex<_FLT>& r)
121 {
122   return __doaml (this, r);
123 }
124 
125 template <class _FLT>
126 inline complex<_FLT>&
127 complex<_FLT>::operator /= (const complex<_FLT>& r)
128 {
129   return __doadv (this, r);
130 }
131 
132 template <class _FLT> inline _FLT
133 imag (const complex<_FLT>& x) __attribute__ ((const));
134 
135 template <class _FLT> inline _FLT
136 imag (const complex<_FLT>& x)
137 {
138   return x.imag ();
139 }
140 
141 template <class _FLT> inline _FLT
142 real (const complex<_FLT>& x) __attribute__ ((const));
143 
144 template <class _FLT> inline _FLT
145 real (const complex<_FLT>& x)
146 {
147   return x.real ();
148 }
149 
150 template <class _FLT> inline complex<_FLT>
151 operator + (const complex<_FLT>& x, const complex<_FLT>& y) __attribute__ ((const));
152 
153 template <class _FLT> inline complex<_FLT>
154 operator + (const complex<_FLT>& x, const complex<_FLT>& y)
155 {
156   return complex<_FLT> (real (x) + real (y), imag (x) + imag (y));
157 }
158 
159 template <class _FLT> inline complex<_FLT>
160 operator + (const complex<_FLT>& x, _FLT y) __attribute__ ((const));
161 
162 template <class _FLT> inline complex<_FLT>
163 operator + (const complex<_FLT>& x, _FLT y)
164 {
165   return complex<_FLT> (real (x) + y, imag (x));
166 }
167 
168 template <class _FLT> inline complex<_FLT>
169 operator + (_FLT x, const complex<_FLT>& y) __attribute__ ((const));
170 
171 template <class _FLT> inline complex<_FLT>
172 operator + (_FLT x, const complex<_FLT>& y)
173 {
174   return complex<_FLT> (x + real (y), imag (y));
175 }
176 
177 template <class _FLT> inline complex<_FLT>
178 operator - (const complex<_FLT>& x, const complex<_FLT>& y) __attribute__ ((const));
179 
180 template <class _FLT> inline complex<_FLT>
181 operator - (const complex<_FLT>& x, const complex<_FLT>& y)
182 {
183   return complex<_FLT> (real (x) - real (y), imag (x) - imag (y));
184 }
185 
186 template <class _FLT> inline complex<_FLT>
187 operator - (const complex<_FLT>& x, _FLT y) __attribute__ ((const));
188 
189 template <class _FLT> inline complex<_FLT>
190 operator - (const complex<_FLT>& x, _FLT y)
191 {
192   return complex<_FLT> (real (x) - y, imag (x));
193 }
194 
195 template <class _FLT> inline complex<_FLT>
196 operator - (_FLT x, const complex<_FLT>& y) __attribute__ ((const));
197 
198 template <class _FLT> inline complex<_FLT>
199 operator - (_FLT x, const complex<_FLT>& y)
200 {
201   return complex<_FLT> (x - real (y), - imag (y));
202 }
203 
204 template <class _FLT> inline complex<_FLT>
205 operator * (const complex<_FLT>& x, const complex<_FLT>& y) __attribute__ ((const));
206 
207 template <class _FLT> inline complex<_FLT>
208 operator * (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 
214 template <class _FLT> inline complex<_FLT>
215 operator * (const complex<_FLT>& x, _FLT y) __attribute__ ((const));
216 
217 template <class _FLT> inline complex<_FLT>
218 operator * (const complex<_FLT>& x, _FLT y)
219 {
220   return complex<_FLT> (real (x) * y, imag (x) * y);
221 }
222 
223 template <class _FLT> inline complex<_FLT>
224 operator * (_FLT x, const complex<_FLT>& y) __attribute__ ((const));
225 
226 template <class _FLT> inline complex<_FLT>
227 operator * (_FLT x, const complex<_FLT>& y)
228 {
229   return complex<_FLT> (x * real (y), x * imag (y));
230 }
231 
232 template <class _FLT> complex<_FLT>
233 operator / (const complex<_FLT>& x, _FLT y) __attribute__ ((const));
234 
235 template <class _FLT> complex<_FLT>
236 operator / (const complex<_FLT>& x, _FLT y)
237 {
238   return complex<_FLT> (real (x) / y, imag (x) / y);
239 }
240 
241 template <class _FLT> inline complex<_FLT>
242 operator + (const complex<_FLT>& x) __attribute__ ((const));
243 
244 template <class _FLT> inline complex<_FLT>
245 operator + (const complex<_FLT>& x)
246 {
247   return x;
248 }
249 
250 template <class _FLT> inline complex<_FLT>
251 operator - (const complex<_FLT>& x) __attribute__ ((const));
252 
253 template <class _FLT> inline complex<_FLT>
254 operator - (const complex<_FLT>& x)
255 {
256   return complex<_FLT> (-real (x), -imag (x));
257 }
258 
259 template <class _FLT> inline bool
260 operator == (const complex<_FLT>& x, const complex<_FLT>& y) __attribute__ ((const));
261 
262 template <class _FLT> inline bool
263 operator == (const complex<_FLT>& x, const complex<_FLT>& y)
264 {
265   return real (x) == real (y) && imag (x) == imag (y);
266 }
267 
268 template <class _FLT> inline bool
269 operator == (const complex<_FLT>& x, _FLT y) __attribute__ ((const));
270 
271 template <class _FLT> inline bool
272 operator == (const complex<_FLT>& x, _FLT y)
273 {
274   return real (x) == y && imag (x) == 0;
275 }
276 
277 template <class _FLT> inline bool
278 operator == (_FLT x, const complex<_FLT>& y) __attribute__ ((const));
279 
280 template <class _FLT> inline bool
281 operator == (_FLT x, const complex<_FLT>& y)
282 {
283   return x == real (y) && imag (y) == 0;
284 }
285 
286 template <class _FLT> inline bool
287 operator != (const complex<_FLT>& x, const complex<_FLT>& y) __attribute__ ((const));
288 
289 template <class _FLT> inline bool
290 operator != (const complex<_FLT>& x, const complex<_FLT>& y)
291 {
292   return real (x) != real (y) || imag (x) != imag (y);
293 }
294 
295 template <class _FLT> inline bool
296 operator != (const complex<_FLT>& x, _FLT y) __attribute__ ((const));
297 
298 template <class _FLT> inline bool
299 operator != (const complex<_FLT>& x, _FLT y)
300 {
301   return real (x) != y || imag (x) != 0;
302 }
303 
304 template <class _FLT> inline bool
305 operator != (_FLT x, const complex<_FLT>& y) __attribute__ ((const));
306 
307 template <class _FLT> inline bool
308 operator != (_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 
316 template <class _FLT> inline _FLT
317 abs (const complex<_FLT>& x) __attribute__ ((const));
318 
319 template <class _FLT> inline _FLT
320 abs (const complex<_FLT>& x)
321 {
322   return hypot (real (x), imag (x));
323 }
324 
325 template <class _FLT> inline _FLT
326 arg (const complex<_FLT>& x) __attribute__ ((const));
327 
328 template <class _FLT> inline _FLT
329 arg (const complex<_FLT>& x)
330 {
331   return atan2 (imag (x), real (x));
332 }
333 
334 template <class _FLT> inline complex<_FLT>
335 polar (_FLT r, _FLT t) __attribute__ ((const));
336 
337 template <class _FLT> inline complex<_FLT>
338 polar (_FLT r, _FLT t)
339 {
340   return complex<_FLT> (r * cos (t), r * sin (t));
341 }
342 
343 template <class _FLT> inline complex<_FLT>
344 conj (const complex<_FLT>& x)  __attribute__ ((const));
345 
346 template <class _FLT> inline complex<_FLT>
347 conj (const complex<_FLT>& x)
348 {
349   return complex<_FLT> (real (x), -imag (x));
350 }
351 
352 template <class _FLT> inline _FLT
353 norm (const complex<_FLT>& x) __attribute__ ((const));
354 
355 template <class _FLT> inline _FLT
356 norm (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 
363 template <class _FLT> complex<_FLT>
364   operator / (const complex<_FLT>&, const complex<_FLT>&) __attribute__ ((const));
365 template <class _FLT> complex<_FLT>
366   operator / (_FLT, const complex<_FLT>&) __attribute__ ((const));
367 template <class _FLT> complex<_FLT>
368   cos (const complex<_FLT>&) __attribute__ ((const));
369 template <class _FLT> complex<_FLT>
370   cosh (const complex<_FLT>&) __attribute__ ((const));
371 template <class _FLT> complex<_FLT>
372   exp (const complex<_FLT>&) __attribute__ ((const));
373 template <class _FLT> complex<_FLT>
374   log (const complex<_FLT>&) __attribute__ ((const));
375 template <class _FLT> complex<_FLT>
376   pow (const complex<_FLT>&, const complex<_FLT>&) __attribute__ ((const));
377 template <class _FLT> complex<_FLT>
378   pow (const complex<_FLT>&, _FLT) __attribute__ ((const));
379 template <class _FLT> complex<_FLT>
380   pow (const complex<_FLT>&, int) __attribute__ ((const));
381 template <class _FLT> complex<_FLT>
382   pow (_FLT, const complex<_FLT>&) __attribute__ ((const));
383 template <class _FLT> complex<_FLT>
384   sin (const complex<_FLT>&) __attribute__ ((const));
385 template <class _FLT> complex<_FLT>
386   sinh (const complex<_FLT>&) __attribute__ ((const));
387 template <class _FLT> complex<_FLT>
388   sqrt (const complex<_FLT>&) __attribute__ ((const));
389 
390 template <class _FLT> inline complex<_FLT>
391 tan (const complex<_FLT>& x)
392 {
393   return sin (x) / cos (x);
394 }
395 template <class _FLT> inline complex<_FLT>
396 tanh (const complex<_FLT>& x)
397 {
398   return sinh (x) / cosh (x);
399 }
400 template <class _FLT> inline complex<_FLT>
401 log10 (const complex<_FLT>& x)
402 {
403   return log (x) / log (10.0);
404 }
405 
406 template <class _FLT> istream& operator >> (istream&, complex<_FLT>&);
407 template <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