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

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

Comparing trunk/src/math/Vector.hpp (property svn:keywords):
Revision 110 by tim, Tue Oct 19 21:28:55 2004 UTC vs.
Revision 1879 by gezelter, Sun Jun 16 15:15:42 2013 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines