Heidelberg Educational Numerics Library
Version 0.24 (from 9 September 2011)
|
Class with mathematical matrix operations. More...
#include <densematrix.hh>
Public Types | |
typedef std::size_t | size_type |
Type used for array indices. | |
typedef std::vector< REAL > | VType |
typedef VType::const_iterator | ConstVectorIterator |
typedef VType::iterator | VectorIterator |
Public Member Functions | |
DenseMatrix () | |
default constructor (empty Matrix) | |
DenseMatrix (const std::size_t _rows, const std::size_t _cols, const REAL def_val=0) | |
constructor | |
void | addNewRow (const hdnum::Vector< REAL > &rowvector) |
size_t | rowsize () const |
get number of rows of the matrix More... | |
size_t | colsize () const |
get number of columns of the matrix More... | |
bool | scientific () const |
void | scientific (bool b) const |
Switch between floating point (default=true) and fixed point (false) display. More... | |
std::size_t | iwidth () const |
get index field width for pretty-printing | |
std::size_t | width () const |
get data field width for pretty-printing | |
std::size_t | precision () const |
get data precision for pretty-printing | |
void | iwidth (std::size_t i) const |
set index field width for pretty-printing | |
void | width (std::size_t i) const |
set data field width for pretty-printing | |
void | precision (std::size_t i) const |
set data precision for pretty-printing | |
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 More... | |
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) | |
const ConstVectorIterator | operator[] (const std::size_t row) const |
read-access on matrix element A_ij using A[i][j] | |
VectorIterator | operator[] (const std::size_t row) |
write-access on matrix element A_ij using A[i][j] | |
DenseMatrix & | operator= (const DenseMatrix &A) |
assignment operator More... | |
DenseMatrix & | operator= (const REAL value) |
assignment from a scalar value More... | |
DenseMatrix | sub (size_type i, size_type j, size_type rows, size_type cols) |
Submatrix extraction. More... | |
DenseMatrix & | operator+= (const DenseMatrix &B) |
Addition assignment. More... | |
DenseMatrix & | operator-= (const DenseMatrix &B) |
Subtraction assignment. More... | |
DenseMatrix & | operator*= (const REAL s) |
Scalar multiplication assignment. More... | |
DenseMatrix & | operator/= (const REAL s) |
Scalar division assignment. More... | |
void | update (const REAL s, const DenseMatrix &B) |
Scaled update of a Matrix. More... | |
template<class V > | |
void | mv (Vector< V > &y, const Vector< V > &x) const |
matrix vector product y = A*x More... | |
template<class V > | |
void | umv (Vector< V > &y, const Vector< V > &x) const |
update matrix vector product y += A*x More... | |
template<class V > | |
void | umv (Vector< V > &y, const V &s, const Vector< V > &x) const |
update matrix vector product y += sA*x More... | |
void | mm (const DenseMatrix< REAL > &A, const DenseMatrix< REAL > &B) |
assign to matrix product C = A*B to matrix C More... | |
void | umm (const DenseMatrix< REAL > &A, const DenseMatrix< REAL > &B) |
add matrix product A*B to matrix C More... | |
void | sc (const Vector< REAL > &x, std::size_t k) |
set column: make x the k'th column of A More... | |
void | sr (const Vector< REAL > &x, std::size_t k) |
set row: make x the k'th row of A More... | |
REAL | norm_infty () const |
compute row sum norm | |
REAL | norm_1 () const |
compute column sum norm | |
Vector< REAL > | operator* (const Vector< REAL > &x) |
vector = matrix * vector More... | |
DenseMatrix | operator* (const DenseMatrix &x) const |
matrix = matrix * matrix More... | |
DenseMatrix | operator+ (const DenseMatrix &x) const |
matrix = matrix + matrix More... | |
DenseMatrix | operator- (const DenseMatrix &x) const |
matrix = matrix - matrix More... | |
Related Functions | |
(Note that these are not member functions.) | |
template<class T > | |
void | identity (DenseMatrix< T > &A) |
template<typename REAL > | |
void | spd (DenseMatrix< REAL > &A) |
template<typename REAL > | |
void | vandermonde (DenseMatrix< REAL > &A, const Vector< REAL > x) |
template<typename REAL > | |
void | readMatrixFromFile (const std::string &filename, DenseMatrix< REAL > &A) |
Read matrix from a text file. More... | |
Class with mathematical matrix operations.
|
inline |
get number of columns of the matrix
Example:
Output:
Matrix A has 5 columns.
|
inline |
assign to matrix product C = A*B to matrix C
Implements C = A*B where A and B are matrices
[in] | A | constant reference to a DenseMatrix |
[in] | B | constant reference to a DenseMatrix \b Example: hdnum::DenseMatrix<double> A(2,6,1.0); hdnum::DenseMatrix<double> B(6,3,-1.0); A.scientific(false); // fixed point representation for all DenseMatrix objects A.width(6); // use at least 6 columns for displaying matrix entries A.precision(3); // display 3 digits behind the point std::cout << "A =" << A << std::endl; std::cout << "B =" << B << std::endl; hdnum::DenseMatrix<double> C(2,3); C.mm(A,B); std::cout << "C = A*B =" << C << std::endl; |
Output:
A = 0 1 2 3 4 5 0 1.000 1.000 1.000 1.000 1.000 1.000 1 1.000 1.000 1.000 1.000 1.000 1.000 B = 0 1 2 0 -1.000 -1.000 -1.000 1 -1.000 -1.000 -1.000 2 -1.000 -1.000 -1.000 3 -1.000 -1.000 -1.000 4 -1.000 -1.000 -1.000 5 -1.000 -1.000 -1.000 C = A*B = 0 1 2 0 -6.000 -6.000 -6.000 1 -6.000 -6.000 -6.000
|
inline |
matrix vector product y = A*x
Implements y = A*x where x and y are a vectors and A is a matrix
[in] | y | reference to the resulting Vector |
[in] | x | constant reference to a Vector \b Example: hdnum::Vector<double> x(3,10.0); hdnum::Vector<double> y(2); hdnum::DenseMatrix<double> A(2,3,1.0); x.scientific(false); // fixed point representation for all Vector objects A.scientific(false); // fixed point representation for all DenseMatrix objects std::cout << "A =" << A << std::endl; std::cout << "x =" << x << std::endl; A.mv(y,x); std::cout << "y = A*x =" << y << std::endl; |
Output:
A = 0 1 2 0 1.000 1.000 1.000 1 1.000 1.000 1.000 x = [ 0] 10.0000000 [ 1] 10.0000000 [ 2] 10.0000000 y = A*x = [ 0] 30.0000000 [ 1] 30.0000000
|
inline |
(i,j)-operator for accessing entries of a (m x n)-matrix directly
[in] | row | row index (0...m-1) |
[in] | col | column index (0...n-1) \b Example: hdnum::DenseMatrix<double> A(4,4); A.scientific(false); // fixed point representation for all DenseMatrix objects A.width(8); A.precision(3); identity(A); // Defines the identity matrix of the same dimension std::cout << "A=" << A << std::endl; std::cout << "reading A(0,0)=" << A(0,0) << std::endl; std::cout << "resetting A(0,0) and A(2,3)..." << std::endl; A(0,0) = 1.234; A(2,3) = 432.1; std::cout << "A=" << A << std::endl; |
Output:
A= 0 1 2 3 0 1.000 0.000 0.000 0.000 1 0.000 1.000 0.000 0.000 2 0.000 0.000 1.000 0.000 3 0.000 0.000 0.000 1.000 reading A(0,0)=1.000 resetting A(0,0) and A(2,3)... A= 0 1 2 3 0 1.234 0.000 0.000 0.000 1 0.000 1.000 0.000 0.000 2 0.000 0.000 1.000 432.100 3 0.000 0.000 0.000 1.000
|
inline |
vector = matrix * vector
[in] | x | constant reference to a Vector \b Example: hdnum::Vector<double> x(3,4.0); hdnum::DenseMatrix<double> A(3,3,2.0); hdnum::Vector<double> y(3); A.scientific(false); // fixed point representation for all DenseMatrix objects A.width(8); A.precision(1); x.scientific(false); // fixed point representation for all Vector objects x.width(8); x.precision(1); std::cout << "A=" << A << std::endl; std::cout << "x=" << x << std::endl; y=A*x; std::cout << "y=A*x" << y << std::endl; |
Output:
A= 0 1 2 0 2.0 2.0 2.0 1 2.0 2.0 2.0 2 2.0 2.0 2.0 x= [ 0] 4.0 [ 1] 4.0 [ 2] 4.0 y=A*x [ 0] 24.0 [ 1] 24.0 [ 2] 24.0
|
inline |
matrix = matrix * matrix
[in] | x | constant reference to a DenseMatrix \b Example: hdnum::DenseMatrix<double> A(3,3,2.0); hdnum::DenseMatrix<double> B(3,3,4.0); hdnum::DenseMatrix<double> C(3,3); A.scientific(false); // fixed point representation for all DenseMatrix objects A.width(8); A.precision(1); std::cout << "A=" << A << std::endl; std::cout << "B=" << B << std::endl; C=A*B; std::cout << "C=A*B=" << C << std::endl; |
Output:
A= 0 1 2 0 2.0 2.0 2.0 1 2.0 2.0 2.0 2 2.0 2.0 2.0 B= 0 1 2 0 4.0 4.0 4.0 1 4.0 4.0 4.0 2 4.0 4.0 4.0 C=A*B= 0 1 2 0 24.0 24.0 24.0 1 24.0 24.0 24.0 2 24.0 24.0 24.0
|
inline |
Scalar multiplication assignment.
Implements A *= s where s is a scalar
[in] | s | scalar value to multiply with \b Example: double s = 0.5; hdnum::DenseMatrix<double> A(2,3,1.0); std::cout << "A=" << A << std::endl; A *= s; std::cout << "A=" << A << std::endl; |
Output:
A= 0 1 2 0 1.000e+00 1.000e+00 1.000e+00 1 1.000e+00 1.000e+00 1.000e+00 0.5*A = 0 1 2 0 5.000e-01 5.000e-01 5.000e-01 1 5.000e-01 5.000e-01 5.000e-01
|
inline |
matrix = matrix + matrix
[in] | x | constant reference to a DenseMatrix \b Example: hdnum::DenseMatrix<double> A(3,3,2.0); hdnum::DenseMatrix<double> B(3,3,4.0); hdnum::DenseMatrix<double> C(3,3); A.scientific(false); // fixed point representation for all DenseMatrix objects A.width(8); A.precision(1); std::cout << "A=" << A << std::endl; std::cout << "B=" << B << std::endl; C=A+B; std::cout << "C=A+B=" << C << std::endl; |
Output:
A= 0 1 2 0 2.0 2.0 2.0 1 2.0 2.0 2.0 2 2.0 2.0 2.0 B= 0 1 2 0 4.0 4.0 4.0 1 4.0 4.0 4.0 2 4.0 4.0 4.0 C=A+B= 0 1 2 0 6.0 6.0 6.0 1 6.0 6.0 6.0 2 6.0 6.0 6.0
|
inline |
Addition assignment.
Implements A += B matrix addition
[in] | B | another Matrix |
|
inline |
matrix = matrix - matrix
[in] | x | constant reference to a DenseMatrix \b Example: hdnum::DenseMatrix<double> A(3,3,2.0); hdnum::DenseMatrix<double> B(3,3,4.0); hdnum::DenseMatrix<double> C(3,3); A.scientific(false); // fixed point representation for all DenseMatrix objects A.width(8); A.precision(1); std::cout << "A=" << A << std::endl; std::cout << "B=" << B << std::endl; C=A-B; std::cout << "C=A-B=" << C << std::endl; |
Output:
A= 0 1 2 0 2.0 2.0 2.0 1 2.0 2.0 2.0 2 2.0 2.0 2.0 B= 0 1 2 0 4.0 4.0 4.0 1 4.0 4.0 4.0 2 4.0 4.0 4.0 C=A-B= 0 1 2 0 -2.0 -2.0 -2.0 1 -2.0 -2.0 -2.0 2 -2.0 -2.0 -2.0
|
inline |
Subtraction assignment.
Implements A -= B matrix subtraction
[in] | B | another matrix |
|
inline |
Scalar division assignment.
Implements A /= s where s is a scalar
[in] | s | scalar value to multiply with \b Example: double s = 0.5; hdnum::DenseMatrix<double> A(2,3,1.0); std::cout << "A=" << A << std::endl; A /= s; std::cout << "A=" << A << std::endl; |
Output:
A= 0 1 2 0 1.000e+00 1.000e+00 1.000e+00 1 1.000e+00 1.000e+00 1.000e+00 A/0.5 = 0 1 2 0 2.000e+00 2.000e+00 2.000e+00 1 2.000e+00 2.000e+00 2.000e+00
|
inline |
assignment operator
Example:
Output:
B= 0 1 2 3 0 4.000e+00 -1.000e+00 -2.500e-01 -1.111e-01 1 -1.000e+00 4.000e+00 -1.000e+00 -2.500e-01 2 -2.500e-01 -1.000e+00 4.000e+00 -1.000e+00 3 -1.111e-01 -2.500e-01 -1.000e+00 4.000e+00
|
inline |
assignment from a scalar value
Example:
Output:
A= 0 1 2 0 5.432 5.432 5.432 1 5.432 5.432 5.432
|
inline |
get number of rows of the matrix
Example:
Output:
Matrix A has 4 rows.
|
inline |
set column: make x the k'th column of A
[in] | x | constant reference to a Vector |
[in] | k | number of the column of A to be set \b Example: hdnum::Vector<double> x(2,434.0); hdnum::DenseMatrix<double> A(2,6); A.scientific(false); // fixed point representation for all DenseMatrix objects A.width(8); A.precision(1); std::cout << "original A=" << A << std::endl; A.sc(x,3); // redefine fourth column of the matrix std::cout << "modified A=" << A << std::endl; |
Output:
original A= 0 1 2 3 4 5 0 0.0 0.0 0.0 0.0 0.0 0.0 1 0.0 0.0 0.0 0.0 0.0 0.0 modified A= 0 1 2 3 4 5 0 0.0 0.0 0.0 434.0 0.0 0.0 1 0.0 0.0 0.0 434.0 0.0 0.0
|
inline |
Switch between floating point (default=true) and fixed point (false) display.
Example:
Output:
A= 0 1 2 3 0 1.000 0.000 0.000 0.000 1 0.000 1.000 0.000 0.000 2 0.000 0.000 1.000 0.000 3 0.000 0.000 0.000 1.000
|
inline |
set row: make x the k'th row of A
[in] | x | constant reference to a Vector |
[in] | k | number of the row of A to be set \b Example: hdnum::Vector<double> x(3,434.0); hdnum::DenseMatrix<double> A(3,3); A.scientific(false); // fixed point representation for all DenseMatrix objects A.width(8); A.precision(1); std::cout << "original A=" << A << std::endl; A.sr(x,1); // redefine second row of the matrix std::cout << "modified A=" << A << std::endl; |
Output:
original A= 0 1 2 0 0.0 0.0 0.0 1 0.0 0.0 0.0 2 0.0 0.0 0.0 modified A= 0 1 2 0 0.0 0.0 0.0 1 434.0 434.0 434.0 2 0.0 0.0 0.0
|
inline |
Submatrix extraction.
Returns a new matrix that is a subset of the components of the given matrix.
[in] | i | first row index of the new matrix |
[in] | j | first column index of the new matrix |
[in] | rows | row size of the new matrix, i.e. it has components [i,i+rows-1] |
[in] | cols | column size of the new matrix, i.e. it has components [j,j+m-1] |
|
inline |
add matrix product A*B to matrix C
Implements C += A*B where A, B and C are matrices
[in] | A | constant reference to a DenseMatrix |
[in] | B | constant reference to a DenseMatrix \b Example: hdnum::DenseMatrix<double> A(2,6,1.0); hdnum::DenseMatrix<double> B(6,3,-1.0); hdnum::DenseMatrix<double> C(2,3,0.5); A.scientific(false); // fixed point representation for all DenseMatrix objects A.width(6); A.precision(3); std::cout << "C =" << C << std::endl; std::cout << "A =" << A << std::endl; std::cout << "B =" << B << std::endl; C.umm(A,B); std::cout << "C + A*B =" << C << std::endl; |
Output:
C = 0 1 2 0 0.500 0.500 0.500 1 0.500 0.500 0.500 A = 0 1 2 3 4 5 0 1.000 1.000 1.000 1.000 1.000 1.000 1 1.000 1.000 1.000 1.000 1.000 1.000 B = 0 1 2 0 -1.000 -1.000 -1.000 1 -1.000 -1.000 -1.000 2 -1.000 -1.000 -1.000 3 -1.000 -1.000 -1.000 4 -1.000 -1.000 -1.000 5 -1.000 -1.000 -1.000 C + A*B = 0 1 2 0 -5.500 -5.500 -5.500 1 -5.500 -5.500 -5.500
|
inline |
update matrix vector product y += A*x
Implements y += A*x where x and y are a vectors and A is a matrix
[in] | y | reference to the resulting Vector |
[in] | x | constant reference to a Vector \b Example: hdnum::Vector<double> x(3,10.0); hdnum::Vector<double> y(2,5.0); hdnum::DenseMatrix<double> A(2,3,1.0); x.scientific(false); // fixed point representation for all Vector objects A.scientific(false); // fixed point representation for all DenseMatrix objects std::cout << "y =" << y << std::endl; std::cout << "A =" << A << std::endl; std::cout << "x =" << x << std::endl; A.umv(y,x); std::cout << "y = A*x =" << y << std::endl; |
Output:
y = [ 0] 5.0000000 [ 1] 5.0000000 A = 0 1 2 0 1.000 1.000 1.000 1 1.000 1.000 1.000 x = [ 0] 10.0000000 [ 1] 10.0000000 [ 2] 10.0000000 y + A*x = [ 0] 35.0000000 [ 1] 35.0000000
|
inline |
update matrix vector product y += sA*x
Implements y += sA*x where s is a scalar value, x and y are a vectors and A is a matrix
[in] | y | reference to the resulting Vector |
[in] | s | constant reference to a number type |
[in] | x | constant reference to a Vector \b Example: double s=0.5; hdnum::Vector<double> x(3,10.0); hdnum::Vector<double> y(2,5.0); hdnum::DenseMatrix<double> A(2,3,1.0); x.scientific(false); // fixed point representation for all Vector objects A.scientific(false); // fixed point representation for all DenseMatrix objects std::cout << "y =" << y << std::endl; std::cout << "A =" << A << std::endl; std::cout << "x =" << x << std::endl; A.umv(y,s,x); std::cout << "y = s*A*x =" << y << std::endl; |
Output:
y = [ 0] 5.0000000 [ 1] 5.0000000 A = 0 1 2 0 1.000 1.000 1.000 1 1.000 1.000 1.000 x = [ 0] 10.0000000 [ 1] 10.0000000 [ 2] 10.0000000 y = s*A*x = [ 0] 20.0000000 [ 1] 20.0000000
|
inline |
Scaled update of a Matrix.
Implements A += s*B where s is a scalar and B a matrix
[in] | s | scalar value to multiply with |
[in] | B | another matrix \b Example: double s = 0.5; hdnum::DenseMatrix<double> A(2,3,1.0); hdnum::DenseMatrix<double> B(2,3,2.0); A.update(s,B); std::cout << "A + s*B =" << A << std::endl; |
Output:
A + s*B = 0 1 2 0 1.500 1.500 1.500 1 1.500 1.500 1.500
|
related |
Function: make identity matrix
[in] | A | reference to a DenseMatrix that shall be filled with entries |
Example:
Output:
A= 0 1 2 3 0 1.00000 0.00000 0.00000 0.00000 1 0.00000 1.00000 0.00000 0.00000 2 0.00000 0.00000 1.00000 0.00000 3 0.00000 0.00000 0.00000 1.00000
|
related |
Read matrix from a text file.
[in] | filename | name of the text file |
[in,out] | A | reference to a DenseMatrix \b Example: readMatrixFromFile("matrixL.dat", L ); std::cout << "L=" << L << std::endl; |
Output:
Contents of "matrixL.dat": 1.000e+00 0.000e+00 0.000e+00 2.000e+00 1.000e+00 0.000e+00 3.000e+00 2.000e+00 1.000e+00 would give: L= 0 1 2 0 1.000e+00 0.000e+00 0.000e+00 1 2.000e+00 1.000e+00 0.000e+00 2 3.000e+00 2.000e+00 1.000e+00
|
related |
Function: make a symmetric and positive definite matrix
[in] | A | reference to a DenseMatrix that shall be filled with entries |
Example:
Output:
A= 0 1 2 3 0 4.00000 -1.00000 -0.25000 -0.11111 1 -1.00000 4.00000 -1.00000 -0.25000 2 -0.25000 -1.00000 4.00000 -1.00000 3 -0.11111 -0.25000 -1.00000 4.00000
|
related |
Function: make a vandermonde matrix
[in] | A | reference to a DenseMatrix that shall be filled with entries |
[in] | x | constant reference to a Vector |
Example:
Output:
x= [ 0] 2.00000 [ 1] 3.00000 [ 2] 4.00000 [ 3] 5.00000 A= 0 1 2 3 0 1.00000 2.00000 4.00000 8.00000 1 1.00000 3.00000 9.00000 27.00000 2 1.00000 4.00000 16.00000 64.00000 3 1.00000 5.00000 25.00000 125.00000