IT++ Logo Newcom Logo

fix_operators.cpp

Go to the documentation of this file.
00001 
00034 #include <itpp/fixedpoint/fix_operators.h>
00035 
00036 
00037 namespace itpp {
00038 
00040   // Operators for Fix and Fixed //
00042 
00043   Fix operator+(const Fix &x, const Fix &y)
00044   {
00045     return Fix(x.get_re() + y.get_re(),
00046                assert_shifts(x, y),
00047                0, 0);
00048   }
00049 
00050   Fix operator-(const Fix &x, const Fix &y)
00051   {
00052     return Fix(x.get_re() - y.get_re(),
00053                assert_shifts(x, y),
00054                0, 0);
00055   }
00056 
00057   Fix operator*(const Fix &x, const Fix &y)
00058   {
00059     return Fix(x.get_re() * y.get_re(),
00060                x.get_shift() + y.get_shift(),
00061                0, 0);
00062   }
00063 
00064   Fix operator/(const Fix &x, const Fix &y)
00065   {
00066     return Fix(x.get_re() / y.get_re(),
00067                x.get_shift() - y.get_shift(),
00068                0, 0);
00069   }
00070 
00071   Fix operator+(const Fix &x, const int y)
00072   {
00073     return Fix(x.get_re() + y,
00074                assert_shifts(x, y),
00075                0, 0);
00076   }
00077 
00078   Fix operator-(const Fix &x, const int y)
00079   {
00080     return Fix(x.get_re() - y,
00081                assert_shifts(x, y),
00082                0, 0);
00083   }
00084 
00085   Fix operator*(const Fix &x, const int y)
00086   {
00087     return Fix(x.get_re() * y,
00088                x.get_shift(),
00089                0, 0);
00090   }
00091 
00092   Fix operator/(const Fix &x, const int y)
00093   {
00094     return Fix(x.get_re() / y,
00095                x.get_shift(),
00096                0, 0);
00097   }
00098 
00099   Fix operator+(const int x, const Fix &y)
00100   {
00101     return Fix(x + y.get_re(),
00102                assert_shifts(y, x),
00103                0, 0);
00104   }
00105 
00106   Fix operator-(const int x, const Fix &y)
00107   {
00108     return Fix(x - y.get_re(),
00109                assert_shifts(y, x),
00110                0, 0);
00111   }
00112 
00113   Fix operator*(const int x, const Fix &y)
00114   {
00115     return Fix(x * y.get_re(),
00116                y.get_shift(),
00117                0, 0);
00118   }
00119 
00120   Fix operator/(const int x, const Fix &y)
00121   {
00122     return Fix(x / y.get_re(),
00123                -y.get_shift(),
00124                0, 0);
00125   }
00126 
00127 
00128   fixvec operator+(const fixvec &a, const ivec &b)
00129   {
00130     it_assert1(a.size() == b.size(), "operator+(): sizes do not match");
00131     fixvec temp(a);
00132     for (int i=0; i<a.size(); i++) {
00133       temp(i) += b(i);
00134     }
00135     return temp;
00136   }
00137 
00138   Fix operator*(const fixvec &a, const ivec &b)
00139   {
00140     it_assert1(a.size() == b.size(), "operator+(): sizes do not match");
00141     Fix temp(0);
00142     for (int i=0; i<a.size(); i++) {
00143       temp += a(i) * b(i);
00144     }
00145     return temp;
00146   }
00147 
00148   fixmat operator+(const fixmat &a, const imat &b)
00149   {
00150     it_assert1(a.cols()==b.cols() && a.rows()==b.rows(), "operator+(): sizes do not match");
00151     fixmat temp(a);
00152 
00153     for (int i=0; i<a.rows(); i++) {
00154       for (int j=0; j<a.cols(); j++) {
00155         temp(i,j) += b(i,j);
00156       }
00157     }
00158     return temp;
00159   }
00160 
00161   fixmat operator*(const fixmat &a, const imat &b)
00162   {
00163     it_assert1(a.cols() == b.rows(), "operator*: wrong sizes");
00164     fixmat r(a.rows(), b.cols());
00165   
00166     Fix tmp;
00167     int i, j, k;
00168     Fix *tr=r._data();
00169     const Fix *t1;
00170     const int *t2=b._data();
00171   
00172     for (i=0; i<r.cols(); i++) {
00173       for (j=0; j<r.rows(); j++) {
00174         tmp = Fix(0); t1 = a._data()+j;
00175         for (k=a.cols(); k>0; k--) {
00176           tmp += *(t1) * *(t2++);
00177           t1 += a.rows();
00178         }
00179         *(tr++) = tmp; 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_assert1(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_assert1(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_assert1(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_assert1(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); t1 = a._data()+j;
00400         for (k=a.cols(); k>0; k--) {
00401           tmp += *(t1) * *(t2++);
00402           t1 += a.rows();
00403         }
00404         *(tr++) = tmp; t2 -= b.rows();
00405       }
00406       t2 += b.rows();
00407     }  
00408     return r;
00409   }
00410 
00411   cfixvec operator+(const cfixvec &a, const ivec &b)
00412   {
00413     it_assert1(a.size() == b.size(), "operator+(): sizes do not match");
00414     cfixvec temp(a);
00415     for (int i=0; i<a.size(); i++) {
00416       temp(i) += b(i);
00417     }
00418     return temp;
00419   }
00420 
00421   CFix operator*(const cfixvec &a, const ivec &b)
00422   {
00423     it_assert1(a.size() == b.size(), "operator+(): sizes do not match");
00424     CFix temp(0);
00425     for (int i=0; i<a.size(); i++) {
00426       temp += a(i) * b(i);
00427     }
00428     return temp;
00429   }
00430 
00431   cfixmat operator+(const cfixmat &a, const imat &b)
00432   {
00433     it_assert1(a.cols()==b.cols() && a.rows()==b.rows(), "operator+(): sizes do not match");
00434     cfixmat temp(a);
00435 
00436     for (int i=0; i<a.rows(); i++) {
00437       for (int j=0; j<a.cols(); j++) {
00438         temp(i,j) += b(i,j);
00439       }
00440     }
00441     return temp;
00442   }
00443 
00444   cfixmat operator*(const cfixmat &a, const imat &b)
00445   {
00446     it_assert1(a.cols() == b.rows(), "operator*: wrong sizes");
00447     cfixmat r(a.rows(), b.cols());
00448   
00449     CFix tmp;
00450     int i, j, k;
00451     CFix *tr=r._data();
00452     const CFix *t1;
00453     const int *t2=b._data();
00454   
00455     for (i=0; i<r.cols(); i++) {
00456       for (j=0; j<r.rows(); j++) {
00457         tmp = CFix(0); t1 = a._data()+j;
00458         for (k=a.cols(); k>0; k--) {
00459           tmp += *(t1) * *(t2++);
00460           t1 += a.rows();
00461         }
00462         *(tr++) = tmp; t2 -= b.rows();
00463       }
00464       t2 += b.rows();
00465     }  
00466     return r;
00467   }
00468 
00469 } // namespace itpp
SourceForge Logo

Generated on Thu Apr 19 14:14:58 2007 for IT++ by Doxygen 1.5.1