FEI Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
fei_CSVec.cpp
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright 2005 Sandia Corporation. */
3/* Under the terms of Contract DE-AC04-94AL85000, there is a */
4/* non-exclusive license for use of this work by or on behalf */
5/* of the U.S. Government. Export of this program may require */
6/* a license from the United States Government. */
7/*--------------------------------------------------------------------*/
8
9#include <fei_CSVec.hpp>
10#include <algorithm>
11#include <sstream>
12
13namespace fei {
14
15CSVec::CSVec(unsigned sz)
16 : indices_(sz, 0),
17 coefs_(sz, 0.0)
18{
19}
20
22{
23}
24
25CSVec&
27{
28 indices_ = invec.indices_;
29 coefs_ = invec.coefs_;
30
31 return *this;
32}
33
34void add_entries(CSVec& vec, int num, const int* eqns, const double* coefs)
35{
36 for(int i=0; i<num; ++i) add_entry(vec, eqns[i], coefs[i]);
37}
38
39void put_entry(CSVec& vec, int eqn, double coef)
40{
41 std::vector<int>& v_ind = vec.indices();
42 std::vector<double>& v_coef = vec.coefs();
43
44 std::vector<int>::iterator
45 iter = std::lower_bound(v_ind.begin(), v_ind.end(), eqn);
46
47 size_t offset = iter - v_ind.begin();
48
49 if (iter == v_ind.end() || *iter != eqn) {
50 v_ind.insert(iter, eqn);
51 v_coef.insert(v_coef.begin()+offset, coef);
52 }
53 else {
54 v_coef[offset] = coef;
55 }
56}
57
58double get_entry(const CSVec& vec, int eqn)
59{
60 const std::vector<int>& v_ind = vec.indices();
61 const std::vector<double>& v_coef = vec.coefs();
62
63 if (vec.size() == 0) {
64 throw std::runtime_error("get_entry error, CSVec is empty");
65 }
66
67 std::vector<int>::const_iterator
68 iter = std::lower_bound(v_ind.begin(), v_ind.end(), eqn);
69
70 if (iter == v_ind.end()) {
71 throw std::runtime_error("get_entry error, entry not found.");
72 }
73
74 return v_coef[iter - v_ind.begin()];
75}
76
77void remove_entry(CSVec& vec, int eqn)
78{
79 std::vector<int>& v_ind = vec.indices();
80 std::vector<double>& v_coef = vec.coefs();
81
82 std::vector<int>::iterator
83 iter = std::lower_bound(v_ind.begin(), v_ind.end(), eqn);
84
85 if (iter != v_ind.end() && *iter == eqn) {
86 size_t offset = iter - v_ind.begin();
87 v_ind.erase(iter);
88
89 std::vector<double>::iterator coef_iter = v_coef.begin()+offset;
90 v_coef.erase(coef_iter);
91 }
92}
93
94void CSVec::subtract(const CSVec& rhs)
95{
96 for(size_t i=0; i<rhs.coefs_.size(); ++i) {
97 add_entry(*this, rhs.indices_[i], -rhs.coefs_[i]);
98 }
99}
100
101void set_values(CSVec& vec, double scalar)
102{
103 std::fill(vec.coefs().begin(), vec.coefs().end(), scalar);
104}
105
106void add_CSVec_CSVec(const CSVec& u, CSVec& v)
107{
108 const std::vector<int>& indices = u.indices();
109 const std::vector<double>& coefs = u.coefs();
110
111 for(size_t i=0; i<indices.size(); ++i) {
112 add_entry(v, indices[i], coefs[i]);
113 }
114}
115
116}//namespace fei
117
std::vector< int > indices_
Definition fei_CSVec.hpp:51
std::vector< int > & indices()
Definition fei_CSVec.hpp:31
size_t size() const
Definition fei_CSVec.hpp:36
std::vector< double > coefs_
Definition fei_CSVec.hpp:52
CSVec(unsigned sz=0)
Definition fei_CSVec.cpp:15
CSVec & operator=(const CSVec &invec)
Definition fei_CSVec.cpp:26
void subtract(const CSVec &rhs)
Definition fei_CSVec.cpp:94
std::vector< double > & coefs()
Definition fei_CSVec.hpp:33
virtual ~CSVec()
Definition fei_CSVec.cpp:21
void put_entry(CSVec &vec, int eqn, double coef)
Definition fei_CSVec.cpp:39
void add_CSVec_CSVec(const CSVec &u, CSVec &v)
double get_entry(const CSVec &vec, int eqn)
Definition fei_CSVec.cpp:58
void add_entries(CSVec &vec, int num, const int *eqns, const double *coefs)
Definition fei_CSVec.cpp:34
void remove_entry(CSVec &vec, int eqn)
Definition fei_CSVec.cpp:77
void add_entry(CSVec &vec, int eqn, double coef)
Definition fei_CSVec.hpp:56
void set_values(CSVec &vec, double scalar)