yat  0.16pre
Public Types | Public Member Functions | Related Functions | List of all members
theplu::yat::utility::Matrix Class Reference

Interface to GSL matrix. More...

#include <yat/utility/Matrix.h>

Inheritance diagram for theplu::yat::utility::Matrix:
theplu::yat::utility::BasicMatrix< Matrix >

Public Types

typedef double value_type
 
typedef double & reference
 
typedef const double & const_reference
 
typedef StrideIterator< double * > iterator
 
typedef StrideIterator< const double * > const_iterator
 
typedef StrideIterator< double * > column_iterator
 
typedef StrideIterator< const double * > const_column_iterator
 
typedef StrideIterator< double * > row_iterator
 
typedef StrideIterator< const double * > const_row_iterator
 
typedef Matrix derived_type
 

Public Member Functions

 Matrix (void)
 The default constructor. More...
 
 Matrix (const size_t &r, const size_t &c, double init_value=0)
 Constructor allocating memory space for r times c elements, and sets all elements to init_value. More...
 
 Matrix (const Matrix &)
 The copy constructor. More...
 
template<class T >
 Matrix (const MatrixExpression< T > &other)
 
 Matrix (std::istream &, char sep='\0') throw (utility::IO_error, std::exception)
 The istream constructor. More...
 
 ~Matrix (void)
 The destructor.
 
void all (const double value)
 
iterator begin (void)
 
const_iterator begin (void) const
 
iterator begin_column (size_t i)
 
const_iterator begin_column (size_t i) const
 
iterator begin_row (size_t i)
 
const_iterator begin_row (size_t i) const
 
VectorView column_view (size_t i)
 
const VectorConstView column_const_view (size_t) const
 
size_t columns (void) const
 
void div (const Matrix &b)
 
iterator end (void)
 
const_iterator end (void) const
 
iterator end_column (size_t i)
 
const_iterator end_column (size_t i) const
 
iterator end_row (size_t i)
 
const_iterator end_row (size_t i) const
 
bool equal (const Matrix &, const double precision=0) const
 Check whether matrices are equal within a user defined precision, set by precision. More...
 
const gsl_matrix * gsl_matrix_p (void) const
 
gsl_matrix * gsl_matrix_p (void)
 
void mul (const Matrix &b)
 
void resize (size_t r, size_t c, double init_value=0)
 Resize Matrix. More...
 
size_t rows (void) const
 
VectorView row_view (size_t)
 
const VectorConstView row_const_view (size_t) const
 
void swap_columns (const size_t i, const size_t j)
 Swap columns i and j. More...
 
void swap_rowcol (const size_t i, const size_t j)
 Swap row i and column j. More...
 
void swap_rows (const size_t i, const size_t j)
 Swap rows i and j. More...
 
void transpose (void)
 Transpose the matrix. More...
 
double & operator() (size_t row, size_t column)
 Element access operator. More...
 
const double & operator() (size_t row, size_t column) const
 Element access operator. More...
 
bool operator== (const Matrix &other) const
 Comparison operator. Takes squared time. More...
 
bool operator!= (const Matrix &other) const
 Comparison operator. Takes squared time. More...
 
const Matrixoperator= (const Matrix &other)
 The assignment operator. More...
 
template<class T >
Matrixoperator= (const MatrixExpression< T > &rhs)
 
const Matrixoperator+= (const Matrix &b)
 Add and assign operator. More...
 
template<class T >
Matrixoperator+= (const MatrixExpression< T > &rhs)
 Addition and assign operator. More...
 
const Matrixoperator+= (const double d)
 Add and assign operator. More...
 
const Matrixoperator-= (const Matrix &)
 Subtract and assign operator. More...
 
template<class T >
Matrixoperator-= (const MatrixExpression< T > &rhs)
 Subtraction and assign operator. More...
 
const Matrixoperator-= (const double d)
 Subtract and assign operator. More...
 
const Matrixoperator*= (const Matrix &)
 Multiply and assignment operator. More...
 
const Matrixoperator*= (double d)
 Multiply and assignment operator. More...
 

Related Functions

(Note that these are not member functions.)

template<class Derived1 , class Derived2 >
expression::MatrixBinary< Derived1, Derived2, expression::Plusoperator+ (const BasicMatrix< Derived1 > &lhs, const BasicMatrix< Derived2 > &rhs)
 Matrix addition operator. More...
 
