42 "I expected a column-compressed sparse matrix, not a triplet form.");
125 cs* sm = cs_compress(&triplet);
138 "I expected a column-compressed sparse matrix, not a triplet form.");
141 sparse_matrix.x = (
double*)malloc(
sizeof(
double) * sm.nzmax);
163 const size_t row,
const size_t col,
const double val)
167 "insert_entry() is only available for sparse matrix in 'triplet' " 171 "Error inserting element in sparse matrix (out of mem?)");
177 if (&other ==
this)
return *
this;
217 const double* y = &(b[0]);
218 double* x = &(out_res[0]);
239 for (
int idx = 0; idx < SM.nz; ++idx)
240 d_M(SM.i[idx], SM.p[idx]) +=
248 for (
int j = 0; j < SM.n; j++)
250 const int p0 = SM.p[j];
251 const int p1 = SM.p[j + 1];
252 for (
int p = p0; p < p1; p++)
253 d_M(SM.i[p], j) += SM.x[p];
269 "compressFromTriplet(): Matrix is already in column-compressed " 301 FILE* f =
fopen(filName.c_str(),
"wt");
302 if (!f)
return false;
308 # This sparse matrix can be loaded in Octave/Matlab as follows:\n\ 309 # D=load('file.txt');\n\ 310 # SM=spconvert(D(2:end,:));\n\ 312 # m=D(1,1); n=D(1,2); nzmax=D(1,3);\n\ 313 # Di=D(2:end,1); Dj=D(2:end,2); Ds=D(2:end,3);\n\ 314 # SM=sparse(Di,Dj,Ds, m,n, nzmax);\n\n");
338 for (
int p = p0; p < p1; p++)
359 : m_symbolic_structure(nullptr),
360 m_numeric_structure(nullptr),
376 "CSparseMatrix::CholeskyDecomp: Not positive definite matrix.");
382 cs_nfree(m_numeric_structure);
383 cs_sfree(m_symbolic_structure);
398 this->backsub(&b[0], &sol[0], b.
size());
403 const double* b,
double* sol,
const size_t N)
const 406 std::vector<double> tmp(N);
409 m_symbolic_structure->pinv, &b[0], &tmp[0], N);
411 cs_lsolve(m_numeric_structure->L, &tmp[0]);
412 cs_ltsolve(m_numeric_structure->L, &tmp[0]);
414 m_symbolic_structure->pinv, &tmp[0], &sol[0],
429 m_originalSM->sparse_matrix.nzmax == new_SM.
sparse_matrix.nzmax,
430 "New matrix doesn't have the same sparse structure!");
433 "New matrix doesn't have the same sparse structure!");
435 m_originalSM = &new_SM;
438 cs_nfree(m_numeric_structure);
439 m_numeric_structure =
nullptr;
442 m_numeric_structure =
443 cs_chol(&m_originalSM->sparse_matrix, m_symbolic_structure);
444 if (!m_numeric_structure)
446 "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.
size_type size() const
Get a 2-vector with [NROWS NCOLS] (as in MATLAB command size(x))
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 saveToTextFile(const std::string &file, mrpt::math::TMatrixTextFileFormat fileFormat=mrpt::math::MATRIX_FORMAT_ENG, bool appendMRPTHeader=false, const std::string &userHeader=std::string()) const
Saves the vector/matrix to a file compatible with MATLAB/Octave text format.
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
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
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...
void matProductOf_Ab(const mrpt::math::CVectorDouble &b, mrpt::math::CVectorDouble &out_res) const
out_res = this * b
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.
CSparseMatrix & operator=(const CSparseMatrix &other)
Copy operator from another existing object.
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...
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.
void resize(std::size_t N, bool zeroNewElements=false)
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".