41 "I expected a column-compressed sparse matrix, not a triplet form.");
124 cs* sm = cs_compress(&triplet);
137 "I expected a column-compressed sparse matrix, not a triplet form.");
140 sparse_matrix.x = (
double*)malloc(
sizeof(
double) * sm.nzmax);
162 const size_t row,
const size_t col,
const double val)
166 "insert_entry() is only available for sparse matrix in 'triplet' " 170 "Error inserting element in sparse matrix (out of mem?)")
176 if (&other ==
this)
return;
214 out_res.resize(
rows());
215 const double*
y = &(
b[0]);
216 double*
x = &(out_res[0]);
234 d_M.zeros(SM.m, SM.n);
237 for (
int idx = 0; idx < SM.nz; ++idx)
238 d_M(SM.i[idx], SM.p[idx]) +=
246 for (
int j = 0; j < SM.n; j++)
248 const int p0 = SM.p[j];
249 const int p1 = SM.p[j + 1];
250 for (
int p = p0;
p < p1;
p++)
251 d_M(SM.i[
p], j) += SM.x[
p];
267 "compressFromTriplet(): Matrix is already in column-compressed " 284 dense.saveToTextFile(filName);
299 FILE* f =
fopen(filName.c_str(),
"wt");
300 if (!f)
return false;
306 # This sparse matrix can be loaded in Octave/Matlab as follows:\n\ 307 # D=load('file.txt');\n\ 308 # SM=spconvert(D(2:end,:));\n\ 310 # m=D(1,1); n=D(1,2); nzmax=D(1,3);\n\ 311 # Di=D(2:end,1); Dj=D(2:end,2); Ds=D(2:end,3);\n\ 312 # SM=sparse(Di,Dj,Ds, m,n, nzmax);\n\n");
336 for (
int p = p0;
p < p1;
p++)
357 : m_symbolic_structure(nullptr),
358 m_numeric_structure(nullptr),
374 "CSparseMatrix::CholeskyDecomp: Not positive definite matrix.");
380 cs_nfree(m_numeric_structure);
381 cs_sfree(m_symbolic_structure);
392 const Eigen::VectorXd&
b, Eigen::VectorXd& sol)
const 395 sol.resize(
b.size());
396 this->backsub(&
b[0], &sol[0],
b.size());
401 const double*
b,
double* sol,
const size_t N)
const 404 std::vector<double> tmp(N);
407 m_symbolic_structure->pinv, &
b[0], &tmp[0], N);
409 cs_lsolve(m_numeric_structure->L, &tmp[0]);
410 cs_ltsolve(m_numeric_structure->L, &tmp[0]);
412 m_symbolic_structure->pinv, &tmp[0], &sol[0],
427 m_originalSM->sparse_matrix.nzmax == new_SM.
sparse_matrix.nzmax,
428 "New matrix doesn't have the same sparse structure!");
431 "New matrix doesn't have the same sparse structure!");
433 m_originalSM = &new_SM;
436 cs_nfree(m_numeric_structure);
437 m_numeric_structure =
nullptr;
440 m_numeric_structure =
441 cs_chol(&m_originalSM->sparse_matrix, m_symbolic_structure);
442 if (!m_numeric_structure)
444 "CholeskyDecomp::update: Not positive definite matrix.");
virtual ~CholeskyDecomp()
Destructor.
#define THROW_EXCEPTION(msg)
const CSparseMatrix * m_originalSM
A const reference to the original matrix used to build this decomposition.
Used in mrpt::math::CSparseMatrix.
int void fclose(FILE *f)
An OS-independent version of fclose.
void construct_from_triplet(const cs &triplet)
Initialization from a triplet "cs", which is first compressed.
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
A sparse matrix structure, wrapping T.
void compressFromTriplet()
ONLY for TRIPLET matrices: convert the matrix in a column-compressed form.
void swap(CSparseMatrix &other)
Fast swap contents with another sparse matrix.
void insert_entry(const size_t row, const size_t col, const double val)
@ Access the matrix, get, set elements, etc.
CMatrixDouble get_L() const
Return the L matrix (L*L' = M), as a dense matrix.
void get_dense(CMatrixDouble &outMat) const
Return a dense representation of the sparse matrix.
#define ASSERT_(f)
Defines an assertion mechanism.
This base provides a set of functions for maths stuff.
void multiply_AB(const CSparseMatrix &A, const CSparseMatrix &B)
this = A*B
void construct_from_existing_cs(const cs &sm)
To be called by constructors only, assume previous pointers are trash and overwrite them...
#define ASSERT_EQUAL_(__A, __B)
Assert comparing two values, reporting their actual values upon failure.
CSparseMatrix(const size_t nRows=0, const size_t nCols=0)
Create an initially empty sparse matrix, in the "triplet" form.
void add_AB(const CSparseMatrix &A, const CSparseMatrix &B)
this = A+B
void operator=(const CSparseMatrix &other)
Copy operator from another existing object.
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
void multiply_Ab(const mrpt::math::CVectorDouble &b, mrpt::math::CVectorDouble &out_res) const
out_res = this * b
GLsizei const GLchar ** string
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 nonzer...
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
bool isTriplet() const
Returns true if this sparse matrix is in "triplet" form.
css * m_symbolic_structure
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
CholeskyDecomp(const CSparseMatrix &A)
Constructor from a square definite-positive sparse matrix A, which can be use to solve Ax=b The actua...
bool saveToTextFile_dense(const std::string &filName)
save as a dense matrix to a text file
void update(const CSparseMatrix &new_SM)
Update the Cholesky factorization from an updated vesion of the original input, square definite-posit...
GLenum GLenum GLvoid * row
virtual ~CSparseMatrix()
Destructor.
CSparseMatrix transpose() const
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)...
static void cs2dense(const cs &SM, CMatrixDouble &outMat)
Static method to convert a "cs" structure into a dense representation of the sparse matrix...
csn * m_numeric_structure
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
VECTOR backsub(const VECTOR &b) const
Return the vector from a back-substitution step that solves: Ux=b.
bool isColumnCompressed() const
Returns true if this sparse matrix is in "column compressed" form.
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 NUL...
void internal_free_mem()
free buffers (deallocate the memory of the i,p,x buffers)
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".