CppNoddy  0.90
Public Member Functions | List of all members
CppNoddy::SparseVector< _Type > Class Template Reference

An SparseVector class – a sparse vector object. More...

#include <SparseVector.h>

Public Member Functions

 SparseVector (const std::size_t &max)
 Constructor for a non-filled vector, to be filled by the user. More...
 
 SparseVector (const SparseVector &source)
 Copy constructor. More...
 
SparseVectoroperator= (const SparseVector &source)
 Assignment operator. More...
 
iter begin ()
 Not a full iterator implementation – just a pass through to the storage container. More...
 
citer begin () const
 Not a full iterator implementation – just a pass through to the storage container. More...
 
iter end ()
 Not a full iterator implementation – just a pass through to the storage container. More...
 
citer end () const
 Not a full iterator implementation – just a pass through to the storage container. More...
 
void resize (const std::size_t &length)
 Resize the maximum length of the sparse vector. More...
 
void clear ()
 Remove all elements from the sparse vector. More...
 
void erase (const iter &pos)
 Erase an element from the vector. More...
 
void erase (const std::size_t &index)
 Erase an element from th vector. More...
 
void swap (const std::size_t &i, const std::size_t &j)
 Swap elements i and j. More...
 
void swap (SparseVector< _Type > &X)
 Swap ALL elements with those of another SparseVector. More...
 
std::size_t size () const
 Find the (max) size of the vector. More...
 
std::size_t nelts () const
 Find the number of non-zero elements in the vector. More...
 
_Type & set (const std::size_t &i)
 Set an element of the vector. More...
 
const _Type & get (const std::size_t &i) const
 Get an element of the vector. More...
 
const _Type & operator[] (const std::size_t &i) const
 Equivalent to the 'get' method. More...
 
_Type & operator[] (const std::size_t &i)
 Equivalent to the 'set' method. More...
 
SparseVector< _Type > operator+ (const SparseVector< _Type > &X) const
 Operator overloading for sparse vector addition. More...
 
SparseVector< _Type > operator+ () const
 Overloading for +. More...
 
SparseVector< _Type > operator- (const SparseVector< _Type > &X) const
 Operator overloading for sparse vector subtraction. More...
 
SparseVector< _Type > operator- () const
 Overloading for -. More...
 
SparseVector< _Type > operator* (const _Type &m) const
 Overloading multiplication for a scalar. More...
 
SparseVector< _Type > & operator*= (const _Type &m)
 Overloading *= for scalar multiplication. More...
 
SparseVector< _Type > & operator-= (const SparseVector< _Type > &X)
 Overloading -= for sparse vectors. More...
 
SparseVector< _Type > & operator+= (const SparseVector< _Type > &X)
 Overloading += for sparse vectors. More...
 
citer find (std::size_t i) const
 Look for an element index. More...
 
const _Type dot (const SparseVector< _Type > &x) const
 A dot product. More...
 
double one_norm () const
 l1-norm. More...
 
double two_norm () const
 l2-norm. More...
 
double inf_norm () const
 Infinity norm. More...
 
void scale (const _Type &scale)
 Scale each element of the vector. More...
 
void add (const SparseVector< _Type > &X)
 Add a vector, element wise. More...
 
void sub (const SparseVector< _Type > &X)
 Subtract a vector, element wise. More...
 
std::size_t nearest_index (const _Type &value) const
 Find the index of the element NEAREST in value to that specified. More...
 
std::size_t maxabs_index () const
 Find the index of the maximum element in the vector. More...
 
std::size_t minabs_index () const
 Find the index of the maximum element in the vector. More...
 
void dump () const
 Output the sparse vector's contents. More...
 

Detailed Description

template<typename _Type>
class CppNoddy::SparseVector< _Type >

An SparseVector class – a sparse vector object.

This is templated but intended ONLY for double or std::complex<double>.

Definition at line 17 of file SparseVector.h.

Constructor & Destructor Documentation

template<typename _Type >
CppNoddy::SparseVector< _Type >::SparseVector ( const std::size_t &  max)
explicit

