Main MRPT website > C++ reference for MRPT 1.4.0
List of all members | Public Types | Public Member Functions | Protected Member Functions | Protected Attributes
mrpt::math::CMatrixTemplate< T > Class Template Reference

Detailed Description

template<class T>
class mrpt::math::CMatrixTemplate< T >

This template class provides the basic functionality for a general 2D any-size, resizable container of numerical or non-numerical elements.

NOTES:

Note
Memory blocks for each row are 16-bytes aligned (since MRPT 0.7.0).
For a complete introduction to Matrices and vectors in MRPT, see: http://www.mrpt.org/Matrices_vectors_arrays_and_Linear_Algebra_MRPT_and_Eigen_classes
See also
CMatrixTemplateNumeric

Definition at line 39 of file CMatrixTemplate.h.

#include <mrpt/math/CMatrixTemplate.h>

Inheritance diagram for mrpt::math::CMatrixTemplate< T >:
Inheritance graph

Public Types

typedef T value_type
 The type of the matrix elements. More...
 
typedef T & reference
 
typedef const T & const_reference
 
typedef std::size_t size_type
 
typedef std::ptrdiff_t difference_type
 

Public Member Functions

template<size_t N>
void ASSERT_ENOUGHROOM (size_t r, size_t c) const
 Checks whether the rows [r-N,r+N] and the columns [c-N,c+N] are present in the matrix. More...
 
void fillAll (const T &val)
 
void swap (CMatrixTemplate< T > &o)
 Swap with another matrix very efficiently (just swaps a pointer and two integer values). More...
 
 CMatrixTemplate (const CMatrixTemplate &m)
 Constructors. More...
 
 CMatrixTemplate (size_t row=1, size_t col=1)
 
 CMatrixTemplate (const CMatrixTemplate &m, const size_t cropRowCount, const size_t cropColCount)
 Copy constructor & crop from another matrix. More...
 
template<typename V , size_t N>
 CMatrixTemplate (size_t row, size_t col, V(&theArray)[N])
 Constructor from a given size and a C array. More...
 
template<typename V >
 CMatrixTemplate (size_t row, size_t col, const V &theVector)
 Constructor from a given size and a STL container (std::vector, std::list,...) with the initial values. More...
 
virtual ~CMatrixTemplate ()
 Destructor. More...
 
CMatrixTemplateoperator= (const CMatrixTemplate &m)
 Assignment operator from another matrix. More...
 
template<typename V , size_t N>
CMatrixTemplateoperator= (V(&theArray)[N])
 Assignment operator for initializing from a C array (The matrix must be set to the correct size before invoking this asignament) More...
 
size_t getRowCount () const
 Number of rows in the matrix. More...
 
size_t getColCount () const
 Number of columns in the matrix. More...
 
CMatrixTemplateSize size () const
 Get a 2-vector with [NROWS NCOLS] (as in MATLAB command size(x)) More...
 
void setSize (size_t row, size_t col, bool zeroNewElements=false)
 Changes the size of matrix, maintaining the previous contents. More...
 
void resize (const CMatrixTemplateSize &siz, bool zeroNewElements=false)
 This method just checks has no effects in this class, but raises an exception if the expected size does not match. More...
 
T & operator() (size_t row, size_t col)
 Subscript operator to get/set individual elements. More...
 
const T & operator() (size_t row, size_t col) const
 Subscript operator to get individual elements. More...
 
T & operator() (size_t ith)
 Subscript operator to get/set an individual element from a row or column matrix. More...
 
operator() (size_t ith) const
 Subscript operator to get/set an individual element from a row or column matrix. More...
 
void set_unsafe (size_t row, size_t col, const T &v)
 Fast but unsafe method to write a value in the matrix. More...
 
const T & get_unsafe (size_t row, size_t col) const
 Fast but unsafe method to read a value from the matrix. More...
 
T & get_unsafe (size_t row, size_t col)
 Fast but unsafe method to get a reference from the matrix. More...
 
T * get_unsafe_row (size_t row)
 Fast but unsafe method to obtain a pointer to a given row of the matrix (Use only in time critical applications) More...
 
