mat.cpp
Go to the documentation of this file.
#include <itpp/base/mat.h>
#ifndef _MSC_VER
# include <itpp/config.h>
#else
# include <itpp/config_msvc.h>
#endif
#if defined (HAVE_BLAS)
# include <itpp/base/blas.h>
#endif
namespace itpp
{
template<>
cmat cmat::hermitian_transpose() const
{
cmat temp(no_cols, no_rows);
for (int i = 0; i < no_rows; i++)
for (int j = 0; j < no_cols; j++)
temp(j, i) = std::conj(operator()(i,j));
return temp;
}
// -------- Multiplication operator -------------
#if defined(HAVE_BLAS)
template<>
mat& mat::operator*=(const mat &m)
{
it_assert_debug(no_cols == m.no_rows, "mat::operator*=(): Wrong sizes");
mat r(no_rows, m.no_cols); // unnecessary memory??
double alpha = 1.0;
double beta = 0.0;
char trans = 'n';
blas::dgemm_(&trans, &trans, &no_rows, &m.no_cols, &no_cols, &alpha, data,
&no_rows, m.data, &m.no_rows, &beta, r.data, &r.no_rows);
operator=(r); // time consuming
return *this;
}
template<>
cmat& cmat::operator*=(const cmat &m)
{
it_assert_debug(no_cols == m.no_rows, "cmat::operator*=(): Wrong sizes");
cmat r(no_rows, m.no_cols); // unnecessary memory??
std::complex<double> alpha = std::complex<double>(1.0);
std::complex<double> beta = std::complex<double>(0.0);
char trans = 'n';
blas::zgemm_(&trans, &trans, &no_rows, &m.no_cols, &no_cols, &alpha, data,
&no_rows, m.data, &m.no_rows, &beta, r.data, &r.no_rows);
operator=(r); // time consuming
return *this;
}
#else
template<>
mat& mat::operator*=(const mat &m)
{
it_assert_debug(no_cols == m.no_rows, "Mat<>::operator*=(): Wrong sizes");
mat r(no_rows, m.no_cols);
int r_pos = 0, pos = 0, m_pos = 0;
for (int i = 0; i < r.no_cols; i++) {
for (int j = 0; j < r.no_rows; j++) {
double tmp = 0.0;
pos = 0;
for (int k = 0; k < no_cols; k++) {
tmp += data[pos+j] * m.data[m_pos+k];
pos += no_rows;
}
r.data[r_pos+j] = tmp;
}
r_pos += r.no_rows;
m_pos += m.no_rows;
}
operator=(r); // time consuming
return *this;
}
template<>
cmat& cmat::operator*=(const cmat &m)
{
it_assert_debug(no_cols == m.no_rows, "Mat<>::operator*=(): Wrong sizes");
cmat r(no_rows, m.no_cols);
int r_pos = 0, pos = 0, m_pos = 0;
for (int i = 0; i < r.no_cols; i++) {
for (int j = 0; j < r.no_rows; j++) {
std::complex<double> tmp(0.0);
pos = 0;
for (int k = 0; k < no_cols; k++) {
tmp += data[pos+j] * m.data[m_pos+k];
pos += no_rows;
}
r.data[r_pos+j] = tmp;
}
r_pos += r.no_rows;
m_pos += m.no_rows;
}
operator=(r); // time consuming
return *this;
}
#endif // HAVE_BLAS
#if defined(HAVE_BLAS)
template<>
mat operator*(const mat &m1, const mat &m2)
{
it_assert_debug(m1.cols() == m2.rows(), "mat::operator*(): Wrong sizes");
int m1_r = m1.rows(); int m1_c = m1.cols();
int m2_r = m2.rows(); int m2_c = m2.cols();
mat r(m1_r, m2_c);
double alpha = 1.0;
double beta = 0.0;
char trans = 'n';
blas::dgemm_(&trans, &trans, &m1_r, &m2_c, &m1_c, &alpha,
m1._data(), &m1_r, m2._data(), &m2_r, &beta, r._data(),
&m1_r);
return r;
}
template<>
cmat operator*(const cmat &m1, const cmat &m2)
{
it_assert_debug(m1.cols() == m2.rows(), "cmat::operator*(): Wrong sizes");
int m1_r = m1.rows(); int m1_c = m1.cols();
int m2_r = m2.rows(); int m2_c = m2.cols();
cmat r(m1_r, m2_c);
std::complex<double> alpha = std::complex<double>(1.0);
std::complex<double> beta = std::complex<double>(0.0);
char trans = 'n';
blas::zgemm_(&trans, &trans, &m1_r, &m2_c, &m1_c, &alpha,
m1._data(), &m1_r, m2._data(), &m2_r, &beta, r._data(),
&m1_r);
return r;
}
#else
template<>
mat operator*(const mat &m1, const mat &m2)
{
it_assert_debug(m1.rows() == m2.cols(),
"Mat<>::operator*(): Wrong sizes");
mat r(m1.rows(), m2.cols());
double *tr = r._data();
const double *t1;
const double *t2 = m2._data();
for (int i = 0; i < r.cols(); i++) {
for (int j = 0; j < r.rows(); j++) {
double tmp = 0.0;
t1 = m1._data() + j;
for (int k = m1.cols(); k > 0; k--) {
tmp += *(t1) * *(t2++);
t1 += m1.rows();
}
*(tr++) = tmp;
t2 -= m2.rows();
}
t2 += m2.rows();
}
return r;
}
template<>
cmat operator*(const cmat &m1, const cmat &m2)
{
it_assert_debug(m1.cols() == m2.rows(),
"Mat<>::operator*(): Wrong sizes");
cmat r(m1.rows(), m2.cols());
std::complex<double> *tr = r._data();
const std::complex<double> *t1;
const std::complex<double> *t2 = m2._data();
for (int i = 0; i < r.cols(); i++) {
for (int j = 0; j < r.rows(); j++) {
std::complex<double> tmp(0.0);
t1 = m1._data() + j;
for (int k = m1.cols(); k > 0; k--) {
tmp += *(t1) * *(t2++);
t1 += m1.rows();
}
*(tr++) = tmp;
t2 -= m2.rows();
}
t2 += m2.rows();
}
return r;
}
#endif // HAVE_BLAS
#if defined(HAVE_BLAS)
template<>
vec operator*(const mat &m, const vec &v)
{
it_assert_debug(m.cols() == v.size(), "mat::operator*(): Wrong sizes");
int m_r = m.rows(); int m_c = m.cols();
vec r(m_r);
double alpha = 1.0;
double beta = 0.0;
char trans = 'n';
int incr = 1;
blas::dgemv_(&trans, &m_r, &m_c, &alpha, m._data(), &m_r,
v._data(), &incr, &beta, r._data(), &incr);
return r;
}
template<>
cvec operator*(const cmat &m, const cvec &v)
{
it_assert_debug(m.cols() == v.size(), "cmat::operator*(): Wrong sizes");
int m_r = m.rows(); int m_c = m.cols();
cvec r(m_r);
std::complex<double> alpha = std::complex<double>(1.0);
std::complex<double> beta = std::complex<double>(0.0);
char trans = 'n';
int incr = 1;
blas::zgemv_(&trans, &m_r, &m_c, &alpha, m._data(), &m_r,
v._data(), &incr, &beta, r._data(), &incr);
return r;
}
#else
template<>
vec operator*(const mat &m, const vec &v)
{
it_assert_debug(m.cols() == v.size(),
"Mat<>::operator*(): Wrong sizes");
vec r(m.rows());
for (int i = 0; i < m.rows(); i++) {
r(i) = 0.0;
int m_pos = 0;
for (int k = 0; k < m.cols(); k++) {
r(i) += m._data()[m_pos+i] * v(k);
m_pos += m.rows();
}
}
return r;
}
template<>
cvec operator*(const cmat &m, const cvec &v)
{
it_assert_debug(m.cols() == v.size(),
"Mat<>::operator*(): Wrong sizes");
cvec r(m.rows());
for (int i = 0; i < m.rows(); i++) {
r(i) = std::complex<double>(0.0);
int m_pos = 0;
for (int k = 0; k < m.cols(); k++) {
r(i) += m._data()[m_pos+i] * v(k);
m_pos += m.rows();
}
}
return r;
}
#endif // HAVE_BLAS
//---------------------------------------------------------------------
// Instantiations
//---------------------------------------------------------------------
// class instantiations
template class ITPP_EXPORT Mat<double>;
template class ITPP_EXPORT Mat<std::complex<double> >;
template class ITPP_EXPORT Mat<int>;
template class ITPP_EXPORT Mat<short int>;
template class ITPP_EXPORT Mat<bin>;
// addition operators
template ITPP_EXPORT mat operator+(const mat &m1, const mat &m2);
template ITPP_EXPORT cmat operator+(const cmat &m1, const cmat &m2);
template ITPP_EXPORT imat operator+(const imat &m1, const imat &m2);
template ITPP_EXPORT smat operator+(const smat &m1, const smat &m2);
template ITPP_EXPORT bmat operator+(const bmat &m1, const bmat &m2);
template ITPP_EXPORT mat operator+(const mat &m, double t);
template ITPP_EXPORT cmat operator+(const cmat &m, std::complex<double> t);
template ITPP_EXPORT imat operator+(const imat &m, int t);
template ITPP_EXPORT smat operator+(const smat &m, short t);
template ITPP_EXPORT bmat operator+(const bmat &m, bin t);
template ITPP_EXPORT mat operator+(double t, const mat &m);
template ITPP_EXPORT cmat operator+(std::complex<double> t, const cmat &m);
template ITPP_EXPORT imat operator+(int t, const imat &m);
template ITPP_EXPORT smat operator+(short t, const smat &m);
template ITPP_EXPORT bmat operator+(bin t, const bmat &m);
// subraction operators
template ITPP_EXPORT mat operator-(const mat &m1, const mat &m2);
template ITPP_EXPORT cmat operator-(const cmat &m1, const cmat &m2);
template ITPP_EXPORT imat operator-(const imat &m1, const imat &m2);
template ITPP_EXPORT smat operator-(const smat &m1, const smat &m2);
template ITPP_EXPORT bmat operator-(const bmat &m1, const bmat &m2);
template ITPP_EXPORT mat operator-(const mat &m, double t);
template ITPP_EXPORT cmat operator-(const cmat &m, std::complex<double> t);
template ITPP_EXPORT imat operator-(const imat &m, int t);
template ITPP_EXPORT smat operator-(const smat &m, short t);
template ITPP_EXPORT bmat operator-(const bmat &m, bin t);
template ITPP_EXPORT mat operator-(double t, const mat &m);
template ITPP_EXPORT cmat operator-(std::complex<double> t, const cmat &m);
template ITPP_EXPORT imat operator-(int t, const imat &m);
template ITPP_EXPORT smat operator-(short t, const smat &m);
template ITPP_EXPORT bmat operator-(bin t, const bmat &m);
// unary minus
template ITPP_EXPORT mat operator-(const mat &m);
template ITPP_EXPORT cmat operator-(const cmat &m);
template ITPP_EXPORT imat operator-(const imat &m);
template ITPP_EXPORT smat operator-(const smat &m);
template ITPP_EXPORT bmat operator-(const bmat &m);
// multiplication operators
template ITPP_EXPORT imat operator*(const imat &m1, const imat &m2);
template ITPP_EXPORT smat operator*(const smat &m1, const smat &m2);
template ITPP_EXPORT bmat operator*(const bmat &m1, const bmat &m2);
template ITPP_EXPORT ivec operator*(const imat &m, const ivec &v);
template ITPP_EXPORT svec operator*(const smat &m, const svec &v);
template ITPP_EXPORT bvec operator*(const bmat &m, const bvec &v);
template ITPP_EXPORT mat operator*(const mat &m, double t);
template ITPP_EXPORT cmat operator*(const cmat &m, std::complex<double> t);
template ITPP_EXPORT imat operator*(const imat &m, int t);
template ITPP_EXPORT smat operator*(const smat &m, short t);
template ITPP_EXPORT bmat operator*(const bmat &m, bin t);
template ITPP_EXPORT mat operator*(double t, const mat &m);
template ITPP_EXPORT cmat operator*(std::complex<double> t, const cmat &m);
template ITPP_EXPORT imat operator*(int t, const imat &m);
template ITPP_EXPORT smat operator*(short t, const smat &m);
template ITPP_EXPORT bmat operator*(bin t, const bmat &m);
// elementwise multiplication
template ITPP_EXPORT mat elem_mult(const mat &m1, const mat &m2);
template ITPP_EXPORT cmat elem_mult(const cmat &m1, const cmat &m2);
template ITPP_EXPORT imat elem_mult(const imat &m1, const imat &m2);
template ITPP_EXPORT smat elem_mult(const smat &m1, const smat &m2);
template ITPP_EXPORT bmat elem_mult(const bmat &m1, const bmat &m2);
template ITPP_EXPORT void elem_mult_out(const mat &m1, const mat &m2, mat &out);
template ITPP_EXPORT void elem_mult_out(const cmat &m1, const cmat &m2, cmat &out);
template ITPP_EXPORT void elem_mult_out(const imat &m1, const imat &m2, imat &out);
template ITPP_EXPORT void elem_mult_out(const smat &m1, const smat &m2, smat &out);
template ITPP_EXPORT void elem_mult_out(const bmat &m1, const bmat &m2, bmat &out);
template ITPP_EXPORT void elem_mult_out(const mat &m1, const mat &m2,
const mat &m3, mat &out);
template ITPP_EXPORT void elem_mult_out(const cmat &m1, const cmat &m2,
const cmat &m3, cmat &out);
template ITPP_EXPORT void elem_mult_out(const imat &m1, const imat &m2,
const imat &m3, imat &out);
template ITPP_EXPORT void elem_mult_out(const smat &m1, const smat &m2,
const smat &m3, smat &out);
template ITPP_EXPORT void elem_mult_out(const bmat &m1, const bmat &m2,
const bmat &m3, bmat &out);
template ITPP_EXPORT void elem_mult_out(const mat &m1, const mat &m2, const mat &m3,
const mat &m4, mat &out);
template ITPP_EXPORT void elem_mult_out(const cmat &m1, const cmat &m2,
const cmat &m3, const cmat &m4, cmat &out);
template ITPP_EXPORT void elem_mult_out(const imat &m1, const imat &m2,
const imat &m3, const imat &m4, imat &out);
template ITPP_EXPORT void elem_mult_out(const smat &m1, const smat &m2,
const smat &m3, const smat &m4, smat &out);
template ITPP_EXPORT void elem_mult_out(const bmat &m1, const bmat &m2,
const bmat &m3, const bmat &m4, bmat &out);
template ITPP_EXPORT void elem_mult_inplace(const mat &m1, mat &m2);
template ITPP_EXPORT void elem_mult_inplace(const cmat &m1, cmat &m2);
template ITPP_EXPORT void elem_mult_inplace(const imat &m1, imat &m2);
template ITPP_EXPORT void elem_mult_inplace(const smat &m1, smat &m2);
template ITPP_EXPORT void elem_mult_inplace(const bmat &m1, bmat &m2);
template ITPP_EXPORT double elem_mult_sum(const mat &m1, const mat &m2);
template ITPP_EXPORT std::complex<double> elem_mult_sum(const cmat &m1, const cmat &m2);
template ITPP_EXPORT int elem_mult_sum(const imat &m1, const imat &m2);
template ITPP_EXPORT short elem_mult_sum(const smat &m1, const smat &m2);
template ITPP_EXPORT bin elem_mult_sum(const bmat &m1, const bmat &m2);
// division operator
template ITPP_EXPORT mat operator/(double t, const mat &m);
template ITPP_EXPORT cmat operator/(std::complex<double> t, const cmat &m);
template ITPP_EXPORT imat operator/(int t, const imat &m);
template ITPP_EXPORT smat operator/(short t, const smat &m);
template ITPP_EXPORT bmat operator/(bin t, const bmat &m);
template ITPP_EXPORT mat operator/(const mat &m, double t);
template ITPP_EXPORT cmat operator/(const cmat &m, std::complex<double> t);
template ITPP_EXPORT imat operator/(const imat &m, int t);
template ITPP_EXPORT smat operator/(const smat &m, short t);
template ITPP_EXPORT bmat operator/(const bmat &m, bin t);
// elementwise division
template ITPP_EXPORT mat elem_div(const mat &m1, const mat &m2);
template ITPP_EXPORT cmat elem_div(const cmat &m1, const cmat &m2);
template ITPP_EXPORT imat elem_div(const imat &m1, const imat &m2);
template ITPP_EXPORT smat elem_div(const smat &m1, const smat &m2);
template ITPP_EXPORT bmat elem_div(const bmat &m1, const bmat &m2);
template ITPP_EXPORT void elem_div_out(const mat &m1, const mat &m2, mat &out);
template ITPP_EXPORT void elem_div_out(const cmat &m1, const cmat &m2, cmat &out);
template ITPP_EXPORT void elem_div_out(const imat &m1, const imat &m2, imat &out);
template ITPP_EXPORT void elem_div_out(const smat &m1, const smat &m2, smat &out);
template ITPP_EXPORT void elem_div_out(const bmat &m1, const bmat &m2, bmat &out);
template ITPP_EXPORT double elem_div_sum(const mat &m1, const mat &m2);
template ITPP_EXPORT std::complex<double> elem_div_sum(const cmat &m1,
const cmat &m2);
template ITPP_EXPORT int elem_div_sum(const imat &m1, const imat &m2);
template ITPP_EXPORT short elem_div_sum(const smat &m1, const smat &m2);
template ITPP_EXPORT bin elem_div_sum(const bmat &m1, const bmat &m2);
// concatenation
template ITPP_EXPORT mat concat_horizontal(const mat &m1, const mat &m2);
template ITPP_EXPORT cmat concat_horizontal(const cmat &m1, const cmat &m2);
template ITPP_EXPORT imat concat_horizontal(const imat &m1, const imat &m2);
template ITPP_EXPORT smat concat_horizontal(const smat &m1, const smat &m2);
template ITPP_EXPORT bmat concat_horizontal(const bmat &m1, const bmat &m2);
template ITPP_EXPORT mat concat_vertical(const mat &m1, const mat &m2);
template ITPP_EXPORT cmat concat_vertical(const cmat &m1, const cmat &m2);
template ITPP_EXPORT imat concat_vertical(const imat &m1, const imat &m2);
template ITPP_EXPORT smat concat_vertical(const smat &m1, const smat &m2);
template ITPP_EXPORT bmat concat_vertical(const bmat &m1, const bmat &m2);
// I/O streams
template ITPP_EXPORT std::ostream &operator<<(std::ostream &os, const mat &m);
template ITPP_EXPORT std::ostream &operator<<(std::ostream &os, const cmat &m);
template ITPP_EXPORT std::ostream &operator<<(std::ostream &os, const imat &m);
template ITPP_EXPORT std::ostream &operator<<(std::ostream &os, const smat &m);
template ITPP_EXPORT std::ostream &operator<<(std::ostream &os, const bmat &m);
template ITPP_EXPORT std::istream &operator>>(std::istream &is, mat &m);
template ITPP_EXPORT std::istream &operator>>(std::istream &is, cmat &m);
template ITPP_EXPORT std::istream &operator>>(std::istream &is, imat &m);
template ITPP_EXPORT std::istream &operator>>(std::istream &is, smat &m);
template ITPP_EXPORT std::istream &operator>>(std::istream &is, bmat &m);
} // namespace itpp
it_assert_debug
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition itassert.h:107
mat.h
Matrix Class Definitions.
bmat
Mat< bin > bmat
bin matrix
Definition mat.h:508
itpp
itpp namespace
Definition itmex.h:37
itpp::elem_mult_inplace
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,...
Definition mat.h:1630
itpp::operator<<
std::ostream & operator<<(std::ostream &output, const bin &inbin)
Output stream of bin.
Definition binary.cpp:36
itpp::concat_horizontal
Mat< Num_T > concat_horizontal(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Horizontal concatenation of two matrices.
Definition mat.h:1194
itpp::operator-
Mat< Num_T > operator-(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Subtraction of two matrices.
Definition mat.h:1382
itpp::operator*
GF2mat operator*(const GF2mat &X, const GF2mat &Y)
GF(2) matrix multiplication.
Definition gf2mat.cpp:847
itpp::operator/
Mat< Num_T > operator/(const Mat< Num_T > &m, Num_T t)
Element-wise division by a scalar.
Definition mat.h:1670
itpp::elem_div_out
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.
Definition mat.h:1696
itpp::operator>>
std::istream & operator>>(std::istream &input, bin &outbin)
Input stream of bin.
Definition binary.cpp:42
itpp::elem_div
Mat< Num_T > elem_div(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Element wise division of two matrices.
Definition mat.h:1688
itpp::elem_div_sum
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....
Definition mat.h:1710
itpp::operator+
GF2mat operator+(const GF2mat &X, const GF2mat &Y)
GF(2) matrix addition.
Definition gf2mat.cpp:948
itpp::elem_mult
Mat< Num_T > elem_mult(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Element wise multiplication of two matrices.
Definition mat.h:1582
itpp::elem_mult_sum
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....
Definition mat.h:1639
itpp::concat_vertical
Mat< Num_T > concat_vertical(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Vertical concatenation of two matrices.
Definition mat.h:1216
itpp::elem_mult_out
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.
Definition mat.h:1590

Generated on Mon Apr 22 2024 09:45:27 for IT++ by Doxygen 1.9.8