template<class Derived1 , class Derived2 >
expression::MatrixBinary< Derived1, Derived2, expression::Minusoperator- (const BasicMatrix< Derived1 > &lhs, const BasicMatrix< Derived2 > &rhs)
 Matrix subtraction operator. More...
 
template<class Derived1 , class Derived2 >
expression::MatrixBinary< Derived1, Derived2, expression::Multipliesoperator* (const BasicMatrix< Derived1 > &lhs, const BasicMatrix< Derived2 > &rhs)
 Matrix multiplication operator. More...
 
template<class T >
expression::ScaledMatrix< T > operator* (const BasicMatrix< T > &A, double k)
 
template<class T >
expression::ScaledMatrix< T > operator* (double k, const BasicMatrix< T > &A)
 
template<typename T >
expression::ScaledMatrix< T > operator- (const BasicMatrix< T > &m)
 negation operator More...
 
template<typename T >
expression::TransposedMatrix< T > transpose (const BasicMatrix< T > &A)
 transpose function More...
 
void inverse_svd (const Matrix &input, Matrix &result)
 
bool isnull (const Matrix &)
 Check if all elements of the Matrix are zero. More...
 
double max (const Matrix &)
 Get the maximum value of the Matrix. More...
 
double min (const Matrix &)
 Get the minimum value of the Matrix. More...
 
void minmax_index (const Matrix &, std::pair< size_t, size_t > &min, std::pair< size_t, size_t > &max)
 Locate the maximum and minimum element in the Matrix. More...
 
bool nan (const Matrix &templat, Matrix &flag)
 Create a Matrix flag indicating NaN's in another Matrix templat. More...
 
void swap (Matrix &, Matrix &)
 Exchange all elements between the matrices by copying. More...
 
std::ostream & operator<< (std::ostream &s, const Matrix &)
 The output operator for the Matrix class.
 
double trace (const Matrix &)
 Trace of matrix. More...
 

Detailed Description

Interface to GSL matrix.

For the time being 'double' is the only type supported.

[File streams] Reading and writing vectors to file streams
are of course supported. These are implemented without using GSL functionality, and thus binary read and write to streams are not supported.
Note
All GSL matrix related functions are not implement but most functionality defined for GSL matrices can be achieved with this interface class. Most notable GSL functionality not supported are no binary file support and views on arrays, utility::vectors, gsl_vectors, diagonals, subdiagonals, and superdiagonals.

Member Typedef Documentation

Mutable iterator that iterates over one column

Read-only iterator that iterates over one column

Read-only iterator that iterates over all elements

const_reference type is const double&

Since
New in yat 0.5

Read-only iterator that iterates over one row

Class that inherits from BasicMatrix

Mutable iterator that iterates over all elements

reference type is double&

Since
New in yat 0.5

Mutable iterator that iterates over one row

value_type is double

Since
New in yat 0.5

Constructor & Destructor Documentation

theplu::yat::utility::Matrix::Matrix ( void  )

The default constructor.

This constructor does not initialize underlying (essential) structures.

theplu::yat::utility::Matrix::Matrix ( const size_t &  r,
const size_t &  c,
double  init_value = 0 
)

Constructor allocating memory space for r times c elements, and sets all elements to init_value.

If r is zero c must be zero and vice versa.

Exceptions
GSL_errorif memory allocation fails.
theplu::yat::utility::Matrix::Matrix ( const Matrix )

The copy constructor.

Exceptions
AGSL_error is indirectly thrown if memory allocation fails.
template<class T >
theplu::yat::utility::Matrix::Matrix ( const MatrixExpression< T > &  other)

Constructor from a matrix expression. A matrix expression is result operator+, operator-, and operator*, or combinations of them.

A typical usage looks like

Matrix A( B * C + D)

where B, C, and D are all instances of class Matrix.

Typically this constructor is not used, if rvalues are enabled.

See also
Matrix(MatrixExpression<T>&&).
Since
new in yat 0.15
theplu::yat::utility::Matrix::Matrix ( std::istream &  ,
char  sep = '\0' 
)
throw (utility::IO_error,
std::exception
)
explicit

The istream constructor.

The std::istream will be interpreted as outlined here:

Missing values, i.e. empty elements, are treated as NaN values (std::numeric_limits<double>::quiet_NaN() to be specific).

Matrix rows are separated with the new line character.

Column element separation has two modes depending on the value of sep.

  • If sep is the default '\0' value then column elements are separated with white space characters except the new line character. Multiple sequential white space characters are treated as one separator.
  • Setting sep to something else than the default value will change the behaviour to use the sep character as the separator between column elements. Multiple sequential sep characters will be treated as separating elements with missing values.