Constructor for a non-filled vector, to be filled by the user.

Parameters
maxA maximum size for the sparse vector, used in geometry checking.

Definition at line 19 of file SparseVector.cpp.

19  : ZERO( 0.0 ), MAX_SIZE( max )
20  {}
template<typename _Type >
CppNoddy::SparseVector< _Type >::SparseVector ( const SparseVector< _Type > &  source)

Copy constructor.

Parameters
sourceThe source object to be copied

Definition at line 23 of file SparseVector.cpp.

References CppNoddy::Example::source().

24  {
25  *this = source;
26  }
double source(const double &x, const double &y, const double &t)
Definition: IBVP_linear.cpp:25

Member Function Documentation

template<typename _Type >
void CppNoddy::SparseVector< _Type >::add ( const SparseVector< _Type > &  X)

Add a vector, element wise.

Parameters
XThe vector to be added to this object.

Definition at line 295 of file SparseVector.cpp.

Referenced by CppNoddy::SparseVector< _Type >::find(), and CppNoddy::SparseVector< _Type >::scale().

296  {
297  *this += X;
298  }
template<typename _Type >
std::map< std::size_t, _Type >::iterator CppNoddy::SparseVector< _Type >::begin ( )
inline

Not a full iterator implementation – just a pass through to the storage container.

Return an iterator pointing to the beginning of the encpsulated STL map storage

Definition at line 214 of file SparseVector.h.

215  {
216  return MAP_VEC.begin();
217  }
template<typename _Type >
std::map< std::size_t, _Type >::const_iterator CppNoddy::SparseVector< _Type >::begin ( ) const
inline

Not a full iterator implementation – just a pass through to the storage container.

Return a constant iterator pointing to the beginning of the encapsulated STL map storage

Definition at line 220 of file SparseVector.h.

221  {
222  return MAP_VEC.begin();
223  }
template<typename _Type >
void CppNoddy::SparseVector< _Type >::clear ( )

Remove all elements from the sparse vector.

Definition at line 213 of file SparseVector.cpp.

214  {
215  MAP_VEC.clear();
216  }
template<typename _Type >
const _Type CppNoddy::SparseVector< _Type >::dot ( const SparseVector< _Type > &  x) const

A dot product.

Parameters
xThe vector to be "dotted" with.
Todo:
Using a 'get' (aka find) here is slooow
  • obviously this can be improved.

Definition at line 219 of file SparseVector.cpp.

References CppNoddy::SparseVector< _Type >::size().

Referenced by CppNoddy::SparseVector< _Type >::find().

220  {
221 #ifdef PARANOID
222  if ( X.size() != size() )
223  {
224  std::string problem;
225  problem = " The SparseVector.dot method is trying to use \n";
226  problem += " two vectors of unequal length \n";
227  throw ExceptionGeom( problem, size(), X.size() );
228  }
229 #endif
230  SparseVector<_Type> temp( X );
231  _Type sum( 0.0 );
232  for ( iter pos = temp.MAP_VEC.begin(); pos != temp.MAP_VEC.end(); ++pos )
233  {
234  std::size_t index = pos -> first;
235  /// \todo Using a 'get' (aka find) here is slooow
236  /// - obviously this can be improved.
237  // the get method returns 0.0 if not stored
238  temp[ index ] *= get( index );
239  }
240  return sum;
241  }
std::size_t size() const
Find the (max) size of the vector.
Definition: SparseVector.h:307
template<typename _Type >
void CppNoddy::SparseVector< _Type >::dump ( ) const

Output the sparse vector's contents.

Definition at line 375 of file SparseVector.cpp.

Referenced by CppNoddy::SparseVector< _Type >::find().

376  {
377  if ( MAP_VEC.begin() == MAP_VEC.end() )
378  {
379  std::cout << "[ Empty vector ]\n";
380  }
381  else
382  {
383  for ( citer pos = MAP_VEC.begin(); pos != MAP_VEC.end(); ++pos )
384  {
385  std::cout << "[ " << pos -> first << " ]" << " = " << pos -> second << " ";
386  }
387  std::cout << "\n";
388  }
389  }
template<typename _Type >
std::map< std::size_t, _Type >::iterator CppNoddy::SparseVector< _Type >::end ( )
inline

