nux-1.14.0
|
00001 /* 00002 * Copyright 2010 Inalogic® Inc. 00003 * 00004 * This program is free software: you can redistribute it and/or modify it 00005 * under the terms of the GNU Lesser General Public License, as 00006 * published by the Free Software Foundation; either version 2.1 or 3.0 00007 * of the License. 00008 * 00009 * This program is distributed in the hope that it will be useful, but 00010 * WITHOUT ANY WARRANTY; without even the implied warranties of 00011 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR 00012 * PURPOSE. See the applicable version of the GNU Lesser General Public 00013 * License for more details. 00014 * 00015 * You should have received a copy of both the GNU Lesser General Public 00016 * License along with this program. If not, see <http://www.gnu.org/licenses/> 00017 * 00018 * Authored by: Jay Taoko <jaytaoko@inalogic.com> 00019 * 00020 */ 00021 00022 00023 #include "../NuxCore.h" 00024 #include "Complex.h" 00025 00026 namespace nux 00027 { 00028 00029 ComplexNumber::ComplexNumber (t_float re, t_float im) 00030 { 00031 real_ = re; 00032 imaginary_ = im; 00033 } 00034 00035 ComplexNumber::ComplexNumber (const ComplexNumber &complex) 00036 { 00037 real_ = complex.real_; 00038 imaginary_ = complex.imaginary_; 00039 } 00040 00041 ComplexNumber::~ComplexNumber() 00042 { 00043 } 00044 00045 ComplexNumber &ComplexNumber::operator= (const ComplexNumber &complex) 00046 { 00047 real_ = complex.real_; 00048 imaginary_ = complex.imaginary_; 00049 00050 return *this; 00051 } 00052 00053 /*const ComplexNumber ComplexNumber::operator + (const ComplexNumber& complex) const 00054 { 00055 ComplexNumber result; 00056 00057 result.real_ = real_ + complex.real_; 00058 result.imaginary_ = imaginary_ + complex.imaginary_; 00059 00060 return result; 00061 } 00062 00063 const ComplexNumber ComplexNumber::operator - (const ComplexNumber& complex) const 00064 { 00065 ComplexNumber result; 00066 00067 result.real_ = real_ - complex.real_; 00068 result.imaginary_ = imaginary_ - complex.imaginary_; 00069 00070 return result; 00071 } 00072 00073 const ComplexNumber ComplexNumber::operator*(const ComplexNumber& complex) const 00074 { 00075 ComplexNumber result; 00076 t_float a, b, c, d; 00077 00078 a = real_; b = imaginary_; 00079 c = complex.real_; d = complex.imaginary_; 00080 00081 result.real_ = (a*c - b*d); 00082 result.imaginary_ = (a*d + b*c); 00083 00084 return result; 00085 } 00086 00087 const ComplexNumber ComplexNumber::operator / (const ComplexNumber& complex) const 00088 { 00089 ComplexNumber result; 00090 t_float a, b, c, d; 00091 t_float inv_denominator; 00092 00093 a = real_; b = imaginary_; 00094 c = complex.real_; d = complex.imaginary_; 00095 inv_denominator = (t_float) 1.0 / (c*c + d*d); 00096 00097 result.real_ = (a*c + b*d) * inv_denominator; 00098 result.imaginary_ = (b*c - a*d) * inv_denominator; 00099 00100 return result; 00101 } 00102 */ 00103 /*const ComplexNumber ComplexNumber::operator * (const t_float& f) const 00104 { 00105 ComplexNumber result; 00106 00107 result.real_ = real_ * f; 00108 result.imaginary_ = imaginary_ * f; 00109 00110 return result; 00111 }*/ 00112 00113 /*const ComplexNumber ComplexNumber::operator / (const t_float& f) const 00114 { 00115 ComplexNumber result; 00116 00117 //if(f == 0.0f) 00118 // trow(Exception); 00119 00120 result.real_ = real_ / f ; 00121 result.imaginary_ = imaginary_ / f; 00122 00123 return result; 00124 }*/ 00125 00126 void ComplexNumber::operator+= (const ComplexNumber &complex) 00127 { 00128 real_ += complex.real_; 00129 imaginary_ += complex.imaginary_; 00130 } 00131 00132 void ComplexNumber::operator-= (const ComplexNumber &complex) 00133 { 00134 real_ -= complex.real_; 00135 imaginary_ -= complex.imaginary_; 00136 } 00137 00138 void ComplexNumber::operator*= (const ComplexNumber &complex) 00139 { 00140 ComplexNumber result; 00141 t_float a, b, c, d; 00142 00143 a = real_; 00144 b = imaginary_; 00145 c = complex.real_; 00146 d = complex.imaginary_; 00147 00148 real_ = (a * c - b * d); 00149 imaginary_ = (a * d + b * c); 00150 } 00151 00152 void ComplexNumber::operator /= (const ComplexNumber &complex) 00153 { 00154 ComplexNumber result; 00155 t_float a, b, c, d; 00156 t_float inv_denominator; 00157 00158 //if(complex.real_ == 0 && complex.imaginary_ == 0) 00159 // trow(Exeption); 00160 00161 a = real_; 00162 b = imaginary_; 00163 c = complex.real_; 00164 d = complex.imaginary_; 00165 inv_denominator = (t_float) 1.0 / (c * c + d * d); 00166 00167 real_ = (a * c + b * d) * inv_denominator; 00168 imaginary_ = (b * c - a * d) * inv_denominator; 00169 } 00170 00171 /*void ComplexNumber::operator *= (const t_float& f) 00172 { 00173 real_ *= f; 00174 imaginary_ *= f; 00175 }*/ 00176 00177 /*void ComplexNumber::operator/=(const t_float& f) 00178 { 00179 //if(f == 0.0f) 00180 // trow(Exception); 00181 00182 real_ *= (t_float)1.0 / f; 00183 imaginary_ *= (t_float)1.0 / f; 00184 }*/ 00185 00186 void ComplexNumber::conjugue() 00187 { 00188 imaginary_ = -imaginary_; 00189 } 00190 00191 t_float ComplexNumber::absolute() 00192 { 00193 t_float x, y, result, temp; 00194 00195 x = (t_float) std::fabs (real_); 00196 y = (t_float) std::fabs (imaginary_); 00197 00198 if (x == 0.0) 00199 result = y; 00200 else 00201 { 00202 if (y == 0.0) 00203 result = x; 00204 else 00205 { 00206 if (x > y) 00207 { 00208 temp = y / x; 00209 result = x * (t_float) std::sqrt (1.0 + temp * temp); 00210 } 00211 else 00212 { 00213 temp = x / y; 00214 result = y * (t_float) std::sqrt (1.0 + temp * temp); 00215 } 00216 } 00217 } 00218 00219 return result; 00220 } 00221 00222 t_bool ComplexNumber::IsNull() 00223 { 00224 if ( (real_ == 0) && (imaginary_ == 0) ) 00225 { 00226 return true; 00227 } 00228 00229 return false; 00230 } 00231 00232 t_float ComplexNumber::real() const 00233 { 00234 return real_; 00235 } 00236 00237 t_float ComplexNumber::imaginary() const 00238 { 00239 return imaginary_; 00240 } 00241 00242 void ComplexNumber::real (t_float r) 00243 { 00244 real_ = r; 00245 } 00246 00247 void ComplexNumber::imaginary (t_float i) 00248 { 00249 imaginary_ = i; 00250 } 00251 00252 const ComplexNumber operator + (const ComplexNumber &lhs, const ComplexNumber &rhs) 00253 { 00254 return ComplexNumber (lhs.real() + rhs.real(), lhs.imaginary() + rhs.imaginary() ); 00255 } 00256 00257 const ComplexNumber operator - (const ComplexNumber &lhs, const ComplexNumber &rhs) 00258 { 00259 return ComplexNumber (lhs.real() - rhs.real(), lhs.imaginary() - rhs.imaginary() ); 00260 } 00261 00262 const ComplexNumber operator* (const ComplexNumber &lhs, const ComplexNumber &rhs) 00263 { 00264 ComplexNumber result; 00265 t_float a, b, c, d; 00266 00267 a = lhs.real(); 00268 b = lhs.imaginary(); 00269 c = rhs.real(); 00270 d = rhs.imaginary(); 00271 00272 result.real (a * c - b * d); 00273 result.imaginary (a * d + b * c); 00274 00275 return result; 00276 } 00277 00278 const ComplexNumber operator/ (const ComplexNumber &lhs, const ComplexNumber &rhs) 00279 { 00280 ComplexNumber result; 00281 t_float a, b, c, d; 00282 t_float inv_denominator; 00283 00284 a = lhs.real(); 00285 b = lhs.imaginary(); 00286 c = rhs.real(); 00287 d = rhs.imaginary(); 00288 inv_denominator = (t_float) 1.0 / (c * c + d * d); 00289 00290 result.real ( (a * c + b * d) * inv_denominator); 00291 result.imaginary ( (b * c - a * d) * inv_denominator); 00292 return result; 00293 } 00294 00295 /*fcomplex Cdiv(fcomplex a, fcomplex b) 00296 { 00297 fcomplex c; 00298 t_float den, r; 00299 00300 if (fabs(b.r) >= fabs(b.i)) 00301 { 00302 r = b.i/b.r; 00303 den = b.r + r*b.i; 00304 c.r = (a.r+r*a.i) / den; 00305 c.i = (a.i-r*a.r) / den; 00306 } 00307 else 00308 { 00309 r = b.r/b.i; 00310 den = b.i + r*b.r; 00311 c.r = (a.r*r+a.i) / den; 00312 c.i = (a.i*r-a.r) / den; 00313 } 00314 00315 return c; 00316 } 00317 */ 00318 00319 00320 /* 00321 fcomplex Csqrt(fcomplex z) 00322 { 00323 fcomplex c; 00324 t_float w; 00325 00326 if ((z.r == 0.0) && (z.i == 0.0)) 00327 { 00328 c.r = 0.0; 00329 c.i = 0.0; 00330 } 00331 else 00332 { 00333 w = sqrt( (sqrt( z.r*z.r + z.i*z.i ) + fabs(z.r)) * 0.5); 00334 if (z.r >= 0.0) 00335 { 00336 c.r = w; 00337 c.i = z.i / (2.0*w); 00338 } 00339 else 00340 { 00341 c.i = (z.i >= 0) ? w : -w; 00342 c.r = z.i / (2.0*c.i); 00343 } 00344 } 00345 00346 return c; 00347 } 00348 00349 00350 fcomplex RCmul(t_float x, fcomplex a) 00351 { 00352 fcomplex c; 00353 00354 c.r = x*a.r; 00355 c.i = x*a.i; 00356 00357 return c; 00358 } 00359 00360 00361 fcomplex Cinv( fcomplex z) 00362 { 00363 fcomplex c; 00364 t_float s = 1.0 / (z.r*z.r + z.i*z.i); 00365 00366 c.r = z.r * s; 00367 c.i = -z.i * s; 00368 00369 return c; 00370 } 00371 */ 00372 }