Geogram  Version 1.9.1-rc
A programming library of geometric algorithms
GEO::Matrix< DIM, FT > Class Template Reference

A matrix type. More...

#include <geogram/basic/matrix.h>

Public Types

typedef Matrix< DIM, FT > matrix_type
 
typedef FT value_type
 

Public Member Functions

 Matrix ()
 Default constructor. More...
 
 Matrix (const FT *vals)
 Constructs a matrix from an array of values. More...
 
 Matrix (const std::initializer_list< std::initializer_list< FT > > &Mi)
 Constructs a matrix from 2d array of initializers. More...
 
index_t dimension () const
 Gets the matrix dimension. More...
 
void load_zero ()
 Clears the matrix. More...
 
void load_identity ()
 Sets the matrix to identity. More...
 
bool is_identity () const
 Tests whether a matrix is the identity matrix. More...
 
FT & operator() (index_t i, index_t j)
 Gets a modifiable element. More...
 
const FT & operator() (index_t i, index_t j) const
 Gets a non-modifiable element. More...
 
matrix_typeoperator+= (const matrix_type &m)
 Adds a matrix in place. More...
 
matrix_typeoperator-= (const matrix_type &m)
 Subtracts a matrix in place. More...
 
matrix_typeoperator*= (FT val)
 Multiplies by a scalar in place. More...
 
matrix_typeoperator/= (FT val)
 Divides by a scalar in place. More...
 
matrix_type operator+ (const matrix_type &m) const
 Adds 2 matrices. More...
 
matrix_type operator- (const matrix_type &m) const
 Subtracts 2 matrices. More...
 
matrix_type operator* (FT val) const
 Multiplies a matrix by a scalar. More...
 
matrix_type operator/ (FT val) const
 Divides a matrix by a scalar. More...
 
matrix_type operator* (const matrix_type &m) const
 Multiplies 2 matrices. More...
 
matrix_type inverse () const
 Computes the inverse matrix. More...
 
bool compute_inverse (matrix_type &result) const
 Computes the inverse matrix. More...
 
matrix_type transpose () const
 Computes the transposed matrix. More...
 
const FT * data () const
 Gets non-modifiable matrix data. More...
 
FT * data ()
 Gets modifiable matrix data. More...
 
void get_lower_triangle (FT *store) const
 Gets the lower triangle of the matrix. More...
 

Static Public Attributes

static const index_t dim = DIM
 

Related Functions

(Note that these are not member functions.)

template<index_t DIM, class FT >
std::ostream & operator<< (std::ostream &output, const Matrix< DIM, FT > &m)
 Writes a matrix to a stream. More...
 
template<index_t DIM, class FT >
std::istream & operator>> (std::istream &input, Matrix< DIM, FT > &m)
 Reads a matrix from a stream. More...
 
template<index_t DIM, class FT >
void mult (const Matrix< DIM, FT > &M, const FT *x, FT *y)
 Multiplies a matrix by a vector. More...
 

Detailed Description

template<index_t DIM, class FT>
class GEO::Matrix< DIM, FT >

A matrix type.

Matrix implements a square matrix of dimension DIM. containing coefficients of type T. Type T is expected to be a numeric type. Matrix provides the classical matrix operations.

Template Parameters
FTtype of the matrix elements
DIMdimension of the matrix

Definition at line 66 of file matrix.h.

Member Typedef Documentation

◆ matrix_type

template<index_t DIM, class FT >
typedef Matrix<DIM, FT> GEO::Matrix< DIM, FT >::matrix_type

This matrix type

Definition at line 69 of file matrix.h.

◆ value_type

template<index_t DIM, class FT >
typedef FT GEO::Matrix< DIM, FT >::value_type

The type of the values

Definition at line 72 of file matrix.h.

Constructor & Destructor Documentation

◆ Matrix() [1/3]

template<index_t DIM, class FT >
GEO::Matrix< DIM, FT >::Matrix ( )
inline

Default constructor.

This initializes the matrix to the identity matrix

See also
load_identity()

Definition at line 82 of file matrix.h.

◆ Matrix() [2/3]

template<index_t DIM, class FT >
GEO::Matrix< DIM, FT >::Matrix ( const FT *  vals)
inlineexplicit

Constructs a matrix from an array of values.

Parameters
[in]valsa const pointer to the DIM*DIM values, coefficients of the same rows are consecutive in memory, i is the slowly varying index and j the quickly varying one.

Definition at line 92 of file matrix.h.

◆ Matrix() [3/3]

template<index_t DIM, class FT >
GEO::Matrix< DIM, FT >::Matrix ( const std::initializer_list< std::initializer_list< FT > > &  Mi)
inline

