31 construct_from_existing_cs(*sm);
40 "I expected a column-compressed sparse matrix, not a triplet form.")
42 sparse_matrix.m = sm->m;
43 sparse_matrix.n = sm->n;
44 sparse_matrix.nz = sm->nz;
45 sparse_matrix.nzmax = sm->nzmax;
49 ::memcpy(sparse_matrix.i, sm->i,
sizeof(
int) * sm->nzmax);
50 ::memcpy(sparse_matrix.p, sm->p,
sizeof(
int) * (sm->n + 1));
51 ::memcpy(sparse_matrix.x, sm->x,
sizeof(
double) * sm->nzmax);
64 sparse_matrix.m = sm->m;
65 sparse_matrix.n = sm->n;
66 sparse_matrix.nz = sm->nz;
67 sparse_matrix.nzmax = sm->nzmax;
69 sparse_matrix.i = sm->i;
70 sparse_matrix.p = sm->p;
71 sparse_matrix.x = sm->x;
84 std::swap(sparse_matrix.n, sparse_matrix.n);
103 sparse_matrix.nzmax = 1;
104 sparse_matrix.m = nRows;
105 sparse_matrix.n = nCols;
106 sparse_matrix.i = (
int*)malloc(
sizeof(
int) * sparse_matrix.nzmax);
107 sparse_matrix.p = (
int*)malloc(
sizeof(
int) * (sparse_matrix.n + 1));
108 sparse_matrix.x = (
double*)malloc(
sizeof(
double) * sparse_matrix.nzmax);
109 sparse_matrix.nz = 0;
115 cs_free(sparse_matrix.i);
116 cs_free(sparse_matrix.p);
117 cs_free(sparse_matrix.x);
123 cs* sm = cs_compress(&triplet);
136 "I expected a column-compressed sparse matrix, not a triplet form.")
137 sparse_matrix.i = (
int*)malloc(
sizeof(
int) * sm.nzmax);
138 sparse_matrix.p = (
int*)malloc(
sizeof(
int) * (sm.n + 1));
139 sparse_matrix.x = (
double*)malloc(
sizeof(
double) * sm.nzmax);
150 sparse_matrix.nzmax = 1;
151 sparse_matrix.m = nRows;
152 sparse_matrix.n = nCols;
153 sparse_matrix.i = (
int*)malloc(
sizeof(
int) * sparse_matrix.nzmax);
154 sparse_matrix.p = (
int*)malloc(
sizeof(
int) * (sparse_matrix.n + 1));
155 sparse_matrix.x = (
double*)malloc(
sizeof(
double) * sparse_matrix.nzmax);
156 sparse_matrix.nz = 0;
161 const size_t row,
const size_t col,
const double val)
165 "insert_entry() is only available for sparse matrix in 'triplet' " 167 if (!cs_entry(&sparse_matrix,
row, col,
val))
169 "Error inserting element in sparse matrix (out of mem?)")
175 if (&other ==
this)
return;
177 cs_free(sparse_matrix.i);
178 cs_free(sparse_matrix.p);
179 cs_free(sparse_matrix.x);
181 sparse_matrix.i = (
int*)malloc(
sizeof(
int) * other.
sparse_matrix.nzmax);
182 sparse_matrix.p = (
int*)malloc(
sizeof(
int) * (other.
sparse_matrix.n + 1));
216 const double*
y = &(
b[0]);
217 double*
x = &(out_res[0]);
218 cs_gaxpy(&sparse_matrix,
y,
x);
223 cs* sm = cs_transpose(&sparse_matrix, 1);
235 d_M.zeros(SM.m, SM.n);
238 for (
int idx = 0; idx < SM.nz; ++idx)
239 d_M(SM.i[idx], SM.p[idx]) +=
247 for (
int j = 0; j < SM.n; j++)
249 const int p0 = SM.p[j];
250 const int p1 = SM.p[j + 1];
251 for (
int p = p0;
p < p1;
p++)
252 d_M(SM.i[
p], j) += SM.x[
p];
261 cs2dense(sparse_matrix, d_M);
268 "compressFromTriplet(): Matrix is already in column-compressed " 271 cs* sm = cs_compress(&this->sparse_matrix);
282 this->get_dense(dense);
285 dense.saveToTextFile(filName);
300 FILE* f =
fopen(filName.c_str(),
"wt");
301 if (!f)
return false;
307 # This sparse matrix can be loaded in Octave/Matlab as follows:\n\ 308 # D=load('file.txt');\n\ 309 # SM=spconvert(D(2:end,:));\n\ 311 # m=D(1,1); n=D(1,2); nzmax=D(1,3);\n\ 312 # Di=D(2:end,1); Dj=D(2:end,2); Ds=D(2:end,3);\n\ 313 # SM=sparse(Di,Dj,Ds, m,n, nzmax);\n\n");
317 f,
"%i %i %i\n", sparse_matrix.m, sparse_matrix.n,
318 sparse_matrix.nzmax);
321 if (sparse_matrix.nz >= 0)
323 for (
int i = 0; i < sparse_matrix.nzmax; i++)
324 if (sparse_matrix.x[i] != 0)
326 f,
"%4i %4i %e\n", 1 + sparse_matrix.i[i],
327 1 + sparse_matrix.p[i], sparse_matrix.x[i]);
333 for (
int j = 0; j < sparse_matrix.n; j++)
335 const int p0 = sparse_matrix.p[j];
336 const int p1 = sparse_matrix.p[j + 1];
337 for (
int p = p0;
p < p1;
p++)
339 f,
"%4i %4i %e\n", 1 + sparse_matrix.i[
p], 1 + j,
358 : m_symbolic_structure(nullptr),
359 m_numeric_structure(nullptr),
375 "CSparseMatrix::CholeskyDecomp: Not positive definite matrix.");
381 cs_nfree(m_numeric_structure);
382 cs_sfree(m_symbolic_structure);
393 const Eigen::VectorXd&
b, Eigen::VectorXd& sol)
const 396 sol.resize(
b.size());
397 this->backsub(&
b[0], &sol[0],
b.size());
402 const double*
b,
double* sol,
const size_t N)
const 405 std::vector<double> tmp(N);
408 m_symbolic_structure->pinv, &
b[0], &tmp[0], N);
410 cs_lsolve(m_numeric_structure->L, &tmp[0]);
411 cs_ltsolve(m_numeric_structure->L, &tmp[0]);
413 m_symbolic_structure->pinv, &tmp[0], &sol[0],
428 m_originalSM->sparse_matrix.nzmax == new_SM.
sparse_matrix.nzmax,
429 "New matrix doesn't have the same sparse structure!");
432 "New matrix doesn't have the same sparse structure!");
434 m_originalSM = &new_SM;
437 cs_nfree(m_numeric_structure);
438 m_numeric_structure =
nullptr;
441 m_numeric_structure =
442 cs_chol(&m_originalSM->sparse_matrix, m_symbolic_structure);
443 if (!m_numeric_structure)
445 "CholeskyDecomp::update: Not positive definite matrix.");
#define ASSERT_EQUAL_(__A, __B)
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
virtual ~CholeskyDecomp()
Destructor.
size_t getRowCount() const
const CSparseMatrix * m_originalSM
A const reference to the original matrix used to build this decomposition.
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.
#define THROW_EXCEPTION(msg)
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.
Used in mrpt::math::CSparseMatrix.
void get_dense(CMatrixDouble &outMat) const
Return a dense representation of the sparse matrix.
EIGEN_STRONG_INLINE size_t getColCount() const
Get number of columns.
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...
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.
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.
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.
size_t getColCount() const
EIGEN_STRONG_INLINE size_t getRowCount() const
Get number of rows.
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.
#define ASSERTMSG_(f, __ERROR_MSG)
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".