MeteoIODoc  MeteoIODoc-2.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mio::Matrix Class Reference

Detailed Description

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).

Author
Mathias Bavay

#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...
 
Matrixoperator+= (const Matrix &rhs)
 
const Matrix operator+ (const Matrix &rhs) const
 
Matrixoperator+= (const double &rhs)
 
const Matrix operator+ (const double &rhs) const
 
Matrixoperator-= (const Matrix &rhs)
 
const Matrix operator- (const Matrix &rhs) const
 
Matrixoperator-= (const double &rhs)
 
const Matrix operator- (const double &rhs) const
 
Matrixoperator*= (const Matrix &rhs)
 
const Matrix operator* (const Matrix &rhs) const
 
Matrixoperator*= (const double &rhs)
 
const Matrix operator* (const double &rhs) const
 
Matrixoperator/= (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
 

Constructor & Destructor Documentation

mio::Matrix::Matrix ( )
inline
mio::Matrix::Matrix ( const int &  rows,
const int &  cols 
)

A constructor that creates a matrix of a given size.

Parameters
rowsnumber of rows of the new matrix
colsnumber of columns of the new matrix
mio::Matrix::Matrix ( const size_t &  rows,
const size_t &  cols 
)
inline
mio::Matrix::Matrix ( const size_t &  rows,
const size_t &  cols,
const double &  init 
)
inline

A constructor that creates a matrix filled with constant values.

Parameters
rowsnumber of rows of the new matrix
colsnumber of columns of the new matrix
initinitial 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.

Parameters
ndimension of the new square matrix
initinitial value to fill the matrix with
mio::Matrix::Matrix ( const Matrix init)
inline

Copy constructor.

Parameters
initmatrix to copy

Member Function Documentation

void mio::Matrix::clear ( )

free the memory and set the matrix dimensions to (0,0)

double mio::Matrix::det ( ) const

matrix determinant

Returns
determinant
double mio::Matrix::dot ( const Matrix A,
const Matrix B 
)
static

Dot product.

Returns
scalar value
size_t mio::Matrix::findMaxInCol ( const size_t &  col)
protected
size_t mio::Matrix::findMaxInRow ( const size_t &  row)
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

Returns
inversed matrix
Matrix mio::Matrix::getT ( ) const

matrix transpose.

Returns
transposed matrix
void mio::Matrix::identity ( const size_t &  n,
const double &  init 
)

Convert the current matrix to a identity matrix of size n.

Parameters
ndimension of the new square matrix
initinitial value to fill the matrix with
bool mio::Matrix::inv ( )
bool mio::Matrix::isIdentity ( ) const

check if a matrix is the identity matrix

Returns
true if it is I
bool mio::Matrix::isIdentity ( const Matrix A)
static
bool mio::Matrix::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)

Parameters
Llower diagonal matrix
Uupper diagonal matrix
Returns
false if the decomposition can not be performed (division by zero)
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 Matrix rhs) const
const Matrix mio::Matrix::operator* ( const double &  rhs) const
Matrix & mio::Matrix::operator*= ( const Matrix rhs)
Matrix & mio::Matrix::operator*= ( const double &  rhs)
const Matrix mio::Matrix::operator+ ( const Matrix rhs) const
const Matrix mio::Matrix::operator+ ( const double &  rhs) const
Matrix & mio::Matrix::operator+= ( const Matrix rhs)
Matrix & mio::Matrix::operator+= ( const double &  rhs)
const Matrix mio::Matrix::operator- ( const Matrix rhs) const
const Matrix mio::Matrix::operator- ( const double &  rhs) const
Matrix & mio::Matrix::operator-= ( const Matrix rhs)
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)

Parameters
pivot_idxnew 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.

Parameters
rangerange 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.

Returns
scalar value
double mio::Matrix::scalar ( const Matrix m)
static
void mio::Matrix::size ( size_t &  rows,
size_t &  cols 
) const

get the dimensions of the current object

Parameters
rowsnumber of rows of the matrix
colsnumber of columns of the matrix
Matrix mio::Matrix::solve ( const Matrix A,
const Matrix B 
)
static

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

Parameters
AA matrix
BB matrix
Returns
solution matrix
bool mio::Matrix::solve ( const Matrix A,
const Matrix B,
Matrix X 
)
static

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

Parameters
AA matrix
BB matrix
Xsolution matrix
Returns
true is success
void mio::Matrix::swapRows ( const size_t &  i1,
const size_t &  i2 
)
protected
Matrix mio::Matrix::T ( const Matrix m)
static
void mio::Matrix::T ( )
Matrix mio::Matrix::TDMA_solve ( const Matrix A,
const Matrix B 
)
static

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)

Author
Nander Wever, Mathias Bavay
Parameters
AA matrix
BB matrix
Returns
solution matrix
bool mio::Matrix::TDMA_solve ( const Matrix A,
const Matrix B,
Matrix X 
)
static

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)

Author
Nander Wever, Mathias Bavay
Parameters
AA matrix
BB matrix
Xsolution matrix
Returns
true is success
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)

Member Data Documentation

const double mio::Matrix::epsilon = 1e-9
static
const double mio::Matrix::epsilon_mtr = 1e-6
static
size_t mio::Matrix::ncols
protected
size_t mio::Matrix::nrows
protected
std::vector<double> mio::Matrix::vecData
protected

The documentation for this class was generated from the following files: