ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/math/Vector.hpp
Revision: 1767
Committed: Fri Jul 6 22:01:58 2012 UTC (12 years, 9 months ago) by gezelter
File size: 16621 byte(s)
Log Message:
Various fixes required to compile OpenMD with the MS Visual C++ compiler

File Contents

# User Rev Content
1 gezelter 507 /*
2 gezelter 246 * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved.
3 tim 70 *
4 gezelter 246 * 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 gezelter 246 * notice, this list of conditions and the following disclaimer.
11     *
12 gezelter 1390 * 2. Redistributions in binary form must reproduce the above copyright
13 gezelter 246 * 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 70 */
42 gezelter 246
43 tim 70 /**
44     * @file Vector.hpp
45     * @author Teng Lin
46     * @date 09/14/2004
47     * @version 1.0
48     */
49    
50     #ifndef MATH_VECTOR_HPP
51     #define MATH_VECTOR_HPP
52    
53     #include <cassert>
54     #include <cmath>
55 tim 71 #include <iostream>
56 tim 253 #include <math.h>
57 tim 963 #include "config.h"
58 gezelter 1390 namespace OpenMD {
59 tim 70
60 tim 963 static const RealType epsilon = 0.000001;
61 tim 76
62 gezelter 507 template<typename T>
63     inline bool equal(T e1, T e2) {
64     return e1 == e2;
65     }
66 tim 76
67 tim 963 //template<>
68     //inline bool equal(float e1, float e2) {
69     // return fabs(e1 - e2) < epsilon;
70     //}
71 tim 76
72 gezelter 507 template<>
73 tim 963 inline bool equal(RealType e1, RealType e2) {
74 gezelter 507 return fabs(e1 - e2) < epsilon;
75     }
76 tim 76
77 gezelter 507 /**
78     * @class Vector Vector.hpp "math/Vector.hpp"
79     * @brief Fix length vector class
80     */
81     template<typename Real, unsigned int Dim>
82     class Vector{
83     public:
84 tim 70
85 gezelter 507 typedef Real ElemType;
86     typedef Real* ElemPoinerType;
87 tim 137
88 gezelter 507 /** default constructor */
89     inline Vector(){
90     for (unsigned int i = 0; i < Dim; i++)
91     this->data_[i] = 0;
92     }
93 tim 70
94 gezelter 507 /** Constructs and initializes a Vector from a vector */
95     inline Vector(const Vector<Real, Dim>& v) {
96     *this = v;
97     }
98 tim 70
99 gezelter 507 /** copy assignment operator */
100     inline Vector<Real, Dim>& operator=(const Vector<Real, Dim>& v) {
101     if (this == &v)
102     return *this;
103 tim 70
104 gezelter 507 for (unsigned int i = 0; i < Dim; i++)
105     this->data_[i] = v[i];
106 tim 70
107 gezelter 507 return *this;
108     }
109 tim 101
110 gezelter 1767 // template<typename T>
111     // inline Vector(const T& s){
112     inline Vector(const Real& s) {
113 gezelter 507 for (unsigned int i = 0; i < Dim; i++)
114 gezelter 1767 this->data_[i] = s;
115 gezelter 507 }
116 tim 70
117 gezelter 507 /** Constructs and initializes a Vector from an array */
118     inline Vector( Real* v) {
119     for (unsigned int i = 0; i < Dim; i++)
120     this->data_[i] = v[i];
121     }
122 tim 70
123 gezelter 507 /**
124     * Returns reference of ith element.
125     * @return reference of ith element
126     * @param i index
127     */
128     inline Real& operator[](unsigned int i) {
129     assert( i < Dim);
130     return this->data_[i];
131     }
132 tim 70
133 gezelter 507 /**
134     * Returns reference of ith element.
135     * @return reference of ith element
136     * @param i index
137     */
138     inline Real& operator()(unsigned int i) {
139     assert( i < Dim);
140     return this->data_[i];
141     }
142 tim 70
143 gezelter 507 /**
144     * Returns constant reference of ith element.
145     * @return reference of ith element
146     * @param i index
147     */
148     inline const Real& operator[](unsigned int i) const {
149     assert( i < Dim);
150     return this->data_[i];
151     }
152 tim 70
153 gezelter 507 /**
154     * Returns constant reference of ith element.
155     * @return reference of ith element
156     * @param i index
157     */
158     inline const Real& operator()(unsigned int i) const {
159     assert( i < Dim);
160     return this->data_[i];
161     }
162 tim 70
163 gezelter 507 /** Copy the internal data to an array*/
164     void getArray(Real* array) {
165     for (unsigned int i = 0; i < Dim; i ++) {
166     array[i] = this->data_[i];
167     }
168     }
169 tim 151
170 gezelter 507 /** Returns the pointer of internal array */
171     Real* getArrayPointer() {
172     return this->data_;
173     }
174 tim 137
175 gezelter 507 /**
176     * Tests if this vetor is equal to other vector
177     * @return true if equal, otherwise return false
178     * @param v vector to be compared
179     */
180     inline bool operator ==(const Vector<Real, Dim>& v) {
181 tim 76
182 gezelter 507 for (unsigned int i = 0; i < Dim; i ++) {
183     if (!equal(this->data_[i], v[i])) {
184     return false;
185     }
186     }
187 tim 76
188 gezelter 507 return true;
189     }
190 tim 76
191 gezelter 507 /**
192     * Tests if this vetor is not equal to other vector
193     * @return true if equal, otherwise return false
194     * @param v vector to be compared
195     */
196     inline bool operator !=(const Vector<Real, Dim>& v) {
197     return !(*this == v);
198     }
199 tim 76
200 gezelter 507 /** Negates the value of this vector in place. */
201     inline void negate() {
202     for (unsigned int i = 0; i < Dim; i++)
203     this->data_[i] = -this->data_[i];
204     }
205 tim 70
206 gezelter 507 /**
207     * Sets the value of this vector to the negation of vector v1.
208     * @param v1 the source vector
209     */
210     inline void negate(const Vector<Real, Dim>& v1) {
211     for (unsigned int i = 0; i < Dim; i++)
212     this->data_[i] = -v1.data_[i];
213 tim 70
214 gezelter 507 }
215 tim 70
216 gezelter 507 /**
217     * Sets the value of this vector to the sum of itself and v1 (*this += v1).
218     * @param v1 the other vector
219     */
220     inline void add( const Vector<Real, Dim>& v1 ) {
221     for (unsigned int i = 0; i < Dim; i++)
222     this->data_[i] += v1.data_[i];
223 tim 70 }
224    
225     /**
226 gezelter 507 * Sets the value of this vector to the sum of v1 and v2 (*this = v1 + v2).
227 tim 70 * @param v1 the first vector
228     * @param v2 the second vector
229 gezelter 507 */
230     inline void add( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
231     for (unsigned int i = 0; i < Dim; i++)
232     this->data_[i] = v1.data_[i] + v2.data_[i];
233 tim 70 }
234    
235     /**
236 gezelter 507 * Sets the value of this vector to the difference of itself and v1 (*this -= v1).
237     * @param v1 the other vector
238     */
239     inline void sub( const Vector<Real, Dim>& v1 ) {
240     for (unsigned int i = 0; i < Dim; i++)
241     this->data_[i] -= v1.data_[i];
242     }
243    
244     /**
245     * Sets the value of this vector to the difference of vector v1 and v2 (*this = v1 - v2).
246 tim 70 * @param v1 the first vector
247     * @param v2 the second vector
248 gezelter 507 */
249     inline void sub( const Vector<Real, Dim>& v1, const Vector &v2 ){
250     for (unsigned int i = 0; i < Dim; i++)
251     this->data_[i] = v1.data_[i] - v2.data_[i];
252 tim 70 }
253 gezelter 507
254 tim 70 /**
255 gezelter 507 * Sets the value of this vector to the scalar multiplication of itself (*this *= s).
256 tim 70 * @param s the scalar value
257 gezelter 507 */
258     inline void mul( Real s ) {
259     for (unsigned int i = 0; i < Dim; i++)
260     this->data_[i] *= s;
261 tim 70 }
262 gezelter 507
263 tim 70 /**
264 gezelter 507 * Sets the value of this vector to the scalar multiplication of vector v1
265     * (*this = s * v1).
266     * @param v1 the vector
267 tim 70 * @param s the scalar value
268 gezelter 507 */
269     inline void mul( const Vector<Real, Dim>& v1, Real s) {
270     for (unsigned int i = 0; i < Dim; i++)
271     this->data_[i] = s * v1.data_[i];
272 tim 70 }
273    
274     /**
275 gezelter 1562 * Sets the elements of this vector to the multiplication of
276     * elements of two other vectors. Not to be confused with scalar
277     * multiplication (mul) or dot products.
278     *
279     * (*this.data_[i] = v1.data_[i] * v2.data_[i]).
280     * @param v1 the first vector
281     * @param v2 the second vector
282     */
283     inline void Vmul( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) {
284     for (unsigned int i = 0; i < Dim; i++)
285     this->data_[i] = v1.data_[i] * v2.data_[i];
286     }
287    
288 gezelter 1760 /* replaces the elements with the absolute values of those elements */
289     inline Vector<Real, Dim>& abs() {
290     for (unsigned int i = 0; i < Dim; i++) {
291     this->data_[i] = std::abs(this->data_[i]);
292     }
293     return *this;
294     }
295    
296     /* returns the maximum value in this vector */
297     inline Real max() {
298     Real val = this->data_[0];
299     for (unsigned int i = 0; i < Dim; i++) {
300     if (this->data_[i] > val) val = this->data_[i];
301     }
302     return val;
303     }
304    
305 gezelter 1562 /**
306 gezelter 507 * Sets the value of this vector to the scalar division of itself (*this /= s ).
307     * @param s the scalar value
308     */
309     inline void div( Real s) {
310     for (unsigned int i = 0; i < Dim; i++)
311     this->data_[i] /= s;
312     }
313    
314     /**
315     * Sets the value of this vector to the scalar division of vector v1 (*this = v1 / s ).
316 tim 70 * @param v1 the source vector
317     * @param s the scalar value
318 gezelter 507 */
319     inline void div( const Vector<Real, Dim>& v1, Real s ) {
320     for (unsigned int i = 0; i < Dim; i++)
321     this->data_[i] = v1.data_[i] / s;
322 tim 70 }
323    
324 gezelter 1562 /**
325     * Sets the elements of this vector to the division of
326     * elements of two other vectors. Not to be confused with scalar
327     * division (div)
328     *
329     * (*this.data_[i] = v1.data_[i] / v2.data_[i]).
330     * @param v1 the first vector
331     * @param v2 the second vector
332     */
333     inline void Vdiv( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) {
334     for (unsigned int i = 0; i < Dim; i++)
335     this->data_[i] = v1.data_[i] / v2.data_[i];
336     }
337    
338    
339 gezelter 507 /** @see #add */
340     inline Vector<Real, Dim>& operator +=( const Vector<Real, Dim>& v1 ) {
341     add(v1);
342     return *this;
343     }
344 tim 93
345 gezelter 507 /** @see #sub */
346     inline Vector<Real, Dim>& operator -=( const Vector<Real, Dim>& v1 ) {
347     sub(v1);
348     return *this;
349 tim 70 }
350    
351 gezelter 507 /** @see #mul */
352     inline Vector<Real, Dim>& operator *=( Real s) {
353     mul(s);
354     return *this;
355 tim 70 }
356    
357 gezelter 507 /** @see #div */
358     inline Vector<Real, Dim>& operator /=( Real s ) {
359     div(s);
360     return *this;
361     }
362    
363 tim 70 /**
364 cli2 1349 * Returns the sum of all elements of this vector.
365     * @return the sum of all elements of this vector
366     */
367     inline Real sum() {
368     Real tmp;
369     tmp = 0;
370     for (unsigned int i = 0; i < Dim; i++)
371     tmp += this->data_[i];
372     return tmp;
373     }
374 gezelter 1562
375     /**
376     * Returns the product of all elements of this vector.
377     * @return the product of all elements of this vector
378     */
379     inline Real componentProduct() {
380     Real tmp;
381     tmp = 1;
382     for (unsigned int i = 0; i < Dim; i++)
383     tmp *= this->data_[i];
384     return tmp;
385     }
386 cli2 1349
387     /**
388 gezelter 507 * Returns the length of this vector.
389     * @return the length of this vector
390 tim 70 */
391 gezelter 507 inline Real length() {
392     return sqrt(lengthSquare());
393 tim 70 }
394 gezelter 507
395     /**
396     * Returns the squared length of this vector.
397     * @return the squared length of this vector
398     */
399     inline Real lengthSquare() {
400     return dot(*this, *this);
401     }
402    
403     /** Normalizes this vector in place */
404     inline void normalize() {
405     Real len;
406 tim 70
407 gezelter 507 len = length();
408    
409 gezelter 1390 //if (len < OpenMD::NumericConstant::epsilon)
410 gezelter 507 // throw();
411    
412     *this /= len;
413     }
414    
415 tim 70 /**
416 gezelter 507 * Tests if this vector is normalized
417     * @return true if this vector is normalized, otherwise return false
418 tim 70 */
419 gezelter 507 inline bool isNormalized() {
420 tim 963 return equal(lengthSquare(), (RealType)1);
421 gezelter 507 }
422 tim 886
423     unsigned int size() {return Dim;}
424 gezelter 507 protected:
425     Real data_[Dim];
426    
427     };
428 tim 93
429 gezelter 507 /** unary minus*/
430     template<typename Real, unsigned int Dim>
431     inline Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1){
432     Vector<Real, Dim> tmp(v1);
433     tmp.negate();
434     return tmp;
435     }
436    
437     /**
438     * Return the sum of two vectors (v1 - v2).
439     * @return the sum of two vectors
440     * @param v1 the first vector
441     * @param v2 the second vector
442     */
443     template<typename Real, unsigned int Dim>
444     inline Vector<Real, Dim> operator +(const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) {
445     Vector<Real, Dim> result;
446 tim 110
447 gezelter 507 result.add(v1, v2);
448     return result;
449     }
450 tim 110
451 gezelter 507 /**
452     * Return the difference of two vectors (v1 - v2).
453     * @return the difference of two vectors
454     * @param v1 the first vector
455     * @param v2 the second vector
456     */
457     template<typename Real, unsigned int Dim>
458     Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) {
459     Vector<Real, Dim> result;
460     result.sub(v1, v2);
461     return result;
462     }
463    
464     /**
465     * Returns the vaule of scalar multiplication of this vector v1 (v1 * r).
466     * @return the vaule of scalar multiplication of this vector
467     * @param v1 the source vector
468     * @param s the scalar value
469     */
470     template<typename Real, unsigned int Dim>
471     Vector<Real, Dim> operator * ( const Vector<Real, Dim>& v1, Real s) {
472     Vector<Real, Dim> result;
473     result.mul(v1,s);
474     return result;
475     }
476    
477     /**
478     * Returns the vaule of scalar multiplication of this vector v1 (v1 * r).
479     * @return the vaule of scalar multiplication of this vector
480     * @param s the scalar value
481     * @param v1 the source vector
482     */
483     template<typename Real, unsigned int Dim>
484     Vector<Real, Dim> operator * ( Real s, const Vector<Real, Dim>& v1 ) {
485     Vector<Real, Dim> result;
486     result.mul(v1, s);
487     return result;
488     }
489 tim 110
490 gezelter 507 /**
491     * Returns the value of division of a vector by a scalar.
492     * @return the vaule of scalar division of this vector
493     * @param v1 the source vector
494     * @param s the scalar value
495     */
496     template<typename Real, unsigned int Dim>
497     Vector<Real, Dim> operator / ( const Vector<Real, Dim>& v1, Real s) {
498     Vector<Real, Dim> result;
499     result.div( v1,s);
500     return result;
501     }
502    
503     /**
504     * Returns the dot product of two Vectors
505     * @param v1 first vector
506     * @param v2 second vector
507     * @return the dot product of v1 and v2
508     */
509     template<typename Real, unsigned int Dim>
510     inline Real dot( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
511     Real tmp;
512     tmp = 0;
513    
514     for (unsigned int i = 0; i < Dim; i++)
515     tmp += v1[i] * v2[i];
516    
517     return tmp;
518     }
519    
520 gezelter 1562
521    
522    
523 gezelter 507 /**
524 gezelter 1562 * Returns the wide dot product of three Vectors. Compare with
525     * Rapaport's VWDot function.
526     *
527     * @param v1 first vector
528     * @param v2 second vector
529     * @param v3 third vector
530     * @return the wide dot product of v1, v2, and v3.
531     */
532     template<typename Real, unsigned int Dim>
533     inline Real dot( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2, const Vector<Real, Dim>& v3 ) {
534     Real tmp;
535     tmp = 0;
536    
537     for (unsigned int i = 0; i < Dim; i++)
538     tmp += v1[i] * v2[i] * v3[i];
539    
540     return tmp;
541     }
542    
543    
544     /**
545 gezelter 507 * Returns the distance between two Vectors
546     * @param v1 first vector
547     * @param v2 second vector
548     * @return the distance between v1 and v2
549     */
550     template<typename Real, unsigned int Dim>
551     inline Real distance( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
552     Vector<Real, Dim> tempVector = v1 - v2;
553     return tempVector.length();
554     }
555    
556     /**
557     * Returns the squared distance between two Vectors
558     * @param v1 first vector
559     * @param v2 second vector
560     * @return the squared distance between v1 and v2
561     */
562     template<typename Real, unsigned int Dim>
563     inline Real distanceSquare( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
564     Vector<Real, Dim> tempVector = v1 - v2;
565     return tempVector.lengthSquare();
566     }
567    
568     /**
569     * Write to an output stream
570     */
571     template<typename Real, unsigned int Dim>
572     std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v) {
573    
574     o << "[ ";
575    
576     for (unsigned int i = 0 ; i< Dim; i++) {
577     o << v[i];
578    
579     if (i != Dim -1) {
580     o<< ", ";
581     }
582 tim 70 }
583 gezelter 507
584     o << " ]";
585     return o;
586     }
587 tim 70
588     }
589     #endif

Properties

Name Value
svn:keywords Author Id Revision Date