CppNoddy  0.92
Loading...
Searching...
No Matches
SparseVector.h
Go to the documentation of this file.
1/// \file SparseVector.h
2/// A templated SparseVector class -- a sparse, variable size, vector object.
3
4#ifndef SPARSEVECTOR_H
5#define SPARSEVECTOR_H
6
7#include <map>
8#include <Exceptions.h>
9
10#if defined(PETSC_D) || defined(PETSC_Z)
11 #include "petsc.h"
12#endif
13
14namespace CppNoddy {
15
16 /// An SparseVector class -- a sparse vector object.
17 /// This is templated but intended ONLY for double or
18 /// std::complex<double>.
19 template <typename _Type>
21 typedef typename std::map< std::size_t, _Type >::const_iterator citer;
22 typedef typename std::map< std::size_t, _Type >::iterator iter;
23
24 public:
25
26 /// Constructor for a non-filled vector, to be filled by the user.
27 /// \param max A maximum size for the sparse vector, used in geometry
28 /// checking.
29 explicit SparseVector(const std::size_t& max);
30
31 /// Copy constructor.
32 /// \param source The source object to be copied
33 SparseVector(const SparseVector& source);
34
35 /// Assignment operator.
36 /// \param source The source object for the assignment
37 /// \return The newly assigned object
38 SparseVector& operator=(const SparseVector& source);
39
40 /// Not a full iterator implementation -- just a pass
41 /// through to the storage container.
42 /// Return an iterator pointing to the beginning of
43 /// the encpsulated STL map storage
44 iter begin();
45
46 /// Not a full iterator implementation -- just a pass
47 /// through to the storage container.
48 /// Return a constant iterator pointing to the beginning of
49 /// the encapsulated STL map storage
50 citer begin() const;
51
52 /// Not a full iterator implementation -- just a pass
53 /// through to the storage container.
54 /// Return an iterator pointing to the end of the
55 /// encapsulated STL map storage
56 iter end();
57
58 /// Not a full iterator implementation -- just a pass
59 /// through to the storage container.
60 /// Return a constant iterator pointing to the end of
61 /// the encapsulated STL map storage
62 citer end() const;
63
64 /// Resize the maximum length of the sparse vector.
65 /// The maximum length is used in geometry checking.
66 /// \param length The new maximum length of the vector
67 void resize(const std::size_t& length);
68
69 /// Remove all elements from the sparse vector
70 void clear();
71
72 /// Erase an element from the vector
73 /// \param pos An std::map< std::size_t, _Type >::iterator to the map element
74 void erase(const iter& pos);
75
76 /// Erase an element from th vector
77 /// \param index The index of the element to be erased
78 void erase(const std::size_t& index);
79
80 /// Swap elements i and j.
81 /// \param i The index of the element to swap.
82 /// \param j The index of the other element to swap.
83 void swap(const std::size_t& i, const std::size_t& j);
84
85 /// Swap ALL elements with those of another SparseVector
86 /// \param X The sparse vector to exchange with
88
89 /// Find the (max) size of the vector.
90 std::size_t size() const;
91
92 /// Find the number of non-zero elements in the vector.
93 std::size_t nelts() const;
94
95 /// Set an element of the vector
96 /// \param i The index to be accessed
97 /// \return A reference to the element
98 _Type& set(const std::size_t& i);
99
100 /// Get an element of the vector
101 /// \param i The index to be accessed
102 /// \return The contents of the element, including
103 /// a zero if the element has not been set
104 const _Type& get(const std::size_t& i) const;
105
106 /// Equivalent to the 'get' method
107 const _Type& operator[](const std::size_t& i) const;
108
109 /// Equivalent to the 'set' method
110 _Type& operator[](const std::size_t& i);
111
112 /// Operator overloading for sparse vector addition
113 /// \param X A sparse vector to be added
114 /// \return The sum of this and the passed vector X
116
117 /// Overloading for +
118 /// \return +this
120
121 /// Operator overloading for sparse vector subtraction
122 /// \param X A sparse vector to be subtracted
123 /// \return The subtraction of this and the passed vector X
125
126 /// Overloading for -
127 /// \return The negative of the vector
129
130 /// Overloading multiplication for a scalar
131 /// \param m The scalar to multiply by
132 /// \result The scalar multiplication of the vector
133 SparseVector<_Type> operator*(const _Type& m) const;
134
135 /// Overloading *= for scalar multiplication
136 /// \param m The scalar to multiply by
137 SparseVector<_Type>& operator*=(const _Type& m);
138
139 /// Overloading -= for sparse vectors
140 /// \param X The sparse vector to be subtracted
141 /// \return Subtracts X from the 'this' vector
143
144 /// Overloading += for sparse vectors
145 /// \param X The sparse vector to be added
146 /// \return Adds X to the 'this' vector
148
149 /// Look for an element index
150 /// \param i The index of the element to search for
151 citer find(std::size_t i) const {
152 citer pos;
153 pos = MAP_VEC.find(i);
154 return pos;
155 }
156
157 /// A dot product.
158 /// \param x The vector to be "dotted" with.
159 const _Type dot(const SparseVector<_Type>& x) const;
160
161 /// l1-norm.
162 /// \return The square-root of the sum of the squares divided by number of elts.
163 double one_norm() const;
164
165 /// l2-norm.
166 /// No attention paid to possible overflow for large vectors.
167 /// \return The square-root of the sum of the squares divided by number of elts.
168 double two_norm() const;
169
170 /// Infinity norm.
171 /// \return The maximum (abs) element in the vector.
172 double inf_norm() const;
173
174 /// Scale each element of the vector.
175 /// \param scale The value to scale each element by.
176 void scale(const _Type& scale);
177
178 /// Add a vector, element wise.
179 /// \param X The vector to be added to this object.
180 void add(const SparseVector<_Type>& X);
181
182 /// Subtract a vector, element wise.
183 /// \param X The vector to be subtracted from this object.
184 void sub(const SparseVector<_Type>& X);
185
186 /// Find the index of the element NEAREST in value to that specified
187 /// \param value The value to search for.
188 /// \return The index of the element with value that minimises the difference.
189 std::size_t nearest_index(const _Type& value) const;
190
191 /// Find the index of the maximum element in the vector
192 /// \return The index of the maximum element.
193 std::size_t maxabs_index() const;
194
195 /// Find the index of the maximum element in the vector
196 /// \return The index of the maximum element.
197 std::size_t minabs_index() const;
198
199 /// Output the sparse vector's contents.
200 void dump() const;
201
202#if defined(PETSC_D) || defined(PETSC_Z)
203
204 void get_petsc( PetscScalar* storage, PetscInt* cols );
205
206 /*{
207 citer pos;
208 std::size_t i(0);
209 //
210 // start at the begining of this row
211 pos = MAP_VEC.begin();
212 do {
213 // store the value and column
214 storage[ i ] = pos -> second;
215 cols[ i ] = pos -> first;
216 // increment the iterator
217 ++pos;
218 // increment the array index
219 ++i;
220 } while(pos != MAP_VEC.end());
221 }*/
222
223
224#endif
225
226
227 private:
228 // private storage of the data - encapsulated in std::vector
229 std::map< std::size_t, _Type > MAP_VEC;
230 _Type ZERO;
231 std::size_t MAX_SIZE;
232
233 }
234 ; // end class
235
236
237 // INLINE METHODS BELOW
238
239 template <typename _Type>
240 inline typename std::map< std::size_t, _Type >::iterator SparseVector<_Type>::begin() {
241 return MAP_VEC.begin();
242 }
243
244 template <typename _Type>
245 inline typename std::map< std::size_t, _Type >::const_iterator SparseVector<_Type>::begin() const {
246 return MAP_VEC.begin();
247 }
248
249 template <typename _Type>
250 inline typename std::map< std::size_t, _Type >::iterator SparseVector<_Type>::end() {
251 return MAP_VEC.end();
252 }
253
254 template <typename _Type>
255 inline typename std::map< std::size_t, _Type >::const_iterator SparseVector<_Type>::end() const {
256 return MAP_VEC.end();
257 }
258
259 template <typename _Type>
260 inline _Type& SparseVector<_Type>::operator[](const std::size_t& i) {
261#ifdef PARANOID
262 if(i >= size()) {
263 std::string problem;
264 problem = " The SparseVector.operator[] method is trying to access \n";
265 problem += " outside the container. \n";
266 throw ExceptionRange(problem, size(), i);
267 }
268#endif
269 return MAP_VEC[ i ];
270 }
271
272 template <typename _Type>
273 inline const _Type& SparseVector<_Type>::operator[](const std::size_t& i) const {
274#ifdef PARANOID
275 if(i >= size()) {
276 std::string problem;
277 problem = " The SparseVector.operator[] method is trying to access \n";
278 problem += " outside the container. \n";
279 throw ExceptionRange(problem, size(), i);
280 }
281#endif
282 return get(i);
283 }
284
285 template <typename _Type>
286 inline _Type& SparseVector<_Type>::set(const std::size_t& i) {
287#ifdef PARANOID
288 if(i >= size()) {
289 std::string problem;
290 problem = " The SparseVector.set method is trying to access \n";
291 problem += " outside the container. \n";
292 throw ExceptionRange(problem, size(), i);
293 }
294#endif
295 return MAP_VEC[ i ];
296 }
297
298 template <typename _Type>
299 inline const _Type& SparseVector<_Type>::get(const std::size_t& i) const {
300#ifdef PARANOID
301 if(i >= size()) {
302 std::string problem;
303 problem = " The SparseVector.get method is trying to access \n";
304 problem += " outside the container. \n";
305 throw ExceptionRange(problem, size(), i);
306 }
307#endif
308 citer pos;
309 pos = MAP_VEC.find(i);
310 if(pos != MAP_VEC.end()) {
311 return pos -> second;
312 } else {
313 return ZERO;
314 }
315 }
316
317 template <typename _Type>
318 inline std::size_t SparseVector<_Type>::size() const {
319 return MAX_SIZE;
320 }
321
322 template <typename _Type>
323 inline std::size_t SparseVector<_Type>::nelts() const {
324 return MAP_VEC.size();
325 }
326
327 template <typename _Type>
328 inline void SparseVector<_Type>::erase(const std::size_t& index) {
329 MAP_VEC.erase(index);
330 }
331
332 template <typename _Type>
333 inline void SparseVector<_Type>::erase(const iter& pos) {
334 MAP_VEC.erase(pos);
335 }
336
337 template <typename _Type>
339#ifdef PARANOID
340 if(X.size() != size()) {
341 std::string problem;
342 problem = " The SparseVector.swap method is trying to use \n";
343 problem += " two vectors of unequal length \n";
344 throw ExceptionGeom(problem, size(), X.size());
345 }
346#endif
347 MAP_VEC.swap(X.MAP_VEC);
348 }
349
350 template <typename _Type>
352 return * this;
353 }
354
355
356} // end namespace
357
358#endif
359
The collection of CppNoddy exceptions.
An exception class to be thrown when a container of incorrect geometry used in any class/method.
Definition: Exceptions.h:47
An exception to indicate that a CppNoddy container has been accessed with index/indices outside the m...
Definition: Exceptions.h:117
An SparseVector class – a sparse vector object.
Definition: SparseVector.h:20
citer find(std::size_t i) const
Look for an element index.
Definition: SparseVector.h:151
void sub(const SparseVector< _Type > &X)
Subtract a vector, element wise.
iter end()
Not a full iterator implementation – just a pass through to the storage container.
Definition: SparseVector.h:250
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.
const _Type & operator[](const std::size_t &i) const
Equivalent to the 'get' method.
Definition: SparseVector.h:273
void dump() const
Output the sparse vector's contents.
void erase(const iter &pos)
Erase an element from the vector.
Definition: SparseVector.h:333
std::size_t nelts() const
Find the number of non-zero elements in the vector.
Definition: SparseVector.h:323
double two_norm() const
l2-norm.
_Type & set(const std::size_t &i)
Set an element of the vector.
Definition: SparseVector.h:286
iter begin()
Not a full iterator implementation – just a pass through to the storage container.
Definition: SparseVector.h:240
std::size_t size() const
Find the (max) size of the vector.
Definition: SparseVector.h:318
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 +.
Definition: SparseVector.h:351
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 -.
const _Type & get(const std::size_t &i) const
Get an element of the vector.
Definition: SparseVector.h:299
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...

© 2012

R.E. Hewitt