End of input is the end of file marker and this treatment cannot be redefined using the provided API.

Exceptions
GSL_errorif memory allocation fails, IO_error if unexpected input is found in the input stream.

Member Function Documentation

void theplu::yat::utility::Matrix::all ( const double  value)

Set all elements to value.

iterator theplu::yat::utility::Matrix::begin ( void  )

Iterator iterates along a row. When end of row is reached it jumps to beginning of next row.

Returns
iterator pointing to upper-left element.
const_iterator theplu::yat::utility::Matrix::begin ( void  ) const

Iterator iterates along a row. When end of row is reached it jumps to beginning of next row.

Returns
const_iterator pointing to upper-left element.
iterator theplu::yat::utility::Matrix::begin_column ( size_t  i)

Iterator iterates along a column.

Returns
iterator pointing to first element of column i.
const_iterator theplu::yat::utility::Matrix::begin_column ( size_t  i) const

Iterator iterates along a column.

Returns
const_iterator pointing to first element of column i.
iterator theplu::yat::utility::Matrix::begin_row ( size_t  i)

Iterator iterates along a row.

Returns
iterator pointing to first element of row i.
const_iterator theplu::yat::utility::Matrix::begin_row ( size_t  i) const

Iterator iterates along a row.

Returns
const_iterator pointing to first element of row i.
const VectorConstView theplu::yat::utility::Matrix::column_const_view ( size_t  ) const
Returns
const vector view of column i
VectorView theplu::yat::utility::Matrix::column_view ( size_t  i)
Returns
Vector view of column i
size_t theplu::yat::utility::Matrix::columns ( void  ) const
Returns
The number of columns in the matrix.
void theplu::yat::utility::Matrix::div ( const Matrix b)

Elementwise division of the elements of the calling matrix by the elements of matrix b, $ a_{ij} = a_{ij} / b_{ij} \; \forall i,j $. The result is stored into the calling matrix.

Exceptions
GSL_errorif dimensions mismatch.
iterator theplu::yat::utility::Matrix::end ( void  )
Returns
iterator pointing to end of matrix
const_iterator theplu::yat::utility::Matrix::end ( void  ) const
Returns
const_iterator pointing to end of matrix
iterator theplu::yat::utility::Matrix::end_column ( size_t  i)
Returns
iterator pointing to end of column i
const_iterator theplu::yat::utility::Matrix::end_column ( size_t  i) const
Returns
const_iterator pointing to end of column i
iterator theplu::yat::utility::Matrix::end_row ( size_t  i)
Returns
iterator pointing to end of row i
const_iterator theplu::yat::utility::Matrix::end_row ( size_t  i) const
Returns
const_iterator pointing to end of row i
bool theplu::yat::utility::Matrix::equal ( const Matrix ,
const double  precision = 0 
) const

Check whether matrices are equal within a user defined precision, set by precision.

Returns
True if each element deviates less or equal than d. If any matrix contain a NaN, false is always returned.
See also
operator== and operator!=
const gsl_matrix* theplu::yat::utility::Matrix::gsl_matrix_p ( void  ) const
Returns
A const pointer to the internal GSL matrix.
gsl_matrix* theplu::yat::utility::Matrix::gsl_matrix_p ( void  )
Returns
A pointer to the internal GSL matrix.
void theplu::yat::utility::Matrix::mul ( const Matrix b)

Multiply the elements of Matrix b with the elements of the calling Matrix , $ a_{ij} = a_{ij} * b_{ij} \; \forall i,j $. The result is stored into the calling Matrix.

Exceptions
GSL_errorif dimensions mismatch.
bool theplu::yat::utility::Matrix::operator!= ( const Matrix other) const

Comparison operator. Takes squared time.

Checks are performed with exact matching, i.e., rounding off effects may destroy comparison. Use the equal function for comparing elements within a user defined precision.

Returns
False if all elements are equal otherwise true.
See also
equal
double& theplu::yat::utility::Matrix::operator() ( size_t  row,
size_t  column 
)

Element access operator.

Returns
Reference to the element position (row, column).
Exceptions
IfGSL range checks are enabled in the underlying GSL library a GSL_error exception is thrown if either index is out of range.
const double& theplu::yat::utility::Matrix::operator() ( size_t  row,
size_t  column 
) const

Element access operator.