const T * get_unsafe_row (size_t row) const
 Fast but unsafe method to obtain a pointer to a given row of the matrix (Use only in critical applications) More...
 
CMatrixTemplate< T > operator() (const size_t row1, const size_t row2, const size_t col1, const size_t col2) const
 Subscript operator to get a submatrix. More...
 
void extractSubmatrix (const size_t row1, const size_t row2, const size_t col1, const size_t col2, CMatrixTemplate< T > &out) const
 Get a submatrix, given its bounds. More...
 
template<class EIGEN_MATRIX >
void extractSubmatrix (const size_t row1, const size_t row2, const size_t col1, const size_t col2, EIGEN_MATRIX &out) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
void extractRows (size_t firstRow, size_t lastRow, CMatrixTemplate< T > &out) const
 Gets a series of contiguous rows. More...
 
void extractColumns (size_t firstCol, size_t lastCol, CMatrixTemplate< T > &out) const
 Gets a series of contiguous columns. More...
 
void extractCol (size_t nCol, std::vector< T > &out, int startingRow=0) const
 Returns a given column to a vector (without modifying the matrix) More...
 
void extractCol (size_t nCol, CMatrixTemplate< T > &out, int startingRow=0) const
 Gets a given column to a vector (without modifying the matrix) More...
 
void appendRow (const std::vector< T > &in)
 Appends a new row to the MxN matrix from a 1xN vector. More...
 
void appendCol (const std::vector< T > &in)
 Appends a new column to the matrix from a vector. More...
 
void insertCol (size_t nCol, const std::vector< T > &in)
 Inserts a column from a vector, replacing the current contents of that column. More...
 
void getAsVector (std::vector< T > &out) const
 Returns a vector containing the matrix's values. More...
 

Protected Member Functions

void realloc (size_t row, size_t col, bool newElementsToZero=false)
 Internal use only: It reallocs the memory for the 2D matrix, maintaining the previous contents if posible. More...
 

Protected Attributes

T ** m_Val
 
size_t m_Rows
 
size_t m_Cols
 

Member Typedef Documentation

◆ const_reference

template<class T >
typedef const T& mrpt::math::CMatrixTemplate< T >::const_reference

Definition at line 45 of file CMatrixTemplate.h.

◆ difference_type

template<class T >
typedef std::ptrdiff_t mrpt::math::CMatrixTemplate< T >::difference_type

Definition at line 47 of file CMatrixTemplate.h.

◆ reference

template<class T >
typedef T& mrpt::math::CMatrixTemplate< T >::reference

Definition at line 44 of file CMatrixTemplate.h.

◆ size_type

template<class T >
typedef std::size_t mrpt::math::CMatrixTemplate< T >::size_type

Definition at line 46 of file CMatrixTemplate.h.

◆ value_type

template<class T >
typedef T mrpt::math::CMatrixTemplate< T >::value_type

The type of the matrix elements.

Definition at line 43 of file CMatrixTemplate.h.

Constructor & Destructor Documentation

◆ CMatrixTemplate() [1/5]

template<class T >
mrpt::math::CMatrixTemplate< T >::CMatrixTemplate ( const CMatrixTemplate< T > &  m)
inline

Constructors.

Definition at line 127 of file CMatrixTemplate.h.

◆ CMatrixTemplate() [2/5]

template<class T >
mrpt::math::CMatrixTemplate< T >::CMatrixTemplate ( size_t  row = 1,
size_t  col = 1 
)
inline

Definition at line 132 of file CMatrixTemplate.h.

References mrpt::math::CMatrixTemplate< T >::realloc().

◆ CMatrixTemplate() [3/5]

template<class T >
mrpt::math::CMatrixTemplate< T >::CMatrixTemplate ( const CMatrixTemplate< T > &  m,
const size_t  cropRowCount,
const size_t  cropColCount 
)
inline

◆ CMatrixTemplate() [4/5]

template<class T >
template<typename V , size_t N>
mrpt::math::CMatrixTemplate< T >::CMatrixTemplate ( size_t  row,
size_t  col,
V(&)  theArray[N] 
)
inline

Constructor from a given size and a C array.

