32# include <itpp/config.h>
34# include <itpp/config_msvc.h>
37#if defined (HAVE_BLAS)
38# include <itpp/base/blas.h>
47cmat cmat::hermitian_transpose()
const
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++)
52 temp(j, i) = std::conj(
operator()(i,j));
63mat& 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);
77cmat& 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);
91mat& 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;
115cmat& 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)
142mat
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(),
158cmat
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(),
174mat
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++);
199cmat
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)
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);
242cvec
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);
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);
274cvec
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);
306template ITPP_EXPORT mat
operator+(
const mat &m1,
const mat &m2);
307template ITPP_EXPORT cmat
operator+(
const cmat &m1,
const cmat &m2);
308template ITPP_EXPORT imat
operator+(
const imat &m1,
const imat &m2);
309template ITPP_EXPORT smat
operator+(
const smat &m1,
const smat &m2);
312template ITPP_EXPORT mat
operator+(
const mat &m,
double t);
313template ITPP_EXPORT cmat
operator+(
const cmat &m, std::complex<double> t);
314template ITPP_EXPORT imat
operator+(
const imat &m,
int t);
315template ITPP_EXPORT smat
operator+(
const smat &m,
short t);
318template ITPP_EXPORT mat
operator+(
double t,
const mat &m);
319template ITPP_EXPORT cmat
operator+(std::complex<double> t,
const cmat &m);
320template ITPP_EXPORT imat
operator+(
int t,
const imat &m);
321template ITPP_EXPORT smat
operator+(
short t,
const smat &m);
326template ITPP_EXPORT mat
operator-(
const mat &m1,
const mat &m2);
327template ITPP_EXPORT cmat
operator-(
const cmat &m1,
const cmat &m2);
328template ITPP_EXPORT imat
operator-(
const imat &m1,
const imat &m2);
329template ITPP_EXPORT smat
operator-(
const smat &m1,
const smat &m2);
332template ITPP_EXPORT mat
operator-(
const mat &m,
double t);
333template ITPP_EXPORT cmat
operator-(
const cmat &m, std::complex<double> t);
334template ITPP_EXPORT imat
operator-(
const imat &m,
int t);
335template ITPP_EXPORT smat
operator-(
const smat &m,
short t);
338template ITPP_EXPORT mat
operator-(
double t,
const mat &m);
339template ITPP_EXPORT cmat
operator-(std::complex<double> t,
const cmat &m);
340template ITPP_EXPORT imat
operator-(
int t,
const imat &m);
341template ITPP_EXPORT smat
operator-(
short t,
const smat &m);
346template ITPP_EXPORT mat
operator-(
const mat &m);
347template ITPP_EXPORT cmat
operator-(
const cmat &m);
348template ITPP_EXPORT imat
operator-(
const imat &m);
349template ITPP_EXPORT smat
operator-(
const smat &m);
354template ITPP_EXPORT imat
operator*(
const imat &m1,
const imat &m2);
355template ITPP_EXPORT smat
operator*(
const smat &m1,
const smat &m2);
358template ITPP_EXPORT ivec
operator*(
const imat &m,
const ivec &v);
359template ITPP_EXPORT svec
operator*(
const smat &m,
const svec &v);
360template ITPP_EXPORT bvec
operator*(
const bmat &m,
const bvec &v);
362template ITPP_EXPORT mat
operator*(
const mat &m,
double t);
363template ITPP_EXPORT cmat
operator*(
const cmat &m, std::complex<double> t);
364template ITPP_EXPORT imat
operator*(
const imat &m,
int t);
365template ITPP_EXPORT smat
operator*(
const smat &m,
short t);
368template ITPP_EXPORT mat
operator*(
double t,
const mat &m);
369template ITPP_EXPORT cmat
operator*(std::complex<double> t,
const cmat &m);
370template ITPP_EXPORT imat
operator*(
int t,
const imat &m);
371template ITPP_EXPORT smat
operator*(
short t,
const smat &m);
376template ITPP_EXPORT mat
elem_mult(
const mat &m1,
const mat &m2);
377template ITPP_EXPORT cmat
elem_mult(
const cmat &m1,
const cmat &m2);
378template ITPP_EXPORT imat
elem_mult(
const imat &m1,
const imat &m2);
379template ITPP_EXPORT smat
elem_mult(
const smat &m1,
const smat &m2);
382template ITPP_EXPORT
void elem_mult_out(
const mat &m1,
const mat &m2, mat &out);
383template ITPP_EXPORT
void elem_mult_out(
const cmat &m1,
const cmat &m2, cmat &out);
384template ITPP_EXPORT
void elem_mult_out(
const imat &m1,
const imat &m2, imat &out);
385template ITPP_EXPORT
void elem_mult_out(
const smat &m1,
const smat &m2, smat &out);
388template ITPP_EXPORT
void elem_mult_out(
const mat &m1,
const mat &m2,
389 const mat &m3, mat &out);
390template ITPP_EXPORT
void elem_mult_out(
const cmat &m1,
const cmat &m2,
391 const cmat &m3, cmat &out);
392template ITPP_EXPORT
void elem_mult_out(
const imat &m1,
const imat &m2,
393 const imat &m3, imat &out);
394template ITPP_EXPORT
void elem_mult_out(
const smat &m1,
const smat &m2,
395 const smat &m3, smat &out);
399template ITPP_EXPORT
void elem_mult_out(
const mat &m1,
const mat &m2,
const mat &m3,
400 const mat &m4, mat &out);
401template ITPP_EXPORT
void elem_mult_out(
const cmat &m1,
const cmat &m2,
402 const cmat &m3,
const cmat &m4, cmat &out);
403template ITPP_EXPORT
void elem_mult_out(
const imat &m1,
const imat &m2,
404 const imat &m3,
const imat &m4, imat &out);
405template ITPP_EXPORT
void elem_mult_out(
const smat &m1,
const smat &m2,
406 const smat &m3,
const smat &m4, smat &out);
416template ITPP_EXPORT
double elem_mult_sum(
const mat &m1,
const mat &m2);
417template ITPP_EXPORT std::complex<double>
elem_mult_sum(
const cmat &m1,
const cmat &m2);
418template ITPP_EXPORT
int elem_mult_sum(
const imat &m1,
const imat &m2);
419template ITPP_EXPORT
short elem_mult_sum(
const smat &m1,
const smat &m2);
424template ITPP_EXPORT mat
operator/(
double t,
const mat &m);
425template ITPP_EXPORT cmat
operator/(std::complex<double> t,
const cmat &m);
426template ITPP_EXPORT imat
operator/(
int t,
const imat &m);
427template ITPP_EXPORT smat
operator/(
short t,
const smat &m);
430template ITPP_EXPORT mat
operator/(
const mat &m,
double t);
431template ITPP_EXPORT cmat
operator/(
const cmat &m, std::complex<double> t);
432template ITPP_EXPORT imat
operator/(
const imat &m,
int t);
433template ITPP_EXPORT smat
operator/(
const smat &m,
short t);
438template ITPP_EXPORT mat
elem_div(
const mat &m1,
const mat &m2);
439template ITPP_EXPORT cmat
elem_div(
const cmat &m1,
const cmat &m2);
440template ITPP_EXPORT imat
elem_div(
const imat &m1,
const imat &m2);
441template ITPP_EXPORT smat
elem_div(
const smat &m1,
const smat &m2);
444template ITPP_EXPORT
void elem_div_out(
const mat &m1,
const mat &m2, mat &out);
445template ITPP_EXPORT
void elem_div_out(
const cmat &m1,
const cmat &m2, cmat &out);
446template ITPP_EXPORT
void elem_div_out(
const imat &m1,
const imat &m2, imat &out);
447template ITPP_EXPORT
void elem_div_out(
const smat &m1,
const smat &m2, smat &out);
450template ITPP_EXPORT
double elem_div_sum(
const mat &m1,
const mat &m2);
451template ITPP_EXPORT std::complex<double>
elem_div_sum(
const cmat &m1,
453template ITPP_EXPORT
int elem_div_sum(
const imat &m1,
const imat &m2);
454template ITPP_EXPORT
short elem_div_sum(
const smat &m1,
const smat &m2);
466template ITPP_EXPORT cmat
concat_vertical(
const cmat &m1,
const cmat &m2);
467template ITPP_EXPORT imat
concat_vertical(
const imat &m1,
const imat &m2);
468template ITPP_EXPORT smat
concat_vertical(
const smat &m1,
const smat &m2);
473template ITPP_EXPORT std::ostream &
operator<<(std::ostream &os,
const mat &m);
474template ITPP_EXPORT std::ostream &
operator<<(std::ostream &os,
const cmat &m);
475template ITPP_EXPORT std::ostream &
operator<<(std::ostream &os,
const imat &m);
476template ITPP_EXPORT std::ostream &
operator<<(std::ostream &os,
const smat &m);
477template ITPP_EXPORT std::ostream &
operator<<(std::ostream &os,
const bmat &m);
479template ITPP_EXPORT std::istream &
operator>>(std::istream &is, mat &m);
480template ITPP_EXPORT std::istream &
operator>>(std::istream &is, cmat &m);
481template ITPP_EXPORT std::istream &
operator>>(std::istream &is, imat &m);
482template ITPP_EXPORT std::istream &
operator>>(std::istream &is, smat &m);
483template ITPP_EXPORT std::istream &
operator>>(std::istream &is,
bmat &m);
Binary arithmetic (boolean) class.
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Matrix Class Definitions.
Mat< bin > bmat
bin matrix
void elem_mult_inplace(const Mat< Num_T > &m1, Mat< Num_T > &m2)
In-place element wise multiplication of two matrices. Fast version of B = elem_mult(A,...
std::ostream & operator<<(std::ostream &output, const bin &inbin)
Output stream of bin.
Mat< Num_T > concat_horizontal(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Horizontal concatenation of two matrices.
Mat< Num_T > operator-(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Subtraction of two matrices.
GF2mat operator*(const GF2mat &X, const GF2mat &Y)
GF(2) matrix multiplication.
Mat< Num_T > operator/(const Mat< Num_T > &m, Num_T t)
Element-wise division by a scalar.
void elem_div_out(const Mat< Num_T > &m1, const Mat< Num_T > &m2, Mat< Num_T > &out)
Element wise division of two matrices, storing the result in matrix out.
std::istream & operator>>(std::istream &input, bin &outbin)
Input stream of bin.
Mat< Num_T > elem_div(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Element wise division of two matrices.
Num_T elem_div_sum(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Element wise division of two matrices, followed by summation of the resultant elements....
GF2mat operator+(const GF2mat &X, const GF2mat &Y)
GF(2) matrix addition.
Mat< Num_T > elem_mult(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Element wise multiplication of two matrices.
Num_T elem_mult_sum(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Element wise multiplication of two matrices, followed by summation of the resultant elements....
Mat< Num_T > concat_vertical(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Vertical concatenation of two matrices.
void elem_mult_out(const Mat< Num_T > &m1, const Mat< Num_T > &m2, Mat< Num_T > &out)
Element wise multiplication of two matrices, storing the result in matrix out.