Not a full iterator implementation – just a pass through to the storage container.

Return an iterator pointing to the end of the encapsulated STL map storage

Definition at line 226 of file SparseVector.h.

227  {
228  return MAP_VEC.end();
229  }
template<typename _Type >
std::map< std::size_t, _Type >::const_iterator CppNoddy::SparseVector< _Type >::end ( ) const
inline

Not a full iterator implementation – just a pass through to the storage container.

Return a constant iterator pointing to the end of the encapsulated STL map storage

Definition at line 232 of file SparseVector.h.

233  {
234  return MAP_VEC.end();
235  }
template<typename _Type >
void CppNoddy::SparseVector< _Type >::erase ( const iter &  pos)
inline

Erase an element from the vector.

Parameters
posAn std::map< std::size_t, _Type >::iterator to the map element

Definition at line 325 of file SparseVector.h.

326  {
327  MAP_VEC.erase( pos );
328  }
template<typename _Type >
void CppNoddy::SparseVector< _Type >::erase ( const std::size_t &  index)
inline

Erase an element from th vector.

Parameters
indexThe index of the element to be erased

Definition at line 319 of file SparseVector.h.

320  {
321  MAP_VEC.erase( index );
322  }
template<typename _Type>
citer CppNoddy::SparseVector< _Type >::find ( std::size_t  i) const
inline
template<typename _Type >
const _Type & CppNoddy::SparseVector< _Type >::get ( const std::size_t &  i) const
inline

Get an element of the vector.

Parameters
iThe index to be accessed
Returns
The contents of the element, including a zero if the element has not been set

Definition at line 283 of file SparseVector.h.

References CppNoddy::SparseVector< _Type >::size().

284  {
285 #ifdef PARANOID
286  if ( i >= size() )
287  {
288  std::string problem;
289  problem = " The SparseVector.get method is trying to access \n";
290  problem += " outside the container. \n";
291  throw ExceptionRange( problem, size(), i );
292  }
293 #endif
294  citer pos;
295  pos = MAP_VEC.find( i );
296  if ( pos != MAP_VEC.end() )
297  {
298  return pos -> second;
299  }
300  else
301  {
302  return ZERO;
303  }
304  }
std::size_t size() const
Find the (max) size of the vector.
Definition: SparseVector.h:307
template<typename _Type >
double CppNoddy::SparseVector< _Type >::inf_norm ( ) const

Infinity norm.

Returns
The maximum (abs) element in the vector.

Definition at line 270 of file SparseVector.cpp.

Referenced by CppNoddy::SparseVector< _Type >::find().

271  {
272  double max( 0.0 );
273  // Return the maximum (abs) element in the vector
274  for ( citer pos = MAP_VEC.begin(); pos != MAP_VEC.end(); ++pos )
275  {
276  if ( std::abs( pos -> second ) > max )
277  {
278  max = std::abs( pos -> second );
279  }
280  }
281  return max;
282  }
template<typename _Type >
std::size_t CppNoddy::SparseVector< _Type >::maxabs_index ( ) const

Find the index of the maximum element in the vector.

Returns
The index of the maximum element.

Definition at line 325 of file SparseVector.cpp.

Referenced by CppNoddy::SparseVector< _Type >::find().

326  {
327  citer location( MAP_VEC.begin() );
328  double max( std::abs( location -> second ) );
329  citer start( ++location );
330  for ( citer pos = start; pos != MAP_VEC.end(); ++pos )
331  {
332  if ( std::abs( pos -> second ) > max )
333  {
334  max = std::abs( pos -> second );
335  location = pos;
336  }
337  }
338  return location -> first;
339  }
template<typename _Type >
std::size_t CppNoddy::SparseVector< _Type >::minabs_index ( ) const

Find the index of the maximum element in the vector.

Returns
The index of the maximum element.