The array length must match cols x row.

const double numbers[] = {
1,2,3,
4,5,6 };
CMatrixDouble M(3,2, numbers);
CMatrixTemplateNumeric< double > CMatrixDouble
Declares a matrix of double numbers (non serializable).

Definition at line 158 of file CMatrixTemplate.h.

References mrpt::format(), mrpt::math::CMatrixTemplate< T >::m_Cols, mrpt::math::CMatrixTemplate< T >::m_Rows, mrpt::math::CMatrixTemplate< T >::m_Val, MRPT_COMPILE_TIME_ASSERT, mrpt::math::CMatrixTemplate< T >::realloc(), and THROW_EXCEPTION.

◆ CMatrixTemplate() [5/5]

template<class T >
template<typename V >
mrpt::math::CMatrixTemplate< T >::CMatrixTemplate ( size_t  row,
size_t  col,
const V &  theVector 
)
inline

Constructor from a given size and a STL container (std::vector, std::list,...) with the initial values.

The vector length must match cols x row.

Definition at line 172 of file CMatrixTemplate.h.

References mrpt::format(), mrpt::math::CMatrixTemplate< T >::m_Cols, mrpt::math::CMatrixTemplate< T >::m_Rows, mrpt::math::CMatrixTemplate< T >::m_Val, mrpt::math::CMatrixTemplate< T >::realloc(), and THROW_EXCEPTION.

◆ ~CMatrixTemplate()

template<class T >
virtual mrpt::math::CMatrixTemplate< T >::~CMatrixTemplate ( )
inlinevirtual

Destructor.

Definition at line 184 of file CMatrixTemplate.h.

References mrpt::math::CMatrixTemplate< T >::realloc().

Member Function Documentation

◆ appendCol()

template<class T >
void mrpt::math::CMatrixTemplate< T >::appendCol ( const std::vector< T > &  in)
inline

Appends a new column to the matrix from a vector.

The length of the vector must match the number of rows of the matrix, unless it is (0,0).

Exceptions
std::exceptionOn size mismatch.
See also
extractCol
appendRow

Definition at line 517 of file CMatrixTemplate.h.

References ASSERT_, mrpt::math::CMatrixTemplate< T >::m_Cols, mrpt::math::CMatrixTemplate< T >::m_Rows, mrpt::math::CMatrixTemplate< T >::m_Val, and mrpt::math::CMatrixTemplate< T >::realloc().

◆ appendRow()

template<class T >
void mrpt::math::CMatrixTemplate< T >::appendRow ( const std::vector< T > &  in)
inline

Appends a new row to the MxN matrix from a 1xN vector.

The lenght of the vector must match the width of the matrix, unless it's empty: in that case the matrix is resized to 1xN.

CVectorDouble v(7),w(7);
// ...
M.appendRow(v);
M.appendRow(w);
dynamic_vector< double > CVectorDouble
Column vector, like Eigen::MatrixXd, but automatically initialized to zeros since construction.
Definition: eigen_frwds.h:37
Exceptions
std::exceptionOn incorrect vector length.
See also
extractRow
appendCol

Definition at line 488 of file CMatrixTemplate.h.

References ASSERT_, mrpt::math::CMatrixTemplate< T >::m_Cols, mrpt::math::CMatrixTemplate< T >::m_Rows, mrpt::math::CMatrixTemplate< T >::m_Val, and mrpt::math::CMatrixTemplate< T >::realloc().

◆ ASSERT_ENOUGHROOM()

template<class T >
template<size_t N>
void mrpt::math::CMatrixTemplate< T >::ASSERT_ENOUGHROOM ( size_t  r,
size_t  c 
) const
inline

Checks whether the rows [r-N,r+N] and the columns [c-N,c+N] are present in the matrix.

Definition at line 106 of file CMatrixTemplate.h.

References ASSERT_, mrpt::math::CMatrixTemplate< T >::getColCount(), and mrpt::math::CMatrixTemplate< T >::getRowCount().

◆ extractCol() [1/2]

template<class T >
void mrpt::math::CMatrixTemplate< T >::extractCol ( size_t  nCol,
CMatrixTemplate< T > &  out,
int  startingRow = 0 
) const
inline

