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. Acknowledgement of the program authors must be made in any |
10 |
* publication of scientific results based in part on use of the |
11 |
* program. An acceptable form of acknowledgement is citation of |
12 |
* the article in which the program was described (Matthew |
13 |
* A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher |
14 |
* J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented |
15 |
* Parallel Simulation Engine for Molecular Dynamics," |
16 |
* J. Comput. Chem. 26, pp. 252-271 (2005)) |
17 |
* |
18 |
* 2. Redistributions of source code must retain the above copyright |
19 |
* notice, this list of conditions and the following disclaimer. |
20 |
* |
21 |
* 3. Redistributions in binary form must reproduce the above copyright |
22 |
* notice, this list of conditions and the following disclaimer in the |
23 |
* documentation and/or other materials provided with the |
24 |
* distribution. |
25 |
* |
26 |
* This software is provided "AS IS," without a warranty of any |
27 |
* kind. All express or implied conditions, representations and |
28 |
* warranties, including any implied warranty of merchantability, |
29 |
* fitness for a particular purpose or non-infringement, are hereby |
30 |
* excluded. The University of Notre Dame and its licensors shall not |
31 |
* be liable for any damages suffered by licensee as a result of |
32 |
* using, modifying or distributing the software or its |
33 |
* derivatives. In no event will the University of Notre Dame or its |
34 |
* licensors be liable for any lost revenue, profit or data, or for |
35 |
* direct, indirect, special, consequential, incidental or punitive |
36 |
* damages, however caused and regardless of the theory of liability, |
37 |
* arising out of the use of or inability to use software, even if the |
38 |
* University of Notre Dame has been advised of the possibility of |
39 |
* such damages. |
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 oopse { |
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 |
/** |
131 |
* Tests if this vetor is equal to other vector |
132 |
* @return true if equal, otherwise return false |
133 |
* @param v vector to be compared |
134 |
*/ |
135 |
inline bool operator ==(const DynamicVector<Real>& v) { |
136 |
|
137 |
for (unsigned int i = 0; i < this->size(); i ++) { |
138 |
if (!equal((*this)[i], v[i])) { |
139 |
return false; |
140 |
} |
141 |
} |
142 |
|
143 |
return true; |
144 |
} |
145 |
|
146 |
/** |
147 |
* Tests if this vetor is not equal to other vector |
148 |
* @return true if equal, otherwise return false |
149 |
* @param v vector to be compared |
150 |
*/ |
151 |
inline bool operator !=(const DynamicVector<Real>& v) { |
152 |
return !(*this == v); |
153 |
} |
154 |
|
155 |
/** Negates the value of this vector in place. */ |
156 |
inline void negate() { |
157 |
for (unsigned int i = 0; i < this->size(); i++) |
158 |
(*this)[i] = -(*this)[i]; |
159 |
} |
160 |
|
161 |
/** |
162 |
* Sets the value of this vector to the negation of vector v1. |
163 |
* @param v1 the source vector |
164 |
*/ |
165 |
inline void negate(const DynamicVector<Real>& v1) { |
166 |
for (unsigned int i = 0; i < this->size(); i++) |
167 |
(*this)[i] = -v1[i]; |
168 |
|
169 |
} |
170 |
|
171 |
/** |
172 |
* Sets the value of this vector to the sum of itself and v1 (*this += v1). |
173 |
* @param v1 the other vector |
174 |
*/ |
175 |
inline void add( const DynamicVector<Real>& v1 ) { |
176 |
std::transform(this->begin(), this->end(), v1.begin(),this->begin(),std::plus<Real>()); |
177 |
} |
178 |
|
179 |
/** |
180 |
* Sets the value of this vector to the sum of v1 and v2 (*this = v1 + v2). |
181 |
* @param v1 the first vector |
182 |
* @param v2 the second vector |
183 |
*/ |
184 |
inline void add( const DynamicVector<Real>& v1, const DynamicVector<Real>& v2 ) { |
185 |
std::transform(v1.begin(), v1.end(), v2.begin(),this->begin(),std::plus<Real>()); |
186 |
} |
187 |
|
188 |
/** |
189 |
* Sets the value of this vector to the difference of itself and v1 (*this -= v1). |
190 |
* @param v1 the other vector |
191 |
*/ |
192 |
inline void sub( const DynamicVector<Real>& v1 ) { |
193 |
std::transform(this->begin(), this->end(), v1.begin(),this->begin(),std::minus<Real>()); |
194 |
} |
195 |
|
196 |
/** |
197 |
* Sets the value of this vector to the difference of vector v1 and v2 (*this = v1 - v2). |
198 |
* @param v1 the first vector |
199 |
* @param v2 the second vector |
200 |
*/ |
201 |
inline void sub( const DynamicVector<Real>& v1, const DynamicVector &v2 ){ |
202 |
std::transform(v1.begin(), v1.end(), v2.begin(),this->begin(),std::minus<Real>()); |
203 |
} |
204 |
|
205 |
/** |
206 |
* Sets the value of this vector to the scalar multiplication of itself (*this *= s). |
207 |
* @param s the scalar value |
208 |
*/ |
209 |
inline void mul( Real s ) { |
210 |
for (unsigned int i = 0; i < this->size(); i++) |
211 |
(*this)[i] *= s; |
212 |
} |
213 |
|
214 |
/** |
215 |
* Sets the value of this vector to the scalar multiplication of vector v1 |
216 |
* (*this = s * v1). |
217 |
* @param v1 the vector |
218 |
* @param s the scalar value |
219 |
*/ |
220 |
inline void mul( const DynamicVector<Real>& v1, Real s) { |
221 |
for (unsigned int i = 0; i < this->size(); i++) |
222 |
(*this)[i] = s * v1[i]; |
223 |
} |
224 |
|
225 |
/** |
226 |
* Sets the value of this vector to the scalar division of itself (*this /= s ). |
227 |
* @param s the scalar value |
228 |
*/ |
229 |
inline void div( Real s) { |
230 |
for (unsigned int i = 0; i < this->size(); i++) |
231 |
(*this)[i] /= s; |
232 |
} |
233 |
|
234 |
/** |
235 |
* Sets the value of this vector to the scalar division of vector v1 (*this = v1 / s ). |
236 |
* @param v1 the source vector |
237 |
* @param s the scalar value |
238 |
*/ |
239 |
inline void div( const DynamicVector<Real>& v1, Real s ) { |
240 |
for (unsigned int i = 0; i < this->size(); i++) |
241 |
(*this)[i] = v1[i] / s; |
242 |
} |
243 |
|
244 |
/** @see #add */ |
245 |
inline DynamicVector<Real>& operator +=( const DynamicVector<Real>& v1 ) { |
246 |
add(v1); |
247 |
return *this; |
248 |
} |
249 |
|
250 |
/** @see #sub */ |
251 |
inline DynamicVector<Real>& operator -=( const DynamicVector<Real>& v1 ) { |
252 |
sub(v1); |
253 |
return *this; |
254 |
} |
255 |
|
256 |
/** @see #mul */ |
257 |
inline DynamicVector<Real>& operator *=( Real s) { |
258 |
mul(s); |
259 |
return *this; |
260 |
} |
261 |
|
262 |
/** @see #div */ |
263 |
inline DynamicVector<Real>& operator /=( Real s ) { |
264 |
div(s); |
265 |
return *this; |
266 |
} |
267 |
|
268 |
/** |
269 |
* Returns the length of this vector. |
270 |
* @return the length of this vector |
271 |
*/ |
272 |
inline Real length() { |
273 |
return sqrt(lengthSquare()); |
274 |
} |
275 |
|
276 |
/** |
277 |
* Returns the squared length of this vector. |
278 |
* @return the squared length of this vector |
279 |
*/ |
280 |
inline Real lengthSquare() { |
281 |
return dot(*this, *this); |
282 |
} |
283 |
|
284 |
/** Normalizes this vector in place */ |
285 |
inline void normalize() { |
286 |
Real len; |
287 |
|
288 |
len = length(); |
289 |
|
290 |
//if (len < oopse:epsilon) |
291 |
// throw(); |
292 |
|
293 |
*this /= len; |
294 |
} |
295 |
|
296 |
/** |
297 |
* Tests if this vector is normalized |
298 |
* @return true if this vector is normalized, otherwise return false |
299 |
*/ |
300 |
inline bool isNormalized() { |
301 |
return equal(lengthSquare(), 1.0); |
302 |
} |
303 |
|
304 |
}; |
305 |
|
306 |
/** unary minus*/ |
307 |
template<typename Real> |
308 |
inline DynamicVector<Real> operator -(const DynamicVector<Real>& v1){ |
309 |
DynamicVector<Real> tmp(v1); |
310 |
tmp.negate(); |
311 |
return tmp; |
312 |
} |
313 |
|
314 |
/** |
315 |
* Return the sum of two vectors (v1 - v2). |
316 |
* @return the sum of two vectors |
317 |
* @param v1 the first vector |
318 |
* @param v2 the second vector |
319 |
*/ |
320 |
template<typename Real> |
321 |
inline DynamicVector<Real> operator +(const DynamicVector<Real>& v1, const DynamicVector<Real>& v2) { |
322 |
DynamicVector<Real> result; |
323 |
|
324 |
result.add(v1, v2); |
325 |
return result; |
326 |
} |
327 |
|
328 |
/** |
329 |
* Return the difference of two vectors (v1 - v2). |
330 |
* @return the difference of two vectors |
331 |
* @param v1 the first vector |
332 |
* @param v2 the second vector |
333 |
*/ |
334 |
template<typename Real> |
335 |
DynamicVector<Real> operator -(const DynamicVector<Real>& v1, const DynamicVector<Real>& v2) { |
336 |
DynamicVector<Real> result; |
337 |
result.sub(v1, v2); |
338 |
return result; |
339 |
} |
340 |
|
341 |
/** |
342 |
* Returns the vaule of scalar multiplication of this vector v1 (v1 * r). |
343 |
* @return the vaule of scalar multiplication of this vector |
344 |
* @param v1 the source vector |
345 |
* @param s the scalar value |
346 |
*/ |
347 |
template<typename Real> |
348 |
DynamicVector<Real> operator * ( const DynamicVector<Real>& v1, Real s) { |
349 |
DynamicVector<Real> result; |
350 |
result.mul(v1,s); |
351 |
return result; |
352 |
} |
353 |
|
354 |
/** |
355 |
* Returns the vaule of scalar multiplication of this vector v1 (v1 * r). |
356 |
* @return the vaule of scalar multiplication of this vector |
357 |
* @param s the scalar value |
358 |
* @param v1 the source vector |
359 |
*/ |
360 |
template<typename Real> |
361 |
DynamicVector<Real> operator * ( Real s, const DynamicVector<Real>& v1 ) { |
362 |
DynamicVector<Real> result; |
363 |
result.mul(v1, s); |
364 |
return result; |
365 |
} |
366 |
|
367 |
/** |
368 |
* Returns the value of division of a vector by a scalar. |
369 |
* @return the vaule of scalar division of this vector |
370 |
* @param v1 the source vector |
371 |
* @param s the scalar value |
372 |
*/ |
373 |
template<typename Real> |
374 |
DynamicVector<Real> operator / ( const DynamicVector<Real>& v1, Real s) { |
375 |
DynamicVector<Real> result; |
376 |
result.div( v1,s); |
377 |
return result; |
378 |
} |
379 |
|
380 |
/** |
381 |
* Returns the dot product of two DynamicVectors |
382 |
* @param v1 first vector |
383 |
* @param v2 second vector |
384 |
* @return the dot product of v1 and v2 |
385 |
*/ |
386 |
template<typename Real> |
387 |
inline Real dot( const DynamicVector<Real>& v1, const DynamicVector<Real>& v2 ) { |
388 |
Real tmp; |
389 |
tmp = 0; |
390 |
assert(v1.size() == v2.size()); |
391 |
for (unsigned int i = 0; i < v1.size(); i++) |
392 |
tmp += v1[i] * v2[i]; |
393 |
|
394 |
return tmp; |
395 |
} |
396 |
|
397 |
/** |
398 |
* Returns the distance between two DynamicVectors |
399 |
* @param v1 first vector |
400 |
* @param v2 second vector |
401 |
* @return the distance between v1 and v2 |
402 |
*/ |
403 |
template<typename Real> |
404 |
inline Real distance( const DynamicVector<Real>& v1, const DynamicVector<Real>& v2 ) { |
405 |
DynamicVector<Real> tempDynamicVector = v1 - v2; |
406 |
return tempDynamicVector.length(); |
407 |
} |
408 |
|
409 |
/** |
410 |
* Returns the squared distance between two DynamicVectors |
411 |
* @param v1 first vector |
412 |
* @param v2 second vector |
413 |
* @return the squared distance between v1 and v2 |
414 |
*/ |
415 |
template<typename Real> |
416 |
inline Real distanceSquare( const DynamicVector<Real>& v1, const DynamicVector<Real>& v2 ) { |
417 |
DynamicVector<Real> tempDynamicVector = v1 - v2; |
418 |
return tempDynamicVector.lengthSquare(); |
419 |
} |
420 |
|
421 |
/** |
422 |
* Write to an output stream |
423 |
*/ |
424 |
template<typename Real> |
425 |
std::ostream &operator<< ( std::ostream& o, const DynamicVector<Real>& v) { |
426 |
|
427 |
o << "[ "; |
428 |
|
429 |
for (unsigned int i = 0 ; i< v.size(); i++) { |
430 |
o << v[i]; |
431 |
|
432 |
if (i != v.size() -1) { |
433 |
o<< ", "; |
434 |
} |
435 |
} |
436 |
|
437 |
o << " ]"; |
438 |
return o; |
439 |
} |
440 |
|
441 |
} |
442 |
#endif |
443 |
|