1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
/*
* Copyright (c) 2009 Samit Basu
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef __Complex_hpp__
#define __Complex_hpp__
#include "IEEEFP.hpp"
template <typename T>
static inline void complex_divide(const T& ar, const T& ai,
const T& br, const T& bi,
T& c0, T& c1) {
double ratio, den;
double abr, abi, cr;
if ((ai == 0) && (bi == 0)) {
c1 = 0;
c0 = ar/br;
return;
}
if (bi == 0) {
c0 = ar/br;
c1 = ai/br;
return;
}
if ((ar == 0) && (bi == 0)) {
c0 = 0;
c1 = ai/br;
return;
}
if ((ai == 0) && (br == 0)) {
c0 = 0;
c1 = -ar/bi;
return;
}
// if (ar == 0) {
// c0 = (ai*bi)/(br*br + bi*bi);
// c1 = (ai*br)/(br*br + bi*bi);
// return;
// }
if ((ar == br) && (ai == bi)) {
c0 = 1; c1 = 0;
return;
}
if( (abr = br) < 0.)
abr = - abr;
if( (abi = bi) < 0.)
abi = - abi;
if( abr <= abi )
{
if(abi == 0) {
if (ai != 0 || ar != 0)
abi = 1.;
c1 = c0 = abi / abr;
return;
}
ratio = br / bi ;
den = bi * (1 + ratio*ratio);
cr = (ar*ratio + ai) / den;
c1 = (ai*ratio - ar) / den;
}
else
{
ratio = bi / br ;
den = br * (1 + ratio*ratio);
cr = (ar + ai*ratio) / den;
c1 = (ai - ar*ratio) / den;
}
c0 = cr;
}
template <typename T>
static inline void complex_recip(const T& ar, const T& ai, T& cr, T& ci) {
if (ai == 0) {
ci = 0;
cr = 1/ar;
return;
}
complex_divide<T>(1,0,ar,ai,cr,ci);
}
template <class T>
inline T complex_abs(T real, T imag) {
T swap;
if(real < 0)
real = -real;
if(imag < 0)
imag = -imag;
if(imag > real){
swap = real;
real = imag;
imag = swap;
}
if((real+imag) == real)
return(real);
double temp = double(imag)/double(real);
temp = real*sqrt(1.0 + temp*temp); /*overflow!!*/
return(T(temp));
}
template <typename T>
inline T complex_phase(const T &ar, const T &ai) {
return T(atan2(double(ai),double(ar)));
}
template <typename T>
inline void complex_multiply(const T &ar, const T &ai,
const T &br, const T &bi,
T &cr, T &ci) {
if ((ai == 0) && (bi == 0)) {
cr = ar * br;
ci = 0;
} else if (ai == 0) {
cr = ar * br;
ci = ar * bi;
} else if (bi == 0) {
cr = br * ar;
ci = br * ai;
} else {
cr = ar * br - ai * bi;
ci = ar * bi + ai * br;
}
}
template <typename T>
inline bool complex_lt(const T &ar, const T &ai,
const T &br, const T &bi) {
T mag_a = complex_abs(ar,ai);
T mag_b = complex_abs(br,bi);
if ((mag_b-mag_a) > feps(mag_a)*4) return true;
if ((mag_a-mag_b) > feps(mag_b)*4) return false;
return (complex_phase(ar,ai) < complex_phase(br,bi));
}
template <typename T>
inline bool complex_gt(const T &ar, const T &ai,
const T &br, const T &bi) {
T mag_a = complex_abs(ar,ai);
T mag_b = complex_abs(br,bi);
if ((mag_b-mag_a) > feps(mag_a)*4) return false;
if ((mag_a-mag_b) > feps(mag_b)*4) return true;
return (complex_phase(ar,ai) > complex_phase(br,bi));
}
template <typename T>
inline bool complex_eq(const T & ar, const T & ai,
const T & br, const T & bi) {
return ((ar == br) && (ai == bi));
}
template <typename T>
inline bool complex_ne(const T & ar, const T & ai,
const T & br, const T & bi) {
return ((ar != br) || (ai != bi));
}
template <typename T>
inline bool complex_le(const T & ar, const T & ai,
const T & br, const T & bi) {
return complex_eq(ar,ai,br,bi) || complex_lt(ar,ai,br,bi);
}
template <typename T>
inline bool complex_ge(const T & ar, const T & ai,
const T & br, const T & bi) {
return complex_eq(ar,ai,br,bi) || complex_gt(ar,ai,br,bi);
}
template <typename T>
inline void complex_log(T xr, T xi, T &yr, T &yi) {
yr = log(complex_abs(xr,xi));
yi = atan2(xi,xr);
}
template <typename T>
inline void complex_exp(T xr, T xi, T &yr, T &yi) {
yr = exp(xr)*cos(xi);
yi = exp(xr)*sin(xi);
}
template <typename T>
inline void complex_sqrt(T xr, T xi, T &yr, T &yi) {
T tr, ti;
if ((xr >= 0) && (xi == 0)) {
yr = sqrt(xr);
yi = 0;
return;
}
if ((xr < 0) && (xi == 0)) {
yi = sqrt(-xr);
yr = 0;
return;
}
complex_log(xr,xi,tr,ti);
tr /= 2.0;
ti /= 2.0;
complex_exp(tr,ti,yr,yi);
}
template <typename T>
inline void complex_square(T xr, T xi, T &yr, T &yi) {
yr = xr*xr - xi*xi;
yi = 2*xr*xi;
}
#endif
|