00001 00033 #include <itpp/base/mat.h> 00034 00035 00036 namespace itpp { 00037 00038 template<> 00039 bool Mat<std::complex<double> >::set(const char *values) 00040 { 00041 std::istringstream buffer(values); 00042 int rows=0, maxrows=10, cols=0, nocols=0, maxcols=10; 00043 00044 alloc(maxrows, maxcols); 00045 00046 while (buffer.peek()!=EOF) { 00047 rows++; 00048 if (rows > maxrows) { 00049 maxrows=maxrows*2; 00050 set_size(maxrows, maxcols, true); 00051 } 00052 00053 cols=0; 00054 while ( (buffer.peek() != ';') && (buffer.peek() != EOF) ) { 00055 if (buffer.peek()==',') { 00056 buffer.get(); 00057 } else { 00058 cols++; 00059 if (cols > nocols) { 00060 nocols=cols; 00061 if (cols > maxcols) { 00062 maxcols=maxcols*2; 00063 set_size(maxrows, maxcols, true); 00064 } 00065 } 00066 buffer >> this->operator()(rows-1,cols-1); 00067 while (buffer.peek()==' ') { buffer.get(); } 00068 } 00069 } 00070 00071 if (!buffer.eof()) 00072 buffer.get(); 00073 } 00074 set_size(rows, nocols, true); 00075 00076 return true; 00077 } 00078 00079 00080 template<> 00081 bool Mat<bin>::set(const char *values) 00082 { 00083 std::istringstream buffer(values); 00084 int rows=0, maxrows=10, cols=0, nocols=0, maxcols=10; 00085 short intemp; 00086 00087 alloc(maxrows, maxcols); 00088 00089 while (buffer.peek()!=EOF) { 00090 rows++; 00091 if (rows > maxrows) { 00092 maxrows *= 2; 00093 set_size(maxrows, maxcols, true); 00094 } 00095 cols=0; 00096 while ( (buffer.peek() != ';') && (buffer.peek() != EOF) ) { 00097 if (buffer.peek()==',') { 00098 buffer.get(); 00099 } else { 00100 cols++; 00101 if (cols > nocols) { 00102 nocols=cols; 00103 if (cols > maxcols) { 00104 maxcols=maxcols*2; 00105 set_size(maxrows, maxcols, true); 00106 } 00107 } 00108 buffer >> intemp; 00109 this->operator()(rows-1,cols-1)=intemp; 00110 while (buffer.peek()==' ') { buffer.get(); } 00111 } 00112 } 00113 if (!buffer.eof()) 00114 buffer.get(); 00115 } 00116 set_size(rows, nocols, true); 00117 00118 return true; 00119 } 00120 00121 template<> 00122 const cmat Mat<std::complex<double> >::hermitian_transpose() const 00123 { 00124 cmat temp(no_cols, no_rows); 00125 for (int i=0; i<no_rows; i++) 00126 for (int j=0; j<no_cols; j++) 00127 temp(j,i) = std::conj(operator()(i,j)); 00128 00129 return temp; 00130 } 00131 00132 00133 // -------- Multiplication operator ------------- 00134 00135 #if defined(HAVE_CBLAS) 00136 template<> 00137 mat& Mat<double>::operator*=(const mat &m) 00138 { 00139 it_assert1(no_cols == m.no_rows,"mat::operator*=: wrong sizes"); 00140 mat r(no_rows, m.no_cols); // unnecessary memory?? 00141 00142 cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, no_rows, m.no_cols, no_cols, 1.0, 00143 data, no_rows, m.data, m.no_rows, 0.0, r.data, r.no_rows); 00144 00145 operator=(r); // time consuming 00146 return *this; 00147 } 00148 00149 template<> 00150 cmat& Mat<std::complex<double> >::operator*=(const cmat &m) 00151 { 00152 it_assert1(no_cols == m.no_rows,"cmat::operator*=: wrong sizes"); 00153 std::complex<double> alpha = std::complex<double>(1.0), beta = std::complex<double>(0.0); 00154 cmat r(no_rows, m.no_cols); // unnecessary memory?? 00155 00156 cblas_zgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, no_rows, m.no_cols, no_cols, (const void *)&alpha, 00157 data, no_rows, m.data, m.no_rows, (const void*)&beta, &r.data, r.no_rows); 00158 00159 operator=(r); // time consuming 00160 return *this; 00161 } 00162 00163 template<> 00164 const mat operator*(const mat &m1, const mat &m2) 00165 { 00166 it_assert1(m1.no_cols == m2.no_rows,"mat::operator*: wrong sizes"); 00167 mat r(m1.no_rows, m2.no_cols); 00168 00169 cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, m1.no_rows, m2.no_cols, m1.no_cols, 1.0, 00170 m1.data, m1.no_rows, m2.data, m2.no_rows, 0.0, r.data, r.no_rows); 00171 00172 return r; 00173 } 00174 00175 template<> 00176 const cmat operator*(const cmat &m1, const cmat &m2) 00177 { 00178 it_assert1(m1.no_cols == m2.no_rows,"cmat::operator*: wrong sizes"); 00179 std::complex<double> alpha = std::complex<double>(1.0), beta = std::complex<double>(0.0); 00180 cmat r(m1.no_rows, m2.no_cols); 00181 00182 cblas_zgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, m1.no_rows, m2.no_cols, m1.no_cols, (const void *)&alpha, 00183 m1.data, m1.no_rows, m2.data, m2.no_rows, (const void*)&beta, r.data, r.no_rows); 00184 00185 return r; 00186 } 00187 00188 template<> 00189 const Vec<double> operator*(const mat &m, const Vec<double> &v) 00190 { 00191 it_assert1(m.no_cols == v.size(),"mat::operator*: wrong sizes"); 00192 vec r(m.no_rows); 00193 00194 cblas_dgemv(CblasColMajor, CblasNoTrans, m.no_rows, m.no_cols, 1.0, m.data, m.no_rows, v._data(), 1, 00195 0.0, r._data(), 1); 00196 00197 return r; 00198 } 00199 00200 template<> 00201 const Vec<std::complex<double> > operator*(const cmat &m, const Vec<std::complex<double> > &v) 00202 { 00203 it_assert1(m.no_cols == v.size(),"cmat::operator*: wrong sizes"); 00204 std::complex<double> alpha = std::complex<double>(1.0), beta = std::complex<double>(0.0); 00205 cvec r(m.no_rows); 00206 00207 cblas_zgemv(CblasColMajor, CblasNoTrans, m.no_rows, m.no_cols, (const void *)&alpha, m.data, m.no_rows, 00208 v._data(), 1, (const void*)&beta, r._data(), 1); 00209 00210 return r; 00211 } 00212 00213 #endif // HAVE_CBLAS 00214 00215 00216 // ---------------------- Instantiations -------------------------------- 00217 00218 //------- class instantiations -------- 00219 00220 template class Mat<double>; 00221 template class Mat<std::complex<double> >; 00222 template class Mat<int>; 00223 template class Mat<short int>; 00224 template class Mat<bin>; 00225 00226 00227 //-------------------- Operator instantiations -------------------- 00228 00229 //-------- Addition operators --------------- 00230 00231 template const mat operator+(const mat &m1, const mat &m2); 00232 template const cmat operator+(const cmat &m1, const cmat &m2); 00233 template const imat operator+(const imat &m1, const imat &m2); 00234 template const smat operator+(const smat &m1, const smat &m2); 00235 template const bmat operator+(const bmat &m1, const bmat &m2); 00236 00237 template const mat operator+(const mat &m, double t); 00238 template const cmat operator+(const cmat &m, std::complex<double> t); 00239 template const imat operator+(const imat &m, int t); 00240 template const smat operator+(const smat &m, short t); 00241 template const bmat operator+(const bmat &m, bin t); 00242 00243 template const mat operator+(double t, const mat &m); 00244 template const cmat operator+(std::complex<double> t, const cmat &m); 00245 template const imat operator+(int t, const imat &m); 00246 template const smat operator+(short t, const smat &m); 00247 template const bmat operator+(bin t, const bmat &m); 00248 00249 //-------- Subraction operators --------------- 00250 00251 template const mat operator-(const mat &m1, const mat &m2); 00252 template const cmat operator-(const cmat &m1, const cmat &m2); 00253 template const imat operator-(const imat &m1, const imat &m2); 00254 template const smat operator-(const smat &m1, const smat &m2); 00255 template const bmat operator-(const bmat &m1, const bmat &m2); 00256 00257 template const mat operator-(const mat &m, double t); 00258 template const cmat operator-(const cmat &m, std::complex<double> t); 00259 template const imat operator-(const imat &m, int t); 00260 template const smat operator-(const smat &m, short t); 00261 template const bmat operator-(const bmat &m, bin t); 00262 00263 template const mat operator-(double t, const mat &m); 00264 template const cmat operator-(std::complex<double> t, const cmat &m); 00265 template const imat operator-(int t, const imat &m); 00266 template const smat operator-(short t, const smat &m); 00267 template const bmat operator-(bin t, const bmat &m); 00268 00269 //--------- Unary minus --------------- 00270 00271 template const mat operator-(const mat &m); 00272 template const cmat operator-(const cmat &m); 00273 template const imat operator-(const imat &m); 00274 template const smat operator-(const smat &m); 00275 template const bmat operator-(const bmat &m); 00276 00277 //-------- Multiplication operators --------------- 00278 00279 #if !defined(HAVE_CBLAS) 00280 template const mat operator*(const mat &m1, const mat &m2); 00281 template const cmat operator*(const cmat &m1, const cmat &m2); 00282 #endif 00283 template const imat operator*(const imat &m1, const imat &m2); 00284 template const smat operator*(const smat &m1, const smat &m2); 00285 template const bmat operator*(const bmat &m1, const bmat &m2); 00286 00287 #if !defined(HAVE_CBLAS) 00288 template const vec operator*(const mat &m, const vec &v); 00289 template const cvec operator*(const cmat &m, const cvec &v); 00290 #endif 00291 template const ivec operator*(const imat &m, const ivec &v); 00292 template const svec operator*(const smat &m, const svec &v); 00293 template const bvec operator*(const bmat &m, const bvec &v); 00294 00295 template const mat operator*(const vec &v, const mat &m); 00296 template const cmat operator*(const cvec &v, const cmat &m); 00297 template const imat operator*(const ivec &v, const imat &m); 00298 template const smat operator*(const svec &v, const smat &m); 00299 template const bmat operator*(const bvec &v, const bmat &m); 00300 00301 template const mat operator*(const mat &m, double t); 00302 template const cmat operator*(const cmat &m, std::complex<double> t); 00303 template const imat operator*(const imat &m, int t); 00304 template const smat operator*(const smat &m, short t); 00305 template const bmat operator*(const bmat &m, bin t); 00306 00307 template const mat operator*(double t, const mat &m); 00308 template const cmat operator*(std::complex<double> t, const cmat &m); 00309 template const imat operator*(int t, const imat &m); 00310 template const smat operator*(short t, const smat &m); 00311 template const bmat operator*(bin t, const bmat &m); 00312 00313 // ------------ Elementwise multiplication ----------- 00314 00315 template const mat elem_mult(const mat &m1, const mat &m2); 00316 template const cmat elem_mult(const cmat &m1, const cmat &m2); 00317 template const imat elem_mult(const imat &m1, const imat &m2); 00318 template const smat elem_mult(const smat &m1, const smat &m2); 00319 template const bmat elem_mult(const bmat &m1, const bmat &m2); 00320 00321 // ------------ Division operator ----------- 00322 00323 template const mat operator/(const mat &m, double t); 00324 template const cmat operator/(const cmat &m, std::complex<double> t); 00325 template const imat operator/(const imat &m, int t); 00326 template const smat operator/(const smat &m, short t); 00327 template const bmat operator/(const bmat &m, bin t); 00328 00329 // ------------ Elementwise division ----------- 00330 00331 template const mat elem_div(const mat &m1, const mat &m2); 00332 template const cmat elem_div(const cmat &m1, const cmat &m2); 00333 template const imat elem_div(const imat &m1, const imat &m2); 00334 template const smat elem_div(const smat &m1, const smat &m2); 00335 template const bmat elem_div(const bmat &m1, const bmat &m2); 00336 00337 // ------------- Concatenations ----------------- 00338 00339 template const mat concat_horizontal(const mat &m1, const mat &m2); 00340 template const cmat concat_horizontal(const cmat &m1, const cmat &m2); 00341 template const imat concat_horizontal(const imat &m1, const imat &m2); 00342 template const smat concat_horizontal(const smat &m1, const smat &m2); 00343 template const bmat concat_horizontal(const bmat &m1, const bmat &m2); 00344 00345 template const mat concat_vertical(const mat &m1, const mat &m2); 00346 template const cmat concat_vertical(const cmat &m1, const cmat &m2); 00347 template const imat concat_vertical(const imat &m1, const imat &m2); 00348 template const smat concat_vertical(const smat &m1, const smat &m2); 00349 template const bmat concat_vertical(const bmat &m1, const bmat &m2); 00350 00351 //----------- Output stream -------------- 00352 00353 template std::ostream &operator<<(std::ostream &os, const mat &m); 00354 template std::ostream &operator<<(std::ostream &os, const cmat &m); 00355 template std::ostream &operator<<(std::ostream &os, const imat &m); 00356 template std::ostream &operator<<(std::ostream &os, const smat &m); 00357 template std::ostream &operator<<(std::ostream &os, const bmat &m); 00358 00359 //----------- Input stream ------------- 00360 00361 template std::istream &operator>>(std::istream &is, mat &m); 00362 template std::istream &operator>>(std::istream &is, cmat &m); 00363 template std::istream &operator>>(std::istream &is, imat &m); 00364 template std::istream &operator>>(std::istream &is, smat &m); 00365 template std::istream &operator>>(std::istream &is, bmat &m); 00366 00367 } // namespace itpp
Generated on Fri Jan 11 08:51:36 2008 for IT++ by Doxygen 1.3.9.1