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) throw() {}
58  l_complex(const l_real& a, const l_real& b) throw() : re(a), im(b) { }
60  l_complex(const real& a, const real& b) throw() : re(a), im(b) { }
61 
63  l_complex & operator = (const l_real & lr) throw()
64  { re = lr; im = 0; return *this; }
66  l_complex & operator = (const real & r) throw()
67  { re = r; im = 0; return *this; }
69  l_complex & operator = (const complex & c) throw()
70  { re = Re(c); im = Im(c); return *this; }
72  l_complex & operator = (const dotprecision & d) throw()
73  { re = d; im = 0.0; return *this; }
75  l_complex & operator = (const cdotprecision & cd) throw()
76  { re = Re(_l_complex(cd)); im = Im(_l_complex(cd)); return *this; }
77 
79  l_complex & operator = (const lx_complex &) throw();
80 
81  // ------------- Type-Casts ----------------------------------------------
83  explicit inline l_complex(const l_real &r) throw() : re(r), im(0.0) { }
85  explicit inline l_complex(const real &r) throw() : re(r), im(0.0) { }
87  explicit inline l_complex(const complex &r) throw() : re(Re(r)), im(Im(r))
88  { }
90  explicit inline l_complex(const dotprecision &d) throw()
91  : re(d), im(0.0) { }
93  explicit inline l_complex(const cdotprecision &cd) throw()
94  : re(Re(_l_complex(cd))), im(Im(_l_complex(cd))) { }
95 
96 // friend inline l_complex _l_complex(const l_real &a) throw()
97 // { return l_complex(a); }
98 // friend inline l_complex _l_complex(const l_real &a, const l_real &b)
99 // throw() { return l_complex(a,b); }
100 // friend inline l_complex _l_complex(const real &a) throw()
101 // { return l_complex(a); }
102 // friend inline l_complex _l_complex(const real &a, const real &b)
103 // throw() { return l_complex(a,b); }
104 // friend inline l_complex _l_complex(const complex &c)
105 // throw() { return l_complex(c); }
106 // friend inline l_complex _l_complex(const dotprecision &d)
107 // throw() { return l_complex(d); }
113  friend inline l_complex _l_complex(const cdotprecision &cd)
114  throw() { return l_complex(cd); }
115 
116  // ----------------------------------------------------------------------
118  friend int StagPrec(const l_complex&) throw();
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 &) throw();
134 
136  friend inline l_complex operator -
137  (const l_complex &, const l_complex &) throw();
138 
139  // ----------------- l_complex + complex --------------------------------
141  inline l_complex operator + (const complex &) const throw();
142 
144  friend inline l_complex operator +
145  (const complex &, const l_complex &) throw();
146 
147  // ---------------- l_complex + real ------------------------------------
149  inline l_complex operator + (const real &) const throw();
150 
152  friend inline l_complex operator +
153  (const real &, const l_complex &) throw();
154 
155  // ---------------- l_complex + l_real ----------------------------------
157  inline l_complex operator + (const l_real &) const throw();
158 
160  friend inline l_complex operator +
161  (const l_real &, const l_complex &) throw();
162 
163  // ---------------- l_complex - l_real ----------------------------------
165  inline l_complex operator - (const l_real &) const throw();
166 
168  friend inline l_complex operator -
169  (const l_real &, const l_complex &) throw();
170 
171  // ----------------- l_complex - complex -------------------------------
173  inline l_complex operator - (const complex &) const throw();
174 
176  friend inline l_complex operator -
177  (const complex &, const l_complex &) throw();
178 
179  // ---------------- l_complex - real ------------------------------------
181  inline l_complex operator - (const real &) const throw();
182 
184  friend inline l_complex operator -
185  (const real &, const l_complex &) throw();
186 
187  // ---------------- l_complex + cdotprecision ---------------------------
189  friend cdotprecision operator+
190  (const l_complex &, const cdotprecision &) throw();
191 
193  friend cdotprecision operator+
194  (const cdotprecision &, const l_complex &) throw();
195 
196  // ---------------- l_complex - cdotprecision ---------------------------
198  friend cdotprecision operator-
199  (const l_complex &, const cdotprecision &) throw();
200 
202  friend cdotprecision operator-
203  (const cdotprecision &, const l_complex &) throw();
204 
205  // ---------------- l_complex + dotprecision ----------------------------
207  friend cdotprecision operator+
208  (const l_complex &, const dotprecision &) throw();
209 
211  friend cdotprecision operator+
212  (const dotprecision &, const l_complex &) throw();
213 
214  // ---------------- l_complex - dotprecision ----------------------------
216  friend cdotprecision operator-
217  (const l_complex &, const dotprecision &) throw();
218 
220  friend cdotprecision operator-
221  (const dotprecision &, const l_complex &) throw();
222 
223 
224 // ------------- Multiplication ---------------------------------------------
225 // ------------------ l_complex * l_complex ---------------------------------
226 
228 friend l_complex operator * (const l_complex& a, const l_complex& b)
229  throw();
230 
231 // ------------------ l_complex * complex -----------------------------------
233 friend l_complex operator * (const l_complex& a, const complex& b)
234  throw();
235 
237 friend l_complex operator * ( const complex& b, const l_complex& a )
238  throw();
239 
240 // ------------------ l_complex * real ---------------------------------------
242  inline l_complex operator * (const real &) const throw();
243 
245  friend inline l_complex operator *
246  (const real &, const l_complex &) throw();
247 
248 // ------------------ l_complex * l_real -------------------------------------
250  inline l_complex operator * (const l_real &) const throw();
251 
253  friend inline l_complex operator *
254  (const l_real &, const l_complex &) throw();
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&) throw(); // 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&) throw();
283 
285  friend void accumulate(cdotprecision&, const l_complex&,
286  const complex&) throw();
287 
289  friend void accumulate(cdotprecision&, const l_complex&,
290  const real&) throw();
291 
293  friend void accumulate(cdotprecision&, const l_complex&,
294  const l_real&) throw();
295 
296 // ---------------- cdotprecision +(-)= l_complex ----------------------------
298 friend inline cdotprecision & operator += (cdotprecision &cd,
299  const l_complex &lc) throw();
301 friend inline cdotprecision & operator -= (cdotprecision &cd,
302  const l_complex &lc) throw();
303 
304 // ---------------- l_complex +(-)= l_complex|complex|real|l_real ------------
306 friend inline l_complex & operator += (l_complex &,const l_complex &) throw();
308 friend inline l_complex & operator -= (l_complex &,const l_complex &) throw();
310 friend inline l_complex & operator += (l_complex &,const complex &) throw();
312 friend inline l_complex & operator -= (l_complex &,const complex &) throw();
314 friend inline l_complex & operator += (l_complex &,const real &) throw();
316 friend inline l_complex & operator -= (l_complex &,const real &) throw();
318 friend inline l_complex & operator += (l_complex &,const l_real &) throw();
320 friend inline l_complex & operator -= (l_complex &,const l_real &) throw();
321 
322 // ---------------- l_complex *= l_complex|complex|real|l_real ---------------
324 friend inline l_complex & operator *= (l_complex &,const l_complex &) throw();
326 friend inline l_complex & operator *= (l_complex &,const complex &) throw();
328 friend inline l_complex & operator *= (l_complex &,const real &) throw();
330 friend inline l_complex & operator *= (l_complex &,const l_real &) throw();
331 
332 // ---------------- Compare Operators ----------------------------------------
333 friend inline bool operator! (const l_complex &) throw();
335 friend inline bool operator== (const l_complex &, const l_complex &) throw();
337 friend inline bool operator!= (const l_complex &, const l_complex &) throw();
339 friend inline bool operator== (const l_complex &, const complex &) throw();
341 friend inline bool operator== (const complex &, const l_complex &) throw();
343 friend inline bool operator!= (const l_complex &, const complex &) throw();
345 friend inline bool operator!= (const complex &, const l_complex &) throw();
347 friend inline bool operator== (const l_complex &, const real &) throw();
349 friend inline bool operator== (const real &, const l_complex &) throw();
351 friend inline bool operator!= (const l_complex &, const real &) throw();
353 friend inline bool operator!= (const real &, const l_complex &) throw();
355 friend inline bool operator== (const l_complex &, const l_real &) throw();
357 friend inline bool operator== (const l_real &, const l_complex &) throw();
359 friend inline bool operator!= (const l_complex &, const l_real &) throw();
361 friend inline bool operator!= (const l_real &, const l_complex &) throw();
363 friend inline bool operator== (const l_complex &, const dotprecision &)
364  throw();
366 friend inline bool operator== (const dotprecision &, const l_complex &)
367  throw();
369 friend inline bool operator!= (const l_complex &, const dotprecision &)
370  throw();
372 friend inline bool operator!= (const dotprecision &, const l_complex &)
373  throw();
374 
376 friend inline bool operator ==(const cdotprecision &, const l_complex &)
377  throw(); // {l_complex.inl}
379 friend inline bool operator ==(const l_complex &, const cdotprecision &)
380  throw(); // {l_complex.inl}
382 friend inline bool operator !=(const cdotprecision &, const l_complex &)
383  throw(); // {l_complex.inl}
385 friend inline bool operator !=(const l_complex &, const cdotprecision &)
386  throw(); // {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 &) throw();
400 
402  friend inline l_complex operator / (const l_complex & a,
403  const complex & b) throw();
405  friend inline l_complex operator / (const l_complex & a,
406  const l_real & b) throw();
408  friend inline l_complex operator / (const l_complex & a,
409  const real & b) throw();
410 
412  friend inline l_complex operator / (const complex& a,
413  const l_complex& b) throw();
415  friend inline l_complex operator / (const real& a,
416  const l_complex& b) throw();
418  friend inline l_complex operator / (const l_real& a,
419  const l_complex& b) throw();
420 
422  friend inline l_complex& operator /=(l_complex&,const l_complex&) throw();
424  friend inline l_complex& operator /=(l_complex&,const complex&) throw();
426  friend inline l_complex& operator /=(l_complex&,const real&) throw();
428  friend inline l_complex& operator /=(l_complex&,const l_real&) throw();
429 
431 friend l_real abs2(const l_complex &a) throw(); // a.re*a.re + a.im*a.im;
433 friend l_real abs (const l_complex &z) throw();
434 
435 // ----------------------- Output --------------------------------------------
436 
438 friend std::ostream& operator << (std::ostream& s,const l_complex& z ) throw()
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) throw()
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) throw()
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) throw()
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) throw()
512  { return l_complex(a); }
513 inline l_complex _l_complex(const l_real &a, const l_real &b)
514  throw() { return l_complex(a,b); }
515 inline l_complex _l_complex(const real &a) throw()
516  { return l_complex(a); }
517 inline l_complex _l_complex(const real &a, const real &b)
518  throw() { return l_complex(a,b); }
519 inline l_complex _l_complex(const complex &c)
520  throw() { return l_complex(c); }
521 inline l_complex _l_complex(const dotprecision &d)
522  throw() { return l_complex(d); }
523 inline l_complex conj(const l_complex&) throw();
524 //inline l_complex _l_complex(const cdotprecision &cd)
525 // throw() { 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 &) throw();
540 
541 } // end namespace cxsc
542 
543 #include "l_complex.inl"
544 
545 #endif // _CXSC_L_COMPLEX_HPP_INCLUDED
friend bool operator!=(const l_complex &, const l_complex &)
Implementation of standard negated equality operation.
Definition: l_complex.inl:226
l_complex(const real &a, const real &b)
Constructor of class l_complex.
Definition: l_complex.hpp:60
The Multiple-Precision Data Type l_real.
Definition: l_real.hpp:77
The Data Type dotprecision.
Definition: dot.hpp:111
friend cdotprecision & operator-=(cdotprecision &cd, const l_complex &lc)
Implementation of standard algebraic subtraction and allocation operation.
Definition: cdot.inl:255
friend l_real & Im(l_complex &a)
Returns the imaginary part of the complex value.
Definition: l_complex.cpp:531
l_complex(const l_real &a, const l_real &b)
Constructor of class l_complex.
Definition: l_complex.hpp:58
The namespace cxsc, providing all functionality of the class library C-XSC.
Definition: cdot.cpp:29
l_complex(const dotprecision &d)
Constructor of class l_complex.
Definition: l_complex.hpp:90
l_complex(const complex &r)
Constructor of class l_complex.
Definition: l_complex.hpp:87
friend l_complex & operator*=(l_complex &, const l_complex &)
Implementation of standard algebraic multiplication and allocation operation.
Definition: l_complex.inl:162
friend l_complex operator/(const l_complex &, const l_complex &)
Implementation of standard algebraic division operation.
Definition: l_complex.cpp:463
friend bool operator==(const l_complex &, const l_complex &)
Implementation of standard equality operation.
Definition: l_complex.inl:224
friend l_complex conj(const l_complex &)
Returns the conjugated complex value.
Definition: l_complex.inl:270
l_complex(const real &r)
Constructor of class l_complex.
Definition: l_complex.hpp:85
friend int StagPrec(const l_complex &)
Returns the precision of the long datatype value.
Definition: l_complex.cpp:468
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
The Data Type cdotprecision.
Definition: cdot.hpp:60
friend l_real abs(const l_complex &z)
The absolute value of a l_complex value.
Definition: l_complex.cpp:513
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 _l_complex(const cdotprecision &)
Deprecated typecast, which only exist for the reason of compatibility with older versions of C-XSC...
friend l_complex & operator/=(l_complex &, const l_complex &)
Implementation of standard algebraic division and allocation operation.
Definition: l_complex.inl:209
The Multiple-Precision Data Type l_complex.
Definition: l_complex.hpp:45
The Scalar Type complex.
Definition: complex.hpp:49
friend l_real & Re(l_complex &a)
Returns the real part of the complex value.
Definition: l_complex.cpp:529
l_complex & operator=(const l_real &lr)
Implementation of standard assigning operator.
Definition: l_complex.hpp:63
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)
Implementation of standard algebraic addition and allocation operation.
Definition: cdot.inl:251
l_complex(const cdotprecision &cd)
Constructor of class l_complex.
Definition: l_complex.hpp:93
friend void accumulate(cdotprecision &, const l_complex &, const l_complex &)
The accurate scalar product of the last two arguments added to the value of the first argument...
Definition: l_complex.cpp:473
friend l_complex operator*(const l_complex &a, const l_complex &b)
Implementation of standard algebraic multiplication operation.
Definition: l_complex.cpp:86
l_complex(void)
Constructor of class l_complex.
Definition: l_complex.hpp:56
friend l_real abs2(const l_complex &a)
The absolute value of a l_complex value.
Definition: l_complex.cpp:505
friend std::ostream & operator<<(std::ostream &s, const l_complex &z)
Implementation of standard output method.
Definition: l_complex.hpp:438
friend l_complex & SetRe(l_complex &a, const l_real &b)
Sets the real part of a complex value.
Definition: l_complex.cpp:526
The Scalar Type real.
Definition: real.hpp:113
friend std::istream & operator>>(std::istream &s, l_complex &a)
Implementation of standard input method.
Definition: l_complex.hpp:464
l_complex(const l_real &r)
Constructor of class l_complex.
Definition: l_complex.hpp:83
friend l_complex operator-(const l_complex &)
Implementation of standard algebraic negative sign operation.
Definition: l_complex.cpp:31