This class implements the basic operations on matrices. Elements are access in matrix notation: that is A(1,2) represents the second element of the first line. Index go from 1 to nrows/ncols.
It might not be the best ever such implementation, but the goal is to provide a standalone matrix class. It might be later possible to chose between using the embedded implementation or to act as a front end to BLAS for those who have it installed on their system.
If the compilation flag NOSAFECHECKS is used, bounds check is turned off (leading to increased performances).
#include <Matrix.h>
Public Member Functions | |
Matrix () | |
Matrix (const int &rows, const int &cols) | |
A constructor that creates a matrix of a given size. More... | |
Matrix (const size_t &rows, const size_t &cols) | |
Matrix (const size_t &rows, const size_t &cols, const double &init) | |
A constructor that creates a matrix filled with constant values. More... | |
Matrix (const size_t &n, const double &init) | |
A constructor that creates a diagonal matrix of size n. More... | |
Matrix (const Matrix &init) | |
Copy constructor. More... | |
void | identity (const size_t &n, const double &init) |
Convert the current matrix to a identity matrix of size n. More... | |
void | resize (const size_t &rows, const size_t &cols) |
void | resize (const size_t &rows, const size_t &cols, const double &init) |
void | size (size_t &rows, size_t &cols) const |
get the dimensions of the current object More... | |
void | clear () |
free the memory and set the matrix dimensions to (0,0) More... | |
void | random (const double &range) |
fill the matrix with random numbers. More... | |
double & | operator() (const size_t &x, const size_t &y) |
double | operator() (const size_t &x, const size_t &y) const |
double | scalar () const |
Converts a 1x1 matrix to a scalar. More... | |
Matrix | getT () const |
matrix transpose. More... | |
void | T () |
Matrix | getInv () const |
matrix invert. It first performs LU decomposition and then computes the inverse by backward and forward solving of LU * A-1 = I see Press, William H.; Flannery, Brian P.; Teukolsky, Saul A.; Vetterling, William T. (1992), "LU Decomposition and Its Applications", Numerical Recipes in FORTRAN: The Art of Scientific Computing (2nd ed.), Cambridge University Press, pp. 34–42 More... | |
bool | inv () |
double | det () const |
matrix determinant More... | |
bool | LU (Matrix &L, Matrix &U) const |
matrix LU decomposition. Perform LU decomposition by the Dolittle algorithm, (cf http://math.fullerton.edu/mathews/numerical/linear/dol/dol.html) More... | |
void | partialPivoting (std::vector< size_t > &pivot_idx) |
matrix partial pivoting. This reorders the rows so that each diagonal element is the maximum in its column (see https://secure.wikimedia.org/wikipedia/en/wiki/Pivot_element) More... | |
void | partialPivoting () |
void | maximalPivoting () |
const std::string | toString () const |
matrix bidiagonalization This uses Householder's reduction, see Golub, 1970. (see https://secure.wikimedia.org/wikipedia/en/wiki/Bidiagonalization) More... | |
Matrix & | operator+= (const Matrix &rhs) |
const Matrix | operator+ (const Matrix &rhs) const |
Matrix & | operator+= (const double &rhs) |
const Matrix | operator+ (const double &rhs) const |
Matrix & | operator-= (const Matrix &rhs) |
const Matrix | operator- (const Matrix &rhs) const |
Matrix & | operator-= (const double &rhs) |
const Matrix | operator- (const double &rhs) const |
Matrix & | operator*= (const Matrix &rhs) |
const Matrix | operator* (const Matrix &rhs) const |
Matrix & | operator*= (const double &rhs) |
const Matrix | operator* (const double &rhs) const |
Matrix & | operator/= (const double &rhs) |
const Matrix | operator/ (const double &rhs) const |
bool | operator== (const Matrix &) const |
Operator that tests for equality. More... | |
bool | operator!= (const Matrix &) const |
Operator that tests for inequality. More... | |
bool | isIdentity () const |
check if a matrix is the identity matrix More... | |
Static Public Member Functions | |
static double | scalar (const Matrix &m) |
static double | dot (const Matrix &A, const Matrix &B) |
Dot product. More... | |
static Matrix | T (const Matrix &m) |
static Matrix | solve (const Matrix &A, const Matrix &B) |
matrix solving for A·X=B. It first performs LU decomposition and then solves A·X=B by backward and forward solving of LU * X = B More... | |
static bool | solve (const Matrix &A, const Matrix &B, Matrix &X) |
matrix solving for A·X=B. It first performs LU decomposition and then solves A·X=B by backward and forward solving of LU * X = B More... | |
static Matrix | TDMA_solve (const Matrix &A, const Matrix &B) |
Solving system of equations using Thomas Algorithm The following function solves a A·X=B with X and B being vectors and A a tridiagonal matrix, using Thomas Algorithm (see http://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm) More... | |
static bool | TDMA_solve (const Matrix &A, const Matrix &B, Matrix &X) |
Solving system of equations using Thomas Algorithm The following function solves a A·X=B with X and B being vectors and A a tridiagonal matrix, using Thomas Algorithm (see http://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm) More... | |
static bool | isIdentity (const Matrix &A) |
Static Public Attributes | |
static const double | epsilon = 1e-9 |
static const double | epsilon_mtr = 1e-6 |
Protected Member Functions | |
size_t | findMaxInCol (const size_t &col) |
size_t | findMaxInRow (const size_t &row) |
void | swapRows (const size_t &i1, const size_t &i2) |
Protected Attributes | |
std::vector< double > | vecData |
size_t | ncols |
size_t | nrows |
|
inline |
mio::Matrix::Matrix | ( | const int & | rows, |
const int & | cols | ||
) |
A constructor that creates a matrix of a given size.
rows | number of rows of the new matrix |
cols | number of columns of the new matrix |
|
inline |
|
inline |
A constructor that creates a matrix filled with constant values.
rows | number of rows of the new matrix |
cols | number of columns of the new matrix |
init | initial value to fill the matrix with |
mio::Matrix::Matrix | ( | const size_t & | n, |
const double & | init | ||
) |
A constructor that creates a diagonal matrix of size n.
n | dimension of the new square matrix |
init | initial value to fill the matrix with |
|
inline |
Copy constructor.
init | matrix to copy |
void mio::Matrix::clear | ( | ) |
free the memory and set the matrix dimensions to (0,0)
double mio::Matrix::det | ( | ) | const |
matrix determinant
Dot product.
|
protected |
|
protected |
Matrix mio::Matrix::getInv | ( | ) | const |
matrix invert. It first performs LU decomposition and then computes the inverse by backward and forward solving of LU * A-1 = I see Press, William H.; Flannery, Brian P.; Teukolsky, Saul A.; Vetterling, William T. (1992), "LU Decomposition and Its Applications", Numerical Recipes in FORTRAN: The Art of Scientific Computing (2nd ed.), Cambridge University Press, pp. 34–42
Matrix mio::Matrix::getT | ( | ) | const |
matrix transpose.
void mio::Matrix::identity | ( | const size_t & | n, |
const double & | init | ||
) |
Convert the current matrix to a identity matrix of size n.
n | dimension of the new square matrix |
init | initial value to fill the matrix with |
bool mio::Matrix::inv | ( | ) |
bool mio::Matrix::isIdentity | ( | ) | const |
check if a matrix is the identity matrix
|
static |
matrix LU decomposition. Perform LU decomposition by the Dolittle algorithm, (cf http://math.fullerton.edu/mathews/numerical/linear/dol/dol.html)
L | lower diagonal matrix |
U | upper diagonal matrix |
void mio::Matrix::maximalPivoting | ( | ) |
bool mio::Matrix::operator!= | ( | const Matrix & | in | ) | const |
Operator that tests for inequality.
double & mio::Matrix::operator() | ( | const size_t & | x, |
const size_t & | y | ||
) |
double mio::Matrix::operator() | ( | const size_t & | x, |
const size_t & | y | ||
) | const |
const Matrix mio::Matrix::operator* | ( | const double & | rhs | ) | const |
Matrix & mio::Matrix::operator*= | ( | const double & | rhs | ) |
const Matrix mio::Matrix::operator+ | ( | const double & | rhs | ) | const |
Matrix & mio::Matrix::operator+= | ( | const double & | rhs | ) |
const Matrix mio::Matrix::operator- | ( | const double & | rhs | ) | const |
Matrix & mio::Matrix::operator-= | ( | const double & | rhs | ) |
const Matrix mio::Matrix::operator/ | ( | const double & | rhs | ) | const |
Matrix & mio::Matrix::operator/= | ( | const double & | rhs | ) |
bool mio::Matrix::operator== | ( | const Matrix & | in | ) | const |
Operator that tests for equality.
void mio::Matrix::partialPivoting | ( | std::vector< size_t > & | pivot_idx | ) |
matrix partial pivoting. This reorders the rows so that each diagonal element is the maximum in its column (see https://secure.wikimedia.org/wikipedia/en/wiki/Pivot_element)
pivot_idx | new indices (to apply when solving A * X = B, for example |
void mio::Matrix::partialPivoting | ( | ) |
void mio::Matrix::random | ( | const double & | range | ) |
fill the matrix with random numbers.
range | range of the randoms numbers (they will be between -range and +range) |
void mio::Matrix::resize | ( | const size_t & | rows, |
const size_t & | cols | ||
) |
void mio::Matrix::resize | ( | const size_t & | rows, |
const size_t & | cols, | ||
const double & | init | ||
) |
double mio::Matrix::scalar | ( | ) | const |
Converts a 1x1 matrix to a scalar.
|
static |
void mio::Matrix::size | ( | size_t & | rows, |
size_t & | cols | ||
) | const |
get the dimensions of the current object
rows | number of rows of the matrix |
cols | number of columns of the matrix |
matrix solving for A·X=B. It first performs LU decomposition and then solves A·X=B by backward and forward solving of LU * X = B
A | A matrix |
B | B matrix |
matrix solving for A·X=B. It first performs LU decomposition and then solves A·X=B by backward and forward solving of LU * X = B
A | A matrix |
B | B matrix |
X | solution matrix |
|
protected |
void mio::Matrix::T | ( | ) |
Solving system of equations using Thomas Algorithm The following function solves a A·X=B with X and B being vectors and A a tridiagonal matrix, using Thomas Algorithm (see http://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm)
A | A matrix |
B | B matrix |
Solving system of equations using Thomas Algorithm The following function solves a A·X=B with X and B being vectors and A a tridiagonal matrix, using Thomas Algorithm (see http://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm)
A | A matrix |
B | B matrix |
X | solution matrix |
const std::string mio::Matrix::toString | ( | ) | const |
matrix bidiagonalization This uses Householder's reduction, see Golub, 1970. (see https://secure.wikimedia.org/wikipedia/en/wiki/Bidiagonalization)
|
static |
|
static |
|
protected |
|
protected |
|
protected |