Definition at line 342 of file SparseVector.cpp.

Referenced by CppNoddy::SparseVector< _Type >::find().

343  {
344  citer location( MAP_VEC.begin() );
345  double min( std::abs( location -> second ) );
346  citer start( ++location );
347  for ( citer pos = start; pos != MAP_VEC.end(); ++pos )
348  {
349  if ( std::abs( pos -> second ) < min )
350  {
351  min = std::abs( pos -> second );
352  location = pos;
353  }
354  }
355  return location -> first;
356  }
template<typename _Type >
std::size_t CppNoddy::SparseVector< _Type >::nearest_index ( const _Type &  value) const

Find the index of the element NEAREST in value to that specified.

Parameters
valueThe value to search for.
Returns
The index of the element with value that minimises the difference.

Definition at line 307 of file SparseVector.cpp.

Referenced by CppNoddy::SparseVector< _Type >::find().

308  {
309  citer location( MAP_VEC.begin() );
310  _Type min_diff( location -> second - value );
311  citer start( ++location );
312  for ( citer pos = start; pos != MAP_VEC.end(); ++pos )
313  {
314  _Type diff( pos -> second - value );
315  if ( std::abs( diff ) < std::abs( min_diff ) )
316  {
317  min_diff = diff;
318  location = pos;
319  }
320  }
321  return location -> first;
322  }
template<typename _Type >
std::size_t CppNoddy::SparseVector< _Type >::nelts ( ) const
inline

Find the number of non-zero elements in the vector.

Definition at line 313 of file SparseVector.h.

Referenced by CppNoddy::Utility::fill_random().

314  {
315  return MAP_VEC.size();
316  }
template<typename _Type >
double CppNoddy::SparseVector< _Type >::one_norm ( ) const

l1-norm.

Returns
The square-root of the sum of the squares divided by number of elts.

Definition at line 244 of file SparseVector.cpp.

Referenced by CppNoddy::SparseVector< _Type >::find(), and main().

245  {
246  // Accumulate the ABS values of the container entries
247  // using a fuction object.
248  double sum( 0.0 );
249  for ( citer pos = MAP_VEC.begin(); pos != MAP_VEC.end(); ++pos )
250  {
251  sum += std::abs( pos -> second );
252  }
253  return sum;
254  }
template<typename _Type >
SparseVector< _Type > CppNoddy::SparseVector< _Type >::operator* ( const _Type &  m) const

Overloading multiplication for a scalar.

Parameters
mThe scalar to multiply by
Returns
The scalar multiplication of the vector

Definition at line 198 of file SparseVector.cpp.

References m.

199  {
200  SparseVector<_Type> temp( *this );
201  temp *= m;
202  return temp;
203  }
template<typename _Type >
SparseVector< _Type > & CppNoddy::SparseVector< _Type >::operator*= ( const _Type &  m)

Overloading *= for scalar multiplication.

Parameters
mThe scalar to multiply by

Definition at line 40 of file SparseVector.cpp.

References m.

41  {
42  for ( iter pos = MAP_VEC.begin(); pos != MAP_VEC.end(); ++pos )
43  {
44  pos -> second *= m;
45  }
46  return *this;
47  }
template<typename _Type >
SparseVector< _Type > CppNoddy::SparseVector< _Type >::operator+ ( const SparseVector< _Type > &  X) const

Operator overloading for sparse vector addition.

Parameters
XA sparse vector to be added
Returns
The sum of this and the passed vector X

Definition at line 156 of file SparseVector.cpp.

References CppNoddy::SparseVector< _Type >::size().

157  {
158 #ifdef PARANOID
159  if ( X.size() != size() )
160  {
161  std::string problem;
162  problem = " The SparseVector.operator+ method is trying to use \n";
163  problem += " two vectors of unequal length \n";
164  throw ExceptionGeom( problem, size(), X.size() );
165  }
166 #endif
167  SparseVector<_Type> temp( *this );
168  temp += X;
169  return temp;
170  }
std::size_t size() const
Find the (max) size of the vector.
Definition: SparseVector.h:307
template<typename _Type >
SparseVector< _Type > CppNoddy::SparseVector< _Type >::operator+ ( ) const
inline

