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

Comparing:
trunk/src/math/Vector.hpp (property svn:keywords), Revision 137 by tim, Thu Oct 21 21:31:39 2004 UTC vs.
branches/development/src/math/Vector.hpp (property svn:keywords), Revision 1760 by gezelter, Thu Jun 21 19:26:46 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines