Go to the documentation of this file.
9 #ifndef CSparseMatrix_H
10 #define CSparseMatrix_H
25 #if MRPT_HAS_SUITESPARSE
26 # define NCOMPLEX // In MRPT we don't need complex numbers, so avoid the annoying warning: 'cs_ci_house' has C-linkage specified, but returns UDT 'std::complex<double>' which is incompatible with C
29 # include <mrpt/otherlibs/CSparse/cs.h>
98 template <
class MATRIX>
101 std::vector<int> row_list, col_list;
102 std::vector<double> content_list;
103 const int nCol = C.getColCount();
104 const int nRow = C.getRowCount();
105 for (
int c=0; c<nCol; ++c)
107 col_list.push_back(row_list.size());
108 for (
int r=0; r<nRow; ++r)
109 if (C.get_unsafe(r,c)!=0)
111 row_list.push_back(r);
112 content_list.push_back(C(r,c));
115 col_list.push_back(row_list.size());
117 sparse_matrix.m = nRow;
118 sparse_matrix.n = nCol;
119 sparse_matrix.nzmax = content_list.size();
120 sparse_matrix.i = (
int*)malloc(
sizeof(
int)*row_list.size());
121 sparse_matrix.p = (
int*)malloc(
sizeof(
int)*col_list.size());
122 sparse_matrix.x = (
double*)malloc(
sizeof(
double)*content_list.size());
124 ::memcpy(sparse_matrix.i, &row_list[0],
sizeof(row_list[0])*row_list.size() );
125 ::memcpy(sparse_matrix.p, &col_list[0],
sizeof(col_list[0])*col_list.size() );
126 ::memcpy(sparse_matrix.x, &content_list[0],
sizeof(content_list[0])*content_list.size() );
128 sparse_matrix.nz = -1;
132 void construct_from_triplet(
const cs & triplet);
135 void construct_from_existing_cs(
const cs &sm);
138 void internal_free_mem();
141 void copy(
const cs *
const sm);
144 void copy_fast(cs *
const sm);
161 template <
typename T>
164 ASSERTMSG_(!data.
empty(),
"Input data must contain at least one non-zero element.")
165 sparse_matrix.i = NULL;
166 sparse_matrix.p = NULL;
167 sparse_matrix.x = NULL;
204 void clear(
const size_t nRows=1,
const size_t nCols=1);
233 this->add_AB(*
this,other);
251 void insert_entry(
const size_t row,
const size_t col,
const double val );
254 inline void insert_entry_fast(
const size_t row,
const size_t col,
const double val ) { insert_entry(row,col,val); }
262 template <
class MATRIX>
265 if (!isTriplet())
THROW_EXCEPTION(
"insert_entry() is only available for sparse matrix in 'triplet' format.")
266 const size_t nR = M.getRowCount();
267 const size_t nC = M.getColCount();
268 for (
size_t r=0;r<nR;r++)
269 for (
size_t c=0;c<nC;c++)
270 insert_entry_fast(row+r,col+c, M.get_unsafe(r,c));
272 sparse_matrix.m = std::max(sparse_matrix.m,
int(row+nR));
273 sparse_matrix.n = std::max(sparse_matrix.n,
int(col+nC));
280 void compressFromTriplet();
293 bool saveToTextFile_dense(
const std::string &filName);
318 bool saveToTextFile_sparse(
const std::string &filName);
325 inline void setRowCount(
const size_t nRows) {
ASSERT_(nRows>=(
size_t)sparse_matrix.m); sparse_matrix.m = nRows; }
326 inline void setColCount(
const size_t nCols) {
ASSERT_(nCols>=(
size_t)sparse_matrix.n); sparse_matrix.n = nCols; }
329 inline bool isTriplet()
const {
return sparse_matrix.nz>=0; }
386 template <
class VECTOR>
387 inline VECTOR
backsub(
const VECTOR &b)
const { VECTOR res;
backsub(b,res);
return res; }
390 void backsub(
const Eigen::VectorXd &b, Eigen::VectorXd &result_x)
const;
393 void backsub(
const double *b,
double *result,
const size_t N)
const;
bool empty() const
Are there no elements set to !=0 ?
#define THROW_EXCEPTION(msg)
#define ASSERTMSG_(f, __ERROR_MSG)
CExceptionNotDefPos(const std::string &s)
void construct_from_mrpt_mat(const MATRIX &C)
Initialization from a dense matrix of any kind existing in MRPT.
bool isColumnCompressed() const
Returns true if this sparse matrix is in "column compressed" form.
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction.
SparseMatrixMap::const_iterator const_iterator
Const iterator to move through the matrix.
EIGEN_STRONG_INLINE void multiply_Ab(const OTHERVECTOR1 &vIn, OTHERVECTOR2 &vOut, bool accumToOutput=false) const
csn * m_numeric_structure
void BASE_IMPEXP memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) MRPT_NO_THROWS
An OS and compiler independent version of "memcpy".
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 ...
CSparseMatrix(const CSparseMatrixTemplate< T > &data)
A good way to initialize a sparse matrix from a list of non NULL elements.
bool isTriplet() const
Returns true if this sparse matrix is in "triplet" form.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
css * m_symbolic_structure
size_t getColCount() const
Returns the amount of columns in this matrix.
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 compatib...
A matrix of dynamic size.
Used in mrpt::math::CSparseMatrix.
std::vector< T1 > & operator+=(std::vector< T1 > &a, const std::vector< T2 > &b)
a+=b (element-wise sum)
CMRPTException(const std::string &s)
CSparseMatrix(const CMatrixTemplateNumeric< T > &MAT)
Constructor from a dense matrix of any kind existing in MRPT, creating a "column-compressed" sparse m...
std::vector< T1 > operator+(const std::vector< T1 > &a, const std::vector< T2 > &b)
a+b (element-wise sum)
VECTOR backsub(const VECTOR &b) const
Return the vector from a back-substitution step that solves: Ux=b
const_iterator begin() const
Returns an iterator which points to the starting point of the matrix.
A sparse matrix container (with cells of any type), with iterators.
size_t getColCount() const
The base class of classes that cannot be copied: compile-time errors will be issued on any copy opera...
A numeric matrix of compile-time fixed size.
const_iterator end() const
Returns an iterator which points to the end of the matrix.
CMatrixDouble get_L() const
Return the L matrix (L*L' = M), as a dense matrix.
void multiply_AB(const CSparseMatrix &A, const CSparseMatrix &B)
this = A*B
size_t getRowCount() const
Returns the amount of rows in this matrix.
size_t getRowCount() const
EIGEN_STRONG_INLINE void multiply_AB(const MATRIX1 &A, const MATRIX2 &B)
std::vector< T1 > operator*(const std::vector< T1 > &a, const std::vector< T2 > &b)
a*b (element-wise multiplication)
void setColCount(const size_t nCols)
std::vector< T1 > & operator*=(std::vector< T1 > &a, const std::vector< T2 > &b)
a*=b (element-wise multiplication)
const CSparseMatrix * m_originalSM
A const reference to the original matrix used to build this decomposition.
void setRowCount(const size_t nRows)
Change the number of rows in the matrix (can't be lower than current size)
The base for MRPT-especific exceptions.
void add_AB(const CSparseMatrix &A, const CSparseMatrix &B)
this = A+B
CSparseMatrix(const CMatrixFixedNumeric< T, N, M > &MAT)
Constructor from a dense matrix of any kind existing in MRPT, creating a "column-compressed" sparse m...
Auxiliary class to hold the results of a Cholesky factorization of a sparse matrix.
A sparse matrix structure, wrapping T.
Page generated by Doxygen 1.8.17 for MRPT 1.4.0 SVN: at Sat Jan 18 22:37:07 UTC 2020 | | |