15 #include "config_singleton.h"
43 return pos != other.pos;
46 inline double &operator* ()
const;
68 SparseMatrix(
unsigned int nrow = 0,
unsigned int ncol = 1,
bool symmetric =
false,
int initialNumberOfElements = 200);
82 MatrixState getMatrixState()
const;
111 friend std::ostream& operator<<(std::ostream& os,
const SparseMatrix& A);
114 bool hasElement(
unsigned int row,
unsigned int column)
const;
131 size_t getNumberOfElements();
164 void setSymmetry(Symmetry symmetry);
165 Symmetry getSymmetry()
const {
return symmetry; }
167 unsigned int getRows()
const {
return nrow; }
169 unsigned int getColumns()
const {
return ncol; }
173 double operator()(
unsigned int row,
unsigned int column = 0)
const;
175 double& operator()(
unsigned int row,
unsigned int column = 0);
184 long key(
unsigned int row,
unsigned int column)
const;
185 void assertValidIndex(
unsigned int row,
unsigned int column)
const;
186 void assertHasSparse()
const;
187 void increaseTripletCapacity();
188 void assertValidInitAddValue(
unsigned int row,
unsigned int column)
const;
190 int binarySearch(
int *array,
int low,
int high,
unsigned int value)
const;
192 int getIndex(
unsigned int row,
unsigned int column)
const;
194 void createTriplet();
196 double& initAddValue(
unsigned int row,
unsigned int column);
198 double& getValue(
unsigned int row,
unsigned int column);
200 double getValue(
unsigned int row,
unsigned int column)
const;
202 cholmod_sparse *sparse;
203 cholmod_triplet *triplet;
210 int maxTripletElements;
215 double& SparseMatrixIter::operator* ()
const{
216 if (sparseMatrix->getMatrixState() == INIT){
217 double * ptr =
static_cast<double*
>(sparseMatrix->triplet->x);
219 }
else if (sparseMatrix->getMatrixState() == BUILT){
220 double * ptr =
static_cast<double*
>(sparseMatrix->sparse->x);
223 throw std::runtime_error(
"Invalid matrix state");
229 inline double SparseMatrix::operator()(
unsigned int row,
unsigned int column)
const
231 return getValue(row, column);
234 inline double& SparseMatrix::operator()(
unsigned int row,
unsigned int column)
236 if (sparse !=
nullptr){
237 return getValue(row, column);
239 return initAddValue(row, column);
243 inline int SparseMatrix::binarySearch(
int *array,
int low,
int high,
unsigned int value)
const {
247 int midpoint = (((
unsigned int)high + (
unsigned int)low) >> 1);
248 int midpointValue = array[midpoint];
249 if (value == midpointValue) {
251 }
else if (value < midpointValue) {
260 inline int SparseMatrix::getIndex(
unsigned int row,
unsigned int column)
const
263 assertValidIndex(row, column);
265 if ((symmetry == SYMMETRIC_UPPER && row > column) || (symmetry == SYMMETRIC_LOWER && row < column)) {
266 std::swap(row, column);
269 int iFrom = jColumn[column];
270 int iTo = jColumn[column+1]-1;
272 return binarySearch(iRow, iFrom, iTo, row);
275 inline long SparseMatrix::key(
unsigned int row,
unsigned int column)
const {
276 int shiftBits =
sizeof(long)*8/2;
277 return (((
long)row)<<shiftBits)+column;
280 inline void SparseMatrix::createTriplet(){
281 triplet = cholmod_allocate_triplet(nrow, ncol, maxTripletElements, symmetry, CHOLMOD_REAL, ConfigSingleton::getCommonPtr());
282 values = (
double *)triplet->x;
283 iRow = (
int *)triplet->i;
284 jColumn = (
int *)triplet->j;
287 inline double& SparseMatrix::initAddValue(
unsigned int row,
unsigned int column)
291 }
else if (triplet->nnz == maxTripletElements ){
292 increaseTripletCapacity();
294 assertValidInitAddValue(row, column);
295 iRow[triplet->nnz] = row;
296 jColumn[triplet->nnz] = column;
297 values[triplet->nnz] = 0;
300 return values[triplet->nnz - 1];
303 inline double& SparseMatrix::getValue(
unsigned int row,
unsigned int column)
308 int index = getIndex(row, column);
310 static double zero = 0;
314 return values[index];
317 inline double SparseMatrix::getValue(
unsigned int row,
unsigned int column)
const
322 int index = getIndex(row, column);
326 return values[index];
Definition: dense_matrix.h:31
void dropSmallEntries(double tol=1e-7f)
Definition: sparse_matrix.cpp:163
SparseMatrix(unsigned int nrow=0, unsigned int ncol=1, bool symmetric=false, int initialNumberOfElements=200)
Definition: sparse_matrix.cpp:23
void append(const SparseMatrix &m)
Definition: sparse_matrix.cpp:138
Definition: sparse_matrix.h:62
double norm(int norm) const
Definition: sparse_matrix.cpp:363
Definition: sparse_matrix.h:38