CppNoddy  0.92
Loading...
Searching...
No Matches
DenseVector.cpp
Go to the documentation of this file.
1/// \file src/DenseVector.cpp
2/// Implementation of the DenseVector class -- a dense, variable size, vector object.
3
4#include <vector>
5#include <complex>
6#include <algorithm>
7#include <functional>
8#include <numeric>
9
10#include <DenseVector.h>
11#include <Exceptions.h>
12#include <Functors.h>
13
14namespace CppNoddy {
15
16 template <typename _Type>
18 {}
19
20 template <typename _Type>
21 DenseVector<_Type>::DenseVector(const std::size_t& size, const _Type* p) {
22 m_vec.reserve(size);
23 // assign the array contents to the vector
24 m_vec.assign(p, p + size);
25 }
26
27 template <typename _Type>
28 DenseVector<_Type>::DenseVector(const std::size_t& size,
29 const _Type& fill) : m_vec(size, fill)
30 {}
31
32 template <typename _Type>
33 void DenseVector<_Type>::scale(const _Type& m) {
34 operator*=(m);
35 }
36
37 template <typename _Type>
39 (const DenseVector<_Type>& x) {
40 operator+=(x);
41 }
42
43 template <typename _Type>
45 operator-=(x);
46 }
47
48 template <typename _Type>
50 return accumulate(begin(), end(), 0.0, absAdd_functor<_Type>());
51 }
52
53 template <typename _Type>
55 return std::sqrt(accumulate(begin(), end(), 0.0, absSquareAdd_functor<_Type>()));
56 }
57
58 template <typename _Type>
60 return std::abs(*max_element(begin(), end(), abs_predicate<_Type>()));
61 }
62
63 template <typename _Type>
65 std::cout << "size = " << size() << "\n";
66 for(std::size_t i = 0; i < size(); ++i) {
67 std::cout << m_vec[ i ] << ", ";
68 }
69 std::cout << "\n";
70 }
71
72
73 template <>
74 void DenseVector<double>::dump_file(std::string filename, int precision) const {
75 std::ofstream dump;
76 dump.open(filename.c_str());
77 dump.precision(precision);
78 dump.setf(std::ios::showpoint);
79 dump.setf(std::ios::showpos);
80 dump.setf(std::ios::scientific);
81 for(std::size_t i = 0; i < m_vec.size(); ++i) {
82 dump << i << " " << m_vec[ i ] << "\n";
83 }
84 }
85
86 template <>
87 void DenseVector<std::complex<double> >::dump_file(std::string filename, int precision) const {
88 std::ofstream dump;
89 dump.open(filename.c_str());
90 dump.precision(precision);
91 dump.setf(std::ios::showpoint);
92 dump.setf(std::ios::showpos);
93 dump.setf(std::ios::scientific);
94 for(std::size_t i = 0; i < m_vec.size(); ++i) {
95 dump << i << " " << m_vec[ i ].real() << " " << m_vec[ i ].imag() << "\n";
96 }
97 }
98
99 template <typename _Type>
100 void DenseVector<_Type>::swap(const std::size_t& i,
101 const std::size_t& j) {
102#ifdef PARANOID
103 if((i >= size()) || (j >= size())) {
104 std::string problem;
105 problem = " The DenseVector.swap method is trying to access \n";
106 problem += " outside the max/min number of elements. \n";
107 if(i > size())
108 throw ExceptionRange(problem, size(), i);
109 if(j > size())
110 throw ExceptionRange(problem, size(), j);
111 }
112#endif
113 std::swap<_Type>(m_vec[ i ], m_vec[ j ]);
114 }
115
116 // the templated versions we require are:
117 template class DenseVector<double>
118 ;
119 template class DenseVector<std::complex<double> >
120 ;
121 template class DenseVector<int>
122 ;
123
124} // end namespace
Specification for a templated DenseVector class – a dense, dynamic, vector object.
The collection of CppNoddy exceptions.
Some Function Objects that CppNoddy makes use of in algorithms applied to STL containers.
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
double one_norm() const
l1-norm.
Definition: DenseVector.cpp:49
void dump_file(std::string filename, int precision=10) const
Dump the contents to a file, each element on a separate line.
double inf_norm() const
Infinity norm.
Definition: DenseVector.cpp:59
void sub(const DenseVector< _Type > &x)
Subtract a vector, element wise, equivalent to -=.
Definition: DenseVector.cpp:44
void dump() const
Dump to std::cout.
Definition: DenseVector.cpp:64
void add(const DenseVector< _Type > &x)
Add a vector, element wise, equivalent to +=.
Definition: DenseVector.cpp:39
void swap(const std::size_t &i, const std::size_t &j)
Swap elements i and j.
void scale(const _Type &scale)
Scale each element of the vector, equivalent to *=.
Definition: DenseVector.cpp:33
DenseVector()
Constructor for a non-filled vector, to be filled by the user.
Definition: DenseVector.cpp:17
double two_norm() const
l2-norm.
Definition: DenseVector.cpp:54
An exception to indicate that a CppNoddy container has been accessed with index/indices outside the m...
Definition: Exceptions.h:117
A function object used to accumulate the absolute value of a container.
Definition: Functors.h:53
A function object used to accumulate the square of the absolute value of a container.
Definition: Functors.h:63
A function object predicate that compares the absolute value of two elements and returns a true of el...
Definition: Functors.h:15
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...

© 2012

R.E. Hewitt