00001 00031 #include <itpp/fixed/fix_operators.h> 00032 00033 00034 namespace itpp 00035 { 00036 00038 // Operators for Fix and Fixed // 00040 00041 Fix operator+(const Fix &x, const Fix &y) 00042 { 00043 return Fix(x.get_re() + y.get_re(), 00044 assert_shifts(x, y), 00045 0, 0); 00046 } 00047 00048 Fix operator-(const Fix &x, const Fix &y) 00049 { 00050 return Fix(x.get_re() - y.get_re(), 00051 assert_shifts(x, y), 00052 0, 0); 00053 } 00054 00055 Fix operator*(const Fix &x, const Fix &y) 00056 { 00057 return Fix(x.get_re() * y.get_re(), 00058 x.get_shift() + y.get_shift(), 00059 0, 0); 00060 } 00061 00062 Fix operator/(const Fix &x, const Fix &y) 00063 { 00064 return Fix(x.get_re() / y.get_re(), 00065 x.get_shift() - y.get_shift(), 00066 0, 0); 00067 } 00068 00069 Fix operator+(const Fix &x, const int y) 00070 { 00071 return Fix(x.get_re() + y, 00072 assert_shifts(x, y), 00073 0, 0); 00074 } 00075 00076 Fix operator-(const Fix &x, const int y) 00077 { 00078 return Fix(x.get_re() - y, 00079 assert_shifts(x, y), 00080 0, 0); 00081 } 00082 00083 Fix operator*(const Fix &x, const int y) 00084 { 00085 return Fix(x.get_re() * y, 00086 x.get_shift(), 00087 0, 0); 00088 } 00089 00090 Fix operator/(const Fix &x, const int y) 00091 { 00092 return Fix(x.get_re() / y, 00093 x.get_shift(), 00094 0, 0); 00095 } 00096 00097 Fix operator+(const int x, const Fix &y) 00098 { 00099 return Fix(x + y.get_re(), 00100 assert_shifts(y, x), 00101 0, 0); 00102 } 00103 00104 Fix operator-(const int x, const Fix &y) 00105 { 00106 return Fix(x - y.get_re(), 00107 assert_shifts(y, x), 00108 0, 0); 00109 } 00110 00111 Fix operator*(const int x, const Fix &y) 00112 { 00113 return Fix(x * y.get_re(), 00114 y.get_shift(), 00115 0, 0); 00116 } 00117 00118 Fix operator/(const int x, const Fix &y) 00119 { 00120 return Fix(x / y.get_re(), 00121 -y.get_shift(), 00122 0, 0); 00123 } 00124 00125 00126 fixvec operator+(const fixvec &a, const ivec &b) 00127 { 00128 it_assert_debug(a.size() == b.size(), "operator+(): sizes do not match"); 00129 fixvec temp(a); 00130 for (int i = 0; i < a.size(); i++) { 00131 temp(i) += b(i); 00132 } 00133 return temp; 00134 } 00135 00136 Fix operator*(const fixvec &a, const ivec &b) 00137 { 00138 it_assert_debug(a.size() == b.size(), "operator+(): sizes do not match"); 00139 Fix temp(0); 00140 for (int i = 0; i < a.size(); i++) { 00141 temp += a(i) * b(i); 00142 } 00143 return temp; 00144 } 00145 00146 fixmat operator+(const fixmat &a, const imat &b) 00147 { 00148 it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes do not match"); 00149 fixmat temp(a); 00150 00151 for (int i = 0; i < a.rows(); i++) { 00152 for (int j = 0; j < a.cols(); j++) { 00153 temp(i, j) += b(i, j); 00154 } 00155 } 00156 return temp; 00157 } 00158 00159 fixmat operator*(const fixmat &a, const imat &b) 00160 { 00161 it_assert_debug(a.cols() == b.rows(), "operator*: wrong sizes"); 00162 fixmat r(a.rows(), b.cols()); 00163 00164 Fix tmp; 00165 int i, j, k; 00166 Fix *tr = r._data(); 00167 const Fix *t1; 00168 const int *t2 = b._data(); 00169 00170 for (i = 0; i < r.cols(); i++) { 00171 for (j = 0; j < r.rows(); j++) { 00172 tmp = Fix(0); 00173 t1 = a._data() + j; 00174 for (k = a.cols(); k > 0; k--) { 00175 tmp += *(t1) * *(t2++); 00176 t1 += a.rows(); 00177 } 00178 *(tr++) = tmp; 00179 t2 -= b.rows(); 00180 } 00181 t2 += b.rows(); 00182 } 00183 return r; 00184 } 00185 00187 // Operators for CFix and CFixed // 00189 00190 CFix operator+(const CFix &x, const CFix &y) 00191 { 00192 return CFix(x.get_re() + y.get_re(), 00193 x.get_im() + y.get_im(), 00194 assert_shifts(x, y), 00195 0, 0); 00196 } 00197 00198 CFix operator-(const CFix &x, const CFix &y) 00199 { 00200 return CFix(x.get_re() - y.get_re(), 00201 x.get_im() - y.get_im(), 00202 assert_shifts(x, y), 00203 0, 0); 00204 } 00205 00206 CFix operator*(const CFix &x, const CFix &y) 00207 { 00208 return CFix(x.get_re()*y.get_re() - x.get_im()*y.get_im(), 00209 x.get_re()*y.get_im() + x.get_im()*y.get_re(), 00210 x.get_shift() + y.get_shift(), 00211 0, 0); 00212 } 00213 00214 CFix operator/(const CFix &x, const CFix &y) 00215 { 00216 fixrep denominator = y.get_re() * y.get_re() + y.get_im() * y.get_im(); 00217 return CFix((x.get_re()*y.get_re() + x.get_im()*y.get_im()) / denominator, 00218 (x.get_im()*y.get_re() - x.get_re()*y.get_im()) / denominator, 00219 x.get_shift() - y.get_shift(), 00220 0, 0); 00221 } 00222 00223 CFix operator+(const CFix &x, const Fix &y) 00224 { 00225 return CFix(x.get_re() + y.get_re(), 00226 x.get_im(), 00227 assert_shifts(x, y), 00228 0, 0); 00229 } 00230 00231 CFix operator-(const CFix &x, const Fix &y) 00232 { 00233 return CFix(x.get_re() - y.get_re(), 00234 x.get_im(), 00235 assert_shifts(x, y), 00236 0, 0); 00237 } 00238 00239 CFix operator*(const CFix &x, const Fix &y) 00240 { 00241 return CFix(x.get_re() * y.get_re(), 00242 x.get_im() * y.get_re(), 00243 x.get_shift() + y.get_shift(), 00244 0, 0); 00245 } 00246 00247 CFix operator/(const CFix &x, const Fix &y) 00248 { 00249 return CFix(x.get_re() / y.get_re(), 00250 x.get_im() / y.get_re(), 00251 x.get_shift() - y.get_shift(), 00252 0, 0); 00253 } 00254 00255 CFix operator+(const Fix &x, const CFix &y) 00256 { 00257 return CFix(x.get_re() + y.get_re(), 00258 y.get_im(), 00259 assert_shifts(y, x), 00260 0, 0); 00261 } 00262 00263 CFix operator-(const Fix &x, const CFix &y) 00264 { 00265 return CFix(x.get_re() - y.get_re(), 00266 -y.get_im(), 00267 assert_shifts(y, x), 00268 0, 0); 00269 } 00270 00271 CFix operator*(const Fix &x, const CFix &y) 00272 { 00273 return CFix(x.get_re() * y.get_re(), 00274 x.get_re() * y.get_im(), 00275 x.get_shift() + y.get_shift(), 00276 0, 0); 00277 } 00278 00279 CFix operator/(const Fix &x, const CFix &y) 00280 { 00281 fixrep denominator = y.get_re() * y.get_re() + y.get_im() * y.get_im(); 00282 return CFix(x.get_re() * y.get_re() / denominator, 00283 -x.get_re() * y.get_im() / denominator, 00284 x.get_shift() - y.get_shift(), 00285 0, 0); 00286 } 00287 00288 CFix operator+(const CFix &x, const int y) 00289 { 00290 return CFix(x.get_re() + y, 00291 x.get_im(), 00292 assert_shifts(x, y), 00293 0, 0); 00294 } 00295 00296 CFix operator-(const CFix &x, const int y) 00297 { 00298 return CFix(x.get_re() - y, 00299 x.get_im(), 00300 assert_shifts(x, y), 00301 0, 0); 00302 } 00303 00304 CFix operator*(const CFix &x, const int y) 00305 { 00306 return CFix(x.get_re() * y, 00307 x.get_im() * y, 00308 x.get_shift(), 00309 0, 0); 00310 } 00311 00312 CFix operator/(const CFix &x, const int y) 00313 { 00314 return CFix(x.get_re() / y, 00315 x.get_im() / y, 00316 x.get_shift(), 00317 0, 0); 00318 } 00319 00320 CFix operator+(const int x, const CFix &y) 00321 { 00322 return CFix(x + y.get_re(), 00323 y.get_im(), 00324 assert_shifts(y, x), 00325 0, 0); 00326 } 00327 00328 CFix operator-(const int x, const CFix &y) 00329 { 00330 return CFix(x - y.get_re(), 00331 -y.get_im(), 00332 assert_shifts(y, x), 00333 0, 0); 00334 } 00335 00336 CFix operator*(const int x, const CFix &y) 00337 { 00338 return CFix(x * y.get_re(), 00339 x * y.get_im(), 00340 y.get_shift(), 00341 0, 0); 00342 } 00343 00344 CFix operator/(const int x, const CFix &y) 00345 { 00346 fixrep denominator = y.get_re() * y.get_re() + y.get_im() * y.get_im(); 00347 return CFix(x * y.get_re() / denominator, 00348 -x * y.get_im() / denominator, 00349 -y.get_shift(), 00350 0, 0); 00351 } 00352 00353 cfixvec operator+(const cfixvec &a, const fixvec &b) 00354 { 00355 it_assert_debug(a.size() == b.size(), "operator+(): sizes do not match"); 00356 cfixvec temp(a); 00357 for (int i = 0; i < a.size(); i++) { 00358 temp(i) += b(i); 00359 } 00360 return temp; 00361 } 00362 00363 CFix operator*(const cfixvec &a, const fixvec &b) 00364 { 00365 it_assert_debug(a.size() == b.size(), "operator+(): sizes do not match"); 00366 CFix temp(0); 00367 for (int i = 0; i < a.size(); i++) { 00368 temp += a(i) * b(i); 00369 } 00370 return temp; 00371 } 00372 00373 cfixmat operator+(const cfixmat &a, const fixmat &b) 00374 { 00375 it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes do not match"); 00376 cfixmat temp(a); 00377 00378 for (int i = 0; i < a.rows(); i++) { 00379 for (int j = 0; j < a.cols(); j++) { 00380 temp(i, j) += b(i, j); 00381 } 00382 } 00383 return temp; 00384 } 00385 00386 cfixmat operator*(const cfixmat &a, const fixmat &b) 00387 { 00388 it_assert_debug(a.cols() == b.rows(), "operator*: wrong sizes"); 00389 cfixmat r(a.rows(), b.cols()); 00390 00391 CFix tmp; 00392 int i, j, k; 00393 CFix *tr = r._data(); 00394 const CFix *t1; 00395 const Fix *t2 = b._data(); 00396 00397 for (i = 0; i < r.cols(); i++) { 00398 for (j = 0; j < r.rows(); j++) { 00399 tmp = CFix(0); 00400 t1 = a._data() + j; 00401 for (k = a.cols(); k > 0; k--) { 00402 tmp += *(t1) * *(t2++); 00403 t1 += a.rows(); 00404 } 00405 *(tr++) = tmp; 00406 t2 -= b.rows(); 00407 } 00408 t2 += b.rows(); 00409 } 00410 return r; 00411 } 00412 00413 cfixvec operator+(const cfixvec &a, const ivec &b) 00414 { 00415 it_assert_debug(a.size() == b.size(), "operator+(): sizes do not match"); 00416 cfixvec temp(a); 00417 for (int i = 0; i < a.size(); i++) { 00418 temp(i) += b(i); 00419 } 00420 return temp; 00421 } 00422 00423 CFix operator*(const cfixvec &a, const ivec &b) 00424 { 00425 it_assert_debug(a.size() == b.size(), "operator+(): sizes do not match"); 00426 CFix temp(0); 00427 for (int i = 0; i < a.size(); i++) { 00428 temp += a(i) * b(i); 00429 } 00430 return temp; 00431 } 00432 00433 cfixmat operator+(const cfixmat &a, const imat &b) 00434 { 00435 it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes do not match"); 00436 cfixmat temp(a); 00437 00438 for (int i = 0; i < a.rows(); i++) { 00439 for (int j = 0; j < a.cols(); j++) { 00440 temp(i, j) += b(i, j); 00441 } 00442 } 00443 return temp; 00444 } 00445 00446 cfixmat operator*(const cfixmat &a, const imat &b) 00447 { 00448 it_assert_debug(a.cols() == b.rows(), "operator*: wrong sizes"); 00449 cfixmat r(a.rows(), b.cols()); 00450 00451 CFix tmp; 00452 int i, j, k; 00453 CFix *tr = r._data(); 00454 const CFix *t1; 00455 const int *t2 = b._data(); 00456 00457 for (i = 0; i < r.cols(); i++) { 00458 for (j = 0; j < r.rows(); j++) { 00459 tmp = CFix(0); 00460 t1 = a._data() + j; 00461 for (k = a.cols(); k > 0; k--) { 00462 tmp += *(t1) * *(t2++); 00463 t1 += a.rows(); 00464 } 00465 *(tr++) = tmp; 00466 t2 -= b.rows(); 00467 } 00468 t2 += b.rows(); 00469 } 00470 return r; 00471 } 00472 00473 } // namespace itpp
Generated on Wed Mar 2 2011 22:05:16 for IT++ by Doxygen 1.7.3