ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/math/Vector.hpp
(Generate patch)

Comparing:
trunk/src/math/Vector.hpp (file contents), Revision 76 by tim, Thu Oct 14 23:28:09 2004 UTC vs.
branches/development/src/math/Vector.hpp (file contents), Revision 1665 by gezelter, Tue Nov 22 20:38:56 2011 UTC

# Line 1 | Line 1
1   /*
2 < * Copyright (C) 2000-2004  Object Oriented Parallel Simulation Engine (OOPSE) project
3 < *
4 < * Contact: oopse@oopse.org
5 < *
6 < * This program is free software; you can redistribute it and/or
7 < * modify it under the terms of the GNU Lesser General Public License
8 < * as published by the Free Software Foundation; either version 2.1
9 < * of the License, or (at your option) any later version.
10 < * All we ask is that proper credit is given for our work, which includes
11 < * - but is not limited to - adding the above copyright notice to the beginning
12 < * of your source code files, and to any copyright notice that you may distribute
13 < * with programs based on this work.
14 < *
15 < * This program is distributed in the hope that it will be useful,
16 < * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 < * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 < * GNU Lesser General Public License for more details.
19 < *
20 < * You should have received a copy of the GNU Lesser General Public License
21 < * along with this program; if not, write to the Free Software
22 < * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010).
40 + * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41   */
42 <
42 >
43   /**
44   * @file Vector.hpp
45   * @author Teng Lin
# Line 36 | Line 53
53   #include <cassert>
54   #include <cmath>
55   #include <iostream>
56 + #include <math.h>
57 + #include "config.h"
58 + namespace OpenMD {
59  
60 < namespace oopse {
60 >  static const RealType epsilon = 0.000001;
61  
62 <    const double epsilon = 0.000001;
62 >  template<typename T>
63 >  inline bool equal(T e1, T e2) {
64 >    return e1 == e2;
65 >  }
66  
67 <    template<typename T>
68 <    inline bool equal(T e1, T e2) {
69 <        return e1 == e2;
70 <    }
67 >  //template<>
68 >  //inline bool equal(float e1, float e2) {
69 >  //  return fabs(e1 - e2) < epsilon;
70 >  //}
71  
72 <    template<>
73 <    inline bool equal(float e1, float e2) {
74 <        return fabs(e1 - e2) < epsilon;
75 <    }
72 >  template<>
73 >  inline bool equal(RealType e1, RealType e2) {
74 >    return fabs(e1 - e2) < epsilon;
75 >  }
76  
54    template<>
55    inline bool equal(double e1, double e2) {
56        return fabs(e1 - e2) < epsilon;
57    }
77      
78 <    /**
79 <     * @class Vector Vector.hpp "math/Vector.hpp"
80 <     * @brief Fix length vector class
81 <     */
82 <    template<typename Real, unsigned int Dim>
83 <    class Vector{
84 <        public:
78 >  /**
79 >   * @class Vector Vector.hpp "math/Vector.hpp"
80 >   * @brief Fix length vector class
81 >   */
82 >  template<typename Real, unsigned int Dim>
83 >  class Vector{
84 >  public:
85  
86 <            /** default constructor */
87 <            inline Vector(){
69 <                for (unsigned int i = 0; i < Dim; i++)
70 <                    data_[i] = 0.0;
71 <            }
86 >    typedef Real ElemType;
87 >    typedef Real* ElemPoinerType;
88  
89 <            /** Constructs and initializes a Vector from a vector */
90 <            inline Vector(const Vector<Real, Dim>& v) {
91 <                *this  = v;
92 <            }
89 >    /** default constructor */
90 >    inline Vector(){
91 >      for (unsigned int i = 0; i < Dim; i++)
92 >        this->data_[i] = 0;
93 >    }
94  
95 <            /** copy assignment operator */
96 <            inline Vector<Real, Dim>& operator=(const Vector<Real, Dim>& v) {
97 <                if (this == &v)
98 <                    return *this;
95 >    /** Constructs and initializes a Vector from a vector */
96 >    inline Vector(const Vector<Real, Dim>& v) {
97 >      *this  = v;
98 >    }
99 >
100 >    /** copy assignment operator */
101 >    inline Vector<Real, Dim>& operator=(const Vector<Real, Dim>& v) {
102 >      if (this == &v)
103 >        return *this;
104                  
105 <                for (unsigned int i = 0; i < Dim; i++)            
106 <                    data_[i] = v[i];
105 >      for (unsigned int i = 0; i < Dim; i++)            
106 >        this->data_[i] = v[i];
107                  
108 <                return *this;
109 <            }
108 >      return *this;
109 >    }
110 >
111 >    template<typename T>
112 >    inline Vector(const T& s){
113 >      for (unsigned int i = 0; i < Dim; i++)
114 >        this->data_[i] = s;
115 >    }
116              
117 <            /** Constructs and initializes a Vector from an array */            
118 <            inline Vector( double* v) {
119 <                for (unsigned int i = 0; i < Dim; i++)
120 <                    data_[i] = v[i];
121 <            }
117 >    /** 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  
123 <            /**
124 <             * Returns reference of ith element.
125 <             * @return reference of ith element
126 <             * @param i index
127 <             */
128 <            inline double& operator[](unsigned int  i) {
129 <                assert( i < Dim);
130 <                return data_[i];
131 <            }
123 >    /**
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  
133 <            /**
134 <             * Returns reference of ith element.
135 <             * @return reference of ith element
136 <             * @param i index
137 <             */
138 <            inline double& operator()(unsigned int  i) {
139 <                assert( i < Dim);
140 <                return data_[i];
141 <            }
133 >    /**
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  
143 <            /**
144 <             * Returns constant reference of ith element.
145 <             * @return reference of ith element
146 <             * @param i index
147 <             */
148 <            inline  const double& operator[](unsigned int i) const {
149 <                assert( i < Dim);
150 <                return data_[i];
151 <            }
143 >    /**
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  
153 <            /**
154 <             * Returns constant reference of ith element.
155 <             * @return reference of ith element
156 <             * @param i index
157 <             */
158 <            inline  const double& operator()(unsigned int i) const {
159 <                assert( i < Dim);
160 <                return data_[i];
161 <            }
162 <
163 <            /**
164 <             * Tests if this vetor is equal to other vector
165 <             * @return true if equal, otherwise return false
166 <             * @param v vector to be compared
167 <             */
168 <             inline bool operator ==(const Vector<Real, Dim>& v) {
169 <
170 <                for (unsigned int i = 0; i < Dim; i ++) {
171 <                    if (!equal(data_[i], v[i])) {
172 <                        return false;
173 <                    }
174 <                }
153 >    /**
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 >
163 >    /** 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 >
170 >    /** Returns the pointer of internal array */
171 >    Real* getArrayPointer() {
172 >      return this->data_;
173 >    }
174 >            
175 >    /**
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 >
182 >      for (unsigned int i = 0; i < Dim; i ++) {
183 >        if (!equal(this->data_[i], v[i])) {
184 >          return false;
185 >        }
186 >      }
187                  
188 <                return true;
189 <            }
188 >      return true;
189 >    }
190  
191 <            /**
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 <            }
191 >    /**
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              
200 <            /** Negates the value of this vector in place. */          
201 <            inline void negate() {
202 <                data_[0] = -data_[0];
203 <                data_[1] = -data_[1];
204 <                data_[2] = -data_[2];
165 <            }
200 >    /** 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  
206 <            /**
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 <                    data_[i] = -v1.data_[i];
206 >    /**
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  
214 <            }
214 >    }
215              
216 <            /**
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 <                    data_[i] += v1.data_[i];
223 <                }
216 >    /**
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 >    }
224  
225 <            /**
226 <            * Sets the value of this vector to the sum of v1 and v2 (*this = v1 + v2).
227 <            * @param v1 the first vector
228 <            * @param v2 the second vector
229 <            */
230 <            inline void add( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
231 <                for (unsigned int i = 0; i < Dim; i++)
232 <                    data_[i] = v1.data_[i] + v2.data_[i];
233 <            }
225 >    /**
226 >     * Sets the value of this vector to the sum of v1 and v2 (*this = v1 + v2).
227 >     * @param v1 the first vector
228 >     * @param v2 the second vector
229 >     */
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 >    }
234  
235 <            /**
236 <            * 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 <                    data_[i] -= v1.data_[i];
242 <            }
235 >    /**
236 >     * 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 <            * @param v1 the first vector
247 <            * @param v2 the second vector
248 <            */
249 <            inline void sub( const Vector<Real, Dim>& v1, const Vector  &v2 ){
250 <                for (unsigned int i = 0; i < Dim; i++)
251 <                    data_[i] = v1.data_[i] - v2.data_[i];
252 <            }
244 >    /**
245 >     * Sets the value of this vector to the difference of vector v1 and v2 (*this = v1 - v2).
246 >     * @param v1 the first vector
247 >     * @param v2 the second vector
248 >     */
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 >    }
253  
254 <            /**
255 <            * Sets the value of this vector to the scalar multiplication of itself (*this *= s).
256 <            * @param s the scalar value
257 <            */
258 <            inline void mul( double s ) {
259 <                for (unsigned int i = 0; i < Dim; i++)
260 <                   data_[i] *= s;
261 <            }
254 >    /**
255 >     * Sets the value of this vector to the scalar multiplication of itself (*this *= s).
256 >     * @param s the scalar value
257 >     */
258 >    inline void mul( Real s ) {
259 >      for (unsigned int i = 0; i < Dim; i++)
260 >        this->data_[i] *= s;
261 >    }
262  
263 <            /**
264 <            * Sets the value of this vector to the scalar multiplication of vector v1  
265 <            * (*this = s * v1).
266 <            * @param s the scalar value
267 <            * @param v1 the vector
268 <            */
269 <            inline void mul( double s, const Vector<Real, Dim>& v1 ) {
270 <                for (unsigned int i = 0; i < Dim; i++)
271 <                    data_[i] = s * v1.data_[i];
272 <            }
263 >    /**
264 >     * Sets the value of this vector to the scalar multiplication of vector v1  
265 >     * (*this = s * v1).
266 >     * @param v1 the vector            
267 >     * @param s the scalar value
268 >     */
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 >    }
273  
274 <            /**
275 <            * Sets the value of this vector to the scalar division of itself  (*this /= s ).
276 <            * @param s the scalar value
277 <            */            
278 <            inline void div( double s) {
279 <                for (unsigned int i = 0; i < Dim; i++)            
280 <                    data_[i] /= s;
281 <            }
274 >    /**
275 >     * 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 <            /**
289 <            * Sets the value of this vector to the scalar division of vector v1  (*this = v1 / s ).
290 <            * @param v1 the source vector
291 <            * @param s the scalar value
292 <            */                        
293 <            inline void div( const Vector<Real, Dim>& v1, double s ) {
294 <                for (unsigned int i = 0; i < Dim; i++)
295 <                    data_[i] = v1.data_[i] / s;
252 <            }
288 >    /**
289 >     * Sets the value of this vector to the scalar division of itself  (*this /= s ).
290 >     * @param s the scalar value
291 >     */            
292 >    inline void div( Real s) {
293 >      for (unsigned int i = 0; i < Dim; i++)            
294 >        this->data_[i] /= s;
295 >    }
296  
297 <            /** @see #add */
298 <            inline Vector<Real, Dim>& operator +=( const Vector<Real, Dim>& v1 ) {
299 <                add(v1);
300 <                return *this;
301 <            }
297 >    /**
298 >     * Sets the value of this vector to the scalar division of vector v1  (*this = v1 / s ).
299 >     * @param v1 the source vector
300 >     * @param s the scalar value
301 >     */                        
302 >    inline void div( const Vector<Real, Dim>& v1, Real s ) {
303 >      for (unsigned int i = 0; i < Dim; i++)
304 >        this->data_[i] = v1.data_[i] / s;
305 >    }
306  
307 <            /** @see #sub */
308 <            inline Vector<Real, Dim>& operator -=( const Vector<Real, Dim>& v1 ) {
309 <                sub(v1);
310 <                return *this;
311 <            }
307 >    /**
308 >     * Sets the elements of this vector to the division of
309 >     * elements of two other vectors.  Not to be confused with scalar
310 >     * division (div)
311 >     *
312 >     * (*this.data_[i] =  v1.data_[i] / v2.data_[i]).
313 >     * @param v1 the first vector            
314 >     * @param v2 the second vector
315 >     */
316 >    inline void Vdiv( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) {
317 >      for (unsigned int i = 0; i < Dim; i++)
318 >        this->data_[i] = v1.data_[i] / v2.data_[i];
319 >    }
320  
266            /** @see #mul */
267            inline Vector<Real, Dim>& operator *=( double s) {
268                mul(s);
269                return *this;
270            }
321  
322 <            /** @see #div */
323 <            inline Vector<Real, Dim>& operator /=( double s ) {
324 <                div(s);
325 <                return *this;
326 <            }
322 >    /** @see #add */
323 >    inline Vector<Real, Dim>& operator +=( const Vector<Real, Dim>& v1 ) {
324 >      add(v1);
325 >      return *this;
326 >    }
327  
328 <            /**
329 <             * Returns the length of this vector.
330 <             * @return the length of this vector
331 <             */
332 <             inline double length() {
283 <                return sqrt(lengthSquared());  
284 <            }
285 <            
286 <            /**
287 <             * Returns the squared length of this vector.
288 <             * @return the squared length of this vector
289 <             */
290 <             inline double lengthSquared() {
291 <                return dot(*this, *this);
292 <            }
293 <            
294 <            /** Normalizes this vector in place */
295 <            inline void normalize() {
296 <                double len;
328 >    /** @see #sub */
329 >    inline Vector<Real, Dim>& operator -=( const Vector<Real, Dim>& v1 ) {
330 >      sub(v1);
331 >      return *this;
332 >    }
333  
334 <                len = length();
335 <                *this /= len;
336 <            }
337 <            
338 <        protected:
303 <            double data_[3];
304 <        
305 <    };
334 >    /** @see #mul */
335 >    inline Vector<Real, Dim>& operator *=( Real s) {
336 >      mul(s);
337 >      return *this;
338 >    }
339  
340 <    /** unary minus*/
341 <    template<typename Real, unsigned int Dim>    
342 <    inline Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1){
343 <        Vector tmp(v1);
311 <        return tmp.negate();
340 >    /** @see #div */
341 >    inline Vector<Real, Dim>& operator /=( Real s ) {
342 >      div(s);
343 >      return *this;
344      }
345  
346      /**
347 <     * Return the sum of two vectors  (v1 - v2).
348 <     * @return the sum of two vectors
349 <     * @param v1 the first vector
350 <     * @param v2 the second vector
351 <     */  
352 <    template<typename Real, unsigned int Dim>    
353 <    inline Vector<Real, Dim> operator +(const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) {
354 <        Vector<Real, Dim> result;
355 <        
324 <        result.add(v1, v2);
325 <        return result;        
347 >     * Returns the sum of all elements of this vector.
348 >     * @return the sum of all elements of this vector
349 >     */
350 >    inline Real sum() {
351 >      Real tmp;
352 >      tmp = 0;
353 >      for (unsigned int i = 0; i < Dim; i++)
354 >        tmp += this->data_[i];
355 >      return tmp;  
356      }
357  
358      /**
359 <     * Return the difference of two vectors  (v1 - v2).
360 <     * @return the difference of two vectors
361 <     * @param v1 the first vector
362 <     * @param v2 the second vector
363 <     */  
364 <    template<typename Real, unsigned int Dim>    
365 <    Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) {
366 <        Vector<Real, Dim> result;
367 <        result.sub(v1, v2);
338 <        return result;        
359 >     * Returns the product of all elements of this vector.
360 >     * @return the product of all elements of this vector
361 >     */
362 >    inline Real componentProduct() {
363 >      Real tmp;
364 >      tmp = 1;
365 >      for (unsigned int i = 0; i < Dim; i++)
366 >        tmp *= this->data_[i];
367 >      return tmp;  
368      }
369 <    
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 v1 the source vector
374 <     * @param s the scalar value
375 <     */
347 <    template<typename Real, unsigned int Dim>                
348 <    Vector<Real, Dim> operator * ( const Vector<Real, Dim>& v1, double s) {      
349 <        Vector<Real, Dim> result;
350 <        result.mul(s, v1);
351 <        return result;          
371 >     * Returns the length of this vector.
372 >     * @return the length of this vector
373 >     */
374 >    inline Real length() {
375 >      return sqrt(lengthSquare());  
376      }
377 <    
377 >            
378      /**
379 <     * Returns the vaule of scalar multiplication of this vector v1 (v1 * r).
380 <     * @return  the vaule of scalar multiplication of this vector
381 <     * @param s the scalar value
382 <     * @param v1 the source vector
383 <     */  
360 <    template<typename Real, unsigned int Dim>
361 <    Vector<Real, Dim> operator * ( double s, const Vector<Real, Dim>& v1 ) {
362 <        Vector<Real, Dim> result;
363 <        result.mul(s, v1);
364 <        return result;          
379 >     * Returns the squared length of this vector.
380 >     * @return the squared length of this vector
381 >     */
382 >    inline Real lengthSquare() {
383 >      return dot(*this, *this);
384      }
385 +            
386 +    /** Normalizes this vector in place */
387 +    inline void normalize() {
388 +      Real len;
389  
390 <    /**
391 <     * Returns the  value of division of a vector by a scalar.
392 <     * @return  the vaule of scalar division of this vector
393 <     * @param v1 the source vector
394 <     * @param s the scalar value
395 <     */
373 <    template<typename Real, unsigned int Dim>    
374 <    Vector<Real, Dim> operator / ( const Vector<Real, Dim>& v1, double s) {      
375 <        Vector<Real, Dim> result;
376 <        result.div( v1,s);
377 <        return result;          
390 >      len = length();
391 >                
392 >      //if (len < OpenMD::NumericConstant::epsilon)
393 >      //  throw();
394 >                
395 >      *this /= len;
396      }
397 <    
397 >
398      /**
399 <     * Returns the  value of division of a vector by a scalar.
400 <     * @return  the vaule of scalar division of this vector
383 <     * @param s the scalar value
384 <     * @param v1 the source vector
399 >     * Tests if this vector is normalized
400 >     * @return true if this vector is normalized, otherwise return false
401       */
402 <    template<typename Real, unsigned int Dim>        
403 <    inline Vector<Real, Dim> operator /( double s, const Vector<Real, Dim>& v1 ) {
404 <        Vector<Real, Dim> result;
389 <        result.div( v1,s);
390 <        return result;          
391 <    }
402 >    inline bool isNormalized() {
403 >      return equal(lengthSquare(), (RealType)1);
404 >    }          
405  
406 <    /** fuzzy comparson */
407 <    template<typename Real, unsigned int Dim>        
408 <    inline bool epsilonEqual( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
406 >    unsigned int size() {return Dim;}
407 >  protected:
408 >    Real data_[Dim];
409 >        
410 >  };
411  
412 <    }
412 >  /** unary minus*/
413 >  template<typename Real, unsigned int Dim>    
414 >  inline Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1){
415 >    Vector<Real, Dim> tmp(v1);
416 >    tmp.negate();
417 >    return tmp;
418 >  }
419  
420 +  /**
421 +   * Return the sum of two vectors  (v1 - v2).
422 +   * @return the sum of two vectors
423 +   * @param v1 the first vector
424 +   * @param v2 the second vector
425 +   */  
426 +  template<typename Real, unsigned int Dim>    
427 +  inline Vector<Real, Dim> operator +(const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) {
428 +    Vector<Real, Dim> result;
429 +        
430 +    result.add(v1, v2);
431 +    return result;        
432 +  }
433 +
434 +  /**
435 +   * Return the difference of two vectors  (v1 - v2).
436 +   * @return the difference of two vectors
437 +   * @param v1 the first vector
438 +   * @param v2 the second vector
439 +   */  
440 +  template<typename Real, unsigned int Dim>    
441 +  Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) {
442 +    Vector<Real, Dim> result;
443 +    result.sub(v1, v2);
444 +    return result;        
445 +  }
446      
447 <    /**
448 <     * Returns the dot product of two Vectors
449 <     * @param v1 first vector
450 <     * @param v2 second vector
451 <     * @return the dot product of v1 and v2
452 <     */
453 <    template<typename Real, unsigned int Dim>    
454 <    inline Real dot( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
455 <                Real tmp;
456 <                tmp = 0;
447 >  /**
448 >   * Returns the vaule of scalar multiplication of this vector v1 (v1 * r).
449 >   * @return  the vaule of scalar multiplication of this vector
450 >   * @param v1 the source vector
451 >   * @param s the scalar value
452 >   */
453 >  template<typename Real, unsigned int Dim>                
454 >  Vector<Real, Dim> operator * ( const Vector<Real, Dim>& v1, Real s) {      
455 >    Vector<Real, Dim> result;
456 >    result.mul(v1,s);
457 >    return result;          
458 >  }
459 >    
460 >  /**
461 >   * Returns the vaule of scalar multiplication of this vector v1 (v1 * r).
462 >   * @return  the vaule of scalar multiplication of this vector
463 >   * @param s the scalar value
464 >   * @param v1 the source vector
465 >   */  
466 >  template<typename Real, unsigned int Dim>
467 >  Vector<Real, Dim> operator * ( Real s, const Vector<Real, Dim>& v1 ) {
468 >    Vector<Real, Dim> result;
469 >    result.mul(v1, s);
470 >    return result;          
471 >  }
472  
473 <                for (unsigned int i = 0; i < Dim; i++)
474 <                        tmp += v1[i] + v2[i];
475 <                
476 <                return tmp;
477 <    }
473 >  /**
474 >   * Returns the  value of division of a vector by a scalar.
475 >   * @return  the vaule of scalar division of this vector
476 >   * @param v1 the source vector
477 >   * @param s the scalar value
478 >   */
479 >  template<typename Real, unsigned int Dim>    
480 >  Vector<Real, Dim> operator / ( const Vector<Real, Dim>& v1, Real s) {      
481 >    Vector<Real, Dim> result;
482 >    result.div( v1,s);
483 >    return result;          
484 >  }
485 >    
486 >  /**
487 >   * Returns the dot product of two Vectors
488 >   * @param v1 first vector
489 >   * @param v2 second vector
490 >   * @return the dot product of v1 and v2
491 >   */
492 >  template<typename Real, unsigned int Dim>    
493 >  inline Real dot( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
494 >    Real tmp;
495 >    tmp = 0;
496  
497 <    /**
498 <     * Returns the distance between  two Vectors
419 <     * @param v1 first vector
420 <     * @param v2 second vector
421 <     * @return the distance between v1 and v2
422 <     */
423 <    template<typename Real, unsigned int Dim>    
424 <    inline Real distance( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
425 <        Vector<Real, Dim> tempVector = v1 - v2;
426 <        return tempVector.length();
427 <    }
497 >    for (unsigned int i = 0; i < Dim; i++)
498 >      tmp += v1[i] * v2[i];
499  
500 <    /**
501 <     * Returns the squared distance between  two Vectors
431 <     * @param v1 first vector
432 <     * @param v2 second vector
433 <     * @return the squared distance between v1 and v2
434 <     */
435 <    template<typename Real, unsigned int Dim>
436 <    inline Real distanceSquare( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
437 <        Vector<Real, Dim> tempVector = v1 - v2;
438 <        return tempVector.lengthSquare();
439 <    }
500 >    return tmp;
501 >  }
502  
503 <    /**
504 <     * Write to an output stream
505 <     */
506 <    template<typename Real, unsigned int Dim>
507 <    std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v1 ) {
503 >
504 >  
505 >
506 >  /**
507 >   * Returns the wide dot product of three Vectors.  Compare with
508 >   * Rapaport's VWDot function.
509 >   *
510 >   * @param v1 first vector
511 >   * @param v2 second vector
512 >   * @param v3 third vector
513 >   * @return the wide dot product of v1, v2, and v3.
514 >   */
515 >  template<typename Real, unsigned int Dim>    
516 >  inline Real dot( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2, const Vector<Real, Dim>& v3 ) {
517 >    Real tmp;
518 >    tmp = 0;
519 >
520 >    for (unsigned int i = 0; i < Dim; i++)
521 >      tmp += v1[i] * v2[i] * v3[i];
522 >
523 >    return tmp;
524 >  }
525 >
526 >
527 >  /**
528 >   * Returns the distance between  two Vectors
529 >   * @param v1 first vector
530 >   * @param v2 second vector
531 >   * @return the distance between v1 and v2
532 >   */  
533 >  template<typename Real, unsigned int Dim>    
534 >  inline Real distance( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
535 >    Vector<Real, Dim> tempVector = v1 - v2;
536 >    return tempVector.length();
537 >  }
538 >
539 >  /**
540 >   * Returns the squared distance between  two Vectors
541 >   * @param v1 first vector
542 >   * @param v2 second vector
543 >   * @return the squared distance between v1 and v2
544 >   */
545 >  template<typename Real, unsigned int Dim>
546 >  inline Real distanceSquare( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
547 >    Vector<Real, Dim> tempVector = v1 - v2;
548 >    return tempVector.lengthSquare();
549 >  }
550 >
551 >  /**
552 >   * Write to an output stream
553 >   */
554 >  template<typename Real, unsigned int Dim>
555 >  std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v) {
556 >
557 >    o << "[ ";
558          
559 <        return o;        
559 >    for (unsigned int i = 0 ; i< Dim; i++) {
560 >      o << v[i];
561 >
562 >      if (i  != Dim -1) {
563 >        o<< ", ";
564 >      }
565      }
566 +
567 +    o << " ]";
568 +    return o;        
569 +  }
570      
571   }
572   #endif

Comparing:
trunk/src/math/Vector.hpp (property svn:keywords), Revision 76 by tim, Thu Oct 14 23:28:09 2004 UTC vs.
branches/development/src/math/Vector.hpp (property svn:keywords), Revision 1665 by gezelter, Tue Nov 22 20:38:56 2011 UTC

# Line 0 | Line 1
1 + Author Id Revision Date

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines