yat  0.14.5pre
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>

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
 

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...
 
 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...
 
const Matrixoperator+= (const Matrix &b)
 Add and assign operator. More...
 
const Matrixoperator+= (const double d)
 Add and assign operator. More...
 
const Matrixoperator-= (const Matrix &)
 Subtract 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.)

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.
 
Vector operator* (const Matrix &, const VectorBase &)
 Vector Matrix multiplication.
 
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

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

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.
void swap ( Matrix ,
Matrix  
)
related

Exchange all elements between the matrices by copying.

The two matrices must have the same size.

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

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

Generated on Tue Sep 26 2017 02:33:31 for yat by  doxygen 1.8.5