Gets a given column to a vector (without modifying the matrix)

Exceptions
std::exceptionOn index out of bounds

Definition at line 460 of file CMatrixTemplate.h.

References mrpt::math::CMatrixTemplate< T >::m_Cols, mrpt::math::CMatrixTemplate< T >::m_Rows, mrpt::math::CMatrixTemplate< T >::m_Val, mrpt::math::CMatrixTemplate< T >::setSize(), and THROW_EXCEPTION.

◆ extractCol() [2/2]

template<class T >
void mrpt::math::CMatrixTemplate< T >::extractCol ( size_t  nCol,
std::vector< T > &  out,
int  startingRow = 0 
) const
inline

Returns a given column to a vector (without modifying the matrix)

Exceptions
std::exceptionOn index out of bounds

Definition at line 442 of file CMatrixTemplate.h.

References mrpt::math::CMatrixTemplate< T >::m_Cols, mrpt::math::CMatrixTemplate< T >::m_Rows, mrpt::math::CMatrixTemplate< T >::m_Val, and THROW_EXCEPTION.

◆ extractColumns()

template<class T >
void mrpt::math::CMatrixTemplate< T >::extractColumns ( size_t  firstCol,
size_t  lastCol,
CMatrixTemplate< T > &  out 
) const
inline

Gets a series of contiguous columns.

Exceptions
std::logic_errorOn index out of bounds
See also
extractColumn
extractRows

Definition at line 434 of file CMatrixTemplate.h.

References mrpt::math::detail::extractMatrix(), mrpt::math::CMatrixTemplate< T >::m_Rows, and mrpt::math::CMatrixTemplate< T >::setSize().

◆ extractRows()

template<class T >
void mrpt::math::CMatrixTemplate< T >::extractRows ( size_t  firstRow,
size_t  lastRow,
CMatrixTemplate< T > &  out 
) const
inline

Gets a series of contiguous rows.

Exceptions
std::logic_errorOn index out of bounds
See also
extractRow
extractColumns

Definition at line 424 of file CMatrixTemplate.h.

References mrpt::math::detail::extractMatrix(), mrpt::math::CMatrixTemplate< T >::m_Cols, and mrpt::math::CMatrixTemplate< T >::setSize().

◆ extractSubmatrix() [1/2]

template<class T >
void mrpt::math::CMatrixTemplate< T >::extractSubmatrix ( const size_t  row1,
const size_t  row2,
const size_t  col1,
const size_t  col2,
CMatrixTemplate< T > &  out 
) const
inline

◆ extractSubmatrix() [2/2]

template<class T >
template<class EIGEN_MATRIX >
void mrpt::math::CMatrixTemplate< T >::extractSubmatrix ( const size_t  row1,
const size_t  row2,
const size_t  col1,
const size_t  col2,
EIGEN_MATRIX &  out 
) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 405 of file CMatrixTemplate.h.

References mrpt::math::CMatrixTemplate< T >::m_Cols, mrpt::math::CMatrixTemplate< T >::m_Rows, mrpt::math::CMatrixTemplate< T >::m_Val, and THROW_EXCEPTION.

◆ fillAll()

template<class T >
void mrpt::math::CMatrixTemplate< T >::fillAll ( const T &  val)
inline

Fill all the elements with a given value (Note: named "fillAll" since "fill" will be used by child classes)

Definition at line 112 of file CMatrixTemplate.h.

References mrpt::math::CMatrixTemplate< T >::m_Cols, mrpt::math::CMatrixTemplate< T >::m_Rows, and mrpt::math::CMatrixTemplate< T >::m_Val.

◆ get_unsafe() [1/2]

template<class T >
T& mrpt::math::CMatrixTemplate< T >::get_unsafe ( size_t  row,
size_t  col 
)
inline

Fast but unsafe method to get a reference from the matrix.

Definition at line 354 of file CMatrixTemplate.h.

References mrpt::format(), mrpt::math::CMatrixTemplate< T >::m_Cols, mrpt::math::CMatrixTemplate< T >::m_Rows, mrpt::math::CMatrixTemplate< T >::m_Val, and THROW_EXCEPTION.

