TBCI Numerical high perf. C++ Library  2.8.0
builtin_cplx.h
Go to the documentation of this file.
1 
7 // Written: KG 98/08/13
8 // Changed: KG 98/08/14
9 // $Id: builtin_cplx.h,v 1.13.2.11 2019/05/28 11:13:02 garloff Exp $
10 
11 #include "tbci/constants.h"
12 //#include <iostream>
13 #include "tbci/basics.h"
14 
15 #ifndef HAVE_BUILTIN_CPLX
16 # include "tbci/cplx.h"
17 #else
18 
19 #ifndef TBCI_BUILTIN_CPLX_H
20 #define TBCI_BUILTIN_CPLX_H
21 
22 #ifndef CTYP
23 # define CTYP double
24 #endif
25 
27 
28 typedef CTYP cplx_base;
29 
30 #ifndef CPLX
31  typedef __complex__ CTYP cplx_t; //ALIGN(MIN_ALIGN2)
32 # define CPLX(t) cplx_t
33 # define CPLXR(t,a,r) cplx_t a (r)
34 # define CPLXRI(t,a,r,i) cplx_t a; __real__ a = (r); __imag__ a = (i)
35 # define CPLXC(t,a,c) cplx_t a (c)
36 # define CPLXVRI(t,r,m) ((cplx_t) ((r) + (m) * 1.0i))
37 //# define CPLXVRI(t,r,i) (hcplx(r, i))
38 #endif
39 
40 #define USE_BUILTIN_CPLX 1
41 
47 //template <typename CTYP>
48 class hcplx
49 {
50  protected:
51  cplx_t cx ALIGN(MIN_ALIGN2);
52 
53  public:
54  hcplx () {}
55  hcplx (const hcplx & c) : cx(c.cx) {}
56  //hcplx (const hcplx & c) { __real__ cx = __real__ c.cx; __imag__ cx = __imag__ c.cx; }
57  hcplx (const cplx_base r, const cplx_base i = 0) { __real__ cx = r; __imag__ cx = i; }
58  ~hcplx () {}
59 
60  // The most important ones: Provide type conversion
61  hcplx (const cplx_t& c) : cx(c) {}
62  //hcplx (const cplx_t& c) { __real__ cx = __real__ c; __imag__ cx = __imag__ c; }
63  //operator cplx_t () const { return cx; }
64  //operator cplx_t& () { return cx; }
65  operator const cplx_t& () const { return cx; }
66  //hcplx (const cplx_t & c) : cx(c) {}
67  //operator cplx_t & () const { return cx; }
68 
69  /*cplx_base*/
70  double fabssqr () const
71  { return (__real__ cx) * (__real__ cx) + (__imag__ cx) * (__imag__ cx); }
72  double fabs () const
73  { return MATH__ sqrt (fabssqr()); }
74  //friend double fabssqr (const hcplx& c) { return c.fabssqr(); }
75  //friend double fabs (const hcplx& c) { return c.fabs(); }
76 
77 #ifdef PREFER_REF
78  const cplx_base& real () const { return __real__ cx; }
79  const cplx_base& imag () const { return __imag__ cx; }
80 #else
81  cplx_base real () const { return __real__ cx; }
82  cplx_base imag () const { return __imag__ cx; }
83 #endif
84  cplx_base& real () { return __real__ cx; }
85  cplx_base& imag () { return __imag__ cx; }
86 
87  cplx_t operator - () const
88  { cplx_t r; __real__ r = - __real__ cx; __imag__ r = - __imag__ cx; return r; }
89 };
90 
91 //compat
92 inline double fabssqr (const cplx_t& a)
93 { return (__real__ a) * (__real__ a) + (__imag__ a) * (__imag__ a); }
94 
95 inline double fabssqr (const TBCI__ hcplx& c) { return c.fabssqr(); }
96 
97 #if 0
98 inline cplx_t operator - (const cplx_t& c)
99 { hcplx cx(c); return -cx; }
100 #endif
101 
104 inline double fabs (const TBCI__ cplx_t& a)
105 { return MATH__ sqrt (TBCI__ fabssqr (a)); }
106 
107 inline TBCI__ cplx_base abs (const TBCI__ cplx_t& a)
108 { return MATH__ sqrt ((__real__ a) * (__real__ a) + (__imag__ a) * (__imag__ a)); }
109 
110 inline double fabs (const TBCI__ hcplx& c)
111 { return c.fabs(); }
112 
114 
115 #if 0 //def USE_NS
116 inline double MATH__ fabs (const TBCI__ cplx_t& c)
117 { return TBCI__ fabs (c); }
118 
119 //inline T MATH__ abs (const TBCI__ cplx_t& c)
120 //{ return TBCI__ abs (c); }
121 #endif
122 
124 #ifdef PREFER_REF
125 inline const TBCI__ cplx_base& real (const TBCI__ hcplx& c) { return c.real(); }
126 inline const TBCI__ cplx_base& imag (const TBCI__ hcplx& c) { return c.imag(); }
127 #else
128 inline TBCI__ cplx_base real (const TBCI__ hcplx& c) { return c.real(); }
129 inline TBCI__ cplx_base imag (const TBCI__ hcplx& c) { return c.imag(); }
130 #endif
131 inline TBCI__ cplx_base& real (const TBCI__ cplx_t& c) { return __real__ c; }
132 inline TBCI__ cplx_base& imag (const TBCI__ cplx_t& c) { return __imag__ c; }
133 
134 inline TBCI__ cplx_t conj (const TBCI__ cplx_t& c)
135 { return ~c; }
136 
139 
140 inline cplx_t dot (const cplx_t a, const cplx_t b)
141 { return hcplx ((__real__ a) * (__real__ b) + (__imag__ a) * (__imag__ b),
142  (__imag__ a) * (__real__ b) - (__real__ a) * (__imag__ b)); }
143 
144 inline bool operator <= (const hcplx & a, const hcplx & b)
145 { return (fabssqr (a) <= fabssqr(b)); }
146 inline bool operator < (const hcplx & a, const hcplx & b)
147 { return (fabssqr (a) < fabssqr(b)); }
148 inline bool operator >= (const hcplx & a, const hcplx & b)
149 { return (fabssqr (a) >= fabssqr(b)); }
150 inline bool operator > (const hcplx & a, const hcplx & b)
151 { return (fabssqr (a) > fabssqr(b)); }
152 
153 inline bool operator <= (const hcplx & a, const cplx_t & b)
154 { return (fabssqr (a) <= fabssqr(b)); }
155 inline bool operator < (const hcplx & a, const cplx_t & b)
156 { return (fabssqr (a) < fabssqr(b)); }
157 inline bool operator >= (const hcplx & a, const cplx_t & b)
158 { return (fabssqr (a) >= fabssqr(b)); }
159 inline bool operator > (const hcplx & a, const cplx_t & b)
160 { return (fabssqr (a) > fabssqr(b)); }
161 
162 inline bool operator <= (const cplx_t & a, const hcplx & b)
163 { return (fabssqr (a) <= fabssqr(b)); }
164 inline bool operator < (const cplx_t & a, const hcplx & b)
165 { return (fabssqr (a) < fabssqr(b)); }
166 inline bool operator >= (const cplx_t & a, const hcplx & b)
167 { return (fabssqr (a) >= fabssqr(b)); }
168 inline bool operator > (const cplx_t & a, const hcplx & b)
169 { return (fabssqr (a) > fabssqr(b)); }
170 
171 #ifdef NEED_BCPLX_COMP_OP
172 // Does not compile ...
173 inline bool operator <= (const cplx_t & a, const cplx_t & b)
174 { return (fabssqr (a) <= fabssqr(b)); }
175 inline bool operator < (const cplx_t & a, const cplx_t & b)
176 { return (fabssqr (a) < fabssqr(b)); }
177 inline bool operator >= (const cplx_t & a, const cplx_t & b)
178 { return (fabssqr (a) >= fabssqr(b)); }
179 inline bool operator > (const cplx_t & a, const cplx_t & b)
180 { return (fabssqr (a) > fabssqr(b)); }
181 #endif
182 
183 inline STD__ ostream& operator << (STD__ ostream& os, const cplx_t & c)
184 { os << "( " << __real__ c;
185  if ((__imag__ c) < 0) return os << " - i " << - __imag__ c << " )";
186  else return os << " + i " << __imag__ c << " )";
187 }
188 
189 inline STD__ istream& operator >> (STD__ istream& in, cplx_t& c)
190 {
191  cplx_base r, i;
192  char s = '+', b = 'i', bra = '(';
193 
194  if (! (in >> bra))
195  return in;
196  if (bra != '(') in.putback(bra);
197  if (! (in >> r))
198  return in;
199  in >> s;
200  if ((s != '+') && (s != '-')) in.putback(s);
201  in >> b;
202  if ((b != 'i')) in.putback(b);
203  if (! (in >> i))
204  return in;
205  __real__ c = r;
206  if (s == '-') __imag__ c =- i;
207  else __imag__ c = i;
208  if (bra == '(')
209  {
210  if (! (in >> bra))
211  return in;
212  if (bra != ')') in.putback(bra);
213  }
214  return in;
215 }
216 
217 inline cplx_base polar (const cplx_t& c)
218 {
219  if (__real__ c == 0)
220  {
221  if (__imag__ c > 0) return 0.5*pi;
222  else if (__imag__ c < 0) return 1.5*pi;
223  else return 0;
224  }
225 
226  if (__imag__ c >= 0) return MATH__ atan2 (__imag__ c, __real__ c);
227  else return 2*pi + MATH__ atan2 (__imag__ c, __real__ c);
228 }
229 
230 inline cplx_t expi (const cplx_base phi)
231 {
232  cplx_base tmp = fmod (phi, pi2);
233  return cplx_t (MATH__ cos(tmp) + MATH__ sin(tmp) * 1.0i);
234 }
235 
236 inline cplx_t sqr (const cplx_t &c)
237 {
238  return fabssqr (c) * expi (fmod ((cplx_base)2 * polar(c), pi2));
239 }
240 
241 
242 #ifdef NEED_IMAG_UNIT
243 const cplx_t Imag_unit = CPLXVRI (double, 0.0, 1.0);
244 //const CPLXRI(double,Imag_unit,0.0,1.0);
245 #endif
246 
248 
249 #ifdef ENABLE_BCPLX
250 # include "obsolete/b_cplx_old.h"
251 #endif
252 
254 
255 inline TBCI__ cplx_t exp (const TBCI__ cplx_t& z)
256 {
257  return MATH__ exp (__real__ z) * TBCI__ expi (__imag__ z);
258 }
259 
260 inline TBCI__ cplx_t sqrt (const TBCI__ cplx_t& c)
261 {
262  return MATH__ sqrt (MATH__ fabs(c)) * TBCI__ expi ((TBCI__ cplx_base)0.5 * TBCI__ polar(c));
263 }
264 
266 #endif /* TBCI_BUILTIN_CPLX_H */
267 #endif /* __GNUC__ */
268 
bool operator>=(const CPLX__ complex< T > &a, const CPLX__ complex< T > &b)
Definition: std_cplx.h:113
cplx< T > operator-(const T a, const cplx< T > &b)
Definition: cplx.h:323
#define ALIGN(x)
Definition: basics.h:444
STD__ ostream & operator<<(STD__ ostream &ostr, const BdMatrix< T > &mat)
Definition: band_matrix.h:2712
STD__ istream & operator>>(STD__ istream &istr, BdMatrix< T > &mat)
Definition: band_matrix.h:2739
return c
Definition: f_matrix.h:760
TBCI__ cplx< T > cos(const TBCI__ cplx< T > &z)
Definition: cplx.h:786
double fabs(const int a)
Definition: basics.h:1215
#define NAMESPACE_TBCI
Definition: basics.h:317
double atan2(const double, const double)
#define MIN_ALIGN2
Definition: basics.h:424
bool operator<(const CPLX__ complex< T > &a, const CPLX__ complex< T > &b)
Definition: std_cplx.h:118
#define NAMESPACE_CSTD_END
Definition: basics.h:325
cplx< T > expi(const T phi)
Definition: cplx.h:460
const Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > & z
Definition: LM_fit.h:172
#define NAMESPACE_CPLX
Definition: basics.h:322
const double pi
Definition: constants.h:38
double sqrt(const int a)
Definition: basics.h:1216
cplx< T > sqr(const cplx< T > &c)
Definition: cplx.h:449
#define TBCI__
Definition: basics.h:332
TBCI__ cplx< T > sin(const TBCI__ cplx< T > &z)
Definition: cplx.h:776
const double pi2
Definition: constants.h:44
F_TMatrix< T > b
Definition: f_matrix.h:736
NAMESPACE_CSTD_END NAMESPACE_CPLX bool operator>(const CPLX__ complex< T > &a, const CPLX__ complex< T > &b)
defined(NO_NS)
Definition: std_cplx.h:108
TBCI__ cplx< T > exp(const TBCI__ cplx< T > &z)
Definition: cplx.h:756
int conj(const int arg)
conj for elementary types
Definition: basics.h:1055
#define NAMESPACE_CSTD
Definition: basics.h:319
T dot(const T &a1, const T &a2)
Definition: basics.h:1183
int i
Definition: LM_fit.h:71
#define real
#define CPLXVRI(t, r, i)
Definition: cplx.h:35
#define NAMESPACE_CPLX_END
Definition: basics.h:326
#define STD__
Definition: basics.h:338
#define abs(x)
Definition: f2c.h:178
#define NAMESPACE_END
Definition: basics.h:323
double fabssqr(const double a)
Definition: basics.h:1157
const unsigned TMatrix< T > const Matrix< T > * a
bool operator<=(const CPLX__ complex< T > &a, const CPLX__ complex< T > &b)
Definition: std_cplx.h:123
#define MATH__
Definition: basics.h:339
int imag(const int d)
Definition: basics.h:1068
NAMESPACE_CPLX_END NAMESPACE_TBCI TBCI__ cplx< T > polar(const T &r, const T &p)
Definition: cplx.h:706