17 template <
typename _Type>
21 template <
typename _Type>
26 template <
typename _Type>
30 MAP_VEC = source.MAP_VEC;
32 MAX_SIZE = source.MAX_SIZE;
36 template <
typename _Type>
38 for(iter pos = MAP_VEC.begin(); pos != MAP_VEC.end(); ++pos) {
44 template <
typename _Type>
47 if(X.
size() != size()) {
49 problem =
" The SparseVector.operator-= method is trying to use \n";
50 problem +=
" two vectors of unequal length \n";
54 citer pos_ro = X.MAP_VEC.begin();
55 iter pos_rw = MAP_VEC.begin();
57 std::size_t index_rw = pos_rw -> first;
58 std::size_t index_ro = pos_ro -> first;
59 if(index_rw == index_ro) {
61 pos_rw -> second -= pos_ro -> second;
65 if(index_rw > index_ro) {
68 (index_ro) = -(pos_ro -> second);
71 if(index_rw < index_ro) {
75 }
while(pos_ro != X.MAP_VEC.end() && pos_rw != MAP_VEC.end());
76 if(pos_ro != X.MAP_VEC.end()) {
80 (pos_ro -> first) = -(pos_ro -> second);
82 }
while(pos_ro != X.MAP_VEC.end());
87 template <
typename _Type>
90 if(X.
size() != size()) {
92 problem =
" The SparseVector.operator+= method is trying to use \n";
93 problem +=
" two vectors of unequal length \n";
97 citer pos_ro = X.MAP_VEC.begin();
98 iter pos_rw = MAP_VEC.begin();
100 std::size_t index_rw = pos_rw -> first;
101 std::size_t index_ro = pos_ro -> first;
102 if(index_rw == index_ro) {
104 pos_rw -> second += pos_ro -> second;
108 if(index_rw > index_ro) {
111 (index_ro) = pos_ro -> second;
114 if(index_rw < index_ro) {
118 }
while(pos_ro != X.MAP_VEC.end() && pos_rw != MAP_VEC.end());
119 if(pos_ro != X.MAP_VEC.end()) {
123 (pos_ro -> first) = pos_ro -> second;
125 }
while(pos_ro != X.MAP_VEC.end());
130 template <
typename _Type>
133 if(X.
size() != size()) {
135 problem =
" The SparseVector.operator+ method is trying to use \n";
136 problem +=
" two vectors of unequal length \n";
145 template <
typename _Type>
148 if(X.
size() != size()) {
150 problem =
" The SparseVector.operator- method is trying to use \n";
151 problem +=
" two vectors of unequal length \n";
160 template <
typename _Type>
167 template <
typename _Type>
175 template <
typename _Type>
180 template <
typename _Type>
185 template <
typename _Type>
188 if(X.
size() != size()) {
190 problem =
" The SparseVector.dot method is trying to use \n";
191 problem +=
" two vectors of unequal length \n";
197 for(iter pos = temp.MAP_VEC.begin(); pos != temp.MAP_VEC.end(); ++pos) {
198 std::size_t index = pos -> first;
202 temp[ index ] *= get(index);
207 template <
typename _Type>
212 for(citer pos = MAP_VEC.begin(); pos != MAP_VEC.end(); ++pos) {
213 sum += std::abs(pos -> second);
218 template <
typename _Type>
223 for(citer pos = MAP_VEC.begin(); pos != MAP_VEC.end(); ++pos) {
224 sum += std::pow(std::abs(pos -> second), 2);
229 template <
typename _Type>
233 for(citer pos = MAP_VEC.begin(); pos != MAP_VEC.end(); ++pos) {
234 if(std::abs(pos -> second) > max) {
235 max = std::abs(pos -> second);
241 template <
typename _Type>
243 for(iter pos = MAP_VEC.begin(); pos != MAP_VEC.end(); ++pos) {
244 pos -> second *= scale;
248 template <
typename _Type>
254 template <
typename _Type>
259 template <
typename _Type>
261 citer location(MAP_VEC.begin());
262 _Type min_diff(location -> second - value);
263 citer start(++location);
264 for(citer pos = start; pos != MAP_VEC.end(); ++pos) {
265 _Type diff(pos -> second - value);
266 if(std::abs(diff) < std::abs(min_diff)) {
271 return location -> first;
274 template <
typename _Type>
276 citer location(MAP_VEC.begin());
277 double max(std::abs(location -> second));
278 citer start(++location);
279 for(citer pos = start; pos != MAP_VEC.end(); ++pos) {
280 if(std::abs(pos -> second) > max) {
281 max = std::abs(pos -> second);
285 return location -> first;
288 template <
typename _Type>
290 citer location(MAP_VEC.begin());
291 double min(std::abs(location -> second));
292 citer start(++location);
293 for(citer pos = start; pos != MAP_VEC.end(); ++pos) {
294 if(std::abs(pos -> second) < min) {
295 min = std::abs(pos -> second);
299 return location -> first;
302 template <
typename _Type>
304 const std::size_t& j) {
306 if(i >= size() || j >= size()) {
308 problem =
" The SparseVector.swap method is trying to access \n";
309 problem +=
" outside the container. \n";
313 std::swap<_Type>(MAP_VEC[ i ], MAP_VEC[ j ]);
316 template <
typename _Type>
318 std::cout.precision(6);
319 if(MAP_VEC.begin() == MAP_VEC.end()) {
320 std::cout <<
"[ Empty vector ]\n";
322 for(citer pos = MAP_VEC.begin(); pos != MAP_VEC.end(); ++pos) {
323 std::cout <<
"[ " << pos -> first <<
" ]" <<
" = " << pos -> second <<
" ";
337 pos = MAP_VEC.begin();
340 storage[ i ] = pos -> second;
341 cols[ i ] = pos -> first;
346 }
while(pos != MAP_VEC.end());
352 void SparseVector<std::complex<double> >::get_petsc( PetscScalar* storage, PetscInt* cols ) {
357 pos = MAP_VEC.begin();
360 storage[ i ] = std::real(pos -> second) + PETSC_i * std::imag(pos -> second);
361 cols[ i ] = pos -> first;
366 }
while(pos != MAP_VEC.end());
372 template class SparseVector<double>
374 template class SparseVector<std::complex<double> >
The collection of CppNoddy exceptions.
Some Function Objects that CppNoddy makes use of in algorithms applied to STL containers.
A templated SparseVector class – a sparse, variable size, vector object.
An exception class to be thrown when a container of incorrect geometry used in any class/method.
An exception to indicate that a CppNoddy container has been accessed with index/indices outside the m...
An SparseVector class – a sparse vector object.
void sub(const SparseVector< _Type > &X)
Subtract a vector, element wise.
SparseVector(const std::size_t &max)
Constructor for a non-filled vector, to be filled by the user.
double inf_norm() const
Infinity norm.
void scale(const _Type &scale)
Scale each element of the vector.
void resize(const std::size_t &length)
Resize the maximum length of the sparse vector.
SparseVector< _Type > & operator*=(const _Type &m)
Overloading *= for scalar multiplication.
void dump() const
Output the sparse vector's contents.
double two_norm() const
l2-norm.
std::size_t size() const
Find the (max) size of the vector.
const _Type dot(const SparseVector< _Type > &x) const
A dot product.
void swap(const std::size_t &i, const std::size_t &j)
Swap elements i and j.
void add(const SparseVector< _Type > &X)
Add a vector, element wise.
SparseVector< _Type > & operator-=(const SparseVector< _Type > &X)
Overloading -= for sparse vectors.
SparseVector< _Type > operator*(const _Type &m) const
Overloading multiplication for a scalar.
SparseVector< _Type > & operator+=(const SparseVector< _Type > &X)
Overloading += for sparse vectors.
std::size_t maxabs_index() const
Find the index of the maximum element in the vector.
SparseVector & operator=(const SparseVector &source)
Assignment operator.
SparseVector< _Type > operator+() const
Overloading for +.
std::size_t minabs_index() const
Find the index of the maximum element in the vector.
std::size_t nearest_index(const _Type &value) const
Find the index of the element NEAREST in value to that specified.
SparseVector< _Type > operator-() const
Overloading for -.
double one_norm() const
l1-norm.
void clear()
Remove all elements from the sparse vector.
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...