A sparse matrix structure, wrapping T.
Davis' CSparse library (part of suitesparse) The type of the matrix entries is fixed to "double".
There are two formats for the non-zero entries in this matrix:
The latter is the "normal" format, which is expected by all mathematical operations defined in this class. There're two three ways of initializing and populating a sparse matrix:
From an existing dense matrix:
Due to its practical utility, there is a special inner class CSparseMatrix::CholeskyDecomp to handle Cholesky-related methods and data.
Definition at line 107 of file CSparseMatrix.h.
#include <mrpt/math/CSparseMatrix.h>
Classes | |
| class | CholeskyDecomp |
| Auxiliary class to hold the results of a Cholesky factorization of a sparse matrix. More... | |
Public Member Functions | |
Constructors, destructor & copy operations | |
| CSparseMatrix (const size_t nRows=0, const size_t nCols=0) | |
| Create an initially empty sparse matrix, in the "triplet" form. | |
| template<typename T > | |
| CSparseMatrix (const CSparseMatrixTemplate< T > &data) | |
| A good way to initialize a sparse matrix from a list of non NULL elements. | |
| template<typename T , size_t N, size_t M> | |
| CSparseMatrix (const CMatrixFixedNumeric< T, N, M > &MAT) | |
| Constructor from a dense matrix of any kind existing in MRPT, creating a "column-compressed" sparse matrix. | |
| template<typename T > | |
| CSparseMatrix (const CMatrixTemplateNumeric< T > &MAT) | |
| Constructor from a dense matrix of any kind existing in MRPT, creating a "column-compressed" sparse matrix. | |
| CSparseMatrix (const CSparseMatrix &other) | |
| Copy constructor. | |
| CSparseMatrix (const cs *const sm) | |
| Copy constructor from an existing "cs" CSparse data structure. | |
| virtual | ~CSparseMatrix () |
| Destructor. | |
| void | operator= (const CSparseMatrix &other) |
| Copy operator from another existing object. | |
| void | clear () |
| Erase all previous contents and leave the matrix as a "triplet" 1x1 matrix without any data. | |
Math operations (the interesting stuff...) | |
| void | add_AB (const CSparseMatrix &A, const CSparseMatrix &B) |
| this = A+B | |
| void | multiply_AB (const CSparseMatrix &A, const CSparseMatrix &B) |
| this = A*B | |
| void | multiply_Ab (const mrpt::vector_double &b, mrpt::vector_double &out_res) const |
| out_res = this * b | |
| CSparseMatrix | operator+ (const CSparseMatrix &other) const |
| CSparseMatrix | operator* (const CSparseMatrix &other) const |
| mrpt::vector_double | operator* (const mrpt::vector_double &other) const |
| void | operator+= (const CSparseMatrix &other) |
| void | operator*= (const CSparseMatrix &other) |
| CSparseMatrix | transpose () const |
Private Member Functions | |
| template<class MATRIX > | |
| void | construct_from_mrpt_mat (const MATRIX &C) |
| Initialization from a dense matrix of any kind existing in MRPT. | |
| void | construct_from_triplet (const cs &triplet) |
| Initialization from a triplet "cs", which is first compressed. | |
| void | construct_from_existing_cs (const cs &sm) |
| To be called by constructors only, assume previous pointers are trash and overwrite them. | |
| void | internal_free_mem () |
| free buffers (deallocate the memory of the i,p,x buffers) | |
| void | copy (const cs *const sm) |
| Copy the data from an existing "cs" CSparse data structure. | |
| void | copy_fast (cs *const sm) |
| Fast copy the data from an existing "cs" CSparse data structure, copying the pointers and leaving NULLs in the source structure. | |
Private Attributes | |
| cs | sparse_matrix |
| void | insert_entry (const size_t row, const size_t col, const double val) |
| @ Access the matrix, get, set elements, etc. | |
| void | insert_entry_fast (const size_t row, const size_t col, const double val) |
| This was an optimized version, but is now equivalent to insert_entry() due to the need to be compatible with unmodified CSparse system libraries. | |
| template<class MATRIX > | |
| void | insert_submatrix (const size_t row, const size_t col, const MATRIX &M) |
| ONLY for TRIPLET matrices: insert a given matrix (in any of the MRPT formats) at a given location of the sparse matrix. | |
| void | compressFromTriplet () |
| ONLY for TRIPLET matrices: convert the matrix in a column-compressed form. | |
| void | get_dense (CMatrixDouble &outMat) const |
| Return a dense representation of the sparse matrix. | |
| bool | saveToTextFile_dense (const std::string &filName) |
| save as a dense matrix to a text file | |
| bool | saveToTextFile_sparse (const std::string &filName) |
| Save sparse structure to a text file loadable from MATLAB (can be called on triplet or CCS matrices). | |
| size_t | getRowCount () const |
| size_t | getColCount () const |
| void | setRowCount (const size_t nRows) |
| Change the number of rows in the matrix (can't be lower than current size) | |
| void | setColCount (const size_t nCols) |
| bool | isTriplet () const |
| Returns true if this sparse matrix is in "triplet" form. | |
| bool | isColumnCompressed () const |
| Returns true if this sparse matrix is in "column compressed" form. | |
| static void | cs2dense (const cs &SM, CMatrixDouble &outMat) |
| Static method to convert a "cs" structure into a dense representation of the sparse matrix. | |
| mrpt::math::CSparseMatrix::CSparseMatrix | ( | const size_t | nRows = 0, |
| const size_t | nCols = 0 |
||
| ) |
Create an initially empty sparse matrix, in the "triplet" form.
Notice that you must call "compressFromTriplet" after populating the matrix and before using the math operatons on this matrix. The initial size can be later on extended with insert_entry() or setRowCount() & setColCount().
|
inline |
A good way to initialize a sparse matrix from a list of non NULL elements.
This constructor takes all the non-zero entries in "data" and builds a column-compressed sparse representation.
Definition at line 177 of file CSparseMatrix.h.
References ASSERTMSG_, mrpt::math::CSparseMatrixTemplate< T >::begin(), mrpt::math::CSparseMatrixTemplate< T >::empty(), mrpt::math::CSparseMatrixTemplate< T >::end(), mrpt::math::CSparseMatrixTemplate< T >::getColCount(), mrpt::math::CSparseMatrixTemplate< T >::getRowCount(), and insert_entry_fast().
|
inlineexplicit |
Constructor from a dense matrix of any kind existing in MRPT, creating a "column-compressed" sparse matrix.
Definition at line 198 of file CSparseMatrix.h.
|
inlineexplicit |
Constructor from a dense matrix of any kind existing in MRPT, creating a "column-compressed" sparse matrix.
Definition at line 201 of file CSparseMatrix.h.
| mrpt::math::CSparseMatrix::CSparseMatrix | ( | const CSparseMatrix & | other | ) |
Copy constructor.
|
explicit |
Copy constructor from an existing "cs" CSparse data structure.
|
virtual |
Destructor.
| void mrpt::math::CSparseMatrix::add_AB | ( | const CSparseMatrix & | A, |
| const CSparseMatrix & | B | ||
| ) |
this = A+B
Referenced by operator+().
| void mrpt::math::CSparseMatrix::clear | ( | ) |
Erase all previous contents and leave the matrix as a "triplet" 1x1 matrix without any data.
| void mrpt::math::CSparseMatrix::compressFromTriplet | ( | ) |
ONLY for TRIPLET matrices: convert the matrix in a column-compressed form.
Referenced by mrpt::graphslam::optimize_graph_spa_levmarq().
|
private |
To be called by constructors only, assume previous pointers are trash and overwrite them.
|
inlineprivate |
Initialization from a dense matrix of any kind existing in MRPT.
Definition at line 114 of file CSparseMatrix.h.
References mrpt::system::os::memcpy().
|
private |
Initialization from a triplet "cs", which is first compressed.
|
private |
Copy the data from an existing "cs" CSparse data structure.
|
private |
Fast copy the data from an existing "cs" CSparse data structure, copying the pointers and leaving NULLs in the source structure.
|
static |
Static method to convert a "cs" structure into a dense representation of the sparse matrix.
| void mrpt::math::CSparseMatrix::get_dense | ( | CMatrixDouble & | outMat | ) | const |
Return a dense representation of the sparse matrix.
|
inline |
Definition at line 334 of file CSparseMatrix.h.
|
inline |
Definition at line 333 of file CSparseMatrix.h.
| void mrpt::math::CSparseMatrix::insert_entry | ( | const size_t | row, |
| const size_t | col, | ||
| const double | val | ||
| ) |
@ Access the matrix, get, set elements, etc.
ONLY for TRIPLET matrices: insert a new non-zero entry in the matrix. This method cannot be used once the matrix is in column-compressed form. The size of the matrix will be automatically extended if the indices are out of the current limits.
|
inline |
This was an optimized version, but is now equivalent to insert_entry() due to the need to be compatible with unmodified CSparse system libraries.
Definition at line 266 of file CSparseMatrix.h.
Referenced by CSparseMatrix(), and mrpt::graphslam::optimize_graph_spa_levmarq().
|
inline |
ONLY for TRIPLET matrices: insert a given matrix (in any of the MRPT formats) at a given location of the sparse matrix.
This method cannot be used once the matrix is in column-compressed form. The size of the matrix will be automatically extended if the indices are out of the current limits. Since this is inline, it can be very efficient for fixed-size MRPT matrices.
Definition at line 275 of file CSparseMatrix.h.
References THROW_EXCEPTION.
Referenced by mrpt::graphslam::optimize_graph_spa_levmarq().
|
private |
free buffers (deallocate the memory of the i,p,x buffers)
|
inline |
Returns true if this sparse matrix is in "column compressed" form.
Definition at line 344 of file CSparseMatrix.h.
|
inline |
Returns true if this sparse matrix is in "triplet" form.
Definition at line 341 of file CSparseMatrix.h.
| void mrpt::math::CSparseMatrix::multiply_AB | ( | const CSparseMatrix & | A, |
| const CSparseMatrix & | B | ||
| ) |
this = A*B
Referenced by operator*().
| void mrpt::math::CSparseMatrix::multiply_Ab | ( | const mrpt::vector_double & | b, |
| mrpt::vector_double & | out_res | ||
| ) | const |
out_res = this * b
|
inline |
Definition at line 233 of file CSparseMatrix.h.
References multiply_AB().
|
inline |
Definition at line 239 of file CSparseMatrix.h.
References multiply_Ab().
|
inline |
Definition at line 247 of file CSparseMatrix.h.
References multiply_AB().
|
inline |
Definition at line 227 of file CSparseMatrix.h.
References add_AB().
|
inline |
Definition at line 244 of file CSparseMatrix.h.
| void mrpt::math::CSparseMatrix::operator= | ( | const CSparseMatrix & | other | ) |
Copy operator from another existing object.
| bool mrpt::math::CSparseMatrix::saveToTextFile_dense | ( | const std::string & | filName | ) |
save as a dense matrix to a text file
| bool mrpt::math::CSparseMatrix::saveToTextFile_sparse | ( | const std::string & | filName | ) |
Save sparse structure to a text file loadable from MATLAB (can be called on triplet or CCS matrices).
The format of the text file is:
Instructions for loading from MATLAB in triplet form will be automatically writen to the output file as comments in th first lines:
|
inline |
Definition at line 338 of file CSparseMatrix.h.
References ASSERT_.
|
inline |
Change the number of rows in the matrix (can't be lower than current size)
Definition at line 337 of file CSparseMatrix.h.
References ASSERT_.
| CSparseMatrix mrpt::math::CSparseMatrix::transpose | ( | ) | const |
|
private |
Definition at line 110 of file CSparseMatrix.h.
| Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013 |