Returns
Const reference to the element position (row, column).
Exceptions
IfGSL range checks are enabled in the underlying GSL library a GSL_error exception is thrown if either index is out of range.
const Matrix& theplu::yat::utility::Matrix::operator*= ( const Matrix )

Multiply and assignment operator.

Returns
Const reference to the resulting Matrix.
Note
invalidates views and iterators
Exceptions
GSL_errorif memory allocation fails.
const Matrix& theplu::yat::utility::Matrix::operator*= ( double  d)

Multiply and assignment operator.

Multiply the elements of the left hand side Matrix with a scalar d, $ a_{ij} = d * a_{ij} \; \forall i,j $.

Exceptions
GSL_errorif memory allocation fails.
const Matrix& theplu::yat::utility::Matrix::operator+= ( const Matrix b)

Add and assign operator.

Elementwise addition of the elements of Matrix b to the left hand side Matrix , $ a_{ij} = a_{ij} + b_{ij} \; \forall i,j $.

Returns
A const reference to the resulting Matrix.
Exceptions
GSL_errorif dimensions mismatch.
template<class T >
Matrix& theplu::yat::utility::Matrix::operator+= ( const MatrixExpression< T > &  rhs)
inline

Addition and assign operator.

Returns
A reference to the resulting VectorMutable.
const Matrix& theplu::yat::utility::Matrix::operator+= ( const double  d)

Add and assign operator.

Add the scalar value d to the left hand side Matrix, $ a_{ij} = a_{ij} + d \; \forall i,j $.

const Matrix& theplu::yat::utility::Matrix::operator-= ( const Matrix )

Subtract and assign operator.

Elementwise subtraction of the elements of Matrix b to the left hand side Matrix , $ a_{ij} = a_{ij} + b_{ij} \; \forall i,j $.

Returns
A const reference to the resulting Matrix.
Exceptions
GSL_errorif dimensions mismatch.
template<class T >
Matrix& theplu::yat::utility::Matrix::operator-= ( const MatrixExpression< T > &  rhs)
inline

Subtraction and assign operator.

Returns
A reference to the resulting VectorMutable.
const Matrix& theplu::yat::utility::Matrix::operator-= ( const double  d)

Subtract and assign operator.

Subtract the scalar value d to the left hand side Matrix, $ a_{ij} = a_{ij} + d \; \forall i,j $.

const Matrix& theplu::yat::utility::Matrix::operator= ( const Matrix other)

The assignment operator.

Note
If lhs needs to be resized, views and iterators are invalidated.
Returns
A const reference to the resulting Matrix.
template<class T >
Matrix & theplu::yat::utility::Matrix::operator= ( const MatrixExpression< T > &  rhs)

Assignment from a matrix expression. A matrix expression is the result of operator+, operator-, and operator*, or combinations of them.

A typical usage looks like

A = B * C + D;

where A, B, C, and D are all instances of class Matrix.

Typically this constructor is not used, if rvalues are enabled.

See also
Matrix(MatrixExpression<T>&&).
Since
new in yat 0.15
bool theplu::yat::utility::Matrix::operator== ( const Matrix other) const

Comparison operator. Takes squared time.

Checks are performed with exact matching, i.e., rounding off effects may destroy comparison. Use the equal function for comparing elements within a user defined precision.

Returns
True if all elements are equal otherwise false.
See also
equal
void theplu::yat::utility::Matrix::resize ( size_t  r,
size_t  c,
double  init_value = 0 
)

Resize Matrix.

All elements are set to init_value.

If r is zero c must be zero and vice versa.

Note
underlying GSL matrix is destroyed and views and iterators are invalidated
const VectorConstView theplu::yat::utility::Matrix::row_const_view ( size_t  ) const
Returns
const vector view of i
VectorView theplu::yat::utility::Matrix::row_view ( size_t  )
Returns
Vector view of i
size_t theplu::yat::utility::Matrix::rows ( void  ) const
Returns
The number of rows in the matrix.
void theplu::yat::utility::Matrix::swap_columns ( const size_t  i,
const size_t  j 
)

Swap columns i and j.

Exceptions
GSL_errorif either index is out of bounds.
void theplu::yat::utility::Matrix::swap_rowcol ( const size_t  i,
const size_t  j 
)

Swap row i and column j.

The Matrix must be square.

Exceptions
GSL_errorif either index is out of bounds, or if matrix is not square.
void theplu::yat::utility::Matrix::swap_rows ( const size_t  i,
const size_t  j 
)

