1 |
/* |
2 |
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
* |
4 |
* The University of Notre Dame grants you ("Licensee") a |
5 |
* non-exclusive, royalty free, license to use, modify and |
6 |
* redistribute this software in source and binary code form, provided |
7 |
* that the following conditions are met: |
8 |
* |
9 |
* 1. Redistributions of source code must retain the above copyright |
10 |
* notice, this list of conditions and the following disclaimer. |
11 |
* |
12 |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
* notice, this list of conditions and the following disclaimer in the |
14 |
* documentation and/or other materials provided with the |
15 |
* distribution. |
16 |
* |
17 |
* This software is provided "AS IS," without a warranty of any |
18 |
* kind. All express or implied conditions, representations and |
19 |
* warranties, including any implied warranty of merchantability, |
20 |
* fitness for a particular purpose or non-infringement, are hereby |
21 |
* excluded. The University of Notre Dame and its licensors shall not |
22 |
* be liable for any damages suffered by licensee as a result of |
23 |
* using, modifying or distributing the software or its |
24 |
* derivatives. In no event will the University of Notre Dame or its |
25 |
* licensors be liable for any lost revenue, profit or data, or for |
26 |
* direct, indirect, special, consequential, incidental or punitive |
27 |
* damages, however caused and regardless of the theory of liability, |
28 |
* arising out of the use of or inability to use software, even if the |
29 |
* University of Notre Dame has been advised of the possibility of |
30 |
* such damages. |
31 |
* |
32 |
* SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your |
33 |
* research, please cite the appropriate papers when you publish your |
34 |
* work. Good starting points are: |
35 |
* |
36 |
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
37 |
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
38 |
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). |
39 |
* [4] Vardeman & Gezelter, in progress (2009). |
40 |
*/ |
41 |
|
42 |
/** |
43 |
* @file DynamicVector.hpp |
44 |
* @author Teng Lin |
45 |
* @date 09/14/2004 |
46 |
* @version 1.0 |
47 |
*/ |
48 |
|
49 |
#ifndef MATH_DYNAMICVECTOR_HPP |
50 |
#define MATH_DYNAMICVECTOR_HPP |
51 |
|
52 |
#include <cassert> |
53 |
#include <cmath> |
54 |
#include <iostream> |
55 |
#include <math.h> |
56 |
#include <algorithm> |
57 |
#include <vector> |
58 |
|
59 |
namespace OpenMD { |
60 |
|
61 |
/** |
62 |
* @class DynamicVector DynamicVector.hpp "math/DynamicVector.hpp" |
63 |
* @brief Fix length vector class |
64 |
*/ |
65 |
template<typename Real, typename Alloc = std::allocator<Real> > |
66 |
class DynamicVector : public std::vector<Real, Alloc> { |
67 |
|
68 |
public: |
69 |
typedef Real value_type; |
70 |
typedef std::vector<Real, Alloc> VectorType; |
71 |
typedef typename VectorType::pointer pointer; |
72 |
typedef typename VectorType::const_pointer const_pointer; |
73 |
typedef typename VectorType::reference reference; |
74 |
typedef typename VectorType::const_reference const_reference; |
75 |
typedef typename VectorType::iterator iterator; |
76 |
typedef typename VectorType::const_iterator const_iterator; |
77 |
typedef typename VectorType::const_reverse_iterator const_reverse_iterator; |
78 |
typedef typename VectorType::reverse_iterator reverse_iterator; |
79 |
typedef typename VectorType::size_type size_type; |
80 |
typedef typename VectorType::difference_type difference_type; |
81 |
typedef typename VectorType::allocator_type allocator_type; |
82 |
|
83 |
|
84 |
// [23.2.4.1] construct/copy/destroy |
85 |
// (assign() and get_allocator() are also listed in this section) |
86 |
/** |
87 |
* @brief Default constructor creates no elements. |
88 |
*/ explicit |
89 |
DynamicVector(const allocator_type& alloc = allocator_type()) |
90 |
: std::vector<Real, Alloc>(alloc) { } |
91 |
|
92 |
/** |
93 |
* @brief Create a %DynamicVector with copies of an exemplar element. |
94 |
* @param n The number of elements to initially create. |
95 |
* @param value An element to copy. |
96 |
* |
97 |
* This constructor fills the %DynamicVector with @a n copies of @a value. |
98 |
*/ |
99 |
DynamicVector(size_type n, const value_type& value, |
100 |
const allocator_type& alloc = allocator_type()) |
101 |
: std::vector<Real, Alloc>(n, value, alloc){ } |
102 |
|
103 |
/** |
104 |
* @brief Create a %DynamicVector with default elements. |
105 |
* @param n The number of elements to initially create. |
106 |
* |
107 |
* This constructor fills the %DynamicVector with @a n copies of a |
108 |
* default-constructed element. |
109 |
*/ |
110 |
explicit |
111 |
DynamicVector(size_type n) : std::vector<Real, Alloc>(n) { } |
112 |
|
113 |
/** |
114 |
* @brief %Vector copy constructor. |
115 |
* @param x A %DynamicVector of identical element and allocator types. |
116 |
* |
117 |
* The newly-created %DynamicVector uses a copy of the allocation |
118 |
* object used by @a x. All the elements of @a x are copied, |
119 |
* but any extra memory in |
120 |
* @a x (for fast expansion) will not be copied. |
121 |
*/ |
122 |
DynamicVector(const DynamicVector& x) |
123 |
: std::vector<Real, Alloc>(x) {} |
124 |
|
125 |
template<typename _InputIterator> |
126 |
DynamicVector(_InputIterator first, _InputIterator last, |
127 |
const allocator_type& alloc = allocator_type()) |
128 |
: std::vector<Real, Alloc>(first, last, alloc) {} |
129 |
|
130 |
inline Real operator()(unsigned int i) const{ |
131 |
return (*this)[i]; |
132 |
} |
133 |
|
134 |
inline Real& operator()(unsigned int i){ |
135 |
return (*this)[i]; |
136 |
} |
137 |
/** |
138 |
* Tests if this vetor is equal to other vector |
139 |
* @return true if equal, otherwise return false |
140 |
* @param v vector to be compared |
141 |
*/ |
142 |
inline bool operator ==(const DynamicVector<Real>& v) { |
143 |
|
144 |
for (unsigned int i = 0; i < this->size(); i ++) { |
145 |
if (!equal((*this)[i], v[i])) { |
146 |
return false; |
147 |
} |
148 |
} |
149 |
|
150 |
return true; |
151 |
} |
152 |
|
153 |
/** |
154 |
* Tests if this vetor is not equal to other vector |
155 |
* @return true if equal, otherwise return false |
156 |
* @param v vector to be compared |
157 |
*/ |
158 |
inline bool operator !=(const DynamicVector<Real>& v) { |
159 |
return !(*this == v); |
160 |
} |
161 |
|
162 |
/** Negates the value of this vector in place. */ |
163 |
inline void negate() { |
164 |
for (unsigned int i = 0; i < this->size(); i++) |
165 |
(*this)[i] = -(*this)[i]; |
166 |
} |
167 |
|
168 |
/** |
169 |
* Sets the value of this vector to the negation of vector v1. |
170 |
* @param v1 the source vector |
171 |
*/ |
172 |
inline void negate(const DynamicVector<Real>& v1) { |
173 |
for (unsigned int i = 0; i < this->size(); i++) |
174 |
(*this)[i] = -v1[i]; |
175 |
|
176 |
} |
177 |
|
178 |
/** |
179 |
* Sets the value of this vector to the sum of itself and v1 (*this += v1). |
180 |
* @param v1 the other vector |
181 |
*/ |
182 |
inline void add( const DynamicVector<Real>& v1 ) { |
183 |
std::transform(this->begin(), this->end(), v1.begin(),this->begin(),std::plus<Real>()); |
184 |
} |
185 |
|
186 |
/** |
187 |
* Sets the value of this vector to the sum of v1 and v2 (*this = v1 + v2). |
188 |
* @param v1 the first vector |
189 |
* @param v2 the second vector |
190 |
*/ |
191 |
inline void add( const DynamicVector<Real>& v1, const DynamicVector<Real>& v2 ) { |
192 |
std::transform(v1.begin(), v1.end(), v2.begin(),this->begin(),std::plus<Real>()); |
193 |
} |
194 |
|
195 |
/** |
196 |
* Sets the value of this vector to the difference of itself and v1 (*this -= v1). |
197 |
* @param v1 the other vector |
198 |
*/ |
199 |
inline void sub( const DynamicVector<Real>& v1 ) { |
200 |
std::transform(this->begin(), this->end(), v1.begin(),this->begin(),std::minus<Real>()); |
201 |
} |
202 |
|
203 |
/** |
204 |
* Sets the value of this vector to the difference of vector v1 and v2 (*this = v1 - v2). |
205 |
* @param v1 the first vector |
206 |
* @param v2 the second vector |
207 |
*/ |
208 |
inline void sub( const DynamicVector<Real>& v1, const DynamicVector &v2 ){ |
209 |
std::transform(v1.begin(), v1.end(), v2.begin(),this->begin(),std::minus<Real>()); |
210 |
} |
211 |
|
212 |
/** |
213 |
* Sets the value of this vector to the scalar multiplication of itself (*this *= s). |
214 |
* @param s the scalar value |
215 |
*/ |
216 |
inline void mul( Real s ) { |
217 |
for (unsigned int i = 0; i < this->size(); i++) |
218 |
(*this)[i] *= s; |
219 |
} |
220 |
|
221 |
/** |
222 |
* Sets the value of this vector to the scalar multiplication of vector v1 |
223 |
* (*this = s * v1). |
224 |
* @param v1 the vector |
225 |
* @param s the scalar value |
226 |
*/ |
227 |
inline void mul( const DynamicVector<Real>& v1, Real s) { |
228 |
for (unsigned int i = 0; i < this->size(); i++) |
229 |
(*this)[i] = s * v1[i]; |
230 |
} |
231 |
|
232 |
/** |
233 |
* Sets the value of this vector to the scalar division of itself (*this /= s ). |
234 |
* @param s the scalar value |
235 |
*/ |
236 |
inline void div( Real s) { |
237 |
for (unsigned int i = 0; i < this->size(); i++) |
238 |
(*this)[i] /= s; |
239 |
} |
240 |
|
241 |
/** |
242 |
* Sets the value of this vector to the scalar division of vector v1 (*this = v1 / s ). |
243 |
* @param v1 the source vector |
244 |
* @param s the scalar value |
245 |
*/ |
246 |
inline void div( const DynamicVector<Real>& v1, Real s ) { |
247 |
for (unsigned int i = 0; i < this->size(); i++) |
248 |
(*this)[i] = v1[i] / s; |
249 |
} |
250 |
|
251 |
/** @see #add */ |
252 |
inline DynamicVector<Real>& operator +=( const DynamicVector<Real>& v1 ) { |
253 |
add(v1); |
254 |
return *this; |
255 |
} |
256 |
|
257 |
/** @see #sub */ |
258 |
inline DynamicVector<Real>& operator -=( const DynamicVector<Real>& v1 ) { |
259 |
sub(v1); |
260 |
return *this; |
261 |
} |
262 |
|
263 |
/** @see #mul */ |
264 |
inline DynamicVector<Real>& operator *=( Real s) { |
265 |
mul(s); |
266 |
return *this; |
267 |
} |
268 |
|
269 |
/** @see #div */ |
270 |
inline DynamicVector<Real>& operator /=( Real s ) { |
271 |
div(s); |
272 |
return *this; |
273 |
} |
274 |
|
275 |
/** |
276 |
* Returns the length of this vector. |
277 |
* @return the length of this vector |
278 |
*/ |
279 |
inline Real length() { |
280 |
return sqrt(lengthSquare()); |
281 |
} |
282 |
|
283 |
/** |
284 |
* Returns the squared length of this vector. |
285 |
* @return the squared length of this vector |
286 |
*/ |
287 |
inline Real lengthSquare() { |
288 |
return dot(*this, *this); |
289 |
} |
290 |
|
291 |
/** Normalizes this vector in place */ |
292 |
inline void normalize() { |
293 |
Real len; |
294 |
|
295 |
len = length(); |
296 |
|
297 |
//if (len < OpenMD::NumericConstant::epsilon) |
298 |
// throw(); |
299 |
|
300 |
*this /= len; |
301 |
} |
302 |
|
303 |
/** |
304 |
* Tests if this vector is normalized |
305 |
* @return true if this vector is normalized, otherwise return false |
306 |
*/ |
307 |
inline bool isNormalized() { |
308 |
return equal(lengthSquare(), 1.0); |
309 |
} |
310 |
|
311 |
template<class VectorType> |
312 |
void getSubVector(unsigned int beginning, VectorType& v) { |
313 |
assert(beginning + v.size() -1 <= this->size()); |
314 |
|
315 |
for (unsigned int i = 0; i < v.size(); ++i) |
316 |
v(i) = (*this)[beginning+i]; |
317 |
} |
318 |
|
319 |
|
320 |
}; |
321 |
|
322 |
/** unary minus*/ |
323 |
template<typename Real> |
324 |
inline DynamicVector<Real> operator -(const DynamicVector<Real>& v1){ |
325 |
DynamicVector<Real> tmp(v1); |
326 |
tmp.negate(); |
327 |
return tmp; |
328 |
} |
329 |
|
330 |
/** |
331 |
* Return the sum of two vectors (v1 - v2). |
332 |
* @return the sum of two vectors |
333 |
* @param v1 the first vector |
334 |
* @param v2 the second vector |
335 |
*/ |
336 |
template<typename Real> |
337 |
inline DynamicVector<Real> operator +(const DynamicVector<Real>& v1, const DynamicVector<Real>& v2) { |
338 |
DynamicVector<Real> result; |
339 |
|
340 |
result.add(v1, v2); |
341 |
return result; |
342 |
} |
343 |
|
344 |
/** |
345 |
* Return the difference of two vectors (v1 - v2). |
346 |
* @return the difference of two vectors |
347 |
* @param v1 the first vector |
348 |
* @param v2 the second vector |
349 |
*/ |
350 |
template<typename Real> |
351 |
DynamicVector<Real> operator -(const DynamicVector<Real>& v1, const DynamicVector<Real>& v2) { |
352 |
DynamicVector<Real> result; |
353 |
result.sub(v1, v2); |
354 |
return result; |
355 |
} |
356 |
|
357 |
/** |
358 |
* Returns the vaule of scalar multiplication of this vector v1 (v1 * r). |
359 |
* @return the vaule of scalar multiplication of this vector |
360 |
* @param v1 the source vector |
361 |
* @param s the scalar value |
362 |
*/ |
363 |
template<typename Real> |
364 |
DynamicVector<Real> operator * ( const DynamicVector<Real>& v1, Real s) { |
365 |
DynamicVector<Real> result; |
366 |
result.mul(v1,s); |
367 |
return result; |
368 |
} |
369 |
|
370 |
/** |
371 |
* Returns the vaule of scalar multiplication of this vector v1 (v1 * r). |
372 |
* @return the vaule of scalar multiplication of this vector |
373 |
* @param s the scalar value |
374 |
* @param v1 the source vector |
375 |
*/ |
376 |
template<typename Real> |
377 |
DynamicVector<Real> operator * ( Real s, const DynamicVector<Real>& v1 ) { |
378 |
DynamicVector<Real> result; |
379 |
result.mul(v1, s); |
380 |
return result; |
381 |
} |
382 |
|
383 |
/** |
384 |
* Returns the value of division of a vector by a scalar. |
385 |
* @return the vaule of scalar division of this vector |
386 |
* @param v1 the source vector |
387 |
* @param s the scalar value |
388 |
*/ |
389 |
template<typename Real> |
390 |
DynamicVector<Real> operator / ( const DynamicVector<Real>& v1, Real s) { |
391 |
DynamicVector<Real> result; |
392 |
result.div( v1,s); |
393 |
return result; |
394 |
} |
395 |
|
396 |
/** |
397 |
* Returns the dot product of two DynamicVectors |
398 |
* @param v1 first vector |
399 |
* @param v2 second vector |
400 |
* @return the dot product of v1 and v2 |
401 |
*/ |
402 |
template<typename Real> |
403 |
inline Real dot( const DynamicVector<Real>& v1, const DynamicVector<Real>& v2 ) { |
404 |
Real tmp; |
405 |
tmp = 0; |
406 |
assert(v1.size() == v2.size()); |
407 |
for (unsigned int i = 0; i < v1.size(); i++) |
408 |
tmp += v1[i] * v2[i]; |
409 |
|
410 |
return tmp; |
411 |
} |
412 |
|
413 |
/** |
414 |
* Returns the distance between two DynamicVectors |
415 |
* @param v1 first vector |
416 |
* @param v2 second vector |
417 |
* @return the distance between v1 and v2 |
418 |
*/ |
419 |
template<typename Real> |
420 |
inline Real distance( const DynamicVector<Real>& v1, const DynamicVector<Real>& v2 ) { |
421 |
DynamicVector<Real> tempDynamicVector = v1 - v2; |
422 |
return tempDynamicVector.length(); |
423 |
} |
424 |
|
425 |
/** |
426 |
* Returns the squared distance between two DynamicVectors |
427 |
* @param v1 first vector |
428 |
* @param v2 second vector |
429 |
* @return the squared distance between v1 and v2 |
430 |
*/ |
431 |
template<typename Real> |
432 |
inline Real distanceSquare( const DynamicVector<Real>& v1, const DynamicVector<Real>& v2 ) { |
433 |
DynamicVector<Real> tempDynamicVector = v1 - v2; |
434 |
return tempDynamicVector.lengthSquare(); |
435 |
} |
436 |
|
437 |
/** |
438 |
* Write to an output stream |
439 |
*/ |
440 |
template<typename Real> |
441 |
std::ostream &operator<< ( std::ostream& o, const DynamicVector<Real>& v) { |
442 |
|
443 |
o << "[ "; |
444 |
|
445 |
for (unsigned int i = 0 ; i< v.size(); i++) { |
446 |
o << v[i]; |
447 |
|
448 |
if (i != v.size() -1) { |
449 |
o<< ", "; |
450 |
} |
451 |
} |
452 |
|
453 |
o << " ]"; |
454 |
return o; |
455 |
} |
456 |
|
457 |
} |
458 |
#endif |
459 |
|