C-XSC - A C++ Class Library for Extended Scientific Computing  2.5.4
l_complex.hpp
1 /*
2 ** CXSC is a C++ library for eXtended Scientific Computing (V 2.5.4)
3 **
4 ** Copyright (C) 1990-2000 Institut fuer Angewandte Mathematik,
5 ** Universitaet Karlsruhe, Germany
6 ** (C) 2000-2014 Wiss. Rechnen/Softwaretechnologie
7 ** Universitaet Wuppertal, Germany
8 **
9 ** This library is free software; you can redistribute it and/or
10 ** modify it under the terms of the GNU Library General Public
11 ** License as published by the Free Software Foundation; either
12 ** version 2 of the License, or (at your option) any later version.
13 **
14 ** This library is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ** Library General Public License for more details.
18 **
19 ** You should have received a copy of the GNU Library General Public
20 ** License along with this library; if not, write to the Free
21 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23 /* CVS $Id: l_complex.hpp,v 1.19 2014/01/30 17:23:46 cxsc Exp $ */
24 
25 #ifndef _CXSC_L_COMPLEX_HPP_INCLUDED
26 #define _CXSC_L_COMPLEX_HPP_INCLUDED
27 
28 #include <iostream>
29 #include <string>
30 
31 #include "except.hpp"
32 #include "complex.hpp"
33 #include "l_real.hpp"
34 #include "l_rmath.hpp"
35 #include "cdot.hpp"
36 
37 namespace cxsc {
38 
40 
45 class l_complex
46 {
47  private:
48  // ------------- Datenelemente -------------------------------------------
49  l_real re, im;
50 
51  public:
54  // ------------- Constructors --------------------------------------------
56  l_complex(void) noexcept {}
58  l_complex(const l_real& a, const l_real& b) noexcept : re(a), im(b) { }
60  l_complex(const real& a, const real& b) noexcept : re(a), im(b) { }
61 
63  l_complex & operator = (const l_real & lr) noexcept
64  { re = lr; im = 0; return *this; }
66  l_complex & operator = (const real & r) noexcept
67  { re = r; im = 0; return *this; }
69  l_complex & operator = (const complex & c) noexcept
70  { re = Re(c); im = Im(c); return *this; }
72  l_complex & operator = (const dotprecision & d) noexcept
73  { re = d; im = 0.0; return *this; }
75  l_complex & operator = (const cdotprecision & cd) noexcept
76  { re = Re(_l_complex(cd)); im = Im(_l_complex(cd)); return *this; }
77 
79  l_complex & operator = (const lx_complex &) noexcept;
80 
81  // ------------- Type-Casts ----------------------------------------------
83  explicit inline l_complex(const l_real &r) noexcept : re(r), im(0.0) { }
85  explicit inline l_complex(const real &r) noexcept : re(r), im(0.0) { }
87  explicit inline l_complex(const complex &r) noexcept : re(Re(r)), im(Im(r))
88  { }
90  explicit inline l_complex(const dotprecision &d) noexcept
91  : re(d), im(0.0) { }
93  explicit inline l_complex(const cdotprecision &cd) noexcept
94  : re(Re(_l_complex(cd))), im(Im(_l_complex(cd))) { }
95 
96 // friend inline l_complex _l_complex(const l_real &a) noexcept
97 // { return l_complex(a); }
98 // friend inline l_complex _l_complex(const l_real &a, const l_real &b)
99 // noexcept { return l_complex(a,b); }
100 // friend inline l_complex _l_complex(const real &a) noexcept
101 // { return l_complex(a); }
102 // friend inline l_complex _l_complex(const real &a, const real &b)
103 // noexcept { return l_complex(a,b); }
104 // friend inline l_complex _l_complex(const complex &c)
105 // noexcept { return l_complex(c); }
106 // friend inline l_complex _l_complex(const dotprecision &d)
107 // noexcept { return l_complex(d); }
113  friend inline l_complex _l_complex(const cdotprecision &cd)
114  noexcept { return l_complex(cd); }
115 
116  // ----------------------------------------------------------------------
118  friend int StagPrec(const l_complex&) noexcept;
119 
120 // ------------- Arithmetic Operators ---------------------------------------
121  // ----------------------------------------------------------------------
122  // ------------- Unary Operators ----------------------------------------
123 
125  friend l_complex operator-(const l_complex&);
127  friend l_complex operator+(const l_complex&);
128 
129  // ------------- Binary Operators ---------------------------------------
130  // ----------------- l_complex +/- l_complex ----------------------------
132  friend inline l_complex operator +
133  (const l_complex &, const l_complex &) noexcept;
134 
136  friend inline l_complex operator -
137  (const l_complex &, const l_complex &) noexcept;
138 
139  // ----------------- l_complex + complex --------------------------------
141  inline l_complex operator + (const complex &) const noexcept;
142 
144  friend inline l_complex operator +
145  (const complex &, const l_complex &) noexcept;
146 
147  // ---------------- l_complex + real ------------------------------------
149  inline l_complex operator + (const real &) const noexcept;
150 
152  friend inline l_complex operator +
153  (const real &, const l_complex &) noexcept;
154 
155  // ---------------- l_complex + l_real ----------------------------------
157  inline l_complex operator + (const l_real &) const noexcept;
158 
160  friend inline l_complex operator +
161  (const l_real &, const l_complex &) noexcept;
162 
163  // ---------------- l_complex - l_real ----------------------------------
165  inline l_complex operator - (const l_real &) const noexcept;
166 
168  friend inline l_complex operator -
169  (const l_real &, const l_complex &) noexcept;
170 
171  // ----------------- l_complex - complex -------------------------------
173  inline l_complex operator - (const complex &) const noexcept;
174 
176  friend inline l_complex operator -
177  (const complex &, const l_complex &) noexcept;
178 
179  // ---------------- l_complex - real ------------------------------------
181  inline l_complex operator - (const real &) const noexcept;
182 
184  friend inline l_complex operator -
185  (const real &, const l_complex &) noexcept;
186 
187  // ---------------- l_complex + cdotprecision ---------------------------
189  friend cdotprecision operator+
190  (const l_complex &, const cdotprecision &) noexcept;
191 
193  friend cdotprecision operator+
194  (const cdotprecision &, const l_complex &) noexcept;
195 
196  // ---------------- l_complex - cdotprecision ---------------------------
198  friend cdotprecision operator-
199  (const l_complex &, const cdotprecision &) noexcept;
200 
202  friend cdotprecision operator-
203  (const cdotprecision &, const l_complex &) noexcept;
204 
205  // ---------------- l_complex + dotprecision ----------------------------
207  friend cdotprecision operator+
208  (const l_complex &, const dotprecision &) noexcept;
209 
211  friend cdotprecision operator+
212  (const dotprecision &, const l_complex &) noexcept;
213 
214  // ---------------- l_complex - dotprecision ----------------------------
216  friend cdotprecision operator-
217  (const l_complex &, const dotprecision &) noexcept;
218 
220  friend cdotprecision operator-
221  (const dotprecision &, const l_complex &) noexcept;
222 
223 
224 // ------------- Multiplication ---------------------------------------------
225 // ------------------ l_complex * l_complex ---------------------------------
226 
228 friend l_complex operator * (const l_complex& a, const l_complex& b)
229  noexcept;
230 
231 // ------------------ l_complex * complex -----------------------------------
233 friend l_complex operator * (const l_complex& a, const complex& b)
234  noexcept;
235 
237 friend l_complex operator * ( const complex& b, const l_complex& a )
238  noexcept;
239 
240 // ------------------ l_complex * real ---------------------------------------
242  inline l_complex operator * (const real &) const noexcept;
243 
245  friend inline l_complex operator *
246  (const real &, const l_complex &) noexcept;
247 
248 // ------------------ l_complex * l_real -------------------------------------
250  inline l_complex operator * (const l_real &) const noexcept;
251 
253  friend inline l_complex operator *
254  (const l_real &, const l_complex &) noexcept;
255 
256 
257 // ----------------- Others --------------------------------------------------
259  friend l_real & Re(l_complex& a); // { return a.re; }
261  friend l_real Re(const l_complex& a); // { return a.re; }
263  friend l_real & Im(l_complex& a); // { return a.im; }
265  friend l_real Im(const l_complex& a); // { return a.im; }
266 
268  friend inline l_complex conj(const l_complex&) noexcept; // conjugated value
270  friend l_complex & SetRe(l_complex & a,const l_real & b);
271  // { a.re=b; return a; } // The real part of a is substituted by b.
272  // SetRe(lc,lr); --> Re(lc)=lr;
273  // lc1 = SetRe(lc,lr); --> Re(lc)=lr; and lc1 = lc;
275  friend l_complex & SetIm(l_complex & a,const l_real & b);
276  // { a.im=b; return a; } // See SetRe(...);
277 
278 
279 // ----- accumulate(cdotprecision,l_complex,l_complex|complex|real|l_real ----
281  friend void accumulate(cdotprecision&, const l_complex&,
282  const l_complex&) noexcept;
283 
285  friend void accumulate(cdotprecision&, const l_complex&,
286  const complex&) noexcept;
287 
289  friend void accumulate(cdotprecision&, const l_complex&,
290  const real&) noexcept;
291 
293  friend void accumulate(cdotprecision&, const l_complex&,
294  const l_real&) noexcept;
295 
296 // ---------------- cdotprecision +(-)= l_complex ----------------------------
298 friend inline cdotprecision & operator += (cdotprecision &cd,
299  const l_complex &lc) noexcept;
301 friend inline cdotprecision & operator -= (cdotprecision &cd,
302  const l_complex &lc) noexcept;
303 
304 // ---------------- l_complex +(-)= l_complex|complex|real|l_real ------------
306 friend inline l_complex & operator += (l_complex &,const l_complex &) noexcept;
308 friend inline l_complex & operator -= (l_complex &,const l_complex &) noexcept;
310 friend inline l_complex & operator += (l_complex &,const complex &) noexcept;
312 friend inline l_complex & operator -= (l_complex &,const complex &) noexcept;
314 friend inline l_complex & operator += (l_complex &,const real &) noexcept;
316 friend inline l_complex & operator -= (l_complex &,const real &) noexcept;
318 friend inline l_complex & operator += (l_complex &,const l_real &) noexcept;
320 friend inline l_complex & operator -= (l_complex &,const l_real &) noexcept;
321 
322 // ---------------- l_complex *= l_complex|complex|real|l_real ---------------
324 friend inline l_complex & operator *= (l_complex &,const l_complex &) noexcept;
326 friend inline l_complex & operator *= (l_complex &,const complex &) noexcept;
328 friend inline l_complex & operator *= (l_complex &,const real &) noexcept;
330 friend inline l_complex & operator *= (l_complex &,const l_real &) noexcept;
331 
332 // ---------------- Compare Operators ----------------------------------------
333 friend inline bool operator! (const l_complex &) noexcept;
335 friend inline bool operator== (const l_complex &, const l_complex &) noexcept;
337 friend inline bool operator!= (const l_complex &, const l_complex &) noexcept;
339 friend inline bool operator== (const l_complex &, const complex &) noexcept;
341 friend inline bool operator== (const complex &, const l_complex &) noexcept;
343 friend inline bool operator!= (const l_complex &, const complex &) noexcept;
345 friend inline bool operator!= (const complex &, const l_complex &) noexcept;
347 friend inline bool operator== (const l_complex &, const real &) noexcept;
349 friend inline bool operator== (const real &, const l_complex &) noexcept;
351 friend inline bool operator!= (const l_complex &, const real &) noexcept;
353 friend inline bool operator!= (const real &, const l_complex &) noexcept;
355 friend inline bool operator== (const l_complex &, const l_real &) noexcept;
357 friend inline bool operator== (const l_real &, const l_complex &) noexcept;
359 friend inline bool operator!= (const l_complex &, const l_real &) noexcept;
361 friend inline bool operator!= (const l_real &, const l_complex &) noexcept;
363 friend inline bool operator== (const l_complex &, const dotprecision &)
364  noexcept;
366 friend inline bool operator== (const dotprecision &, const l_complex &)
367  noexcept;
369 friend inline bool operator!= (const l_complex &, const dotprecision &)
370  noexcept;
372 friend inline bool operator!= (const dotprecision &, const l_complex &)
373  noexcept;
374 
376 friend inline bool operator ==(const cdotprecision &, const l_complex &)
377  noexcept; // {l_complex.inl}
379 friend inline bool operator ==(const l_complex &, const cdotprecision &)
380  noexcept; // {l_complex.inl}
382 friend inline bool operator !=(const cdotprecision &, const l_complex &)
383  noexcept; // {l_complex.inl}
385 friend inline bool operator !=(const l_complex &, const cdotprecision &)
386  noexcept; // {l_complex.inl}
387 
388 // -------------- Division: Directed rounding, Blomquist 28.11.02 ------------
389 
391  friend l_complex divn (const l_complex &, const l_complex &);
393  friend l_complex divd (const l_complex &, const l_complex &);
395  friend l_complex divu (const l_complex &, const l_complex &);
396 
397 // -------------- Division: Blomquist 28.11.02 ------------------------------
399  friend l_complex operator / (const l_complex &,const l_complex &) noexcept;
400 
402  friend inline l_complex operator / (const l_complex & a,
403  const complex & b) noexcept;
405  friend inline l_complex operator / (const l_complex & a,
406  const l_real & b) noexcept;
408  friend inline l_complex operator / (const l_complex & a,
409  const real & b) noexcept;
410 
412  friend inline l_complex operator / (const complex& a,
413  const l_complex& b) noexcept;
415  friend inline l_complex operator / (const real& a,
416  const l_complex& b) noexcept;
418  friend inline l_complex operator / (const l_real& a,
419  const l_complex& b) noexcept;
420 
422  friend inline l_complex& operator /=(l_complex&,const l_complex&) noexcept;
424  friend inline l_complex& operator /=(l_complex&,const complex&) noexcept;
426  friend inline l_complex& operator /=(l_complex&,const real&) noexcept;
428  friend inline l_complex& operator /=(l_complex&,const l_real&) noexcept;
429 
431 friend l_real abs2(const l_complex &a) noexcept; // a.re*a.re + a.im*a.im;
433 friend l_real abs (const l_complex &z) noexcept;
434 
435 // ----------------------- Output --------------------------------------------
436 
438 friend std::ostream& operator << (std::ostream& s,const l_complex& z ) noexcept
439 // A complex number z of type l_complex is written to the output channel.
440 {
441  s << '('
442  << z.re << ", "
443  << z.im
444  << ')';
445  return s;
446 }
447 
449 friend std::string & operator << (std::string &s, const l_complex& a) noexcept
450 // The value of a variable a of type l_complex is copied to a string s.
451 // s has the form: (Re(a),Im(a))
452 {
453  s+='(';
454  s << a.re;
455  s+=", ";
456  s << a.im;
457  s+=')';
458  return s;
459 }
460 
461 // ----------------------- Input ---------------------------------------------
462 
464 friend std::istream & operator >> (std::istream &s, l_complex &a) noexcept
465 // An input of a complex number z of the form (Re(z),Im(z)) is copied to
466 // the variable a of type l_complex.
467 {
468  char c;
469 
470  skipeolnflag = inpdotflag = true;
471  c = skipwhitespacessinglechar (s, '(');
472  if (inpdotflag)
473  s.putback(c);
474 
475  s >> a.re;
476 
477  skipeolnflag = inpdotflag = true;
478  c = skipwhitespacessinglechar (s, ',');
479  if (inpdotflag) s.putback(c);
480 
481  s >> a.im >> RestoreOpt;
482 
483  if (!waseolnflag)
484  {
485  skipeolnflag = false, inpdotflag = true;
486  c = skipwhitespaces (s);
487  if (inpdotflag && c != ')')
488  s.putback(c);
489  }
490  return s;
491 }
492 
494 friend std::string & operator >> (std::string &s, l_complex &a) noexcept
495 // A complex number z of the form (Re(z),Im(z)), represented in a string s
496 // is copied to a of type l_complex.
497 {
498  s = skipwhitespacessinglechar (s, '(');
499  s >> SaveOpt >> a.re;
500  s = skipwhitespacessinglechar (s, ',');
501  s >> a.im >> RestoreOpt;
502  s = skipwhitespaces (s);
503 
504  if (s[0] == ')')
505  s.erase(0,1);
506  return s;
507 }
508 
509 }; // end of class l_complex
510 
511 inline l_complex _l_complex(const l_real &a) noexcept
512  { return l_complex(a); }
513 inline l_complex _l_complex(const l_real &a, const l_real &b)
514  noexcept { return l_complex(a,b); }
515 inline l_complex _l_complex(const real &a) noexcept
516  { return l_complex(a); }
517 inline l_complex _l_complex(const real &a, const real &b)
518  noexcept { return l_complex(a,b); }
519 inline l_complex _l_complex(const complex &c)
520  noexcept { return l_complex(c); }
521 inline l_complex _l_complex(const dotprecision &d)
522  noexcept { return l_complex(d); }
523 inline l_complex conj(const l_complex&) noexcept;
524 //inline l_complex _l_complex(const cdotprecision &cd)
525 // noexcept { return l_complex(cd); }
526 
527 l_real & Re(l_complex& a);
528 l_real Re(const l_complex& a);
529 l_real & Im(l_complex& a);
530 l_real Im(const l_complex& a);
531 
532 l_complex & SetRe(l_complex & a,const l_real & b);
533  //{ a.re=b; return a; } // The real part of a is substituted by b.
534  // SetRe(lc,lr); --> Re(lc)=lr;
535  // lc1 = SetRe(lc,lr); --> Re(lc)=lr; and lc1 = lc;
536 l_complex & SetIm(l_complex & a,const l_real & b);
537 // { a.im=b; return a; } // See SetRe(...);
538 
539 l_complex _l_complex(const cdotprecision &) noexcept;
540 
541 } // end namespace cxsc
542 
543 #include "l_complex.inl"
544 
545 #endif // _CXSC_L_COMPLEX_HPP_INCLUDED
The Data Type cdotprecision.
Definition: cdot.hpp:61
The Scalar Type complex.
Definition: complex.hpp:50
The Data Type dotprecision.
Definition: dot.hpp:112
The Multiple-Precision Data Type l_complex.
Definition: l_complex.hpp:46
friend l_complex & SetRe(l_complex &a, const l_real &b)
Sets the real part of a complex value.
Definition: l_complex.cpp:526
friend cdotprecision & operator-=(cdotprecision &cd, const l_complex &lc) noexcept
Implementation of standard algebraic subtraction and allocation operation.
Definition: cdot.inl:255
l_complex(const real &a, const real &b) noexcept
Constructor of class l_complex.
Definition: l_complex.hpp:60
l_complex(const cdotprecision &cd) noexcept
Constructor of class l_complex.
Definition: l_complex.hpp:93
friend l_complex & operator*=(l_complex &, const l_complex &) noexcept
Implementation of standard algebraic multiplication and allocation operation.
Definition: l_complex.inl:162
friend l_complex & SetIm(l_complex &a, const l_real &b)
Sets the imaginary part of a complex value.
Definition: l_complex.cpp:523
l_complex & operator=(const l_real &lr) noexcept
Implementation of standard assigning operator.
Definition: l_complex.hpp:63
friend bool operator==(const l_complex &, const l_complex &) noexcept
Implementation of standard equality operation.
Definition: l_complex.inl:224
friend l_real & Im(l_complex &a)
Returns the imaginary part of the complex value.
Definition: l_complex.cpp:531
friend l_complex & operator/=(l_complex &, const l_complex &) noexcept
Implementation of standard algebraic division and allocation operation.
Definition: l_complex.inl:209
friend l_complex operator+(const l_complex &)
Implementation of standard algebraic positive sign operation.
Definition: l_complex.cpp:36
friend cdotprecision & operator+=(cdotprecision &cd, const l_complex &lc) noexcept
Implementation of standard algebraic addition and allocation operation.
Definition: cdot.inl:251
friend l_real & Re(l_complex &a)
Returns the real part of the complex value.
Definition: l_complex.cpp:529
friend l_complex divn(const l_complex &, const l_complex &)
Division of two real values and rounding to the nearest value.
Definition: l_complex.cpp:448
friend l_complex operator*(const l_complex &a, const l_complex &b) noexcept
Implementation of standard algebraic multiplication operation.
Definition: l_complex.cpp:86
friend l_complex operator-(const l_complex &)
Implementation of standard algebraic negative sign operation.
Definition: l_complex.cpp:31
friend int StagPrec(const l_complex &) noexcept
Returns the precision of the long datatype value.
Definition: l_complex.cpp:468
l_complex(const real &r) noexcept
Constructor of class l_complex.
Definition: l_complex.hpp:85
friend l_complex operator/(const l_complex &, const l_complex &) noexcept
Implementation of standard algebraic division operation.
Definition: l_complex.cpp:463
l_complex(const dotprecision &d) noexcept
Constructor of class l_complex.
Definition: l_complex.hpp:90
l_complex(const l_real &r) noexcept
Constructor of class l_complex.
Definition: l_complex.hpp:83
friend std::istream & operator>>(std::istream &s, l_complex &a) noexcept
Implementation of standard input method.
Definition: l_complex.hpp:464
friend std::ostream & operator<<(std::ostream &s, const l_complex &z) noexcept
Implementation of standard output method.
Definition: l_complex.hpp:438
friend l_complex conj(const l_complex &) noexcept
Returns the conjugated complex value.
Definition: l_complex.inl:270
l_complex(void) noexcept
Constructor of class l_complex.
Definition: l_complex.hpp:56
l_complex(const l_real &a, const l_real &b) noexcept
Constructor of class l_complex.
Definition: l_complex.hpp:58
friend void accumulate(cdotprecision &, const l_complex &, const l_complex &) noexcept
The accurate scalar product of the last two arguments added to the value of the first argument.
Definition: l_complex.cpp:473
friend l_real abs(const l_complex &z) noexcept
The absolute value of a l_complex value.
Definition: l_complex.cpp:513
friend l_real abs2(const l_complex &a) noexcept
The absolute value of a l_complex value.
Definition: l_complex.cpp:505
l_complex _l_complex(const cdotprecision &)
Deprecated typecast, which only exist for the reason of compatibility with older versions of C-XSC.
friend l_complex _l_complex(const cdotprecision &cd) noexcept
Definition: l_complex.hpp:113
l_complex(const complex &r) noexcept
Constructor of class l_complex.
Definition: l_complex.hpp:87
friend bool operator!=(const l_complex &, const l_complex &) noexcept
Implementation of standard negated equality operation.
Definition: l_complex.inl:226
The Multiple-Precision Data Type l_real.
Definition: l_real.hpp:78
The Scalar Type real.
Definition: real.hpp:114
The namespace cxsc, providing all functionality of the class library C-XSC.
Definition: cdot.cpp:29