28 #ifndef CSparseMatrix_H
29 #define CSparseMatrix_H
45 #include <suitesparse/cs.h>
113 template <
class MATRIX>
114 void construct_from_mrpt_mat(
const MATRIX & C)
116 std::vector<int> row_list, col_list;
117 std::vector<double> content_list;
118 const int nCol = C.getColCount();
119 const int nRow = C.getRowCount();
120 for (
int c=0; c<nCol; ++c)
122 col_list.push_back(row_list.size());
123 for (
int r=0; r<nRow; ++r)
124 if (C.get_unsafe(r,c)!=0)
126 row_list.push_back(r);
127 content_list.push_back(C(r,c));
130 col_list.push_back(row_list.size());
132 sparse_matrix.m = nRow;
133 sparse_matrix.n = nCol;
134 sparse_matrix.nzmax = content_list.size();
135 sparse_matrix.i = (
int*)malloc(
sizeof(
int)*row_list.size());
136 sparse_matrix.p = (
int*)malloc(
sizeof(
int)*col_list.size());
137 sparse_matrix.x = (
double*)malloc(
sizeof(
double)*content_list.size());
139 ::memcpy(sparse_matrix.i, &row_list[0],
sizeof(row_list[0])*row_list.size() );
140 ::memcpy(sparse_matrix.p, &col_list[0],
sizeof(col_list[0])*col_list.size() );
141 ::memcpy(sparse_matrix.x, &content_list[0],
sizeof(content_list[0])*content_list.size() );
143 sparse_matrix.nz = -1;
147 void construct_from_triplet(
const cs & triplet);
150 void construct_from_existing_cs(
const cs &sm);
153 void internal_free_mem();
156 void copy(
const cs *
const sm);
159 void copy_fast(cs *
const sm);
176 template <
typename T>
179 ASSERTMSG_(!data.
empty(),
"Input data must contain at least one non-zero element.")
180 sparse_matrix.i = NULL;
181 sparse_matrix.p = NULL;
182 sparse_matrix.x = NULL;
191 construct_from_triplet(triplet.sparse_matrix);
245 this->add_AB(*
this,other);
263 void insert_entry(
const size_t row,
const size_t col,
const double val );
266 inline void insert_entry_fast(
const size_t row,
const size_t col,
const double val ) { insert_entry(row,col,val); }
274 template <
class MATRIX>
275 inline void insert_submatrix(
const size_t row,
const size_t col,
const MATRIX &M )
277 if (!isTriplet())
THROW_EXCEPTION(
"insert_entry() is only available for sparse matrix in 'triplet' format.")
278 const size_t nR = M.getRowCount();
279 const size_t nC = M.getColCount();
280 for (
size_t r=0;r<nR;r++)
281 for (
size_t c=0;c<nC;c++)
282 insert_entry_fast(row+r,col+c, M.get_unsafe(r,c));
284 sparse_matrix.m = std::max(sparse_matrix.m,
int(row+nR));
285 sparse_matrix.n = std::max(sparse_matrix.n,
int(col+nC));
292 void compressFromTriplet();
305 bool saveToTextFile_dense(
const std::string &filName);
330 bool saveToTextFile_sparse(
const std::string &filName);
337 inline void setRowCount(
const size_t nRows) {
ASSERT_(nRows>=(
size_t)sparse_matrix.m); sparse_matrix.m = nRows; }
338 inline void setColCount(
const size_t nCols) {
ASSERT_(nCols>=(
size_t)sparse_matrix.n); sparse_matrix.n = nCols; }
341 inline bool isTriplet()
const {
return sparse_matrix.nz>=0; }
404 void backsub(
const double *b,
double *result,
const size_t N)
const;