IT++ Logo

mat.cpp

Go to the documentation of this file.
00001 
00030 #include <itpp/base/mat.h>
00031 
00032 #if defined (HAVE_BLAS)
00033 #  include <itpp/base/blas.h>
00034 #endif
00035 
00037 
00038 namespace itpp {
00039 
00040   template<>
00041   const cmat Mat<std::complex<double> >::hermitian_transpose() const
00042   {
00043     cmat temp(no_cols, no_rows);
00044     for (int i=0; i<no_rows; i++)
00045       for (int j=0; j<no_cols; j++)
00046   temp(j,i) = std::conj(operator()(i,j));
00047 
00048     return temp;
00049   }
00050 
00051 
00052   // -------- Multiplication operator -------------
00053 
00054 #if defined(HAVE_BLAS)
00055 
00056   template<>
00057   mat& Mat<double>::operator*=(const mat &m)
00058   {
00059     it_assert_debug(no_cols == m.no_rows,"mat::operator*=: wrong sizes");
00060     mat r(no_rows, m.no_cols); // unnecessary memory??
00061     double alpha = 1.0;
00062     double beta = 0.0;
00063     char trans = 'n';
00064     blas::dgemm_(&trans, &trans, &no_rows, &m.no_cols, &no_cols, &alpha, data,
00065      &no_rows, m.data, &m.no_rows, &beta, r.data, &r.no_rows);
00066     operator=(r); // time consuming
00067     return *this;
00068   }
00069 
00070   template<>
00071   cmat& Mat<std::complex<double> >::operator*=(const cmat &m)
00072   {
00073     it_assert_debug(no_cols == m.no_rows,"cmat::operator*=: wrong sizes");
00074     cmat r(no_rows, m.no_cols); // unnecessary memory??
00075     std::complex<double> alpha = std::complex<double>(1.0);
00076     std::complex<double> beta = std::complex<double>(0.0);
00077     char trans = 'n';
00078     blas::zgemm_(&trans, &trans, &no_rows, &m.no_cols, &no_cols, &alpha, data,
00079      &no_rows, m.data, &m.no_rows, &beta, r.data, &r.no_rows);
00080     operator=(r); // time consuming
00081     return *this;
00082   }
00083 
00084   template<>
00085   const mat operator*(const mat &m1, const mat &m2)
00086   {
00087     it_assert_debug(m1.no_cols == m2.no_rows,"mat::operator*: wrong sizes");
00088     mat r(m1.no_rows, m2.no_cols);
00089     double alpha = 1.0;
00090     double beta = 0.0;
00091     char trans = 'n';
00092     blas::dgemm_(&trans, &trans, &m1.no_rows, &m2.no_cols, &m1.no_cols, &alpha,
00093      m1.data, &m1.no_rows, m2.data, &m2.no_rows, &beta, r.data,
00094      &r.no_rows);
00095     return r;
00096   }
00097 
00098   template<>
00099   const cmat operator*(const cmat &m1, const cmat &m2)
00100   {
00101     it_assert_debug(m1.no_cols == m2.no_rows,"cmat::operator*: wrong sizes");
00102     cmat r(m1.no_rows, m2.no_cols);
00103     std::complex<double> alpha = std::complex<double>(1.0);
00104     std::complex<double> beta = std::complex<double>(0.0);
00105     char trans = 'n';
00106     blas::zgemm_(&trans, &trans, &m1.no_rows, &m2.no_cols, &m1.no_cols, &alpha,
00107      m1.data, &m1.no_rows, m2.data, &m2.no_rows, &beta, r.data,
00108      &r.no_rows);
00109     return r;
00110   }
00111 
00112   template<>
00113   const Vec<double> operator*(const mat &m, const Vec<double> &v)
00114   {
00115     it_assert_debug(m.no_cols == v.size(), "mat::operator*: wrong sizes");
00116     vec r(m.no_rows);
00117     double alpha = 1.0;
00118     double beta = 0.0;
00119     char trans = 'n';
00120     int incr = 1;
00121     blas::dgemv_(&trans, &m.no_rows, &m.no_cols, &alpha, m.data, &m.no_rows,
00122      v._data(), &incr, &beta, r._data(), &incr);
00123     return r;
00124   }
00125 
00126   template<>
00127   const Vec<std::complex<double> > operator*(const cmat &m, const Vec<std::complex<double> > &v)
00128   {
00129     it_assert_debug(m.no_cols == v.size(), "cmat::operator*: wrong sizes");
00130     cvec r(m.no_rows);
00131     std::complex<double> alpha = std::complex<double>(1.0);
00132     std::complex<double> beta = std::complex<double>(0.0);
00133     char trans = 'n';
00134     int incr = 1;
00135     blas::zgemv_(&trans, &m.no_rows, &m.no_cols, &alpha, m.data, &m.no_rows,
00136      v._data(), &incr, &beta, r._data(), &incr);
00137     return r;
00138   }
00139 
00140 #endif // HAVE_BLAS
00141 
00142 
00143   //---------------------------------------------------------------------
00144   // Instantiations
00145   //---------------------------------------------------------------------
00146 
00147   // class instantiations
00148 
00149   template class Mat<double>;
00150   template class Mat<std::complex<double> >;
00151   template class Mat<int>;
00152   template class Mat<short int>;
00153   template class Mat<bin>;
00154 
00155   // addition operators
00156 
00157   template const mat operator+(const mat &m1, const mat &m2);
00158   template const cmat operator+(const cmat &m1, const cmat &m2);
00159   template const imat operator+(const imat &m1, const imat &m2);
00160   template const smat operator+(const smat &m1, const smat &m2);
00161   template const bmat operator+(const bmat &m1, const bmat &m2);
00162 
00163   template const mat operator+(const mat &m, double t);
00164   template const cmat operator+(const cmat &m, std::complex<double> t);
00165   template const imat operator+(const imat &m, int t);
00166   template const smat operator+(const smat &m, short t);
00167   template const bmat operator+(const bmat &m, bin t);
00168 
00169   template const mat operator+(double t, const mat &m);
00170   template const cmat operator+(std::complex<double> t, const cmat &m);
00171   template const imat operator+(int t, const imat &m);
00172   template const smat operator+(short t, const smat &m);
00173   template const bmat operator+(bin t, const bmat &m);
00174 
00175   // subraction operators
00176 
00177   template const mat operator-(const mat &m1, const mat &m2);
00178   template const cmat operator-(const cmat &m1, const cmat &m2);
00179   template const imat operator-(const imat &m1, const imat &m2);
00180   template const smat operator-(const smat &m1, const smat &m2);
00181   template const bmat operator-(const bmat &m1, const bmat &m2);
00182 
00183   template const mat operator-(const mat &m, double t);
00184   template const cmat operator-(const cmat &m, std::complex<double> t);
00185   template const imat operator-(const imat &m, int t);
00186   template const smat operator-(const smat &m, short t);
00187   template const bmat operator-(const bmat &m, bin t);
00188 
00189   template const mat operator-(double t, const mat &m);
00190   template const cmat operator-(std::complex<double> t, const cmat &m);
00191   template const imat operator-(int t, const imat &m);
00192   template const smat operator-(short t, const smat &m);
00193   template const bmat operator-(bin t, const bmat &m);
00194 
00195   // unary minus
00196 
00197   template const mat operator-(const mat &m);
00198   template const cmat operator-(const cmat &m);
00199   template const imat operator-(const imat &m);
00200   template const smat operator-(const smat &m);
00201   template const bmat operator-(const bmat &m);
00202 
00203   // multiplication operators
00204 
00205 #if !defined(HAVE_BLAS)
00206   template const mat operator*(const mat &m1, const mat &m2);
00207   template const cmat operator*(const cmat &m1, const cmat &m2);
00208 #endif
00209   template const imat operator*(const imat &m1, const imat &m2);
00210   template const smat operator*(const smat &m1, const smat &m2);
00211   template const bmat operator*(const bmat &m1, const bmat &m2);
00212 
00213 #if !defined(HAVE_BLAS)
00214   template const vec operator*(const mat &m, const vec &v);
00215   template const cvec operator*(const cmat &m, const cvec &v);
00216 #endif
00217   template const ivec operator*(const imat &m, const ivec &v);
00218   template const svec operator*(const smat &m, const svec &v);
00219   template const bvec operator*(const bmat &m, const bvec &v);
00220 
00221   template const mat operator*(const vec &v, const mat &m);
00222   template const cmat operator*(const cvec &v, const cmat &m);
00223   template const imat operator*(const ivec &v, const imat &m);
00224   template const smat operator*(const svec &v, const smat &m);
00225   template const bmat operator*(const bvec &v, const bmat &m);
00226 
00227   template const mat operator*(const mat &m, double t);
00228   template const cmat operator*(const cmat &m, std::complex<double> t);
00229   template const imat operator*(const imat &m, int t);
00230   template const smat operator*(const smat &m, short t);
00231   template const bmat operator*(const bmat &m, bin t);
00232 
00233   template const mat operator*(double t, const mat &m);
00234   template const cmat operator*(std::complex<double> t, const cmat &m);
00235   template const imat operator*(int t, const imat &m);
00236   template const smat operator*(short t, const smat &m);
00237   template const bmat operator*(bin t, const bmat &m);
00238 
00239   // elementwise multiplication
00240 
00241   template const mat elem_mult(const mat &A, const mat &B);
00242   template const cmat elem_mult(const cmat &A, const cmat &B);
00243   template const imat elem_mult(const imat &A, const imat &B);
00244   template const smat elem_mult(const smat &A, const smat &B);
00245   template const bmat elem_mult(const bmat &A, const bmat &B);
00246 
00247   template void elem_mult_out(const mat &A, const mat &B, mat &out);
00248   template void elem_mult_out(const cmat &A, const cmat &B, cmat &out);
00249   template void elem_mult_out(const imat &A, const imat &B, imat &out);
00250   template void elem_mult_out(const smat &A, const smat &B, smat &out);
00251   template void elem_mult_out(const bmat &A, const bmat &B, bmat &out);
00252 
00253   template void elem_mult_out(const mat &A, const mat &B, const mat &C,
00254                                      mat &out);
00255   template void elem_mult_out(const cmat &A, const cmat &B,
00256                                      const cmat &C, cmat &out);
00257   template void elem_mult_out(const imat &A, const imat &B,
00258                                      const imat &C, imat &out);
00259   template void elem_mult_out(const smat &A, const smat &B,
00260                                      const smat &C, smat &out);
00261   template void elem_mult_out(const bmat &A, const bmat &B,
00262                                      const bmat &C, bmat &out);
00263 
00264   template void elem_mult_out(const mat &A, const mat &B, const mat &C,
00265                                      const mat &D, mat &out);
00266   template void elem_mult_out(const cmat &A, const cmat &B,
00267                                      const cmat &C, const cmat &D, cmat &out);
00268   template void elem_mult_out(const imat &A, const imat &B,
00269                                      const imat &C, const imat &D, imat &out);
00270   template void elem_mult_out(const smat &A, const smat &B,
00271                                      const smat &C, const smat &D, smat &out);
00272   template void elem_mult_out(const bmat &A, const bmat &B,
00273                                      const bmat &C, const bmat &D, bmat &out);
00274 
00275   template void elem_mult_inplace(const mat &A, mat &B);
00276   template void elem_mult_inplace(const cmat &A, cmat &B);
00277   template void elem_mult_inplace(const imat &A, imat &B);
00278   template void elem_mult_inplace(const smat &A, smat &B);
00279   template void elem_mult_inplace(const bmat &A, bmat &B);
00280 
00281   template double elem_mult_sum(const mat &A, const mat &B);
00282   template std::complex<double> elem_mult_sum(const cmat &A,
00283                                                      const cmat &B);
00284   template int elem_mult_sum(const imat &A, const imat &B);
00285   template short elem_mult_sum(const smat &A, const smat &B);
00286   template bin elem_mult_sum(const bmat &A, const bmat &B);
00287 
00288   // division operator
00289 
00290   template const mat operator/(const mat &m, double t);
00291   template const cmat operator/(const cmat &m, std::complex<double> t);
00292   template const imat operator/(const imat &m, int t);
00293   template const smat operator/(const smat &m, short t);
00294   template const bmat operator/(const bmat &m, bin t);
00295 
00296   // elementwise division
00297 
00298   template const mat elem_div(const mat &A, const mat &B);
00299   template const cmat elem_div(const cmat &A, const cmat &B);
00300   template const imat elem_div(const imat &A, const imat &B);
00301   template const smat elem_div(const smat &A, const smat &B);
00302   template const bmat elem_div(const bmat &A, const bmat &B);
00303 
00304   template void elem_div_out(const mat &A, const mat &B, mat &out);
00305   template void elem_div_out(const cmat &A, const cmat &B, cmat &out);
00306   template void elem_div_out(const imat &A, const imat &B, imat &out);
00307   template void elem_div_out(const smat &A, const smat &B, smat &out);
00308   template void elem_div_out(const bmat &A, const bmat &B, bmat &out);
00309 
00310   template double elem_div_sum(const mat &A, const mat &B);
00311   template std::complex<double> elem_div_sum(const cmat &A,
00312                                                     const cmat &B);
00313   template int elem_div_sum(const imat &A, const imat &B);
00314   template short elem_div_sum(const smat &A, const smat &B);
00315   template bin elem_div_sum(const bmat &A, const bmat &B);
00316 
00317   // concatenation
00318 
00319   template const mat concat_horizontal(const mat &m1, const mat &m2);
00320   template const cmat concat_horizontal(const cmat &m1, const cmat &m2);
00321   template const imat concat_horizontal(const imat &m1, const imat &m2);
00322   template const smat concat_horizontal(const smat &m1, const smat &m2);
00323   template const bmat concat_horizontal(const bmat &m1, const bmat &m2);
00324 
00325   template const mat concat_vertical(const mat &m1, const mat &m2);
00326   template const cmat concat_vertical(const cmat &m1, const cmat &m2);
00327   template const imat concat_vertical(const imat &m1, const imat &m2);
00328   template const smat concat_vertical(const smat &m1, const smat &m2);
00329   template const bmat concat_vertical(const bmat &m1, const bmat &m2);
00330 
00331   // I/O streams
00332 
00333   template std::ostream &operator<<(std::ostream &os, const mat  &m);
00334   template std::ostream &operator<<(std::ostream &os, const cmat &m);
00335   template std::ostream &operator<<(std::ostream &os, const imat  &m);
00336   template std::ostream &operator<<(std::ostream &os, const smat  &m);
00337   template std::ostream &operator<<(std::ostream &os, const bmat  &m);
00338 
00339   template std::istream &operator>>(std::istream &is, mat  &m);
00340   template std::istream &operator>>(std::istream &is, cmat &m);
00341   template std::istream &operator>>(std::istream &is, imat  &m);
00342   template std::istream &operator>>(std::istream &is, smat  &m);
00343   template std::istream &operator>>(std::istream &is, bmat  &m);
00344 
00345 } // namespace itpp
00346 
SourceForge Logo

Generated on Sun Sep 14 18:52:26 2008 for IT++ by Doxygen 1.5.6