00001
00007
00008
00009
00010
00011 #include "constants.h"
00012
00013 #include "basics.h"
00014
00015 #ifndef HAVE_BUILTIN_CPLX
00016 # include "cplx.h"
00017 #else
00018
00019 #ifndef TBCI_BUILTIN_CPLX_H
00020 #define TBCI_BUILTIN_CPLX_H
00021
00022 #ifndef CTYP
00023 # define CTYP double
00024 #endif
00025
00026 NAMESPACE_TBCI
00027
00028 typedef CTYP cplx_base;
00029
00030 #ifndef CPLX
00031 typedef __complex__ CTYP cplx_t;
00032 # define CPLX(t) cplx_t
00033 # define CPLXR(t,a,r) cplx_t a (r)
00034 # define CPLXRI(t,a,r,i) cplx_t a; __real__ a = (r); __imag__ a = (i)
00035 # define CPLXC(t,a,c) cplx_t a (c)
00036 # define CPLXVRI(t,r,m) ((cplx_t) ((r) + (m) * 1.0i))
00037
00038 #endif
00039
00040 #define USE_BUILTIN_CPLX 1
00041
00047
00048 class hcplx
00049 {
00050 protected:
00051 cplx_t cx ALIGN(MIN_ALIGN2);
00052
00053 public:
00054 hcplx () {}
00055 hcplx (const hcplx & c) : cx(c.cx) {}
00056
00057 hcplx (const cplx_base r, const cplx_base i = 0) { __real__ cx = r; __imag__ cx = i; }
00058 ~hcplx () {}
00059
00060
00061 hcplx (const cplx_t& c) : cx(c) {}
00062
00063
00064
00065 operator const cplx_t& () const { return cx; }
00066
00067
00068
00069
00070 double fabssqr () const
00071 { return (__real__ cx) * (__real__ cx) + (__imag__ cx) * (__imag__ cx); }
00072 double fabs () const
00073 { return MATH__ sqrt (fabssqr()); }
00074
00075
00076
00077 #ifdef PREFER_REF
00078 const cplx_base& real () const { return __real__ cx; }
00079 const cplx_base& imag () const { return __imag__ cx; }
00080 #else
00081 cplx_base real () const { return __real__ cx; }
00082 cplx_base imag () const { return __imag__ cx; }
00083 #endif
00084 cplx_base& real () { return __real__ cx; }
00085 cplx_base& imag () { return __imag__ cx; }
00086
00087 cplx_t operator - () const
00088 { cplx_t r; __real__ r = - __real__ cx; __imag__ r = - __imag__ cx; return r; }
00089 };
00090
00091
00092 inline double fabssqr (const cplx_t& a)
00093 { return (__real__ a) * (__real__ a) + (__imag__ a) * (__imag__ a); }
00094
00095 inline double fabssqr (const TBCI__ hcplx& c) { return c.fabssqr(); }
00096
00097 #if 0
00098 inline cplx_t operator - (const cplx_t& c)
00099 { hcplx cx(c); return -cx; }
00100 #endif
00101
00102 NAMESPACE_END
00103 NAMESPACE_CSTD
00104 inline double fabs (const TBCI__ cplx_t& a)
00105 { return MATH__ sqrt (TBCI__ fabssqr (a)); }
00106
00107 inline TBCI__ cplx_base abs (const TBCI__ cplx_t& a)
00108 { return MATH__ sqrt ((__real__ a) * (__real__ a) + (__imag__ a) * (__imag__ a)); }
00109
00110 inline double fabs (const TBCI__ hcplx& c)
00111 { return c.fabs(); }
00112
00113 NAMESPACE_CSTD_END
00114
00115 #if 0 //def USE_NS
00116 inline double MATH__ fabs (const TBCI__ cplx_t& c)
00117 { return TBCI__ fabs (c); }
00118
00119
00120
00121 #endif
00122
00123 NAMESPACE_CPLX
00124 #ifdef PREFER_REF
00125 inline const TBCI__ cplx_base& real (const TBCI__ hcplx& c) { return c.real(); }
00126 inline const TBCI__ cplx_base& imag (const TBCI__ hcplx& c) { return c.imag(); }
00127 #else
00128 inline TBCI__ cplx_base real (const TBCI__ hcplx& c) { return c.real(); }
00129 inline TBCI__ cplx_base imag (const TBCI__ hcplx& c) { return c.imag(); }
00130 #endif
00131 inline TBCI__ cplx_base& real (const TBCI__ cplx_t& c) { return __real__ c; }
00132 inline TBCI__ cplx_base& imag (const TBCI__ cplx_t& c) { return __imag__ c; }
00133
00134 inline TBCI__ cplx_t conj (const TBCI__ cplx_t& c)
00135 { return ~c; }
00136
00137 NAMESPACE_CPLX_END
00138 NAMESPACE_TBCI
00139
00140 inline cplx_t dot (const cplx_t a, const cplx_t b)
00141 { return hcplx ((__real__ a) * (__real__ b) + (__imag__ a) * (__imag__ b),
00142 (__imag__ a) * (__real__ b) - (__real__ a) * (__imag__ b)); }
00143
00144 inline bool operator <= (const hcplx & a, const hcplx & b)
00145 { return (fabssqr (a) <= fabssqr(b)); }
00146 inline bool operator < (const hcplx & a, const hcplx & b)
00147 { return (fabssqr (a) < fabssqr(b)); }
00148 inline bool operator >= (const hcplx & a, const hcplx & b)
00149 { return (fabssqr (a) >= fabssqr(b)); }
00150 inline bool operator > (const hcplx & a, const hcplx & b)
00151 { return (fabssqr (a) > fabssqr(b)); }
00152
00153 inline bool operator <= (const hcplx & a, const cplx_t & b)
00154 { return (fabssqr (a) <= fabssqr(b)); }
00155 inline bool operator < (const hcplx & a, const cplx_t & b)
00156 { return (fabssqr (a) < fabssqr(b)); }
00157 inline bool operator >= (const hcplx & a, const cplx_t & b)
00158 { return (fabssqr (a) >= fabssqr(b)); }
00159 inline bool operator > (const hcplx & a, const cplx_t & b)
00160 { return (fabssqr (a) > fabssqr(b)); }
00161
00162 inline bool operator <= (const cplx_t & a, const hcplx & b)
00163 { return (fabssqr (a) <= fabssqr(b)); }
00164 inline bool operator < (const cplx_t & a, const hcplx & b)
00165 { return (fabssqr (a) < fabssqr(b)); }
00166 inline bool operator >= (const cplx_t & a, const hcplx & b)
00167 { return (fabssqr (a) >= fabssqr(b)); }
00168 inline bool operator > (const cplx_t & a, const hcplx & b)
00169 { return (fabssqr (a) > fabssqr(b)); }
00170
00171 #if 0
00172 inline bool operator <= (const cplx_t & a, const cplx_t & b)
00173 { return (fabssqr (a) <= fabssqr(b)); }
00174 inline bool operator < (const cplx_t & a, const cplx_t & b)
00175 { return (fabssqr (a) < fabssqr(b)); }
00176 inline bool operator >= (const cplx_t & a, const cplx_t & b)
00177 { return (fabssqr (a) >= fabssqr(b)); }
00178 inline bool operator > (const cplx_t & a, const cplx_t & b)
00179 { return (fabssqr (a) > fabssqr(b)); }
00180 #endif
00181
00182 inline STD__ ostream& operator << (STD__ ostream& os, const cplx_t & c)
00183 { os << "( " << __real__ c;
00184 if ((__imag__ c) < 0) return os << " - i " << - __imag__ c << " )";
00185 else return os << " + i " << __imag__ c << " )";
00186 }
00187
00188 inline STD__ istream& operator >> (STD__ istream& in, cplx_t& c)
00189 {
00190 cplx_base r, i;
00191 char s = '+', b = 'i', bra = '(';
00192
00193 if (! (in >> bra))
00194 return in;
00195 if (bra != '(') in.putback(bra);
00196 if (! (in >> r))
00197 return in;
00198 in >> s;
00199 if ((s != '+') && (s != '-')) in.putback(s);
00200 in >> b;
00201 if ((b != 'i')) in.putback(b);
00202 if (! (in >> i))
00203 return in;
00204 __real__ c = r;
00205 if (s == '-') __imag__ c =- i;
00206 else __imag__ c = i;
00207 if (bra == '(')
00208 {
00209 if (! (in >> bra))
00210 return in;
00211 if (bra != ')') in.putback(bra);
00212 }
00213 return in;
00214 }
00215
00216 inline cplx_base polar (const cplx_t& c)
00217 {
00218 if (__real__ c == 0)
00219 {
00220 if (__imag__ c > 0) return 0.5*pi;
00221 else if (__imag__ c < 0) return 1.5*pi;
00222 else return 0;
00223 }
00224
00225 if (__imag__ c >= 0) return MATH__ atan2 (__imag__ c, __real__ c);
00226 else return 2*pi + MATH__ atan2 (__imag__ c, __real__ c);
00227 }
00228
00229 inline cplx_t expi (const cplx_base phi)
00230 {
00231 cplx_base tmp = fmod (phi, pi2);
00232 return cplx_t (MATH__ cos(tmp) + MATH__ sin(tmp) * 1.0i);
00233 }
00234
00235 inline cplx_t sqr (const cplx_t &c)
00236 {
00237 return fabssqr (c) * expi (fmod ((cplx_base)2 * polar(c), pi2));
00238 }
00239
00240
00241 #ifdef NEED_IMAG_UNIT
00242 const cplx_t Imag_unit = CPLXVRI (double, 0.0, 1.0);
00243
00244 #endif
00245
00246 NAMESPACE_END
00247
00248 #ifdef ENABLE_BCPLX
00249 # include "obsolete/b_cplx_old.h"
00250 #endif
00251
00252 NAMESPACE_CSTD
00253
00254 inline TBCI__ cplx_t exp (const TBCI__ cplx_t& z)
00255 {
00256 return MATH__ exp (__real__ z) * TBCI__ expi (__imag__ z);
00257 }
00258
00259 inline TBCI__ cplx_t sqrt (const TBCI__ cplx_t& c)
00260 {
00261 return MATH__ sqrt (MATH__ fabs(c)) * TBCI__ expi ((TBCI__ cplx_base)0.5 * TBCI__ polar(c));
00262 }
00263
00264 NAMESPACE_CSTD_END
00265 #endif
00266 #endif
00267