20#include "sparsematrix.hh"
27template <
typename REAL>
32template <
typename REAL>
37 typedef typename std::vector<REAL> VType;
38 typedef typename VType::const_iterator ConstVectorIterator;
39 typedef typename VType::iterator VectorIterator;
46 static bool bScientific;
47 static std::size_t nIndexWidth;
48 static std::size_t nValueWidth;
49 static std::size_t nValuePrecision;
52 REAL myabs(REAL x)
const {
60 inline REAL& at(
const std::size_t row,
const std::size_t col) {
61 return m_data[row * m_cols + col];
65 inline const REAL& at(
const std::size_t row,
const std::size_t col)
const {
66 return m_data[row * m_cols + col];
75 const REAL def_val = 0)
76 : m_data(_rows * _cols, def_val), m_rows(_rows), m_cols(_cols) {}
79 DenseMatrix(
const std::initializer_list<std::initializer_list<REAL>>& v) {
81 m_cols = v.begin()->size();
83 if (row.size() != m_cols) {
84 std::cout <<
"Zeilen der Matrix nicht gleich lang" << std::endl;
87 for (
auto elem : row) m_data.push_back(elem);
96 counter_type row_index {};
97 for (
auto& row : other) {
98 for (
auto it = row.ibegin(); it != row.iend(); it++) {
99 this->
operator[](row_index)[it.index()] = it.value();
107 m_cols = rowvector.size();
108 for (std::size_t i = 0; i < m_cols; i++) m_data.push_back(rowvector[i]);
156 bool scientific()
const {
return bScientific; }
182 std::size_t
iwidth()
const {
return nIndexWidth; }
185 std::size_t
width()
const {
return nValueWidth; }
188 std::size_t
precision()
const {
return nValuePrecision; }
191 void iwidth(std::size_t i)
const { nIndexWidth = i; }
194 void width(std::size_t i)
const { nValueWidth = i; }
197 void precision(std::size_t i)
const { nValuePrecision = i; }
244 inline REAL&
operator()(
const std::size_t row,
const std::size_t col) {
245 assert(row < m_rows || col < m_cols);
251 const std::size_t col)
const {
252 assert(row < m_rows || col < m_cols);
257 const ConstVectorIterator
operator[](
const std::size_t row)
const {
258 assert(row < m_rows);
259 return m_data.begin() + row * m_cols;
264 assert(row < m_rows);
265 return m_data.begin() + row * m_cols;
317 for (std::size_t i = 0; i <
rowsize(); i++)
318 for (std::size_t j = 0; j <
colsize(); j++) (*
this)(i, j) = value;
337 for (
size_type k1 = 0; k1 < rows; k1++) {
338 for (
size_type k2 = 0; k2 < cols; k2++) {
339 A[k1][k2] = self[k1 + i][k2 + j];
372 (*this)(i, j) += B(i, j);
386 for (std::size_t i = 0; i <
rowsize(); ++i)
387 for (std::size_t j = 0; j <
colsize(); ++j)
388 (*
this)(i, j) -= B(i, j);
422 for (std::size_t i = 0; i <
rowsize(); ++i)
423 for (std::size_t j = 0; j <
colsize(); ++j) (*
this)(i, j) *= s;
458 for (std::size_t i = 0; i <
rowsize(); ++i)
459 for (std::size_t j = 0; j <
colsize(); ++j) (*
this)(i, j) /= s;
490 for (std::size_t i = 0; i <
rowsize(); ++i)
491 for (std::size_t j = 0; j <
colsize(); ++j)
492 (*
this)(i, j) += s * B(i, j);
539 if (this->
rowsize() != y.size())
540 HDNUM_ERROR(
"mv: size of A and y do not match");
541 if (this->
colsize() != x.size())
542 HDNUM_ERROR(
"mv: size of A and x do not match");
543 for (std::size_t i = 0; i <
rowsize(); ++i) {
545 for (std::size_t j = 0; j <
colsize(); ++j)
546 y[i] += (*
this)(i, j) * x[j];
599 if (this->
rowsize() != y.size())
600 HDNUM_ERROR(
"mv: size of A and y do not match");
601 if (this->
colsize() != x.size())
602 HDNUM_ERROR(
"mv: size of A and x do not match");
603 for (std::size_t i = 0; i <
rowsize(); ++i) {
604 for (std::size_t j = 0; j <
colsize(); ++j)
605 y[i] += (*
this)(i, j) * x[j];
661 if (this->
rowsize() != y.size())
662 HDNUM_ERROR(
"mv: size of A and y do not match");
663 if (this->
colsize() != x.size())
664 HDNUM_ERROR(
"mv: size of A and x do not match");
665 for (std::size_t i = 0; i <
rowsize(); ++i) {
666 for (std::size_t j = 0; j <
colsize(); ++j)
667 y[i] += s * (*
this)(i, j) * x[j];
721 HDNUM_ERROR(
"mm: size incompatible");
723 HDNUM_ERROR(
"mm: size incompatible");
724 if (A.
colsize() != B.
rowsize()) HDNUM_ERROR(
"mm: size incompatible");
726 for (std::size_t i = 0; i <
rowsize(); i++)
727 for (std::size_t j = 0; j <
colsize(); j++) {
729 for (std::size_t k = 0; k < A.
colsize(); k++)
730 (*
this)(i, j) += A(i, k) * B(k, j);
789 HDNUM_ERROR(
"mm: size incompatible");
791 HDNUM_ERROR(
"mm: size incompatible");
792 if (A.
colsize() != B.
rowsize()) HDNUM_ERROR(
"mm: size incompatible");
794 for (std::size_t i = 0; i <
rowsize(); i++)
795 for (std::size_t j = 0; j <
colsize(); j++)
796 for (std::size_t k = 0; k < A.
colsize(); k++)
797 (*
this)(i, j) += A(i, k) * B(k, j);
834 if (this->
rowsize() != x.size()) HDNUM_ERROR(
"cc: size incompatible");
836 for (std::size_t i = 0; i <
rowsize(); i++) (*
this)(i, k) = x[i];
875 if (this->
colsize() != x.size()) HDNUM_ERROR(
"cc: size incompatible");
877 for (std::size_t i = 0; i <
colsize(); i++) (*
this)(k, i) = x[i];
883 for (std::size_t i = 0; i <
rowsize(); i++) {
885 for (std::size_t j = 0; j <
colsize(); j++)
886 sum += myabs((*
this)(i, j));
887 if (sum > norm) norm = sum;
895 for (std::size_t j = 0; j <
colsize(); j++) {
897 for (std::size_t i = 0; i <
rowsize(); i++)
898 sum += myabs((*
this)(i, j));
899 if (sum > norm) norm = sum;
953 for (std::size_t r = 0; r <
rowsize(); ++r) {
954 for (std::size_t c = 0; c <
colsize(); ++c) {
955 y[r] += at(r, c) * x[c];
1006 const std::size_t out_rows =
rowsize();
1007 const std::size_t out_cols = x.
colsize();
1009 for (std::size_t r = 0; r < out_rows; ++r)
1010 for (std::size_t c = 0; c < out_cols; ++c)
1011 for (std::size_t i = 0; i <
colsize(); ++i)
1012 y(r, c) += at(r, i) * x(i, c);
1063 const std::size_t out_rows =
rowsize();
1064 const std::size_t out_cols = x.
colsize();
1117 const std::size_t out_rows =
rowsize();
1118 const std::size_t out_cols = x.
colsize();
1126template <
typename REAL>
1127bool DenseMatrix<REAL>::bScientific =
true;
1128template <
typename REAL>
1129std::size_t DenseMatrix<REAL>::nIndexWidth = 10;
1130template <
typename REAL>
1131std::size_t DenseMatrix<REAL>::nValueWidth = 10;
1132template <
typename REAL>
1133std::size_t DenseMatrix<REAL>::nValuePrecision = 3;
1158template <
typename REAL>
1159inline std::ostream& operator<<(std::ostream& s,
const DenseMatrix<REAL>& A) {
1161 s <<
" " << std::setw(A.iwidth()) <<
" "
1164 s << std::setw(A.width()) << j <<
" ";
1168 s <<
" " << std::setw(A.iwidth()) << i <<
" ";
1171 if (A.scientific()) {
1172 s << std::setw(A.width()) << std::scientific << std::showpoint
1173 << std::setprecision(A.precision()) << A[i][j] <<
" ";
1175 s << std::setw(A.width()) << std::fixed << std::showpoint
1176 << std::setprecision(A.precision()) << A[i][j] <<
" ";
1190template <
typename REAL>
1191inline void fill(DenseMatrix<REAL>& A,
const REAL& t) {
1198template <
typename REAL>
1199inline void zero(DenseMatrix<REAL>& A) {
1200 for (std::size_t i = 0; i < A.rowsize(); ++i)
1201 for (std::size_t j = 0; j < A.colsize(); ++j) A(i, j) = REAL(0);
1282template <
typename REAL>
1285 HDNUM_ERROR(
"need square and nonempty matrix");
1286 for (std::size_t
i = 0;
i < A.
rowsize(); ++
i)
1287 for (std::size_t
j = 0;
j < A.
colsize(); ++
j)
1343template <
typename REAL>
1346 HDNUM_ERROR(
"need square and nonempty matrix");
1347 if (A.
rowsize() != x.size()) HDNUM_ERROR(
"need A and x of same size");
1359template <
typename REAL>
1361 std::fstream f(
fname.c_str(), std::ios::out);
1365 if (A.scientific()) {
1366 f << std::setw(A.
width()) << std::scientific << std::showpoint
1369 f << std::setw(A.
width()) << std::fixed << std::showpoint
1370 << std::setprecision(A.
precision()) << A[i][j];
1407template <
typename REAL>
1414 if (
fin.is_open()) {
1422 if (sub.length() > 0) {
1438 HDNUM_ERROR(
"Could not open file!");
1475template <
typename REAL>
1482 if (
fin.is_open()) {
1484 while (
fin.peek() ==
'%')
fin.ignore(2048,
'\n');
1495 iss >>
i >>
j >> value;
1502 HDNUM_ERROR(
"Could not open file! \"" +
filename +
"\"");
Class with mathematical matrix operations.
Definition densematrix.hh:33
VectorIterator operator[](const std::size_t row)
write-access on matrix element A_ij using A[i][j]
Definition densematrix.hh:263
const ConstVectorIterator operator[](const std::size_t row) const
read-access on matrix element A_ij using A[i][j]
Definition densematrix.hh:257
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:250
std::size_t iwidth() const
get index field width for pretty-printing
Definition densematrix.hh:182
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:244
REAL norm_infty() const
compute row sum norm
Definition densematrix.hh:881
void readMatrixFromFileMatrixMarket(const std::string &filename, DenseMatrix< REAL > &A)
Read matrix from a matrix market file.
Definition densematrix.hh:1476
void mv(Vector< V > &y, const Vector< V > &x) const
matrix vector product y = A*x
Definition densematrix.hh:538
void umm(const DenseMatrix< REAL > &A, const DenseMatrix< REAL > &B)
add matrix product A*B to matrix C
Definition densematrix.hh:787
DenseMatrix & operator/=(const REAL s)
Scalar division assignment.
Definition densematrix.hh:457
DenseMatrix & operator*=(const REAL s)
Scalar multiplication assignment.
Definition densematrix.hh:421
void umv(Vector< V > &y, const V &s, const Vector< V > &x) const
update matrix vector product y += sA*x
Definition densematrix.hh:660
DenseMatrix & operator=(const DenseMatrix &A)
assignment operator
Definition densematrix.hh:290
void width(std::size_t i) const
set data field width for pretty-printing
Definition densematrix.hh:194
void update(const REAL s, const DenseMatrix &B)
Scaled update of a Matrix.
Definition densematrix.hh:489
DenseMatrix operator-(const DenseMatrix &x) const
matrix = matrix - matrix
Definition densematrix.hh:1113
REAL norm_1() const
compute column sum norm
Definition densematrix.hh:893
DenseMatrix()
default constructor (empty Matrix)
Definition densematrix.hh:71
DenseMatrix sub(size_type i, size_type j, size_type rows, size_type cols)
Submatrix extraction.
Definition densematrix.hh:334
DenseMatrix & operator=(const REAL value)
assignment from a scalar value
Definition densematrix.hh:316
void sc(const Vector< REAL > &x, std::size_t k)
set column: make x the k'th column of A
Definition densematrix.hh:833
void sr(const Vector< REAL > &x, std::size_t k)
set row: make x the k'th row of A
Definition densematrix.hh:874
DenseMatrix(const std::initializer_list< std::initializer_list< REAL > > &v)
constructor from initializer list
Definition densematrix.hh:79
void identity(DenseMatrix< T > &A)
Definition densematrix.hh:1238
void scientific(bool b) const
Switch between floating point (default=true) and fixed point (false) display.
Definition densematrix.hh:179
DenseMatrix & operator-=(const DenseMatrix &B)
Subtraction assignment.
Definition densematrix.hh:385
void iwidth(std::size_t i) const
set index field width for pretty-printing
Definition densematrix.hh:191
std::size_t precision() const
get data precision for pretty-printing
Definition densematrix.hh:188
size_t rowsize() const
get number of rows of the matrix
Definition densematrix.hh:136
DenseMatrix(const std::size_t _rows, const std::size_t _cols, const REAL def_val=0)
constructor
Definition densematrix.hh:74
void readMatrixFromFileDat(const std::string &filename, DenseMatrix< REAL > &A)
Read matrix from a text file.
Definition densematrix.hh:1408
void spd(DenseMatrix< REAL > &A)
Definition densematrix.hh:1283
size_t colsize() const
get number of columns of the matrix
Definition densematrix.hh:153
DenseMatrix operator*(const DenseMatrix &x) const
matrix = matrix * matrix
Definition densematrix.hh:1003
std::size_t size_type
Type used for array indices.
Definition densematrix.hh:36
DenseMatrix operator+(const DenseMatrix &x) const
matrix = matrix + matrix
Definition densematrix.hh:1059
void precision(std::size_t i) const
set data precision for pretty-printing
Definition densematrix.hh:197
void umv(Vector< V > &y, const Vector< V > &x) const
update matrix vector product y += A*x
Definition densematrix.hh:598
DenseMatrix & operator+=(const DenseMatrix &B)
Addition assignment.
Definition densematrix.hh:369
void mm(const DenseMatrix< REAL > &A, const DenseMatrix< REAL > &B)
assign to matrix product C = A*B to matrix C
Definition densematrix.hh:719
std::size_t width() const
get data field width for pretty-printing
Definition densematrix.hh:185
DenseMatrix transpose() const
Transposition.
Definition densematrix.hh:350
Vector< REAL > operator*(const Vector< REAL > &x) const
vector = matrix * vector
Definition densematrix.hh:949
void vandermonde(DenseMatrix< REAL > &A, const Vector< REAL > x)
Definition densematrix.hh:1344
DenseMatrix(const hdnum::SparseMatrix< REAL > &other)
constructor from hdnum::SparseMatrix
Definition densematrix.hh:92
Sparse matrix Class with mathematical matrix operations.
Definition sparsematrix.hh:31
std::size_t size_type
Types used for array indices.
Definition sparsematrix.hh:34
Class with mathematical vector operations.
Definition vector.hh:31
A few common exception classes.