void SparseMatrixDynamicCSR:: add_sparse_row(int i, const DynamicSparseVector &x, double multiplier) { assert(i>=0 && i<m); DynamicSparseVector &r=row[i]; DynamicSparseVector::iterator p=r.begin(); DynamicSparseVector::const_iterator q=x.begin(); while(p!=r.end() && q!=x.end()){ if(p->index<q->index) ++p; else if(p->index>q->index){ r.insert(p, SparseEntry(q->index, multiplier*q->value)); ++q; }else{ p->value+=multiplier*q->value; ++p; ++q; } } for(; q!=x.end(); ++q) r.push_back(SparseEntry(q->index, multiplier*q->value)); }
double &SparseMatrixDynamicCSR:: operator()(int i, int j) { assert(i>=0 && i<m && j>=0 && j<n); DynamicSparseVector &r=row[i]; DynamicSparseVector::iterator p; for(p=r.begin(); p!=r.end(); ++p){ if(p->index==j) return p->value; else if(p->index>j) break; } return (r.insert(p, SparseEntry(j, 0)))->value; }
static void add_to_vector(double multiplier, const StaticSparseVector &u, DynamicSparseVector &v) { StaticSparseVector::const_iterator p=u.begin(); if(p==u.end()) return; DynamicSparseVector::iterator q=v.begin(); for(;;){ if(q==v.end()) break; if(p->index < q->index){ q=v.insert(q, SparseEntry(p->index, multiplier*p->value)); ++p; if(p==u.end()) return; }else if(p->index > q->index){ ++q; if(q==v.end()) break; }else{ // p->index == q->index){ q->value+=multiplier*p->value; ++p; if(p==u.end()) return; ++q; if(q==v.end()) break; } } for(;;){ v.push_back(SparseEntry(p->index, multiplier*p->value)); ++p; if(p==u.end()) return; } }
static void copy_column(int colstart, int nextcolstart, const int *rowindex, const double *value, DynamicSparseVector &v) { for(int i=colstart; i<nextcolstart; ++i) v.push_back(SparseEntry(rowindex[i], value[i])); }