Overloading for +.

Returns
+this

Definition at line 346 of file SparseVector.h.

347  {
348  return * this;
349  }
template<typename _Type >
SparseVector< _Type > & CppNoddy::SparseVector< _Type >::operator+= ( const SparseVector< _Type > &  X)

Overloading += for sparse vectors.

Parameters
XThe sparse vector to be added
Returns
Adds X to the 'this' vector

Definition at line 103 of file SparseVector.cpp.

References CppNoddy::SparseVector< _Type >::size().

104  {
105 #ifdef PARANOID
106  if ( X.size() != size() )
107  {
108  std::string problem;
109  problem = " The SparseVector.operator+= method is trying to use \n";
110  problem += " two vectors of unequal length \n";
111  throw ExceptionGeom( problem, size(), X.size() );
112  }
113 #endif
114  citer pos_ro = X.MAP_VEC.begin();
115  iter pos_rw = MAP_VEC.begin();
116  do
117  {
118  std::size_t index_rw = pos_rw -> first;
119  std::size_t index_ro = pos_ro -> first;
120  if ( index_rw == index_ro )
121  {
122  // element in both vectors
123  pos_rw -> second += pos_ro -> second;
124  ++pos_rw;
125  ++pos_ro;
126  }
127  if ( index_rw > index_ro )
128  {
129  // element is in X but not 'this'
130  set
131  ( index_ro ) = pos_ro -> second;
132  ++pos_ro;
133  }
134  if ( index_rw < index_ro )
135  {
136  // element is in 'this' but not X
137  ++pos_rw;
138  }
139  }
140  while ( pos_ro != X.MAP_VEC.end() && pos_rw != MAP_VEC.end() );
141  if ( pos_ro != X.MAP_VEC.end() )
142  {
143  // need to finish the X data
144  do
145  {
146  set
147  ( pos_ro -> first ) = pos_ro -> second;
148  ++pos_ro;
149  }
150  while ( pos_ro != X.MAP_VEC.end() );
151  }
152  return *this;
153  }
std::size_t size() const
Find the (max) size of the vector.
Definition: SparseVector.h:307
template<typename _Type >
SparseVector< _Type > CppNoddy::SparseVector< _Type >::operator- ( const SparseVector< _Type > &  X) const

Operator overloading for sparse vector subtraction.

Parameters
XA sparse vector to be subtracted
Returns
The subtraction of this and the passed vector X

Definition at line 173 of file SparseVector.cpp.

References CppNoddy::SparseVector< _Type >::size().

174  {
175 #ifdef PARANOID
176  if ( X.size() != size() )
177  {
178  std::string problem;
179  problem = " The SparseVector.operator- method is trying to use \n";
180  problem += " two vectors of unequal length \n";
181  throw ExceptionGeom( problem, size(), X.size() );
182  }
183 #endif
184  SparseVector<_Type> temp( *this );
185  temp -= X;
186  return temp;
187  }
std::size_t size() const
Find the (max) size of the vector.
Definition: SparseVector.h:307
template<typename _Type >
SparseVector< _Type > CppNoddy::SparseVector< _Type >::operator- ( ) const

Overloading for -.

Returns
The negative of the vector

Definition at line 190 of file SparseVector.cpp.

191  {
192  SparseVector<_Type> temp( *this );
193  temp *= -1.0;
194  return temp;
195  }
template<typename _Type >
SparseVector< _Type > & CppNoddy::SparseVector< _Type >::operator-= ( const SparseVector< _Type > &  X)

Overloading -= for sparse vectors.

Parameters
XThe sparse vector to be subtracted
Returns
Subtracts X from the 'this' vector

Definition at line 50 of file SparseVector.cpp.

References CppNoddy::SparseVector< _Type >::size().