◆ get_unsafe() [2/2]

template<class T >
const T& mrpt::math::CMatrixTemplate< T >::get_unsafe ( size_t  row,
size_t  col 
) const
inline

Fast but unsafe method to read a value from the matrix.

Definition at line 343 of file CMatrixTemplate.h.

References mrpt::format(), mrpt::math::CMatrixTemplate< T >::m_Cols, mrpt::math::CMatrixTemplate< T >::m_Rows, mrpt::math::CMatrixTemplate< T >::m_Val, and THROW_EXCEPTION.

◆ get_unsafe_row() [1/2]

template<class T >
T* mrpt::math::CMatrixTemplate< T >::get_unsafe_row ( size_t  row)
inline

Fast but unsafe method to obtain a pointer to a given row of the matrix (Use only in time critical applications)

Definition at line 365 of file CMatrixTemplate.h.

References mrpt::format(), mrpt::math::CMatrixTemplate< T >::m_Cols, mrpt::math::CMatrixTemplate< T >::m_Rows, mrpt::math::CMatrixTemplate< T >::m_Val, and THROW_EXCEPTION.

◆ get_unsafe_row() [2/2]

template<class T >
const T* mrpt::math::CMatrixTemplate< T >::get_unsafe_row ( size_t  row) const
inline

Fast but unsafe method to obtain a pointer to a given row of the matrix (Use only in critical applications)

Definition at line 376 of file CMatrixTemplate.h.

References mrpt::math::CMatrixTemplate< T >::m_Val.

◆ getAsVector()

template<class T >
void mrpt::math::CMatrixTemplate< T >::getAsVector ( std::vector< T > &  out) const
inline

Returns a vector containing the matrix's values.

Definition at line 545 of file CMatrixTemplate.h.

References mrpt::math::CMatrixTemplate< T >::m_Cols, mrpt::math::CMatrixTemplate< T >::m_Rows, and mrpt::math::CMatrixTemplate< T >::m_Val.

◆ getColCount()

template<class T >
size_t mrpt::math::CMatrixTemplate< T >::getColCount ( ) const
inline

◆ getRowCount()

template<class T >
size_t mrpt::math::CMatrixTemplate< T >::getRowCount ( ) const
inline

◆ insertCol()

template<class T >
void mrpt::math::CMatrixTemplate< T >::insertCol ( size_t  nCol,
const std::vector< T > &  in 
)
inline

Inserts a column from a vector, replacing the current contents of that column.

Exceptions
std::exceptionOn index out of bounds
See also
extractCol

Definition at line 532 of file CMatrixTemplate.h.

References ASSERT_, mrpt::math::CMatrixTemplate< T >::m_Cols, mrpt::math::CMatrixTemplate< T >::m_Rows, mrpt::math::CMatrixTemplate< T >::m_Val, and THROW_EXCEPTION.

◆ operator()() [1/5]

template<class T >
CMatrixTemplate<T> mrpt::math::CMatrixTemplate< T >::operator() ( const size_t  row1,
const size_t  row2,
const size_t  col1,
const size_t  col2 
) const
inline

Subscript operator to get a submatrix.

Definition at line 382 of file CMatrixTemplate.h.

References mrpt::math::CMatrixTemplate< T >::extractSubmatrix().

◆ operator()() [2/5]

template<class T >
T& mrpt::math::CMatrixTemplate< T >::operator() ( size_t  ith)
inline

Subscript operator to get/set an individual element from a row or column matrix.

Exceptions
std::exceptionIf the object is not a column or row matrix.

Definition at line 277 of file CMatrixTemplate.h.

References ASSERT_, mrpt::math::CMatrixTemplate< T >::m_Cols, mrpt::math::CMatrixTemplate< T >::m_Rows, mrpt::math::CMatrixTemplate< T >::m_Val, and THROW_EXCEPTION_CUSTOM_MSG1.

◆ operator()() [3/5]

template<class T >
T mrpt::math::CMatrixTemplate< T >::operator() ( size_t  ith) const
inline

Subscript operator to get/set an individual element from a row or column matrix.

