Template Class CSparseMatrixTemplate

Inheritance Relationships

Derived Type

Class Documentation

template<class T>
class CSparseMatrixTemplate

A sparse matrix container (with cells of any type), with iterators. This class stores only those elements created by assigning them a value, for example: “M(2,3)=8;”.

This class doesn’t implement math operations since it’s a generic sparse container, but it can be used to initialize the contents of a CSparse library-based matrix of type mrpt::math::CSparseMatrix.

Note that reading non-existing cell elements will return the default value (0 for numbers) and that cell will remain non-created in the matrix.

There is an additional method “exists(i,j)” to check whether a given element exists in the matrix.

See also

mrpt::math::MatrixBlockSparseCols, mrpt::math::CSparseMatrix, CSparseSymmetricalMatrix

Note

Methods marked as “Doesn’t check bounds” mean that if an access to an element out of the matrix size is tried, an empty element will be assumed, but this will not raise any invalid memory access.

Subclassed by mrpt::math::CSparseSymmetricalMatrix< T >

Public Types

using SparseMatrixMap = typename std::map<std::pair<size_t, size_t>, T>

Internal map type, used to store the actual matrix.

using const_iterator = typename SparseMatrixMap::const_iterator

Const iterator to move through the matrix.

using const_reverse_iterator = typename SparseMatrixMap::const_reverse_iterator

Const reverse iterator to move through the matrix.

Public Functions

CSparseMatrixTemplate() = default

Basic constructor with no data. Size is set to (0,0).

inline CSparseMatrixTemplate(size_t nR, size_t nC)

Constructor with default size.

inline T operator()(size_t r, size_t c) const

Element access operator. Doesn’t check bounds.

inline bool exists(size_t r, size_t c) const

Element access operator. Checks bounds.

inline T &operator()(size_t r, size_t c)

Reference access operator. Checks for bounds.

inline size_t rows() const

Returns the amount of rows in this matrix.

See also

getColCount,getRow

inline size_t cols() const

Returns the amount of columns in this matrix.

See also

rows()

template<typename VECTOR>
inline void getRow(size_t nRow, VECTOR &vec) const

Extracts a full row from the matrix.

Throws:

std::logic_error – on out of range.

template<typename VECTOR>
inline void getColumn(size_t nCol, VECTOR &vec) const

Extracts a full column from the matrix.

See also

getColCount,getRow,setColumn

Throws:

std::logic_error – on out of range.

inline void insert(size_t row, size_t column, const T &obj)

Inserts an element into the matrix.

template<class MATRIX_LIKE>
inline void insertMatrix(size_t row, size_t column, const MATRIX_LIKE &mat)

Inserts submatrix at a given location

inline const_iterator begin() const

Returns an iterator which points to the starting point of the matrix. It’s a const_iterator, so that the usar isn’t able to modify the matrix content into an invalid state.

See also

end,rbegin,rend

inline const_iterator end() const

Returns an iterator which points to the end of the matrix. It’s a const_iterator, so that the usar isn’t able to modify the matrix content into an invalid state.

See also

begin,rbegin,rend

inline const_reverse_iterator rbegin() const

Returns an iterator which points to the end of the matrix, and can be used to move backwards. It’s a const_reverse_iterator, so that the usar isn’t able to modify the matrix content into an invalid state.

See also

begin,end,rend

inline const_reverse_iterator rend() const

Returns an iterator which points to the starting point of the matrix, although it’s the upper limit of the matrix since it’s a reverse iterator. Also, it’s a const_reverse_iterator, so that the usar isn’t able to modify the matrix content into an invalid state.

See also

begin,end,rbegin

template<typename VECTOR>
inline void setRow(size_t nRow, const VECTOR &vec, const T &nullObject = T())

Inserts a full row into the matrix. The third argument is used to specify a null object (which won’t be inserted, since the matrix is sparse).

See also

getRow

Throws:

std::logic_error – on out of range or wrong sized vector.

template<typename VECTOR>
inline void setColumn(size_t nCol, const VECTOR &vec, const T &nullObject = T())

Inserts a full column into the matrix. The third argument is used to specify a null object (which won’t be inserted, since the matrix is sparse).

See also

getColumn

Throws:

std::logic_error – on out of range or wrong sized vector.

inline void resize(size_t nRows, size_t nCols)

Changes the size of the matrix.

inline CSparseMatrixTemplate<T> operator()(size_t firstRow, size_t lastRow, size_t firstColumn, size_t lastColumn) const

Extracts a submatrix form the matrix.

Throws:

std::logic_error – on invalid bounds.

template<typename VECTOR>
inline void asVector(VECTOR &vec) const

Gets a vector containing all the elements of the matrix, ignoring their position.

inline size_t getNonNullElements() const

Gets the amount of non-null elements inside the matrix.

inline bool empty() const

Are there no elements set to !=0 ?

inline size_t getNullElements() const

Gets the amount of null elements inside the matrix.

inline bool isNull(size_t nRow, size_t nCol) const

Checks whether an element of the matrix is the default object.

Throws:

std::logic_error – on out of range

inline bool isNotNull(size_t nRow, size_t nCol) const

Checks whether an element of the matrix is not the default object.

inline void clear()

Completely removes all elements, although maintaining the matrix’s size.

inline void purge(T nullObject = T())

Checks each non-null elements against the basic objects, erasing unnecesary references to it.

Protected Attributes

size_t mRows = {0}

Size of the matrix.

size_t mColumns = {0}
SparseMatrixMap objectList

Actual matrix.