ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/math/DynamicVector.hpp
Revision: 1665
Committed: Tue Nov 22 20:38:56 2011 UTC (13 years, 5 months ago) by gezelter
File size: 14592 byte(s)
Log Message:
updated copyright notices

File Contents

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

Properties

Name Value
svn:executable *
svn:keywords Author Id Revision Date