Swap rows i and j.

Exceptions
GSL_errorif either index is out of bounds.
void theplu::yat::utility::Matrix::transpose ( void  )

Transpose the matrix.

Note
Unless matrix is square, views and iterators are invalidated.
Exceptions
GSL_errorif memory allocation fails for the new transposed matrix.

Friends And Related Function Documentation

void inverse_svd ( const Matrix input,
Matrix result 
)
related

Calculate inverse of a (square) Matrix using SVD

Since
New in yat 0.15
bool isnull ( const Matrix )
related

Check if all elements of the Matrix are zero.

Returns
True if all elements in the Matrix is zero, false otherwise.
double max ( const Matrix )
related

Get the maximum value of the Matrix.

Returns
The maximum value of the Matrix.
double min ( const Matrix )
related

Get the minimum value of the Matrix.

Returns
The minimum value of the Matrix.
void minmax_index ( const Matrix ,
std::pair< size_t, size_t > &  min,
std::pair< size_t, size_t > &  max 
)
related

Locate the maximum and minimum element in the Matrix.

Returns
The indices to the element with the minimum and maximum values of the Matrix, respectively.
Note
Lower index has precedence (searching in row-major order).
bool nan ( const Matrix templat,
Matrix flag 
)
related

Create a Matrix flag indicating NaN's in another Matrix templat.

The flag Matrix is changed to contain 1's and 0's only. A 1 means that the corresponding element in the templat Matrix is valid and a zero means that the corresponding element is a NaN.

Note
Space for Matrix flag is reallocated to fit the size of Matrix templat if sizes mismatch.
Returns
True if the templat Matrix contains at least one NaN.
template<class Derived1 , class Derived2 >
expression::MatrixBinary< Derived1, Derived2, expression::Multiplies > operator* ( const BasicMatrix< Derived1 > &  lhs,
const BasicMatrix< Derived2 > &  rhs 
)
related

Matrix multiplication operator.

Since
new in yat 0.15
Returns
A Matrix Expression that can be converted to a Matrix
template<class T >
expression::ScaledMatrix< T > operator* ( const BasicMatrix< T > &  A,
double  k 
)
related

Calculate a scaled Matrix of A as $ result_{ij} = k * A_{ij} $

Since
new in yat 0.15
Returns
A Matrix Expression that can be converted to a Matrix
template<class T >
expression::ScaledMatrix< T > operator* ( double  k,
const BasicMatrix< T > &  A 
)
related

Calculate a scaled Matrix of A as $ result_{ij} = k * A_{ij} $

Since
new in yat 0.15
Returns
A Matrix Expression that can be converted to a Matrix
template<class Derived1 , class Derived2 >
expression::MatrixBinary< Derived1, Derived2, expression::Plus > operator+ ( const BasicMatrix< Derived1 > &  lhs,
const BasicMatrix< Derived2 > &  rhs 
)
related

Matrix addition operator.

Elementwise addition of the elements , $ result_{ij} = lhs_{ij} + rhs_{ij} \; \forall i,j $.

Since
new in yat 0.15
Returns
A Matrix Expression that can be converted to a Matrix
template<class Derived1 , class Derived2 >
expression::MatrixBinary< Derived1, Derived2, expression::Minus > operator- ( const BasicMatrix< Derived1 > &  lhs,
const BasicMatrix< Derived2 > &  rhs 
)
related

Matrix subtraction operator.

Elementwise addition of the elements , $ result_{ij} = lhs_{ij} - rhs_{ij} \; \forall i,j $.

Since
new in yat 0.15
Returns
A Matrix Expression that can be converted to a Matrix
template<typename T >
expression::ScaledMatrix< T > operator- ( const BasicMatrix< T > &  m)
related

negation operator

Since
new in yat 0.15
void swap ( Matrix ,
Matrix  
)
related

Exchange all elements between the matrices by copying.

This function swaps element by element and matrices must have the same size. References and iterators are not invalidated.

If you use c++11, do not care about iterators and references, std::swap is typically faster (only swapping a couple of pointers)

Exceptions
GSL_errorif sizes are not equal.
double trace ( const Matrix )
related

Trace of matrix.

Returns
sum of diagonal elements
Since
New in yat 0.9
template<typename T >
expression::TransposedMatrix< T > transpose ( const BasicMatrix< T > &  A)
related

transpose function

Since
new in yat 0.15

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

Generated on Wed Aug 15 2018 02:22:40 for yat by  doxygen 1.8.11