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

Generated on Sat Jul 9 2011 15:21:32 for IT++ by Doxygen 1.7.4