Constructs a matrix from 2d array of initializers.

Parameters
[in]Mia 2d array of values to be copied to the matrix.

Definition at line 105 of file matrix.h.

Member Function Documentation

◆ compute_inverse()

template<index_t DIM, class FT >
bool GEO::Matrix< DIM, FT >::compute_inverse ( matrix_type result) const
inline

Computes the inverse matrix.

Computes matrix M such that (this * M) = identity

Parameters
[out]resultthe inverse matrix
Returns
true if the matrix is inversible
Return values
falseotherwise

Definition at line 350 of file matrix.h.

◆ data() [1/2]

template<index_t DIM, class FT >
FT* GEO::Matrix< DIM, FT >::data ( )
inline

Gets modifiable matrix data.

For interfacing with Fortran, OpenGL etc...

Returns
a pointer to the first element of the matrix

Definition at line 431 of file matrix.h.

◆ data() [2/2]

template<index_t DIM, class FT >
const FT* GEO::Matrix< DIM, FT >::data ( ) const
inline

Gets non-modifiable matrix data.

For interfacing with Fortran, OpenGL etc...

Returns
a const pointer to the first element of the matrix

Definition at line 421 of file matrix.h.

◆ dimension()

template<index_t DIM, class FT >
index_t GEO::Matrix< DIM, FT >::dimension ( ) const
inline

Gets the matrix dimension.

Returns
the value of DIM

Definition at line 124 of file matrix.h.

◆ get_lower_triangle()

template<index_t DIM, class FT >
void GEO::Matrix< DIM, FT >::get_lower_triangle ( FT *  store) const
inline

Gets the lower triangle of the matrix.

Gets all the coefficients of the matrix under the diagonal (included) to array store, Array store must be large enough to contain (DIM * (DIM+1))/2 values.

Parameters
[in]storean array of at least (DIM * (DIM+1))/2 values

Definition at line 442 of file matrix.h.

◆ inverse()

template<index_t DIM, class FT >
matrix_type GEO::Matrix< DIM, FT >::inverse ( ) const
inline

Computes the inverse matrix.

Computes matrix M such that (this * M) = identity

Returns
the inverse matrix

Definition at line 335 of file matrix.h.

◆ is_identity()

template<index_t DIM, class FT >
bool GEO::Matrix< DIM, FT >::is_identity ( ) const
inline

Tests whether a matrix is the identity matrix.

Return values
trueif the matrix is the identity matrix
falseotherwise

Definition at line 158 of file matrix.h.

◆ load_identity()

template<index_t DIM, class FT >
void GEO::Matrix< DIM, FT >::load_identity ( )
inline

Sets the matrix to identity.

This sets all coefficients of this matrix to be equal to DIM x DIM identity matrix.

Definition at line 145 of file matrix.h.

◆ load_zero()

template<index_t DIM, class FT >
void GEO::Matrix< DIM, FT >::load_zero ( )
inline

Clears the matrix.

This resets all values to 0 (zero)

Definition at line 132 of file matrix.h.

◆ operator()() [1/2]

template<index_t DIM, class FT >
FT& GEO::Matrix< DIM, FT >::operator() ( index_t  i,
index_t  j 
)
inline

Gets a modifiable element.

Gets element at row i and column j in the matrix. If indices are out of range, the function calls abort().

Parameters
[in]irow index of the element
[in]jcolumn index of the element
Returns
a reference to the element at coordinates (i, j).

Definition at line 178 of file matrix.h.

◆ operator()() [2/2]

template<index_t DIM, class FT >
const FT& GEO::Matrix< DIM, FT >::operator() ( index_t  i,
index_t  j 
) const
inline

Gets a non-modifiable element.

Gets element at row i and column j in the matrix. If indices are out of range, the function calls abort().

Parameters
[in]irow index of the element
[in]jcolumn index of the element
Returns
a const reference to the element at coordinates (i, j).

Definition at line 192 of file matrix.h.

◆ operator*() [1/2]

template<index_t DIM, class FT >
matrix_type GEO::Matrix< DIM, FT >::operator* ( const matrix_type m) const
inline

Multiplies 2 matrices.

Builds a matrix by multiplying this matrix by matrix m.

Parameters
[in]manother matrix
Returns
the matrix (this * m)

Definition at line 317 of file matrix.h.

◆ operator*() [2/2]

template<index_t DIM, class FT >
matrix_type GEO::Matrix< DIM, FT >::operator* ( FT  val) const
inline

Multiplies a matrix by a scalar.

Builds a matrix by multiplying all the coefficients of this matrix by scalar value val.