51  {
52 #ifdef PARANOID
53  if ( X.size() != size() )
54  {
55  std::string problem;
56  problem = " The SparseVector.operator-= method is trying to use \n";
57  problem += " two vectors of unequal length \n";
58  throw ExceptionGeom( problem, size(), X.size() );
59  }
60 #endif
61  citer pos_ro = X.MAP_VEC.begin();
62  iter pos_rw = MAP_VEC.begin();
63  do
64  {
65  std::size_t index_rw = pos_rw -> first;
66  std::size_t index_ro = pos_ro -> first;
67  if ( index_rw == index_ro )
68  {
69  // element in both vectors
70  pos_rw -> second -= pos_ro -> second;
71  ++pos_rw;
72  ++pos_ro;
73  }
74  if ( index_rw > index_ro )
75  {
76  // element is in X but not 'this'
77  set
78  ( index_ro ) = -( pos_ro -> second );
79  ++pos_ro;
80  }
81  if ( index_rw < index_ro )
82  {
83  // element is in 'this' but not X
84  ++pos_rw;
85  }
86  }
87  while ( pos_ro != X.MAP_VEC.end() && pos_rw != MAP_VEC.end() );
88  if ( pos_ro != X.MAP_VEC.end() )
89  {
90  // need to finish the X data
91  do
92  {
93  set
94  ( pos_ro -> first ) = -( pos_ro -> second );
95  ++pos_ro;
96  }
97  while ( pos_ro != X.MAP_VEC.end() );
98  }
99  return *this;
100  }
std::size_t size() const
Find the (max) size of the vector.
Definition: SparseVector.h:307
template<typename _Type >
SparseVector< _Type > & CppNoddy::SparseVector< _Type >::operator= ( const SparseVector< _Type > &  source)

Assignment operator.

Parameters
sourceThe source object for the assignment
Returns
The newly assigned object

Definition at line 29 of file SparseVector.cpp.

30  {
31  if ( this == &source )
32  return * this;
33  MAP_VEC = source.MAP_VEC;
34  ZERO = source.ZERO;
35  MAX_SIZE = source.MAX_SIZE;
36  return *this;
37  }
double source(const double &x, const double &y, const double &t)
Definition: IBVP_linear.cpp:25
template<typename _Type >
const _Type & CppNoddy::SparseVector< _Type >::operator[] ( const std::size_t &  i) const
inline

Equivalent to the 'get' method.

Definition at line 253 of file SparseVector.h.

References CppNoddy::SparseVector< _Type >::size().

254  {
255 #ifdef PARANOID
256  if ( i >= size() )
257  {
258  std::string problem;
259  problem = " The SparseVector.operator[] method is trying to access \n";
260  problem += " outside the container. \n";
261  throw ExceptionRange( problem, size(), i );
262  }
263 #endif
264  return get( i );
265  }
std::size_t size() const
Find the (max) size of the vector.
Definition: SparseVector.h:307
template<typename _Type >
_Type & CppNoddy::SparseVector< _Type >::operator[] ( const std::size_t &  i)
inline

Equivalent to the 'set' method.

Definition at line 238 of file SparseVector.h.

References CppNoddy::SparseVector< _Type >::size().

239  {
240 #ifdef PARANOID
241  if ( i >= size() )
242  {
243  std::string problem;
244  problem = " The SparseVector.operator[] method is trying to access \n";
245  problem += " outside the container. \n";
246  throw ExceptionRange( problem, size(), i );
247  }
248 #endif
249  return MAP_VEC[ i ];
250  }
std::size_t size() const
Find the (max) size of the vector.
Definition: SparseVector.h:307
template<typename _Type >
void CppNoddy::SparseVector< _Type >::resize ( const std::size_t &  length)

Resize the maximum length of the sparse vector.

The maximum length is used in geometry checking.

Parameters
lengthThe new maximum length of the vector

Definition at line 207 of file SparseVector.cpp.

208  {
209  MAX_SIZE = length;
210  }
template<typename _Type >
void CppNoddy::SparseVector< _Type >::scale ( const _Type &  scale)

Scale each element of the vector.

Parameters
scaleThe value to scale each element by.

