32 # include <itpp/config.h>
34 # include <itpp/config_msvc.h>
37 #if defined (HAVE_BLAS)
38 # include <itpp/base/blas.h>
49 cmat temp(no_cols, no_rows);
50 for (
int i = 0; i < no_rows; i++)
51 for (
int j = 0; j < no_cols; j++)
60 #if defined(HAVE_BLAS)
63 mat& mat::operator*=(
const mat &m)
65 it_assert_debug(no_cols == m.no_rows,
"mat::operator*=(): Wrong sizes");
66 mat r(no_rows, m.no_cols);
70 blas::dgemm_(&trans, &trans, &no_rows, &m.no_cols, &no_cols, &alpha, data,
71 &no_rows, m.data, &m.no_rows, &beta, r.data, &r.no_rows);
77 cmat& cmat::operator*=(
const cmat &m)
79 it_assert_debug(no_cols == m.no_rows,
"cmat::operator*=(): Wrong sizes");
80 cmat r(no_rows, m.no_cols);
81 std::complex<double> alpha = std::complex<double>(1.0);
82 std::complex<double> beta = std::complex<double>(0.0);
84 blas::zgemm_(&trans, &trans, &no_rows, &m.no_cols, &no_cols, &alpha, data,
85 &no_rows, m.data, &m.no_rows, &beta, r.data, &r.no_rows);
91 mat& mat::operator*=(
const mat &m)
93 it_assert_debug(no_cols == m.no_rows,
"Mat<>::operator*=(): Wrong sizes");
94 mat r(no_rows, m.no_cols);
95 int r_pos = 0, pos = 0, m_pos = 0;
97 for (
int i = 0; i < r.no_cols; i++) {
98 for (
int j = 0; j < r.no_rows; j++) {
101 for (
int k = 0; k < no_cols; k++) {
102 tmp += data[pos+j] * m.data[m_pos+k];
105 r.data[r_pos+j] = tmp;
115 cmat& cmat::operator*=(
const cmat &m)
117 it_assert_debug(no_cols == m.no_rows,
"Mat<>::operator*=(): Wrong sizes");
118 cmat r(no_rows, m.no_cols);
119 int r_pos = 0, pos = 0, m_pos = 0;
121 for (
int i = 0; i < r.no_cols; i++) {
122 for (
int j = 0; j < r.no_rows; j++) {
123 std::complex<double> tmp(0.0);
125 for (
int k = 0; k < no_cols; k++) {
126 tmp += data[pos+j] * m.data[m_pos+k];
129 r.data[r_pos+j] = tmp;
140 #if defined(HAVE_BLAS)
142 mat
operator*(
const mat &m1,
const mat &m2)
144 it_assert_debug(m1.cols() == m2.rows(),
"mat::operator*(): Wrong sizes");
145 int m1_r = m1.rows();
int m1_c = m1.cols();
146 int m2_r = m2.rows();
int m2_c = m2.cols();
151 blas::dgemm_(&trans, &trans, &m1_r, &m2_c, &m1_c, &alpha,
152 m1._data(), &m1_r, m2._data(), &m2_r, &beta, r._data(),
158 cmat
operator*(
const cmat &m1,
const cmat &m2)
160 it_assert_debug(m1.cols() == m2.rows(),
"cmat::operator*(): Wrong sizes");
161 int m1_r = m1.rows();
int m1_c = m1.cols();
162 int m2_r = m2.rows();
int m2_c = m2.cols();
164 std::complex<double> alpha = std::complex<double>(1.0);
165 std::complex<double> beta = std::complex<double>(0.0);
167 blas::zgemm_(&trans, &trans, &m1_r, &m2_c, &m1_c, &alpha,
168 m1._data(), &m1_r, m2._data(), &m2_r, &beta, r._data(),
174 mat
operator*(
const mat &m1,
const mat &m2)
177 "Mat<>::operator*(): Wrong sizes");
178 mat r(m1.rows(), m2.cols());
179 double *tr = r._data();
181 const double *t2 = m2._data();
182 for (
int i = 0; i < r.cols(); i++) {
183 for (
int j = 0; j < r.rows(); j++) {
186 for (
int k = m1.cols(); k > 0; k--) {
187 tmp += *(t1) * *(t2++);
199 cmat
operator*(
const cmat &m1,
const cmat &m2)
202 "Mat<>::operator*(): Wrong sizes");
203 cmat r(m1.rows(), m2.cols());
204 std::complex<double> *tr = r._data();
205 const std::complex<double> *t1;
206 const std::complex<double> *t2 = m2._data();
207 for (
int i = 0; i < r.cols(); i++) {
208 for (
int j = 0; j < r.rows(); j++) {
209 std::complex<double> tmp(0.0);
211 for (
int k = m1.cols(); k > 0; k--) {
212 tmp += *(t1) * *(t2++);
225 #if defined(HAVE_BLAS)
227 vec
operator*(
const mat &m,
const vec &v)
229 it_assert_debug(m.cols() == v.size(),
"mat::operator*(): Wrong sizes");
230 int m_r = m.rows();
int m_c = m.cols();
236 blas::dgemv_(&trans, &m_r, &m_c, &alpha, m._data(), &m_r,
237 v._data(), &incr, &beta, r._data(), &incr);
242 cvec
operator*(
const cmat &m,
const cvec &v)
244 it_assert_debug(m.cols() == v.size(),
"cmat::operator*(): Wrong sizes");
245 int m_r = m.rows();
int m_c = m.cols();
247 std::complex<double> alpha = std::complex<double>(1.0);
248 std::complex<double> beta = std::complex<double>(0.0);
251 blas::zgemv_(&trans, &m_r, &m_c, &alpha, m._data(), &m_r,
252 v._data(), &incr, &beta, r._data(), &incr);
257 vec
operator*(
const mat &m,
const vec &v)
260 "Mat<>::operator*(): Wrong sizes");
262 for (
int i = 0; i < m.rows(); i++) {
265 for (
int k = 0; k < m.cols(); k++) {
266 r(i) += m._data()[m_pos+i] * v(k);
274 cvec
operator*(
const cmat &m,
const cvec &v)
277 "Mat<>::operator*(): Wrong sizes");
279 for (
int i = 0; i < m.rows(); i++) {
280 r(i) = std::complex<double>(0.0);
282 for (
int k = 0; k < m.cols(); k++) {
283 r(i) += m._data()[m_pos+i] * v(k);
298 template class ITPP_EXPORT Mat<double>;
299 template class ITPP_EXPORT Mat<std::complex<double> >;
300 template class ITPP_EXPORT Mat<int>;
301 template class ITPP_EXPORT Mat<short int>;
302 template class ITPP_EXPORT Mat<bin>;
306 template ITPP_EXPORT mat
operator+(
const mat &m1,
const mat &m2);
307 template ITPP_EXPORT cmat
operator+(
const cmat &m1,
const cmat &m2);
308 template ITPP_EXPORT imat
operator+(
const imat &m1,
const imat &m2);
309 template ITPP_EXPORT smat
operator+(
const smat &m1,
const smat &m2);
312 template ITPP_EXPORT mat
operator+(
const mat &m,
double t);
313 template ITPP_EXPORT cmat
operator+(
const cmat &m, std::complex<double> t);
314 template ITPP_EXPORT imat
operator+(
const imat &m,
int t);
315 template ITPP_EXPORT smat
operator+(
const smat &m,
short t);
318 template ITPP_EXPORT mat
operator+(
double t,
const mat &m);
319 template ITPP_EXPORT cmat
operator+(std::complex<double> t,
const cmat &m);
320 template ITPP_EXPORT imat
operator+(
int t,
const imat &m);
321 template ITPP_EXPORT smat
operator+(
short t,
const smat &m);
326 template ITPP_EXPORT mat
operator-(
const mat &m1,
const mat &m2);
327 template ITPP_EXPORT cmat
operator-(
const cmat &m1,
const cmat &m2);
328 template ITPP_EXPORT imat
operator-(
const imat &m1,
const imat &m2);
329 template ITPP_EXPORT smat
operator-(
const smat &m1,
const smat &m2);
332 template ITPP_EXPORT mat
operator-(
const mat &m,
double t);
333 template ITPP_EXPORT cmat
operator-(
const cmat &m, std::complex<double> t);
334 template ITPP_EXPORT imat
operator-(
const imat &m,
int t);
335 template ITPP_EXPORT smat
operator-(
const smat &m,
short t);
338 template ITPP_EXPORT mat
operator-(
double t,
const mat &m);
339 template ITPP_EXPORT cmat
operator-(std::complex<double> t,
const cmat &m);
340 template ITPP_EXPORT imat
operator-(
int t,
const imat &m);
341 template ITPP_EXPORT smat
operator-(
short t,
const smat &m);
346 template ITPP_EXPORT mat
operator-(
const mat &m);
347 template ITPP_EXPORT cmat
operator-(
const cmat &m);
348 template ITPP_EXPORT imat
operator-(
const imat &m);
349 template ITPP_EXPORT smat
operator-(
const smat &m);
354 template ITPP_EXPORT imat
operator*(
const imat &m1,
const imat &m2);
355 template ITPP_EXPORT smat
operator*(
const smat &m1,
const smat &m2);
358 template ITPP_EXPORT ivec
operator*(
const imat &m,
const ivec &v);
359 template ITPP_EXPORT svec
operator*(
const smat &m,
const svec &v);
360 template ITPP_EXPORT bvec
operator*(
const bmat &m,
const bvec &v);
362 template ITPP_EXPORT mat
operator*(
const mat &m,
double t);
363 template ITPP_EXPORT cmat
operator*(
const cmat &m, std::complex<double> t);
364 template ITPP_EXPORT imat
operator*(
const imat &m,
int t);
365 template ITPP_EXPORT smat
operator*(
const smat &m,
short t);
368 template ITPP_EXPORT mat
operator*(
double t,
const mat &m);
369 template ITPP_EXPORT cmat
operator*(std::complex<double> t,
const cmat &m);
370 template ITPP_EXPORT imat
operator*(
int t,
const imat &m);
371 template ITPP_EXPORT smat
operator*(
short t,
const smat &m);
376 template ITPP_EXPORT mat
elem_mult(
const mat &m1,
const mat &m2);
377 template ITPP_EXPORT cmat
elem_mult(
const cmat &m1,
const cmat &m2);
378 template ITPP_EXPORT imat
elem_mult(
const imat &m1,
const imat &m2);
379 template ITPP_EXPORT smat
elem_mult(
const smat &m1,
const smat &m2);
382 template ITPP_EXPORT
void elem_mult_out(
const mat &m1,
const mat &m2, mat &out);
383 template ITPP_EXPORT
void elem_mult_out(
const cmat &m1,
const cmat &m2, cmat &out);
384 template ITPP_EXPORT
void elem_mult_out(
const imat &m1,
const imat &m2, imat &out);
385 template ITPP_EXPORT
void elem_mult_out(
const smat &m1,
const smat &m2, smat &out);
388 template ITPP_EXPORT
void elem_mult_out(
const mat &m1,
const mat &m2,
389 const mat &m3, mat &out);
390 template ITPP_EXPORT
void elem_mult_out(
const cmat &m1,
const cmat &m2,
391 const cmat &m3, cmat &out);
392 template ITPP_EXPORT
void elem_mult_out(
const imat &m1,
const imat &m2,
393 const imat &m3, imat &out);
394 template ITPP_EXPORT
void elem_mult_out(
const smat &m1,
const smat &m2,
395 const smat &m3, smat &out);
399 template ITPP_EXPORT
void elem_mult_out(
const mat &m1,
const mat &m2,
const mat &m3,
400 const mat &m4, mat &out);
401 template ITPP_EXPORT
void elem_mult_out(
const cmat &m1,
const cmat &m2,
402 const cmat &m3,
const cmat &m4, cmat &out);
403 template ITPP_EXPORT
void elem_mult_out(
const imat &m1,
const imat &m2,
404 const imat &m3,
const imat &m4, imat &out);
405 template ITPP_EXPORT
void elem_mult_out(
const smat &m1,
const smat &m2,
406 const smat &m3,
const smat &m4, smat &out);
416 template ITPP_EXPORT
double elem_mult_sum(
const mat &m1,
const mat &m2);
417 template ITPP_EXPORT std::complex<double>
elem_mult_sum(
const cmat &m1,
const cmat &m2);
418 template ITPP_EXPORT
int elem_mult_sum(
const imat &m1,
const imat &m2);
419 template ITPP_EXPORT
short elem_mult_sum(
const smat &m1,
const smat &m2);
424 template ITPP_EXPORT mat
operator/(
double t,
const mat &m);
425 template ITPP_EXPORT cmat
operator/(std::complex<double> t,
const cmat &m);
426 template ITPP_EXPORT imat
operator/(
int t,
const imat &m);
427 template ITPP_EXPORT smat
operator/(
short t,
const smat &m);
430 template ITPP_EXPORT mat
operator/(
const mat &m,
double t);
431 template ITPP_EXPORT cmat
operator/(
const cmat &m, std::complex<double> t);
432 template ITPP_EXPORT imat
operator/(
const imat &m,
int t);
433 template ITPP_EXPORT smat
operator/(
const smat &m,
short t);
438 template ITPP_EXPORT mat
elem_div(
const mat &m1,
const mat &m2);
439 template ITPP_EXPORT cmat
elem_div(
const cmat &m1,
const cmat &m2);
440 template ITPP_EXPORT imat
elem_div(
const imat &m1,
const imat &m2);
441 template ITPP_EXPORT smat
elem_div(
const smat &m1,
const smat &m2);
444 template ITPP_EXPORT
void elem_div_out(
const mat &m1,
const mat &m2, mat &out);
445 template ITPP_EXPORT
void elem_div_out(
const cmat &m1,
const cmat &m2, cmat &out);
446 template ITPP_EXPORT
void elem_div_out(
const imat &m1,
const imat &m2, imat &out);
447 template ITPP_EXPORT
void elem_div_out(
const smat &m1,
const smat &m2, smat &out);
450 template ITPP_EXPORT
double elem_div_sum(
const mat &m1,
const mat &m2);
451 template ITPP_EXPORT std::complex<double>
elem_div_sum(
const cmat &m1,
453 template ITPP_EXPORT
int elem_div_sum(
const imat &m1,
const imat &m2);
454 template ITPP_EXPORT
short elem_div_sum(
const smat &m1,
const smat &m2);
465 template ITPP_EXPORT mat
concat_vertical(
const mat &m1,
const mat &m2);
466 template ITPP_EXPORT cmat
concat_vertical(
const cmat &m1,
const cmat &m2);
467 template ITPP_EXPORT imat
concat_vertical(
const imat &m1,
const imat &m2);
468 template ITPP_EXPORT smat
concat_vertical(
const smat &m1,
const smat &m2);
473 template ITPP_EXPORT std::ostream &
operator<<(std::ostream &os,
const mat &m);
474 template ITPP_EXPORT std::ostream &
operator<<(std::ostream &os,
const cmat &m);
475 template ITPP_EXPORT std::ostream &
operator<<(std::ostream &os,
const imat &m);
476 template ITPP_EXPORT std::ostream &
operator<<(std::ostream &os,
const smat &m);
477 template ITPP_EXPORT std::ostream &
operator<<(std::ostream &os,
const bmat &m);
479 template ITPP_EXPORT std::istream &
operator>>(std::istream &is, mat &m);
480 template ITPP_EXPORT std::istream &
operator>>(std::istream &is, cmat &m);
481 template ITPP_EXPORT std::istream &
operator>>(std::istream &is, imat &m);
482 template ITPP_EXPORT std::istream &
operator>>(std::istream &is, smat &m);
483 template ITPP_EXPORT std::istream &
operator>>(std::istream &is,
bmat &m);