TBCI Numerical high perf. C++ Library 2.8.0
builtin_cplx.h
Go to the documentation of this file.
1
6
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
28typedef 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
46
47//template <typename CTYP>
48class 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
92inline double fabssqr (const cplx_t& a)
93{ return (__real__ a) * (__real__ a) + (__imag__ a) * (__imag__ a); }
94
95inline double fabssqr (const TBCI__ hcplx& c) { return c.fabssqr(); }
96
97#if 0
98inline cplx_t operator - (const cplx_t& c)
99{ hcplx cx(c); return -cx; }
100#endif
101
104inline double fabs (const TBCI__ cplx_t& a)
105{ return MATH__ sqrt (TBCI__ fabssqr (a)); }
106
107inline TBCI__ cplx_base abs (const TBCI__ cplx_t& a)
108{ return MATH__ sqrt ((__real__ a) * (__real__ a) + (__imag__ a) * (__imag__ a)); }
109
110inline double fabs (const TBCI__ hcplx& c)
111{ return c.fabs(); }
112
114
115#if 0 //def USE_NS
116inline 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
125inline const TBCI__ cplx_base& real (const TBCI__ hcplx& c) { return c.real(); }
126inline const TBCI__ cplx_base& imag (const TBCI__ hcplx& c) { return c.imag(); }
127#else
128inline TBCI__ cplx_base real (const TBCI__ hcplx& c) { return c.real(); }
129inline TBCI__ cplx_base imag (const TBCI__ hcplx& c) { return c.imag(); }
130#endif
131inline TBCI__ cplx_base& real (const TBCI__ cplx_t& c) { return __real__ c; }
132inline TBCI__ cplx_base& imag (const TBCI__ cplx_t& c) { return __imag__ c; }
133
134inline TBCI__ cplx_t conj (const TBCI__ cplx_t& c)
135{ return ~c; }
136
139
140inline 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
144inline bool operator <= (const hcplx & a, const hcplx & b)
145{ return (fabssqr (a) <= fabssqr(b)); }
146inline bool operator < (const hcplx & a, const hcplx & b)
147{ return (fabssqr (a) < fabssqr(b)); }
148inline bool operator >= (const hcplx & a, const hcplx & b)
149{ return (fabssqr (a) >= fabssqr(b)); }
150inline bool operator > (const hcplx & a, const hcplx & b)
151{ return (fabssqr (a) > fabssqr(b)); }
152
153inline bool operator <= (const hcplx & a, const cplx_t & b)
154{ return (fabssqr (a) <= fabssqr(b)); }
155inline bool operator < (const hcplx & a, const cplx_t & b)
156{ return (fabssqr (a) < fabssqr(b)); }
157inline bool operator >= (const hcplx & a, const cplx_t & b)
158{ return (fabssqr (a) >= fabssqr(b)); }
159inline bool operator > (const hcplx & a, const cplx_t & b)
160{ return (fabssqr (a) > fabssqr(b)); }
161
162inline bool operator <= (const cplx_t & a, const hcplx & b)
163{ return (fabssqr (a) <= fabssqr(b)); }
164inline bool operator < (const cplx_t & a, const hcplx & b)
165{ return (fabssqr (a) < fabssqr(b)); }
166inline bool operator >= (const cplx_t & a, const hcplx & b)
167{ return (fabssqr (a) >= fabssqr(b)); }
168inline 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 ...
173inline bool operator <= (const cplx_t & a, const cplx_t & b)
174{ return (fabssqr (a) <= fabssqr(b)); }
175inline bool operator < (const cplx_t & a, const cplx_t & b)
176{ return (fabssqr (a) < fabssqr(b)); }
177inline bool operator >= (const cplx_t & a, const cplx_t & b)
178{ return (fabssqr (a) >= fabssqr(b)); }
179inline bool operator > (const cplx_t & a, const cplx_t & b)
180{ return (fabssqr (a) > fabssqr(b)); }
181#endif
182
183inline 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
189inline 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
217inline 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
230inline 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
236inline 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
243const 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
255inline TBCI__ cplx_t exp (const TBCI__ cplx_t& z)
256{
257 return MATH__ exp (__real__ z) * TBCI__ expi (__imag__ z);
258}
259
260inline 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
int i
Definition LM_fit.h:71
STD__ istream & operator>>(STD__ istream &istr, BdMatrix< T > &mat)
STD__ ostream & operator<<(STD__ ostream &ostr, const BdMatrix< T > &mat)
#define STD__
Definition basics.h:338
#define NAMESPACE_CSTD
Definition basics.h:319
#define MIN_ALIGN2
Definition basics.h:424
#define NAMESPACE_CPLX_END
Definition basics.h:326
#define NAMESPACE_END
Definition basics.h:323
#define NAMESPACE_CSTD_END
Definition basics.h:325
#define NAMESPACE_TBCI
Definition basics.h:317
#define TBCI__
Definition basics.h:332
#define MATH__
Definition basics.h:339
#define ALIGN(x)
Definition basics.h:444
#define NAMESPACE_CPLX
Definition basics.h:322
const double pi2
Definition constants.h:44
const double pi
Definition constants.h:38
T imag(const TBCI__ cplx< T > &z)
Definition cplx.h:674
double fabs(const TBCI__ cplx< T > &c)
Definition cplx.h:746
cplx< T > expi(const T phi)
Definition cplx.h:460
cplx< T > dot(const cplx< T > &a, const cplx< T > &b)
Definition cplx.h:300
TBCI__ cplx< T > exp(const TBCI__ cplx< T > &z)
Definition cplx.h:756
double fabssqr(const cplx< T > &c)
Definition cplx.h:390
cplx< T > sqr(const cplx< T > &c)
Definition cplx.h:449
NAMESPACE_END NAMESPACE_CPLX TBCI__ cplx< T > conj(const TBCI__ cplx< T > &c)
Definition cplx.h:663
#define CPLXVRI(t, r, i)
Definition cplx.h:35
TBCI__ cplx< T > sin(const TBCI__ cplx< T > &z)
Definition cplx.h:776
NAMESPACE_END NAMESPACE_CSTD TBCI__ cplx< T > sqrt(const TBCI__ cplx< T > &z)
Definition cplx.h:751
TBCI__ cplx< T > cos(const TBCI__ cplx< T > &z)
Definition cplx.h:786
cplx< T > operator-(const T a, const cplx< T > &b)
Definition cplx.h:323
NAMESPACE_CPLX_END NAMESPACE_TBCI TBCI__ cplx< T > polar(const T &r, const T &p)
Definition cplx.h:706
#define abs(x)
Definition f2c.h:178
return c
Definition f_matrix.h:760
F_TMatrix< T > b
Definition f_matrix.h:736
const unsigned TMatrix< T > const Matrix< T > * a
#define real
double atan2(const double, const double)
bool operator<(const CPLX__ complex< T > &a, const CPLX__ complex< T > &b)
Definition std_cplx.h:118
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
bool operator<=(const CPLX__ complex< T > &a, const CPLX__ complex< T > &b)
Definition std_cplx.h:123
bool operator>=(const CPLX__ complex< T > &a, const CPLX__ complex< T > &b)
Definition std_cplx.h:113