Definition at line 285 of file SparseVector.cpp.

References CppNoddy::SparseVector< _Type >::add().

Referenced by CppNoddy::SparseVector< _Type >::find().

286  {
287  for ( iter pos = MAP_VEC.begin(); pos != MAP_VEC.end(); ++pos )
288  {
289  pos -> second *= scale;
290  }
291  }
void scale(const _Type &scale)
Scale each element of the vector.
template<typename _Type >
_Type & CppNoddy::SparseVector< _Type >::set ( const std::size_t &  i)
inline

Set an element of the vector.

Parameters
iThe index to be accessed
Returns
A reference to the element

Definition at line 268 of file SparseVector.h.

References CppNoddy::SparseVector< _Type >::size().

269  {
270 #ifdef PARANOID
271  if ( i >= size() )
272  {
273  std::string problem;
274  problem = " The SparseVector.set method is trying to access \n";
275  problem += " outside the container. \n";
276  throw ExceptionRange( problem, size(), i );
277  }
278 #endif
279  return MAP_VEC[ i ];
280  }
std::size_t size() const
Find the (max) size of the vector.
Definition: SparseVector.h:307
template<typename _Type >
std::size_t CppNoddy::SparseVector< _Type >::size ( ) const
inline
template<typename _Type >
void CppNoddy::SparseVector< _Type >::sub ( const SparseVector< _Type > &  X)

Subtract a vector, element wise.

Parameters
XThe vector to be subtracted from this object.

Definition at line 301 of file SparseVector.cpp.

Referenced by CppNoddy::SparseVector< _Type >::find().

302  {
303  *this -= X;
304  }
template<typename _Type >
void CppNoddy::SparseVector< _Type >::swap ( const std::size_t &  i,
const std::size_t &  j 
)

Swap elements i and j.

Parameters
iThe index of the element to swap.
jThe index of the other element to swap.

Definition at line 359 of file SparseVector.cpp.

References CppNoddy::SparseVector< _Type >::size().

361  {
362 #ifdef PARANOID
363  if ( i >= size() || j >= size() )
364  {
365  std::string problem;
366  problem = " The SparseVector.swap method is trying to access \n";
367  problem += " outside the container. \n";
368  throw ExceptionRange( problem, size(), std::max( i, j ) );
369  }
370 #endif
371  std::swap<_Type>( MAP_VEC[ i ], MAP_VEC[ j ] );
372  }
std::size_t size() const
Find the (max) size of the vector.
Definition: SparseVector.h:307
template<typename _Type >
void CppNoddy::SparseVector< _Type >::swap ( SparseVector< _Type > &  X)
inline

Swap ALL elements with those of another SparseVector.

Parameters
XThe sparse vector to exchange with

Definition at line 331 of file SparseVector.h.

References CppNoddy::SparseVector< _Type >::size().

332  {
333 #ifdef PARANOID
334  if ( X.size() != size() )
335  {
336  std::string problem;
337  problem = " The SparseVector.swap method is trying to use \n";
338  problem += " two vectors of unequal length \n";
339  throw ExceptionGeom( problem, size(), X.size() );
340  }
341 #endif
342  MAP_VEC.swap( X.MAP_VEC );
343  }
std::size_t size() const
Find the (max) size of the vector.
Definition: SparseVector.h:307
template<typename _Type >
double CppNoddy::SparseVector< _Type >::two_norm ( ) const

l2-norm.

No attention paid to possible overflow for large vectors.

Returns
The square-root of the sum of the squares divided by number of elts.

Definition at line 257 of file SparseVector.cpp.

Referenced by CppNoddy::SparseVector< _Type >::find().

258  {
259  // Accumulate the ABS values of the container entries SQUARED
260  // using a fuction object.
261  double sum( 0.0 );
262  for ( citer pos = MAP_VEC.begin(); pos != MAP_VEC.end(); ++pos )
263  {
264  sum += std::pow( std::abs( pos -> second ), 2 );
265  }
266  return sum;
267  }

The documentation for this class was generated from the following files:

© 2012

R.E. Hewitt