Parameters
[in]vala scalar value of the same type than matrix elements
Returns
the resulting matrix

Definition at line 291 of file matrix.h.

◆ operator*=()

template<index_t DIM, class FT >
matrix_type& GEO::Matrix< DIM, FT >::operator*= ( FT  val)
inline

Multiplies by a scalar in place.

This multiplies all the coefficients of this matrix by the value val.

Parameters
[in]vala scalar value of the same type than matrix elements
Returns
a reference to this matrix

Definition at line 235 of file matrix.h.

◆ operator+()

template<index_t DIM, class FT >
matrix_type GEO::Matrix< DIM, FT >::operator+ ( const matrix_type m) const
inline

Adds 2 matrices.

Builds a matrix by adding matrix m to this matrix.

Parameters
[in]manother matrix
Returns
the matrix (this + m)

Definition at line 266 of file matrix.h.

◆ operator+=()

template<index_t DIM, class FT >
matrix_type& GEO::Matrix< DIM, FT >::operator+= ( const matrix_type m)
inline

Adds a matrix in place.

This adds matrix m to this matrix in place.

Parameters
[in]ma matrix of the same dimension
Returns
a reference to this matrix

Definition at line 204 of file matrix.h.

◆ operator-()

template<index_t DIM, class FT >
matrix_type GEO::Matrix< DIM, FT >::operator- ( const matrix_type m) const
inline

Subtracts 2 matrices.

Builds a matrix by subtracting matrix m to this matrix.

Parameters
[in]manother matrix
Returns
the matrix (this + m)

Definition at line 278 of file matrix.h.

◆ operator-=()

template<index_t DIM, class FT >
matrix_type& GEO::Matrix< DIM, FT >::operator-= ( const matrix_type m)
inline

Subtracts a matrix in place.

This subtracts matrix m from this matrix in place.

Parameters
[in]ma matrix of the same dimension
Returns
a reference to this matrix

Definition at line 219 of file matrix.h.

◆ operator/()

template<index_t DIM, class FT >
matrix_type GEO::Matrix< DIM, FT >::operator/ ( FT  val) const
inline

Divides a matrix by a scalar.

Builds a matrix by dividing all the coefficients of this matrix by scalar value val.

Parameters
[in]vala scalar value of the same type than matrix elements
Returns
the resulting matrix

Definition at line 304 of file matrix.h.

◆ operator/=()

template<index_t DIM, class FT >
matrix_type& GEO::Matrix< DIM, FT >::operator/= ( FT  val)
inline

Divides by a scalar in place.

This divides all the coefficients of this matrix by the value val.

Parameters
[in]vala scalar value of the same type than matrix elements
Returns
a reference to this matrix

Definition at line 251 of file matrix.h.

◆ transpose()

template<index_t DIM, class FT >
matrix_type GEO::Matrix< DIM, FT >::transpose ( ) const
inline

Computes the transposed matrix.

Returns
the transposed matrix

Definition at line 405 of file matrix.h.

Friends And Related Function Documentation

◆ mult()

template<index_t DIM, class FT >
void mult ( const Matrix< DIM, FT > &  M,
const FT *  x,
FT *  y 
)
related

Multiplies a matrix by a vector.

Multiplies matrix M by vector x and stores the result in vector y. Vectors x and y are given as arrays of elements and must at least contain DIM elements, otherwise the result is undefined.

Parameters
[in]Ma DIM x DIM matrix
[in]xthe input vector
[in]ythe result of the multiplication
Template Parameters
FTthe type of the matrix elements
DIMthe dimension of the matrix

Definition at line 516 of file matrix.h.

◆ operator<<()

template<index_t DIM, class FT >
std::ostream & operator<< ( std::ostream &  output,
const Matrix< DIM, FT > &  m 
)
related

Writes a matrix to a stream.

This writes the coefficients of matrix m separated by a space character to the output stream output.

Parameters
[in]outputthe output stream
[in]mthe matrix to write
Returns
a reference to the output stream output

Definition at line 466 of file matrix.h.

◆ operator>>()

template<index_t DIM, class FT >
std::istream & operator>> ( std::istream &  input,
Matrix< DIM, FT > &  m 
)
related

Reads a matrix from a stream.

This reads DIM * DIM coefficients from the input stream input and stores them in matrix m

Parameters
[in]inputthe input stream
[out]mthe matrix to read
Returns
a reference to the input stream input

Definition at line 489 of file matrix.h.

Member Data Documentation

◆ dim

template<index_t DIM, class FT >
const index_t GEO::Matrix< DIM, FT >::dim = DIM
static

The dimension of the matrix

Definition at line 75 of file matrix.h.


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