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 cmat cmat::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::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& cmat::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 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 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 vec operator*(const mat &m, const vec &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 cvec operator*(const cmat &m, const cvec &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 mat operator+(const mat &m1, const mat &m2); 00158 template cmat operator+(const cmat &m1, const cmat &m2); 00159 template imat operator+(const imat &m1, const imat &m2); 00160 template smat operator+(const smat &m1, const smat &m2); 00161 template bmat operator+(const bmat &m1, const bmat &m2); 00162 00163 template mat operator+(const mat &m, double t); 00164 template cmat operator+(const cmat &m, std::complex<double> t); 00165 template imat operator+(const imat &m, int t); 00166 template smat operator+(const smat &m, short t); 00167 template bmat operator+(const bmat &m, bin t); 00168 00169 template mat operator+(double t, const mat &m); 00170 template cmat operator+(std::complex<double> t, const cmat &m); 00171 template imat operator+(int t, const imat &m); 00172 template smat operator+(short t, const smat &m); 00173 template bmat operator+(bin t, const bmat &m); 00174 00175 // subraction operators 00176 00177 template mat operator-(const mat &m1, const mat &m2); 00178 template cmat operator-(const cmat &m1, const cmat &m2); 00179 template imat operator-(const imat &m1, const imat &m2); 00180 template smat operator-(const smat &m1, const smat &m2); 00181 template bmat operator-(const bmat &m1, const bmat &m2); 00182 00183 template mat operator-(const mat &m, double t); 00184 template cmat operator-(const cmat &m, std::complex<double> t); 00185 template imat operator-(const imat &m, int t); 00186 template smat operator-(const smat &m, short t); 00187 template bmat operator-(const bmat &m, bin t); 00188 00189 template mat operator-(double t, const mat &m); 00190 template cmat operator-(std::complex<double> t, const cmat &m); 00191 template imat operator-(int t, const imat &m); 00192 template smat operator-(short t, const smat &m); 00193 template bmat operator-(bin t, const bmat &m); 00194 00195 // unary minus 00196 00197 template mat operator-(const mat &m); 00198 template cmat operator-(const cmat &m); 00199 template imat operator-(const imat &m); 00200 template smat operator-(const smat &m); 00201 template bmat operator-(const bmat &m); 00202 00203 // multiplication operators 00204 00205 #if !defined(HAVE_BLAS) 00206 template mat operator*(const mat &m1, const mat &m2); 00207 template cmat operator*(const cmat &m1, const cmat &m2); 00208 #endif 00209 template imat operator*(const imat &m1, const imat &m2); 00210 template smat operator*(const smat &m1, const smat &m2); 00211 template bmat operator*(const bmat &m1, const bmat &m2); 00212 00213 #if !defined(HAVE_BLAS) 00214 template vec operator*(const mat &m, const vec &v); 00215 template cvec operator*(const cmat &m, const cvec &v); 00216 #endif 00217 00218 template ivec operator*(const imat &m, const ivec &v); 00219 template svec operator*(const smat &m, const svec &v); 00220 template bvec operator*(const bmat &m, const bvec &v); 00221 00222 template mat operator*(const vec &v, const mat &m); 00223 template cmat operator*(const cvec &v, const cmat &m); 00224 template imat operator*(const ivec &v, const imat &m); 00225 template smat operator*(const svec &v, const smat &m); 00226 template bmat operator*(const bvec &v, const bmat &m); 00227 00228 template mat operator*(const mat &m, double t); 00229 template cmat operator*(const cmat &m, std::complex<double> t); 00230 template imat operator*(const imat &m, int t); 00231 template smat operator*(const smat &m, short t); 00232 template bmat operator*(const bmat &m, bin t); 00233 00234 template mat operator*(double t, const mat &m); 00235 template cmat operator*(std::complex<double> t, const cmat &m); 00236 template imat operator*(int t, const imat &m); 00237 template smat operator*(short t, const smat &m); 00238 template bmat operator*(bin t, const bmat &m); 00239 00240 // elementwise multiplication 00241 00242 template mat elem_mult(const mat &m1, const mat &m2); 00243 template cmat elem_mult(const cmat &m1, const cmat &m2); 00244 template imat elem_mult(const imat &m1, const imat &m2); 00245 template smat elem_mult(const smat &m1, const smat &m2); 00246 template bmat elem_mult(const bmat &m1, const bmat &m2); 00247 00248 template void elem_mult_out(const mat &m1, const mat &m2, mat &out); 00249 template void elem_mult_out(const cmat &m1, const cmat &m2, cmat &out); 00250 template void elem_mult_out(const imat &m1, const imat &m2, imat &out); 00251 template void elem_mult_out(const smat &m1, const smat &m2, smat &out); 00252 template void elem_mult_out(const bmat &m1, const bmat &m2, bmat &out); 00253 00254 template void elem_mult_out(const mat &m1, const mat &m2, 00255 const mat &m3, mat &out); 00256 template void elem_mult_out(const cmat &m1, const cmat &m2, 00257 const cmat &m3, cmat &out); 00258 template void elem_mult_out(const imat &m1, const imat &m2, 00259 const imat &m3, imat &out); 00260 template void elem_mult_out(const smat &m1, const smat &m2, 00261 const smat &m3, smat &out); 00262 template void elem_mult_out(const bmat &m1, const bmat &m2, 00263 const bmat &m3, bmat &out); 00264 00265 template void elem_mult_out(const mat &m1, const mat &m2, const mat &m3, 00266 const mat &m4, mat &out); 00267 template void elem_mult_out(const cmat &m1, const cmat &m2, 00268 const cmat &m3, const cmat &m4, cmat &out); 00269 template void elem_mult_out(const imat &m1, const imat &m2, 00270 const imat &m3, const imat &m4, imat &out); 00271 template void elem_mult_out(const smat &m1, const smat &m2, 00272 const smat &m3, const smat &m4, smat &out); 00273 template void elem_mult_out(const bmat &m1, const bmat &m2, 00274 const bmat &m3, const bmat &m4, bmat &out); 00275 00276 template void elem_mult_inplace(const mat &m1, mat &m2); 00277 template void elem_mult_inplace(const cmat &m1, cmat &m2); 00278 template void elem_mult_inplace(const imat &m1, imat &m2); 00279 template void elem_mult_inplace(const smat &m1, smat &m2); 00280 template void elem_mult_inplace(const bmat &m1, bmat &m2); 00281 00282 template double elem_mult_sum(const mat &m1, const mat &m2); 00283 template std::complex<double> elem_mult_sum(const cmat &m1, const cmat &m2); 00284 template int elem_mult_sum(const imat &m1, const imat &m2); 00285 template short elem_mult_sum(const smat &m1, const smat &m2); 00286 template bin elem_mult_sum(const bmat &m1, const bmat &m2); 00287 00288 // division operator 00289 00290 template mat operator/(const mat &m, double t); 00291 template cmat operator/(const cmat &m, std::complex<double> t); 00292 template imat operator/(const imat &m, int t); 00293 template smat operator/(const smat &m, short t); 00294 template bmat operator/(const bmat &m, bin t); 00295 00296 // elementwise division 00297 00298 template mat elem_div(const mat &m1, const mat &m2); 00299 template cmat elem_div(const cmat &m1, const cmat &m2); 00300 template imat elem_div(const imat &m1, const imat &m2); 00301 template smat elem_div(const smat &m1, const smat &m2); 00302 template bmat elem_div(const bmat &m1, const bmat &m2); 00303 00304 template void elem_div_out(const mat &m1, const mat &m2, mat &out); 00305 template void elem_div_out(const cmat &m1, const cmat &m2, cmat &out); 00306 template void elem_div_out(const imat &m1, const imat &m2, imat &out); 00307 template void elem_div_out(const smat &m1, const smat &m2, smat &out); 00308 template void elem_div_out(const bmat &m1, const bmat &m2, bmat &out); 00309 00310 template double elem_div_sum(const mat &m1, const mat &m2); 00311 template std::complex<double> elem_div_sum(const cmat &m1, 00312 const cmat &m2); 00313 template int elem_div_sum(const imat &m1, const imat &m2); 00314 template short elem_div_sum(const smat &m1, const smat &m2); 00315 template bin elem_div_sum(const bmat &m1, const bmat &m2); 00316 00317 // concatenation 00318 00319 template mat concat_horizontal(const mat &m1, const mat &m2); 00320 template cmat concat_horizontal(const cmat &m1, const cmat &m2); 00321 template imat concat_horizontal(const imat &m1, const imat &m2); 00322 template smat concat_horizontal(const smat &m1, const smat &m2); 00323 template bmat concat_horizontal(const bmat &m1, const bmat &m2); 00324 00325 template mat concat_vertical(const mat &m1, const mat &m2); 00326 template cmat concat_vertical(const cmat &m1, const cmat &m2); 00327 template imat concat_vertical(const imat &m1, const imat &m2); 00328 template smat concat_vertical(const smat &m1, const smat &m2); 00329 template 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
Generated on Sat Apr 19 10:43:51 2008 for IT++ by Doxygen 1.5.5