10 #define DENSEMATRIX_HH 25 template<
typename REAL>
31 typedef typename std::vector<REAL> VType;
32 typedef typename VType::const_iterator ConstVectorIterator;
33 typedef typename VType::iterator VectorIterator;
40 static bool bScientific;
41 static std::size_t nIndexWidth;
42 static std::size_t nValueWidth;
43 static std::size_t nValuePrecision;
47 REAL myabs (REAL x)
const 57 inline REAL & at(
const std::size_t row,
const std::size_t col)
59 return m_data[row * m_cols + col];
63 inline const REAL & at(
const std::size_t row,
const std::size_t col)
const 65 return m_data[row * m_cols + col];
80 const std::size_t _cols,
83 : m_data( _rows*_cols, def_val )
91 m_cols = rowvector.size();
92 for(std::size_t i=0; i<m_cols; i++ )
93 m_data.push_back( rowvector[i] );
148 bool scientific()
const 196 return nValuePrecision;
264 inline REAL &
operator()(
const std::size_t row,
const std::size_t col)
266 assert(row < m_rows|| col < m_cols);
271 inline const REAL &
operator()(
const std::size_t row,
const std::size_t col)
const 273 assert(row < m_rows|| col < m_cols);
279 const ConstVectorIterator
operator[](
const std::size_t row)
const 281 assert(row < m_rows);
282 return m_data.begin() + row * m_cols;
288 assert(row < m_rows);
289 return m_data.begin() + row * m_cols;
349 for (std::size_t i=0; i<
rowsize(); i++)
350 for (std::size_t j=0; j<
colsize(); j++)
351 (*
this)(i,j) = value;
370 size_type k1=0, k2=0;
371 for (size_type i_=i; i_ < i+rows; i_++){
372 for (size_type j_=j; j_ < j+cols; j_++){
373 A[k1][k2] =
self[i_][j_];
394 for (std::size_t i=0; i<
rowsize(); ++i)
395 for (std::size_t j=0; j<
colsize(); ++j)
396 (*
this)(i,j) += B(i,j);
409 for (std::size_t i=0; i<
rowsize(); ++i)
410 for (std::size_t j=0; j<
colsize(); ++j)
411 (*
this)(i,j) -= B(i,j);
447 for (std::size_t i=0; i<
rowsize(); ++i)
448 for (std::size_t j=0; j<
colsize(); ++j)
486 for (std::size_t i=0; i<
rowsize(); ++i)
487 for (std::size_t j=0; j<
colsize(); ++j)
521 for (std::size_t i=0; i<
rowsize(); ++i)
522 for (std::size_t j=0; j<
colsize(); ++j)
523 (*
this)(i,j) += s*B(i,j);
573 HDNUM_ERROR(
"mv: size of A and y do not match");
575 HDNUM_ERROR(
"mv: size of A and x do not match");
576 for (std::size_t i=0; i<
rowsize(); ++i)
579 for (std::size_t j=0; j<
colsize(); ++j)
580 y[i] += (*
this)(i,j)*x[j];
635 HDNUM_ERROR(
"mv: size of A and y do not match");
637 HDNUM_ERROR(
"mv: size of A and x do not match");
638 for (std::size_t i=0; i<
rowsize(); ++i)
640 for (std::size_t j=0; j<
colsize(); ++j)
641 y[i] += (*
this)(i,j)*x[j];
698 HDNUM_ERROR(
"mv: size of A and y do not match");
700 HDNUM_ERROR(
"mv: size of A and x do not match");
701 for (std::size_t i=0; i<
rowsize(); ++i)
703 for (std::size_t j=0; j<
colsize(); ++j)
704 y[i] += s*(*
this)(i,j)*x[j];
761 HDNUM_ERROR(
"mm: size incompatible");
763 HDNUM_ERROR(
"mm: size incompatible");
765 HDNUM_ERROR(
"mm: size incompatible");
767 for (std::size_t i=0; i<
rowsize(); i++)
768 for (std::size_t j=0; j<
colsize(); j++)
771 for (std::size_t k=0; k<A.
colsize(); k++)
772 (*
this)(i,j) += A(i,k)*B(k,j);
834 HDNUM_ERROR(
"mm: size incompatible");
836 HDNUM_ERROR(
"mm: size incompatible");
838 HDNUM_ERROR(
"mm: size incompatible");
840 for (std::size_t i=0; i<
rowsize(); i++)
841 for (std::size_t j=0; j<
colsize(); j++)
842 for (std::size_t k=0; k<A.
colsize(); k++)
843 (*
this)(i,j) += A(i,k)*B(k,j);
885 HDNUM_ERROR(
"cc: size incompatible");
887 for (std::size_t i=0; i<
rowsize(); i++)
930 HDNUM_ERROR(
"cc: size incompatible");
932 for (std::size_t i=0; i<
colsize(); i++)
941 for (std::size_t i=0; i<
rowsize(); i++)
944 for (std::size_t j=0; j<
colsize(); j++)
945 sum += myabs((*
this)(i,j));
946 if (sum>norm) norm = sum;
955 for (std::size_t j=0; j<
colsize(); j++)
958 for (std::size_t i=0; i<
rowsize(); i++)
959 sum += myabs((*
this)(i,j));
960 if (sum>norm) norm = sum;
1015 assert( x.size() ==
rowsize() );
1018 for(std::size_t r=0; r<
rowsize(); ++r){
1019 for(std::size_t c=0; c<
colsize(); ++c){
1020 y[r]+= at(r,c) * x[c];
1076 const std::size_t out_rows =
rowsize();
1077 const std::size_t out_cols = x.
colsize();
1079 for(std::size_t r=0; r<out_rows; ++r)
1080 for(std::size_t c=0; c<out_cols; ++c)
1081 for(std::size_t i=0; i<
colsize(); ++i)
1082 y(r,c) += at(r,i) * x(i,c);
1136 const std::size_t out_rows =
rowsize();
1137 const std::size_t out_cols = x.
colsize();
1193 const std::size_t out_rows =
rowsize();
1194 const std::size_t out_cols = x.
colsize();
1206 template<
typename REAL>
1208 template<
typename REAL>
1210 template<
typename REAL>
1212 template<
typename REAL>
1239 template <
typename REAL>
1240 inline std::ostream& operator<< (std::ostream& s, const DenseMatrix<REAL>& A)
1243 s <<
" " << std::setw(A.iwidth()) <<
" " <<
" ";
1245 s << std::setw(A.width()) << j <<
" ";
1250 s <<
" " << std::setw(A.iwidth()) << i <<
" ";
1253 if( A.scientific() )
1255 s << std::setw(A.width())
1258 << std::setprecision(A.precision())
1263 s << std::setw(A.width())
1266 << std::setprecision(A.precision())
1281 template<
typename REAL>
1290 template<
typename REAL>
1293 for (std::size_t i=0; i<A.
rowsize(); ++i)
1294 for (std::size_t j=0; j<A.
colsize(); ++j)
1377 template<
typename REAL>
1381 HDNUM_ERROR(
"need square and nonempty matrix");
1382 for (std::size_t i=0; i<A.
rowsize(); ++i)
1383 for (std::size_t j=0; j<A.
colsize(); ++j)
1387 A(i,j) = - REAL(1.0)/((i-j)*(i-j));
1439 template<
typename REAL>
1443 HDNUM_ERROR(
"need square and nonempty matrix");
1445 HDNUM_ERROR(
"need A and x of same size");
1458 template<
typename REAL>
1461 std::fstream f(fname.c_str(),std::ios::out);
1466 if( A.scientific() )
1468 f << std::setw(A.
width())
1471 << std::setprecision(A.
precision()) << A[i][j];
1475 f << std::setw(A.
width())
1478 << std::setprecision(A.
precision()) << A[i][j];
1517 template<
typename REAL>
1521 std::ifstream fin( filename.c_str() );
1524 if( fin.is_open() ){
1525 while( std::getline( fin, buffer ) ){
1526 std::istringstream iss(buffer);
1532 if( sub.length()>0 ){
1533 REAL a = atof(sub.c_str());
1535 rowvector.push_back(a);
1539 if( rowvector.size()>0 ){
1540 A.addNewRow( rowvector );
1548 HDNUM_ERROR(
"Could not open file!");
1554 #endif // DENSEMATRIX_HH Class with mathematical vector operations.
Definition: vector.hh:28
DenseMatrix operator-(const DenseMatrix &x) const
matrix = matrix - matrix
Definition: densematrix.hh:1189
void iwidth(std::size_t i) const
set index field width for pretty-printing
Definition: densematrix.hh:200
REAL & operator()(const std::size_t row, const std::size_t col)
(i,j)-operator for accessing entries of a (m x n)-matrix directly
Definition: densematrix.hh:264
VectorIterator operator[](const std::size_t row)
write-access on matrix element A_ij using A[i][j]
Definition: densematrix.hh:286
DenseMatrix & operator-=(const DenseMatrix &B)
Subtraction assignment.
Definition: densematrix.hh:407
void vandermonde(DenseMatrix< REAL > &A, const Vector< REAL > x)
Definition: densematrix.hh:1440
void mm(const DenseMatrix< REAL > &A, const DenseMatrix< REAL > &B)
assign to matrix product C = A*B to matrix C
Definition: densematrix.hh:758
REAL norm_infty() const
compute row sum norm
Definition: densematrix.hh:938
DenseMatrix & operator=(const DenseMatrix &A)
assignment operator
Definition: densematrix.hh:316
void umm(const DenseMatrix< REAL > &A, const DenseMatrix< REAL > &B)
add matrix product A*B to matrix C
Definition: densematrix.hh:831
void umv(Vector< V > &y, const Vector< V > &x) const
update matrix vector product y += A*x
Definition: densematrix.hh:632
void sr(const Vector< REAL > &x, std::size_t k)
set row: make x the k'th row of A
Definition: densematrix.hh:927
DenseMatrix & operator+=(const DenseMatrix &B)
Addition assignment.
Definition: densematrix.hh:392
DenseMatrix(const std::size_t _rows, const std::size_t _cols, const REAL def_val=0)
constructor
Definition: densematrix.hh:79
DenseMatrix & operator*=(const REAL s)
Scalar multiplication assignment.
Definition: densematrix.hh:445
void width(std::size_t i) const
set data field width for pretty-printing
Definition: densematrix.hh:206
std::size_t width() const
get data field width for pretty-printing
Definition: densematrix.hh:188
DenseMatrix operator+(const DenseMatrix &x) const
matrix = matrix + matrix
Definition: densematrix.hh:1132
size_t rowsize() const
get number of rows of the matrix
Definition: densematrix.hh:121
DenseMatrix()
default constructor (empty Matrix)
Definition: densematrix.hh:71
size_t colsize() const
get number of columns of the matrix
Definition: densematrix.hh:141
std::size_t size_type
Type used for array indices.
Definition: densematrix.hh:30
void sc(const Vector< REAL > &x, std::size_t k)
set column: make x the k'th column of A
Definition: densematrix.hh:882
DenseMatrix sub(size_type i, size_type j, size_type rows, size_type cols)
Submatrix extraction.
Definition: densematrix.hh:366
void spd(DenseMatrix< REAL > &A)
Definition: densematrix.hh:1378
void precision(std::size_t i) const
set data precision for pretty-printing
Definition: densematrix.hh:212
void readMatrixFromFile(const std::string &filename, DenseMatrix< REAL > &A)
Read matrix from a text file.
Definition: densematrix.hh:1518
const REAL & operator()(const std::size_t row, const std::size_t col) const
read-access on matrix element A_ij using A(i,j)
Definition: densematrix.hh:271
void update(const REAL s, const DenseMatrix &B)
Scaled update of a Matrix.
Definition: densematrix.hh:519
const ConstVectorIterator operator[](const std::size_t row) const
read-access on matrix element A_ij using A[i][j]
Definition: densematrix.hh:279
void identity(DenseMatrix< T > &A)
Definition: densematrix.hh:1332
DenseMatrix & operator/=(const REAL s)
Scalar division assignment.
Definition: densematrix.hh:484
Class with mathematical matrix operations.
Definition: densematrix.hh:26
std::size_t iwidth() const
get index field width for pretty-printing
Definition: densematrix.hh:182
void mv(Vector< V > &y, const Vector< V > &x) const
matrix vector product y = A*x
Definition: densematrix.hh:570
std::size_t precision() const
get data precision for pretty-printing
Definition: densematrix.hh:194
void umv(Vector< V > &y, const V &s, const Vector< V > &x) const
update matrix vector product y += sA*x
Definition: densematrix.hh:695
Definition: densematrix.hh:21
Vector< REAL > operator*(const Vector< REAL > &x)
vector = matrix * vector
Definition: densematrix.hh:1013
void scientific(bool b) const
Switch between floating point (default=true) and fixed point (false) display.
Definition: densematrix.hh:176
REAL norm_1() const
compute column sum norm
Definition: densematrix.hh:952