template class mrpt::math::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.

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.

See also:

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

#include <mrpt/math/CSparseMatrixTemplate.h>

template <class T>
class CSparseMatrixTemplate
{
public:
    // typedefs

    typedef typename std::map<std::pair<size_t, size_t>, T> SparseMatrixMap;
    typedef typename SparseMatrixMap::const_iterator const_iterator;
    typedef typename SparseMatrixMap::const_reverse_iterator const_reverse_iterator;

    // construction

    CSparseMatrixTemplate();
    CSparseMatrixTemplate(size_t nR, size_t nC);

    //
methods

    T operator () (size_t r, size_t c) const;
    bool exists(size_t r, size_t c) const;
    T& operator () (size_t r, size_t c);
    size_t rows() const;
    size_t cols() const;

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

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

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

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

    const_iterator begin() const;
    const_iterator end() const;
    const_reverse_iterator rbegin() const;
    const_reverse_iterator rend() const;

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

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

    void resize(size_t nRows, size_t nCols);

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

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

    size_t getNonNullElements() const;
    bool empty() const;
    size_t getNullElements() const;
    bool isNull(size_t nRow, size_t nCol) const;
    bool isNotNull(size_t nRow, size_t nCol) const;
    void clear();
    void purge(T nullObject = T());
};

// direct descendants

template <class T>
class CSparseSymmetricalMatrix;

Typedefs

typedef typename std::map<std::pair<size_t, size_t>, T> SparseMatrixMap

Internal map type, used to store the actual matrix.

typedef typename SparseMatrixMap::const_iterator const_iterator

Const iterator to move through the matrix.

See also:

CSparseMatrixTemplate::const_reverse_iterator

typedef typename SparseMatrixMap::const_reverse_iterator const_reverse_iterator

Const reverse iterator to move through the matrix.

See also:

CSparseMatrixTemplate::const_iterator

Construction

CSparseMatrixTemplate()

Basic constructor with no data.

Size is set to (0,0).

CSparseMatrixTemplate(size_t nR, size_t nC)

Constructor with default size.

Methods

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

Element access operator.

Doesn’t check bounds.

bool exists(size_t r, size_t c) const

Element access operator.

Checks bounds.

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

Reference access operator.

Checks for bounds.

size_t rows() const

Returns the amount of rows in this matrix.

See also:

getColCount, getRow

size_t cols() const

Returns the amount of columns in this matrix.

See also:

rows()

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

Extracts a full row from the matrix.

Parameters:

std::logic_error

on out of range.

See also:

rows(), getColumn, setRow

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

Extracts a full column from the matrix.

Parameters:

std::logic_error

on out of range.

See also:

getColCount, getRow, setColumn

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

Inserts an element into the matrix.

See also:

operator()(size_t,size_t)

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

Inserts submatrix at a given location.

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

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

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

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

Parameters:

std::logic_error

on out of range or wrong sized vector.

See also:

getRow

template <typename VECTOR>
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).

Parameters:

std::logic_error

on out of range or wrong sized vector.

See also:

getColumn

void resize(size_t nRows, size_t nCols)

Changes the size of the matrix.

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

Extracts a submatrix form the matrix.

Parameters:

std::logic_error

on invalid bounds.

See also:

operator()(size_t,size_t)

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

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

size_t getNonNullElements() const

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

See also:

getNullElements, isNull, isNotNull

bool empty() const

Are there no elements set to !=0 ?

See also:

getNullElements, isNull, isNotNull

size_t getNullElements() const

Gets the amount of null elements inside the matrix.

See also:

getNonNullElements, isNull, isNotNull

bool isNull(size_t nRow, size_t nCol) const

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

Parameters:

std::logic_error

on out of range

See also:

getNonNullElements, getNullElements, isNotNull

bool isNotNull(size_t nRow, size_t nCol) const

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

See also:

getNonNullElements, getNullElements, isNull

void clear()

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

void purge(T nullObject = T())

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