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