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 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 102 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. More... | |
template<typename T > | |
CSparseMatrix (const CSparseMatrixTemplate< T > &data) | |
A good way to initialize a sparse matrix from a list of non nullptr elements. More... | |
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. More... | |
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. More... | |
CSparseMatrix (const CSparseMatrix &other) | |
Copy constructor. More... | |
CSparseMatrix (const cs *const sm) | |
Copy constructor from an existing "cs" CSparse data structure. More... | |
virtual | ~CSparseMatrix () |
Destructor. More... | |
void | operator= (const CSparseMatrix &other) |
Copy operator from another existing object. More... | |
void | swap (CSparseMatrix &other) |
Fast swap contents with another sparse matrix. More... | |
void | clear (const size_t nRows=1, const size_t nCols=1) |
Erase all previous contents and leave the matrix as a "triplet" ROWS x COLS matrix without any nonzero entry. More... | |
Math operations (the interesting stuff...) | |
void | add_AB (const CSparseMatrix &A, const CSparseMatrix &B) |
this = A+B More... | |
void | multiply_AB (const CSparseMatrix &A, const CSparseMatrix &B) |
this = A*B More... | |
void | multiply_Ab (const mrpt::math::CVectorDouble &b, mrpt::math::CVectorDouble &out_res) const |
out_res = this * b More... | |
CSparseMatrix | operator+ (const CSparseMatrix &other) const |
CSparseMatrix | operator* (const CSparseMatrix &other) const |
mrpt::math::CVectorDouble | operator* (const mrpt::math::CVectorDouble &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. More... | |
void | construct_from_triplet (const cs &triplet) |
Initialization from a triplet "cs", which is first compressed. More... | |
void | construct_from_existing_cs (const cs &sm) |
To be called by constructors only, assume previous pointers are trash and overwrite them. More... | |
void | internal_free_mem () |
free buffers (deallocate the memory of the i,p,x buffers) More... | |
void | copy (const cs *const sm) |
Copy the data from an existing "cs" CSparse data structure. More... | |
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. More... | |
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. More... | |
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. More... | |
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. More... | |
void | compressFromTriplet () |
ONLY for TRIPLET matrices: convert the matrix in a column-compressed form. More... | |
void | get_dense (CMatrixDouble &outMat) const |
Return a dense representation of the sparse matrix. More... | |
bool | saveToTextFile_dense (const std::string &filName) |
save as a dense matrix to a text file More... | |
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). More... | |
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) More... | |
void | setColCount (const size_t nCols) |
bool | isTriplet () const |
Returns true if this sparse matrix is in "triplet" form. More... | |
bool | isColumnCompressed () const |
Returns true if this sparse matrix is in "column compressed" form. More... | |
static void | cs2dense (const cs &SM, CMatrixDouble &outMat) |
Static method to convert a "cs" structure into a dense representation of the sparse matrix. More... | |
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().
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().
Definition at line 148 of file CSparseMatrix.cpp.
|
inline |
A good way to initialize a sparse matrix from a list of non nullptr elements.
This constructor takes all the non-zero entries in "data" and builds a column-compressed sparse representation.
Definition at line 185 of file CSparseMatrix.h.
References ASSERTMSG_, construct_from_triplet(), insert_entry_fast(), and sparse_matrix.
|
inlineexplicit |
Constructor from a dense matrix of any kind existing in MRPT, creating a "column-compressed" sparse matrix.
Definition at line 214 of file CSparseMatrix.h.
References construct_from_mrpt_mat().
|
inlineexplicit |
Constructor from a dense matrix of any kind existing in MRPT, creating a "column-compressed" sparse matrix.
Definition at line 222 of file CSparseMatrix.h.
References construct_from_mrpt_mat().
CSparseMatrix::CSparseMatrix | ( | const CSparseMatrix & | other | ) |
|
explicit |
Copy constructor from an existing "cs" CSparse data structure.
Definition at line 29 of file CSparseMatrix.cpp.
|
virtual |
Destructor.
Definition at line 94 of file CSparseMatrix.cpp.
void CSparseMatrix::add_AB | ( | const CSparseMatrix & | A, |
const CSparseMatrix & | B | ||
) |
this = A+B
Definition at line 188 of file CSparseMatrix.cpp.
References ASSERT_, getColCount(), getRowCount(), and sparse_matrix.
Referenced by operator+(), and operator+=().
void CSparseMatrix::clear | ( | const size_t | nRows = 1 , |
const size_t | nCols = 1 |
||
) |
Erase all previous contents and leave the matrix as a "triplet" ROWS x COLS matrix without any nonzero entry.
Erase all previous contents and leave the matrix as a "triplet" 1x1 matrix without any data.
Definition at line 97 of file CSparseMatrix.cpp.
Referenced by generateRandomSparseMatrix().
void CSparseMatrix::compressFromTriplet | ( | ) |
ONLY for TRIPLET matrices: convert the matrix in a column-compressed form.
Definition at line 264 of file CSparseMatrix.cpp.
References THROW_EXCEPTION.
Referenced by mrpt::vision::bundle_adj_full(), generateRandomSparseMatrix(), mrpt::graphslam::optimize_graph_spa_levmarq(), and TEST().
|
private |
To be called by constructors only, assume previous pointers are trash and overwrite them.
Definition at line 132 of file CSparseMatrix.cpp.
References ASSERTMSG_.
|
inlineprivate |
Initialization from a dense matrix of any kind existing in MRPT.
Definition at line 109 of file CSparseMatrix.h.
References mrpt::system::os::memcpy(), and sparse_matrix.
Referenced by CSparseMatrix().
|
private |
Initialization from a triplet "cs", which is first compressed.
Definition at line 121 of file CSparseMatrix.cpp.
Referenced by CSparseMatrix().
|
private |
Copy the data from an existing "cs" CSparse data structure.
Definition at line 36 of file CSparseMatrix.cpp.
References ASSERTMSG_, and mrpt::system::os::memcpy().
|
private |
Fast copy the data from an existing "cs" CSparse data structure, copying the pointers and leaving NULLs in the source structure.
Definition at line 56 of file CSparseMatrix.cpp.
|
static |
Static method to convert a "cs" structure into a dense representation of the sparse matrix.
Definition at line 233 of file CSparseMatrix.cpp.
References ASSERT_.
Referenced by mrpt::math::CSparseMatrix::CholeskyDecomp::get_L().
void CSparseMatrix::get_dense | ( | CMatrixDouble & | outMat | ) | const |
Return a dense representation of the sparse matrix.
Definition at line 259 of file CSparseMatrix.cpp.
Referenced by do_matrix_op_test(), do_test_init_random(), do_test_init_to_unit(), and TEST().
|
inline |
Definition at line 389 of file CSparseMatrix.h.
References sparse_matrix.
Referenced by add_AB(), mrpt::math::CSparseMatrix::CholeskyDecomp::CholeskyDecomp(), and multiply_AB().
|
inline |
Definition at line 388 of file CSparseMatrix.h.
References sparse_matrix.
Referenced by add_AB(), mrpt::math::CSparseMatrix::CholeskyDecomp::CholeskyDecomp(), and multiply_AB().
void CSparseMatrix::insert_entry | ( | const size_t | row, |
const size_t | col, | ||
const double | val | ||
) |
@ Access the matrix, get, set elements, etc.
Insert an element into a "cs", return false on error.
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.
Definition at line 160 of file CSparseMatrix.cpp.
References THROW_EXCEPTION, and val.
Referenced by generateRandomSparseMatrix(), insert_entry_fast(), and TEST().
|
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 306 of file CSparseMatrix.h.
References insert_entry(), and val.
Referenced by CSparseMatrix(), insert_submatrix(), 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 323 of file CSparseMatrix.h.
References insert_entry_fast(), isTriplet(), sparse_matrix, and THROW_EXCEPTION.
Referenced by mrpt::vision::bundle_adj_full(), mrpt::graphslam::optimize_graph_spa_levmarq(), and TEST().
|
private |
free buffers (deallocate the memory of the i,p,x buffers)
Definition at line 113 of file CSparseMatrix.cpp.
|
inline |
Returns true if this sparse matrix is in "column compressed" form.
Definition at line 412 of file CSparseMatrix.h.
References sparse_matrix.
Referenced by mrpt::math::CSparseMatrix::CholeskyDecomp::CholeskyDecomp().
|
inline |
Returns true if this sparse matrix is in "triplet" form.
Definition at line 405 of file CSparseMatrix.h.
References sparse_matrix.
Referenced by insert_submatrix().
void CSparseMatrix::multiply_AB | ( | const CSparseMatrix & | A, |
const CSparseMatrix & | B | ||
) |
this = A*B
Definition at line 200 of file CSparseMatrix.cpp.
References ASSERT_, getColCount(), getRowCount(), and sparse_matrix.
Referenced by operator*(), and operator*=().
void CSparseMatrix::multiply_Ab | ( | const mrpt::math::CVectorDouble & | b, |
mrpt::math::CVectorDouble & | out_res | ||
) | const |
out_res = this * b
Definition at line 210 of file CSparseMatrix.cpp.
References ASSERT_EQUAL_, getColCount(), and getRowCount().
Referenced by operator*().
|
inline |
Definition at line 266 of file CSparseMatrix.h.
References multiply_AB().
|
inline |
Definition at line 272 of file CSparseMatrix.h.
References multiply_Ab().
|
inline |
Definition at line 283 of file CSparseMatrix.h.
References multiply_AB().
|
inline |
Definition at line 260 of file CSparseMatrix.h.
References add_AB().
|
inline |
Definition at line 279 of file CSparseMatrix.h.
References add_AB().
void CSparseMatrix::operator= | ( | const CSparseMatrix & | other | ) |
Copy operator from another existing object.
Definition at line 173 of file CSparseMatrix.cpp.
References sparse_matrix.
bool CSparseMatrix::saveToTextFile_dense | ( | const std::string & | filName | ) |
save as a dense matrix to a text file
Definition at line 279 of file CSparseMatrix.cpp.
bool 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:
Definition at line 298 of file CSparseMatrix.cpp.
References ASSERT_, mrpt::system::os::fclose(), mrpt::system::os::fopen(), and mrpt::system::os::fprintf().
|
inline |
Definition at line 397 of file CSparseMatrix.h.
References ASSERT_, and sparse_matrix.
Referenced by generateRandomSparseMatrix(), and TEST().
|
inline |
Change the number of rows in the matrix (can't be lower than current size)
Definition at line 392 of file CSparseMatrix.h.
References ASSERT_, and sparse_matrix.
Referenced by generateRandomSparseMatrix(), and TEST().
void CSparseMatrix::swap | ( | CSparseMatrix & | other | ) |
Fast swap contents with another sparse matrix.
Definition at line 80 of file CSparseMatrix.cpp.
References sparse_matrix.
CSparseMatrix CSparseMatrix::transpose | ( | ) | const |
Definition at line 221 of file CSparseMatrix.cpp.
References ASSERT_.
|
private |
Definition at line 105 of file CSparseMatrix.h.
Referenced by add_AB(), mrpt::math::CSparseMatrix::CholeskyDecomp::CholeskyDecomp(), construct_from_mrpt_mat(), CSparseMatrix(), getColCount(), getRowCount(), insert_submatrix(), isColumnCompressed(), isTriplet(), multiply_AB(), operator=(), setColCount(), setRowCount(), swap(), and mrpt::math::CSparseMatrix::CholeskyDecomp::update().
Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019 |