32 construct_from_existing_cs(*sm);
39 ASSERTMSG_(sm->nz==-1,
"I expected a column-compressed sparse matrix, not a triplet form.")
41 sparse_matrix.m = sm->m;
42 sparse_matrix.n = sm->n;
43 sparse_matrix.nz = sm->nz;
44 sparse_matrix.nzmax = sm->nzmax;
47 ::memcpy(sparse_matrix.i,sm->i,
sizeof(
int)*sm->nzmax);
48 ::memcpy(sparse_matrix.p,sm->p,
sizeof(
int)*(sm->n+1));
49 ::memcpy(sparse_matrix.x,sm->x,
sizeof(
double)*sm->nzmax);
61 sparse_matrix.m = sm->m;
62 sparse_matrix.n = sm->n;
63 sparse_matrix.nz = sm->nz;
64 sparse_matrix.nzmax = sm->nzmax;
66 sparse_matrix.i = sm->i;
67 sparse_matrix.p = sm->p;
68 sparse_matrix.x = sm->x;
81 std::swap( sparse_matrix.n, sparse_matrix.n);
105 sparse_matrix.nzmax = 1;
106 sparse_matrix.m = nRows;
107 sparse_matrix.n = nCols;
108 sparse_matrix.i = (
int*)malloc(
sizeof(
int)*sparse_matrix.nzmax);
109 sparse_matrix.p = (
int*)malloc(
sizeof(
int)*(sparse_matrix.n+1));
110 sparse_matrix.x = (
double*)malloc(
sizeof(
double)*sparse_matrix.nzmax);
111 sparse_matrix.nz = 0;
117 cs_free(sparse_matrix.i);
118 cs_free(sparse_matrix.p);
119 cs_free(sparse_matrix.x);
125 cs * sm = cs_compress(&triplet);
134 ASSERTMSG_(sm.nz==-1,
"I expected a column-compressed sparse matrix, not a triplet form.")
135 sparse_matrix.i = (
int*)malloc(
sizeof(
int)*sm.nzmax);
136 sparse_matrix.p = (
int*)malloc(
sizeof(
int)*(sm.n+1));
137 sparse_matrix.x = (
double*)malloc(
sizeof(
double)*sm.nzmax);
146 sparse_matrix.nzmax = 1;
147 sparse_matrix.m = nRows;
148 sparse_matrix.n = nCols;
149 sparse_matrix.i = (
int*)malloc(
sizeof(
int)*sparse_matrix.nzmax);
150 sparse_matrix.p = (
int*)malloc(
sizeof(
int)*(sparse_matrix.n+1));
151 sparse_matrix.x = (
double*)malloc(
sizeof(
double)*sparse_matrix.nzmax);
152 sparse_matrix.nz = 0;
160 THROW_EXCEPTION(
"insert_entry() is only available for sparse matrix in 'triplet' format.")
161 if (!cs_entry(&sparse_matrix,
row,col,
val))
162 THROW_EXCEPTION(
"Error inserting element in sparse matrix (out of mem?)")
168 if (&other==
this)
return;
170 cs_free(sparse_matrix.i);
171 cs_free(sparse_matrix.p);
172 cs_free(sparse_matrix.x);
174 sparse_matrix.i = (
int*)malloc(
sizeof(
int)*other.
sparse_matrix.nzmax);
175 sparse_matrix.p = (
int*)malloc(
sizeof(
int)*(other.
sparse_matrix.n+1));
176 sparse_matrix.x = (
double*)malloc(
sizeof(
double)*other.
sparse_matrix.nzmax);
205 const double *
y = &(
b[0]);
206 double *
x = &(out_res[0]);
207 cs_gaxpy(&sparse_matrix,
y,
x);
213 cs * sm = cs_transpose(&sparse_matrix,1);
224 d_M.zeros(SM.m,SM.n);
227 for (
int idx=0;idx<SM.nz; ++idx)
228 d_M(SM.i[idx],SM.p[idx]) += SM.x[idx];
234 for (
int j = 0 ; j < SM.n ; j++)
236 const int p0 = SM.p [j];
237 const int p1 = SM.p [j+1];
238 for (
int p = p0 ;
p < p1 ;
p++)
239 d_M(SM.i[
p],j) += SM.x[
p];
246 cs2dense(sparse_matrix, d_M);
252 THROW_EXCEPTION(
"compressFromTriplet(): Matrix is already in column-compressed format.")
254 cs * sm = cs_compress(&this->sparse_matrix);
265 this->get_dense(dense);
268 dense.saveToTextFile(filName);
271 catch(...) {
return false; }
280 FILE *f=
fopen(filName.c_str(),
"wt");
281 if (!f)
return false;
285 # This sparse matrix can be loaded in Octave/Matlab as follows:\n\ 286 # D=load('file.txt');\n\ 287 # SM=spconvert(D(2:end,:));\n\ 289 # m=D(1,1); n=D(1,2); nzmax=D(1,3);\n\ 290 # Di=D(2:end,1); Dj=D(2:end,2); Ds=D(2:end,3);\n\ 291 # SM=sparse(Di,Dj,Ds, m,n, nzmax);\n\n");
294 fprintf(f,
"%i %i %i\n",sparse_matrix.m, sparse_matrix.n, sparse_matrix.nzmax);
297 if (sparse_matrix.nz>=0)
299 for (
int i=0;i<sparse_matrix.nzmax;i++)
300 if (sparse_matrix.x[i]!=0)
301 fprintf(f,
"%4i %4i %e\n", 1+sparse_matrix.i[i], 1+sparse_matrix.p[i], sparse_matrix.x[i]);
307 for (
int j = 0 ; j < sparse_matrix.n ; j++)
309 const int p0 = sparse_matrix.p [j];
310 const int p1 = sparse_matrix.p [j+1];
311 for (
int p = p0 ;
p < p1 ;
p++)
312 fprintf(f,
"%4i %4i %e\n", 1+sparse_matrix.i[
p],1+j, sparse_matrix.x[
p]);
330 m_symbolic_structure (NULL),
331 m_numeric_structure (NULL),
350 cs_nfree(m_numeric_structure);
351 cs_sfree(m_symbolic_structure);
363 const Eigen::VectorXd &
b,
364 Eigen::VectorXd &sol)
const 367 sol.resize(
b.size());
368 this->backsub(&
b[0],&sol[0],
b.size());
375 const size_t N)
const 378 std::vector<double> tmp(N);
380 cs_ipvec(m_symbolic_structure->pinv,&
b[0],&tmp[0],N);
382 cs_lsolve(m_numeric_structure->L,&tmp[0]);
383 cs_ltsolve(m_numeric_structure->L,&tmp[0]);
384 cs_pvec(m_symbolic_structure->pinv,&tmp[0],&sol[0],N);
395 ASSERTMSG_(m_originalSM->sparse_matrix.nzmax == new_SM.
sparse_matrix.nzmax,
"New matrix doesn't have the same sparse structure!");
396 ASSERTMSG_(m_originalSM->sparse_matrix.n == new_SM.
sparse_matrix.n,
"New matrix doesn't have the same sparse structure!");
398 m_originalSM = &new_SM;
401 cs_nfree(m_numeric_structure);
402 m_numeric_structure = NULL;
405 m_numeric_structure = cs_chol(&m_originalSM->sparse_matrix,m_symbolic_structure);
406 if (!m_numeric_structure)
#define ASSERT_EQUAL_(__A, __B)
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".
FILE BASE_IMPEXP * fopen(const char *fileName, const char *mode) MRPT_NO_THROWS
An OS-independent version of fopen.
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 BASE_IMPEXP void BASE_IMPEXP 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...
int BASE_IMPEXP fprintf(FILE *fil, const char *format,...) MRPT_NO_THROWS MRPT_printf_format_check(2
An OS-independent version of fprintf.
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...
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
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)