Exceptions
std::exceptionIf the object is not a column or row matrix.

Definition at line 305 of file CMatrixTemplate.h.

References ASSERT_, mrpt::math::CMatrixTemplate< T >::m_Cols, mrpt::math::CMatrixTemplate< T >::m_Rows, mrpt::math::CMatrixTemplate< T >::m_Val, and THROW_EXCEPTION_CUSTOM_MSG1.

◆ operator()() [4/5]

template<class T >
T& mrpt::math::CMatrixTemplate< T >::operator() ( size_t  row,
size_t  col 
)
inline

◆ operator()() [5/5]

template<class T >
const T& mrpt::math::CMatrixTemplate< T >::operator() ( size_t  row,
size_t  col 
) const
inline

◆ operator=() [1/2]

template<class T >
CMatrixTemplate& mrpt::math::CMatrixTemplate< T >::operator= ( const CMatrixTemplate< T > &  m)
inline

◆ operator=() [2/2]

template<class T >
template<typename V , size_t N>
CMatrixTemplate& mrpt::math::CMatrixTemplate< T >::operator= ( V(&)  theArray[N])
inline

Assignment operator for initializing from a C array (The matrix must be set to the correct size before invoking this asignament)

CMatrixDouble M(3,2);
const double numbers[] = {
1,2,3,
4,5,6 };
M = numbers;

Refer also to the constructor with initialization data CMatrixTemplate::CMatrixTemplate

Definition at line 207 of file CMatrixTemplate.h.

References mrpt::format(), mrpt::math::CMatrixTemplate< T >::m_Cols, mrpt::math::CMatrixTemplate< T >::m_Rows, mrpt::math::CMatrixTemplate< T >::m_Val, MRPT_COMPILE_TIME_ASSERT, and THROW_EXCEPTION.

◆ realloc()

template<class T >
void mrpt::math::CMatrixTemplate< T >::realloc ( size_t  row,
size_t  col,
bool  newElementsToZero = false 
)
inlineprotected

◆ resize()

template<class T >
void mrpt::math::CMatrixTemplate< T >::resize ( const CMatrixTemplateSize siz,
bool  zeroNewElements = false 
)
inline

This method just checks has no effects in this class, but raises an exception if the expected size does not match.

Definition at line 247 of file CMatrixTemplate.h.

References mrpt::math::CMatrixTemplate< T >::setSize().

◆ set_unsafe()

template<class T >
void mrpt::math::CMatrixTemplate< T >::set_unsafe ( size_t  row,
size_t  col,
const T &  v 
)
inline

◆ setSize()

template<class T >
void mrpt::math::CMatrixTemplate< T >::setSize ( size_t  row,
size_t  col,
bool  zeroNewElements = false 
)
inline

◆ size()

template<class T >
CMatrixTemplateSize mrpt::math::CMatrixTemplate< T >::size ( ) const
inline

Get a 2-vector with [NROWS NCOLS] (as in MATLAB command size(x))

Definition at line 232 of file CMatrixTemplate.h.

References mrpt::math::CMatrixTemplate< T >::m_Cols, and mrpt::math::CMatrixTemplate< T >::m_Rows.

◆ swap()

template<class T >
void mrpt::math::CMatrixTemplate< T >::swap ( CMatrixTemplate< T > &  o)
inline

Swap with another matrix very efficiently (just swaps a pointer and two integer values).

Definition at line 119 of file CMatrixTemplate.h.

References mrpt::math::CMatrixTemplate< T >::m_Cols, mrpt::math::CMatrixTemplate< T >::m_Rows, and mrpt::math::CMatrixTemplate< T >::m_Val.

Member Data Documentation

◆ m_Cols

template<class T >
size_t mrpt::math::CMatrixTemplate< T >::m_Cols
protected

◆ m_Rows

template<class T >
size_t mrpt::math::CMatrixTemplate< T >::m_Rows
protected

◆ m_Val

template<class T >
T** mrpt::math::CMatrixTemplate< T >::m_Val
protected



Page generated by Doxygen 1.9.1 for MRPT 1.4.0 SVN: at Sat Jan 30 21:34:41 UTC 2021