9 #include <fei_CSVec.hpp>
15 CSVec::CSVec(
unsigned sz)
26 CSVec::operator=(
const CSVec& invec)
28 indices_ = invec.indices_;
29 coefs_ = invec.coefs_;
34 void add_entries(CSVec& vec,
int num,
const int* eqns,
const double* coefs)
36 for(
int i=0; i<num; ++i) add_entry(vec, eqns[i], coefs[i]);
39 void put_entry(CSVec& vec,
int eqn,
double coef)
41 std::vector<int>& v_ind = vec.indices();
42 std::vector<double>& v_coef = vec.coefs();
44 std::vector<int>::iterator
45 iter = std::lower_bound(v_ind.begin(), v_ind.end(), eqn);
47 size_t offset = iter - v_ind.begin();
49 if (iter == v_ind.end() || *iter != eqn) {
50 v_ind.insert(iter, eqn);
51 v_coef.insert(v_coef.begin()+offset, coef);
54 v_coef[offset] = coef;
58 double get_entry(
const CSVec& vec,
int eqn)
60 const std::vector<int>& v_ind = vec.indices();
61 const std::vector<double>& v_coef = vec.coefs();
63 if (vec.size() == 0) {
64 throw std::runtime_error(
"get_entry error, CSVec is empty");
67 std::vector<int>::const_iterator
68 iter = std::lower_bound(v_ind.begin(), v_ind.end(), eqn);
70 if (iter == v_ind.end()) {
71 throw std::runtime_error(
"get_entry error, entry not found.");
74 return v_coef[iter - v_ind.begin()];
77 void remove_entry(CSVec& vec,
int eqn)
79 std::vector<int>& v_ind = vec.indices();
80 std::vector<double>& v_coef = vec.coefs();
82 std::vector<int>::iterator
83 iter = std::lower_bound(v_ind.begin(), v_ind.end(), eqn);
85 if (iter != v_ind.end() && *iter == eqn) {
86 size_t offset = iter - v_ind.begin();
89 std::vector<double>::iterator coef_iter = v_coef.begin()+offset;
90 v_coef.erase(coef_iter);
94 void CSVec::subtract(
const CSVec& rhs)
96 for(
size_t i=0; i<rhs.coefs_.size(); ++i) {
97 add_entry(*
this, rhs.indices_[i], -rhs.coefs_[i]);
101 void set_values(CSVec& vec,
double scalar)
103 std::fill(vec.coefs().begin(), vec.coefs().end(), scalar);
108 const std::vector<int>& indices = u.indices();
109 const std::vector<double>& coefs = u.coefs();
111 for(
size_t i=0; i<indices.size(); ++i) {
112 add_entry(v, indices[i], coefs[i]);
void add_CSVec_CSVec(const CSVec &u, CSVec &v)