SynAPSE  0.6.0
Synergistic Analysis of Phase Space Evolution
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
Vector-inl.hh
1 template<typename T> Vector<T>::Vector() :
2  _vec(0) {
3 }
4 
5 template<typename T> Vector<T>::Vector(const size_t& n) :
6  _vec(std::vector<T>(n)) {
7 }
8 
9 template<typename T> Vector<T>::Vector(const std::vector<T>& vec) :
10  _vec(vec) {
11 }
12 
13 template<typename T> Vector<T>::Vector(const T* vec, const size_t& n) :
14  _vec() {
15 
16  size_t i;
17  for (i = 0; i < n; i++)
18  _vec.push_back(vec[i]);
19 }
20 
21 template<typename T> Vector<T>::Vector(const size_t& n, const T& value) :
22  _vec(std::vector<T>(n)) {
23 
24  std::fill(_vec.begin(), _vec.end(), value);
25 }
26 
27 template<typename T> Vector<T>::Vector(const Vector& vec) {
28  *this = vec;
29 }
30 
31 template<typename T> Vector<T>::~Vector() {}
32 
33 template<typename T> Vector<T>& Vector<T>::operator=(const Vector<T>& vec) {
34  if ( this == &vec )
35  return *this;
36 
37  _vec = vec._vec;
38  return *this;
39 }
40 
41 template<typename T> Vector<T> Vector<T>::operator-() const {
42  // Change the sign of every element
43  Vector<T> opp(size());
44  size_t i;
45  for (i = 0; i < size(); i++)
46  opp[i] = -_vec[i];
47 
48  return opp;
49 }
50 
51 template<typename T> Vector<T> Vector<T>::operator+(const Vector<T>& vec) const {
52 
53  // Check that the size of the vector to be added is compatible
54  Assert::SameSize("Vector<T>::operator+", "The two vectors", _vec, vec.std());
55 
56  // Add the elements one to one
57  Vector<T> sum(_vec.size());
58  size_t i;
59  for (i = 0; i < _vec.size(); i++)
60  sum[i] = _vec[i]+vec[i];
61 
62  return sum;
63 }
64 
65 template<typename T> void Vector<T>::operator+=(const Vector<T>& vec) {
66 
67  // Check that the size of the vector to be added is compatible
68  Assert::SameSize("Vector<T>::operator+=", "The two vectors", _vec, vec.std());
69 
70  // Add the elements one to one
71  size_t i;
72  for (i = 0; i < _vec.size(); i++)
73  _vec[i] += vec[i];
74 }
75 
76 template<typename T> Vector<T> Vector<T>::operator-(const Vector<T>& vec) const {
77 
78  // Check that the size of the vector to be substracted is compatible
79  Assert::SameSize("Vector<T>::operator-", "The two vectors", _vec, vec.std());
80 
81  // Add the elements one to one
82  Vector<T> diff(_vec.size());
83  size_t i;
84  for (i = 0; i < _vec.size(); i++)
85  diff[i] = _vec[i]-vec[i];
86 
87  return diff;
88 }
89 
90 template<typename T> void Vector<T>::operator-=(const Vector<T>& vec) {
91 
92  // Check that the size of the vector to be substracted is compatible
93  Assert::SameSize("Vector<T>::operator-=", "The two vectors", _vec, vec.std());
94 
95  // Add the elements one to one
96  size_t i;
97  for (i = 0; i < _vec.size(); i++)
98  _vec[i] -= vec[i];
99 }
100 
101 template<typename T> T Vector<T>::operator*(const Vector<T>& vec) const {
102 
103  // Check that the size of the vector to be multipled is compatible
104  Assert::SameSize("Vector<T>::operator*", "The two vectors", _vec, vec.std());
105 
106  // Multiply the two vectors
107  T prod(0);
108  size_t i;
109  for (i = 0; i < _vec.size(); i++)
110  prod += _vec[i]*vec[i];
111 
112  return prod;
113 }
114 
115 template<typename T> Vector<T> Vector<T>::operator*(const T& factor) const {
116 
117  // Multiply each of the vector element by the factor
118  Vector<T> prod(_vec.size());
119  size_t i;
120  for (i = 0; i < _vec.size(); i++)
121  prod[i] = factor*_vec[i];
122 
123  return prod;
124 }
125 
126 template<typename T> void Vector<T>::operator*=(const T& factor) {
127 
128  // Multiply each of the vector element by the factor
129  size_t i;
130  for (i = 0; i < _vec.size(); i++)
131  _vec[i] *= factor;
132 }
133 
134 template<typename T> Vector<T> Vector<T>::operator/(const T& factor) const {
135 
136  // Check that the divisor is non-zero
137  Assert::IsNonZero("Vector<T>::operator/", "Divisor", factor);
138 
139  // Divide each of the vector element by the factor
140  Vector<T> div(_vec.size());
141  size_t i;
142  for (i = 0; i < _vec.size(); i++)
143  div[i] = _vec[i]/factor;
144 
145  return div;
146 }
147 
148 template<typename T> void Vector<T>::operator/=(const T& factor) {
149 
150  // Check that the divisor is non-zero
151  Assert::IsNonZero("Vector<T>::operator/=", "Divisor", factor);
152 
153  // Multiply each of the vector element by the factor
154  size_t i;
155  for (i = 0; i < _vec.size(); i++)
156  _vec[i] /= factor;
157 }
158 
159 template<typename T> Vector<T> operator*(const T& factor, const Vector<T> vec) {
160 
161  // Multiply the two vecrices
162  Vector<T> prod(vec.size());
163  size_t i;
164  for (i = 0; i < vec.size(); i++)
165  prod[i] = factor*vec[i];
166 
167  return prod;
168 }
169 
170 template<typename T> const T& Vector<T>::operator[](const size_t i) const {
171 
172  Assert::IsWithinBounds("Vector<T>::operator[]", size(), i);
173  return _vec[i];
174 }
175 
176 template<typename T> T& Vector<T>::operator[](const size_t i) {
177 
178  Assert::IsWithinBounds("Vector<T>::operator[]", size(), i);
179  return _vec[i];
180 }
181 
182 template<typename T> std::ostream& operator<<(std::ostream& os, const Vector<T>& vec) {
183 
184  size_t i;
185  os << "[";
186  for (i = 0; i < vec.size()-1; i++)
187  os << vec[i] << ", ";
188  os << vec.back() << "]";
189  return os;
190 }
191 
192 template<typename T> T Vector<T>::mag() const {
193 
194  return sqrt(mag2());
195 }
196 
197 template<typename T> T Vector<T>::mag2() const {
198 
199  T mag2(0);
200  size_t i;
201  for (i = 0; i < _vec.size(); i++)
202  mag2 += _vec[i]*_vec[i];
203 
204  return mag2;
205 }
206 
207 template<typename T> T Vector<T>::dot(const Vector& vec) const {
208 
209  // Check that the size of the vector to be multipled is compatible
210  Assert::SameSize("Vector<T>::dot", "The two vectors", _vec, vec.std());
211 
212  // Multiply the two vectors
213  T prod(0);
214  size_t i;
215  for (i = 0; i < _vec.size(); i++)
216  prod += _vec[i]*vec[i];
217 
218  return prod;
219 }
220 
221 template<typename T> Vector<T> Vector<T>::cross(const Vector& vec) const {
222 
223  // Check that the size of the vector to be multipled is compatible
224  if ( vec.size() != 3 || vec.size() != _vec.size() )
225  throw(Exceptions::Exception(Exceptions::nonRecoverable,
226  "The vectors are not of dimension 3",
227  "Vector<T>::cross"));
228 
229  // Multiply the two vectors
230  Vector<T> cross(3);
231  cross[0] = _vec[1]*vec[2]-_vec[2]*vec[1];
232  cross[1] = _vec[2]*vec[0]-_vec[0]*vec[2];
233  cross[2] = _vec[0]*vec[1]-_vec[1]*vec[0];
234 
235  return cross;
236 }
237 
238 template<typename T> const T& Vector<T>::back() const {
239  return _vec.back();
240 }
241 
242 template<typename T> const T& Vector<T>::front() const {
243  return _vec.front();
244 }
245 
246 template<typename T> T Vector<T>::norm() const {
247  return this->mag();
248 }
249 
250 template<typename T> T Vector<T>::norm2() const {
251  return this->mag2();
252 }
253 
254 template<typename T> void Vector<T>::resize(const size_t n) {
255  _vec.resize(n);
256 }
257 
258 template<typename T> void Vector<T>::push_back(const T& element) {
259  _vec.push_back(element);
260 }
261 
262 template<typename T> void Vector<T>::push_front(const T& element) {
263  _vec.push_front(element);
264 }
265 
266 template<typename T> const size_t Vector<T>::size() const {
267  return _vec.size();
268 }
269 
270 template<typename T> const std::vector<T>& Vector<T>::std() const {
271  return _vec;
272 }
T mag2() const
Returns the square of the magnitude of the vector.
Definition: Vector-inl.hh:197
T dot(const Vector &vec) const
Returns the dot product with another vecror.
Definition: Vector-inl.hh:207
void IsNonZero(const std::string &location, const std::string &what, const T &value)
Asserts that a value is non zero.
Definition: Assert.hh:2
Vector operator/(const T &factor) const
Overload the vector ratio with a scalar.
Definition: Vector-inl.hh:134
Object that can host any type of data in a vector arrangement.
Definition: Vector.hh:17
T mag() const
Returns the magnitude of the vector.
Definition: Vector-inl.hh:192
const T & front() const
Returns the first element of the vector.
Definition: Vector-inl.hh:242
~Vector()
Destructor.
Definition: Vector-inl.hh:31
void operator+=(const Vector &vec)
Overload the vector sum.
Definition: Vector-inl.hh:65
void push_front(const T &element)
Adds element at the back.
Definition: Vector-inl.hh:262
const T & operator[](const size_t i) const
Overload the subscript operator, return a value.
Definition: Vector-inl.hh:170
std::vector< T > _vec
Underlying std::vector.
Definition: Vector.hh:120
Global exception object for all non-standard exceptions thrown by the code.
Definition: Exception.hh:30
const T & back() const
Returns the last element of the vector.
Definition: Vector-inl.hh:238
const size_t size() const
Returns dimension of the vector.
Definition: Vector-inl.hh:266
Vector cross(const Vector &vec) const
Returns the cross product with another vecror.
Definition: Vector-inl.hh:221
const std::vector< T > & std() const
Returns the vector as an std::vector.
Definition: Vector-inl.hh:270
void resize(const size_t n)
Resizes the vector to chosen dimension.
Definition: Vector-inl.hh:254
Vector operator+(const Vector &vec) const
Overload the vector sum.
Definition: Vector-inl.hh:51
void operator-=(const Vector &vec)
Overload the v substraction.
Definition: Vector-inl.hh:90
Vector()
Default constructor, initilizes an empty vector of dimension 0.
Definition: Vector-inl.hh:1
Vector & operator=(const Vector &vec)
Equality operator.
Definition: Vector-inl.hh:33
T operator*(const Vector &vec) const
Overload the vector product (dot product)
Definition: Vector-inl.hh:101
T norm() const
Returns the norm of the vector.
Definition: Vector-inl.hh:246
Vector operator-() const
Overload the unary minus for vector algebra.
Definition: Vector-inl.hh:41
void operator/=(const T &factor)
Overload the vector ratio with a scalar.
Definition: Vector-inl.hh:148
T norm2() const
Returns the square of the norm of the vector.
Definition: Vector-inl.hh:250
void IsWithinBounds(const std::string &location, const size_t n, const size_t i)
Asserts that the index that is accessed is within bounds.
Definition: Assert.cc:5
void push_back(const T &element)
Adds element at the back.
Definition: Vector-inl.hh:258
void SameSize(const std::string &location, const std::string &what, const std::vector< T > &va, const std::vector< T > &vb)
Asserts that two vectors have the same size.
Definition: Assert.hh:83
void operator*=(const T &factor)
Overload the vector product with a scalar.
Definition: Vector-inl.hh:126