TBCI Numerical high perf. C++ Library  2.8.0
std_cplx.h
Go to the documentation of this file.
1 
7 // Written: KG 97/11/30
8 // Changed: KG 97/11/30
9 // $Id: std_cplx.h,v 1.5.2.29 2019/05/28 11:13:02 garloff Exp $
10 
11 #ifndef TBCI_STD_CPLX_H
12 #define TBCI_STD_CPLX_H
13 
14 #include "tbci/basics.h"
15 #ifndef HAVE_NO_NEW_HEADERS_BUG
16 # include <complex>
17 #else
18 # include <complex.h>
19 #endif
20 
21 //#define cplx complex
22 #ifndef CPLX
23 # define CPLX(t) CPLX__ complex<t>
24 # define CPLXR(t,a,r) CPLX__ complex<t> a (r)
25 # define CPLXRI(t,a,r,i) CPLX__ complex<t> a (r,i)
26 # define CPLXC(t,a,c) CPLX__ complex<t> a (c)
27 # define CPLXVRI(t,r,i) (CPLX__ complex<t>(r,i))
28 #endif
29 
30 //typedef typename T complex<T>::cplx_base;
31 //typedef complex<T> complex<T>::cplx_t;
32 
34 // compat
35 #ifndef TEMPLATED_FABSSQR // We do need it as the fabssqr() in basics.h is templated!
36 INST(template <typename T> class CPLX__ complex friend double fabssqr (const CPLX__ complex<T>&);)
37 template <typename T>
38 inline double fabssqr (const CPLX__ complex<T>& a)
39 { return (a.real()*a.real() + a.imag()*a.imag()); }
40 #endif
42 
43 #ifndef HAVE_STDCPLX_FABS
45 INST(template <typename T> class CPLX__ complex friend double fabs (const CPLX__ complex<T>&);)
46 template <typename T>
47 inline double fabs (const CPLX__ complex<T>& a)
48 //{ return abs(a); }
49 { return GLBL__ MATH__ sqrt (TBCI__ fabssqr (a)); }
51 #endif
52 
53 #if 0
54 template <typename T>
55 inline bool operator == (const CPLX__ complex<T>&a, const T d)
56 { return (a.imag() == 0 && a.real() == d); }
57 #endif
58 
59 #if 0
60 NAMESPACE_CPLX
61 
62 INST(template <typename T> class CPLX__ complex friend T real (const CPLX__ complex<T>&);)
63 template <typename T>
64 inline T real (const CPLX__ complex<T>& z)
65 { return CPLX__ real(z); }
66 
67 INST(template <typename T> class CPLX__ complex friend T imag (const CPLX__ complex<T>&);)
68 template <typename T>
69 inline T imag (const CPLX__ complex<T>& z)
70 { return CPLX__ imag(z); }
71 
72 INST(template <typename T> class CPLX__ complex friend CPLX__ complex<T> conj (const CPLX__ complex<T>&);)
73 template <typename T>
74 inline CPLX__ complex<T> conj (const CPLX__ complex<T>& z)
75 { return CPLX__ complex<T> (z.real(), -z.imag()); }
76 
78 #endif
79 
80 #if !defined(NO_NS) && !defined(HAVE_IGNORE_STD_NS_BUG) && defined(HAVE_LIBC_GLOBAL_NS_BUG) && (!defined(HAVE_CPP_GLOBAL_NS_BUG) || !defined(HAVE_CPLX_GLOBAL_NS_BUG))
81 # define NEED_CPLX_TO_CSTD 1
82 #endif
83 
84 #ifdef NEED_CPLX_TO_CSTD
85 //#if !defined(NO_NS) && defined(HAVE_LIBC_GLOBAL_NS_BUG) && (!defined(HAVE_CPP_GLOBAL_NS_BUG) || !defined(HAVE_CPLX_GLOBAL_NS_BUG))
87 template <typename T>
88 inline CPLX__ complex<T> sqrt (const CPLX__ complex<T>& z)
89 { return CPLX__ sqrt (z); }
90 // exp seems to be needed in :: by DEC CXX. Does it break on MSVC?
91 //#if defined(HAVE_CPP_GLOBAL_NS_BUG)
92 #ifndef _MSC_VER
93 template <typename T>
94 inline CPLX__ complex<T> exp (const CPLX__ complex<T>& z)
95 { return CPLX__ exp (z); }
96 #endif
98 //#endif
99 #endif
100 
102 // Mathematically thought, the following is nonsense ...
103 INST(template <typename T> class CPLX__ complex friend bool operator > (const CPLX__ complex<T>&, const CPLX__ complex<T>&);)
107 template <typename T>
108 inline bool operator > (const CPLX__ complex<T>& a, const CPLX__ complex<T>& b)
109 { return (MATH__ fabs(a) > MATH__ fabs(b)); }
110 
111 INST(template <typename T> class CPLX__ complex friend bool operator >= (const CPLX__ complex<T>&, const CPLX__ complex<T>&);)
112 template <typename T>
113 inline bool operator >= (const CPLX__ complex<T>& a, const CPLX__ complex<T>& b)
114 { return (MATH__ fabs(a) >= MATH__ fabs(b)); }
115 
116 INST(template <typename T> class CPLX__ complex friend bool operator < (const CPLX__ complex<T>&, const CPLX__ complex<T>&);)
117 template <typename T>
118 inline bool operator < (const CPLX__ complex<T>& a, const CPLX__ complex<T>& b)
119 { return (MATH__ fabs(a) < MATH__ fabs(b)); }
120 
121 INST(template <typename T> class CPLX__ complex friend bool operator <= (const CPLX__ complex<T>&, const CPLX__ complex<T>&);)
122 template <typename T>
123 inline bool operator <= (const CPLX__ complex<T>& a, const CPLX__ complex<T>& b)
124 { return (MATH__ fabs(a) <= MATH__ fabs(b)); }
125 
126 
127 /* From and for PLASIMO ... */
128 /* We dont't need all those CPLX__ namespace specifications, as we are
129  * inside NAMESPACE_STD */
130 #ifdef HAVE_MISS_STDCPLX_FANCY_FN_BUG
131 INST(template <typename T> class CPLX__ complex friend CPLX__ complex<T> acos (const CPLX__ complex<T>&);)
132 template <typename T>
133 inline CPLX__ complex<T> acos (const CPLX__ complex<T> &x)
134 {
135  return CPLX__ complex<T>(0,-1)*log(x+sqrt(x*x-CPLX__ complex<T>(1,0)));
136 }
137 
138 INST(template <typename T> class CPLX__ complex friend CPLX__ complex<T> asin (const CPLX__ complex<T> &);)
139 template <typename T>
140 inline CPLX__ complex<T> asin (const CPLX__ complex<T> &x)
141 {
142  return CPLX__ complex<T>(0,-1)*log(CPLX__ complex<T>(0,1)*x+sqrt(CPLX__ complex<T>(1,0)-x*x));
143 }
144 
145 INST(template <typename T> class CPLX__ complex friend CPLX__ complex<T> atan (const CPLX__ complex<T> &);)
146 template <typename T>
147 inline CPLX__ complex<T> atan (const CPLX__ complex<T> &x)
148 {
149  return acos( 1.0/sqrt(x*x+CPLX__ complex<T>(1,0)) );
150 }
151 
152 INST(template <typename T> class CPLX__ complex friend CPLX__ complex<T> atanh (const CPLX__ complex<T> &);)
153 template <typename T>
154 inline CPLX__ complex<T> atanh (const CPLX__ complex<T> &x)
155 {
156  CPLX__ complex<T> arg = (1.0 + x) / (1.0 - x);
157  return 0.5 * MATH__ log(arg);
158 }
159 
160 #endif /* MISS_STDCPLX_FANCY_FN */
161 #ifdef MISS_STDCPLX_TAN_BUG
162 INST(template <typename T> class CPLX__ complex friend CPLX__ complex<T> tan(const CPLX__ complex<T> &);)
163 template <typename T>
164 inline CPLX__ complex<T> tan(const CPLX__ complex<T> &x)
165 {
166  return sin(x)/cos(x);
167 }
168 
169 INST(template <typename T> class CPLX__ complex friend CPLX__ complex<T> tanh(const CPLX__ complex<T> &);)
170 template <typename T>
171 inline CPLX__ complex<T> tanh(const CPLX__ complex<T> &x)
172 {
173  return sinh(x)/cosh(x);
174 }
175 #endif
176 #ifdef HAVE_MISS_STDCPLX_LOG10_BUG
177 INST(template <typename T> class CPLX__ complex friend CPLX__ complex<T> log10(const CPLX__ complex<T> &);)
178 template <typename T>
179 inline CPLX__ complex<T> log10(const CPLX__ complex<T> &x)
180 {
181  const static double InvLn10(1.0/GLBL__ MATH__ log(10));
182  return log(x)*InvLn10;
183 }
184 #endif /* MISS_STDCPLX_LOG10 */
185 
187 
189 #ifdef NEED_IMAG_UNIT
190 const CPLXRI(double,Imag_unit,0.0,1.0);
191 #endif
193 
194 #ifdef HAVE_MISS_CPLX_IO_BUG
195 //INST(template <typename T> class CPLX__ complex friend STD__ ostream operator << (STD__ ostream&, const CPLX__ complex<T>&);)
196 template <typename T>
197 STD__ ostream& operator << (STD__ ostream& os, const CPLX__ complex<T>& v)
198 {
199  return os << "(" << v.real() << "," << v.imag() << ")";
200 }
201 
202 //INST(template <typename T> class CPLX__ complex friend STD__ istream operator >> (STD__ istream&, CPLX__ complex<T>&);)
203 template <typename T>
204 STD__ istream& operator >> (STD__ istream& is, CPLX__ complex<T>& v)
205 {
206  char bra; T re, im = 0;
207  is >> bra;
208  is >> re;
209  if (bra != '(') {
210  v = CPLX__ complex<T> (re, im);
211  return is;
212  }
213  is >> bra;
214  if (bra != ',') {
215  if (bra != ')')
216  STD__ cerr << "Error parsing complex!" << STD__ endl;
217  v = CPLX__ complex<T> (re, im);
218  return is;
219  }
220  is >> im;
221  is >> bra;
222  if (bra != ')')
223  STD__ cerr << "Error parsing complex!" << STD__ endl;
224  v = CPLX__ complex<T> (re, im);
225  return is;
226 }
227 #endif
228 
230 #ifdef MALLOC_CACHE
232 typedef CPLX__ complex<double>* stdcomplexdoubleptr;
233 SPECIALIZE_MEMALLOC(stdcomplexdoubleptr)
234 SPECIALIZE_MEMALLOC2(CPLX__ complex<float>,stdcomplex_float)
235 typedef CPLX__ complex<float>* stdcomplexfloatptr;
236 SPECIALIZE_MEMALLOC(stdcomplexfloatptr)
237 #endif
238 
239 /* If used in loops, a single copy is still cheaper than pointer
240  * indirection */
242 SPEC_TBCI_TRAITS_LOOP_COPY(CPLX__ complex<double>);
244 SPEC_TBCI_TRAITS_ALWAYS_COPY(CPLX__ complex<double>*);
245 
247 
248 #endif /* TBCI_STD_CPLX_H */
#define SPECIALIZE_MEMALLOC(TYPE)
Definition: malloc_cache.h:578
TBCI__ cplx< T > asin(const TBCI__ cplx< T > &z)
Definition: cplx.h:806
const Vector< T > const Vector< T > & x
Definition: LM_fit.h:97
TBCI__ cplx< T > tan(const TBCI__ cplx< T > &z)
Definition: cplx.h:796
STD__ istream & operator>>(STD__ istream &istr, BdMatrix< T > &mat)
Definition: band_matrix.h:2739
TBCI__ cplx< T > log(const TBCI__ cplx< T > &z)
Definition: cplx.h:771
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
#define SPEC_TBCI_TRAITS_ALWAYS_COPY(TYPE)
Definition: tbci_traits.h:56
TBCI__ cplx< T > atan(const TBCI__ cplx< T > &z)
Definition: cplx.h:826
#define NAMESPACE_CSTD_END
Definition: basics.h:325
#define CPLXRI(t, a, r, i)
Definition: std_cplx.h:25
#define SPEC_TBCI_TRAITS_LOOP_COPY(TYPE)
Definition: tbci_traits.h:71
TBCI__ cplx< T > atanh(const TBCI__ cplx< T > &z)
Definition: cplx.h:831
const Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > & z
Definition: LM_fit.h:172
TBCI__ cplx< T > log10(const TBCI__ cplx< T > &z)
Definition: cplx.h:836
T arg(const TBCI__ cplx< T > &c)
Definition: cplx.h:690
TBCI__ cplx< T > tanh(const TBCI__ cplx< T > &z)
Definition: cplx.h:801
#define NAMESPACE_CPLX
Definition: basics.h:322
#define SPECIALIZE_MEMALLOC2(TYPE, SHTP)
Definition: malloc_cache.h:587
double sqrt(const int a)
Definition: basics.h:1216
#define TBCI__
Definition: basics.h:332
TBCI__ cplx< T > sin(const TBCI__ cplx< T > &z)
Definition: cplx.h:776
F_TMatrix< T > b
Definition: f_matrix.h:736
#define GLBL__
Definition: basics.h:342
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
#define CPLX__
Definition: basics.h:341
#define INST(x)
Definition: basics.h:238
#define real
#define NAMESPACE_CPLX_END
Definition: basics.h:326
#define STD__
Definition: basics.h:338
#define NAMESPACE_END
Definition: basics.h:323
float real
Definition: f2c.h:31
Definition: f2c.h:33
double fabssqr(const double a)
Definition: basics.h:1157
#define T
Definition: bdmatlib.cc:20
bool operator==(const F_BandMatrix< T > &m1, const F_BandMatrix< T > &m2)
Definition: f_bandmatrix.h:270
TBCI__ cplx< T > sinh(const TBCI__ cplx< T > &z)
Definition: cplx.h:781
const unsigned TMatrix< T > const Matrix< T > * a
const Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > long int int char v
&lt; find minimun of func on grid with resolution res
Definition: LM_fit.h:205
TBCI__ cplx< T > cosh(const TBCI__ cplx< T > &z)
Definition: cplx.h:791
#define MATH__
Definition: basics.h:339
TBCI__ cplx< T > acos(const TBCI__ cplx< T > &z)
Definition: cplx.h:816
int imag(const int d)
Definition: basics.h:1068