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 151 by tim, Mon Oct 25 22:46:19 2004 UTC vs.
Revision 1615 by gezelter, Fri Aug 26 17:55:44 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]  Vardeman & Gezelter, in progress (2009).                        
40   */
41 <
41 >
42   /**
43   * @file Vector.hpp
44   * @author Teng Lin
# Line 36 | Line 52
52   #include <cassert>
53   #include <cmath>
54   #include <iostream>
55 + #include <math.h>
56 + #include "config.h"
57 + namespace OpenMD {
58  
59 < namespace oopse {
59 >  static const RealType epsilon = 0.000001;
60  
61 <    const double epsilon = 0.000001;
61 >  template<typename T>
62 >  inline bool equal(T e1, T e2) {
63 >    return e1 == e2;
64 >  }
65  
66 <    template<typename T>
67 <    inline bool equal(T e1, T e2) {
68 <        return e1 == e2;
69 <    }
66 >  //template<>
67 >  //inline bool equal(float e1, float e2) {
68 >  //  return fabs(e1 - e2) < epsilon;
69 >  //}
70  
71 <    template<>
72 <    inline bool equal(float e1, float e2) {
73 <        return fabs(e1 - e2) < epsilon;
74 <    }
71 >  template<>
72 >  inline bool equal(RealType e1, RealType e2) {
73 >    return fabs(e1 - e2) < epsilon;
74 >  }
75  
54    template<>
55    inline bool equal(double e1, double e2) {
56        return fabs(e1 - e2) < epsilon;
57    }
58
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 <            typedef Real ElemType;
86 <            typedef Real* ElemPoinerType;
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 <                    data_[i] = 0.0;
92 <            }
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 <            }
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;
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 >      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 <                    data_[i] = v[i];
120 <            }
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 <            /**
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 data_[i];
130 <            }
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 <            /**
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 data_[i];
140 <            }
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 <             * Returns constant reference of ith element.
144 <             * @return reference of ith element
145 <             * @param i index
146 <             */
147 <            inline  const Real& operator[](unsigned int i) const {
148 <                assert( i < Dim);
149 <                return data_[i];
150 <            }
142 >    /**
143 >     * Returns constant reference of ith element.
144 >     * @return reference of ith element
145 >     * @param i index
146 >     */
147 >    inline  const Real& operator[](unsigned int i) const {
148 >      assert( i < Dim);
149 >      return this->data_[i];
150 >    }
151  
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 data_[i];
160 <            }
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 <            /** Copy the internal data to an array*/
163 <            void getArray(Real* array) {
164 <                for (unsigned int i = 0; i < Dim; i ++) {
165 <                    array[i] = data_[i];
166 <                }                
167 <            }
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 <            /** Returns the pointer of internal array */
170 <            Real* getArrayPointer() {
171 <                return data_;
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) {
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 <                for (unsigned int i = 0; i < Dim; i ++) {
182 <                    if (!equal(data_[i], v[i])) {
183 <                        return false;
184 <                    }
185 <                }
181 >      for (unsigned int i = 0; i < Dim; i ++) {
182 >        if (!equal(this->data_[i], v[i])) {
183 >          return false;
184 >        }
185 >      }
186                  
187 <                return true;
188 <            }
187 >      return true;
188 >    }
189  
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 <            }
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 <                    data_[i] = -data_[i];
203 <            }
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 <            * 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 <                    data_[i] = -v1.data_[i];
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 <            }
213 >    }
214              
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 <                    data_[i] += v1.data_[i];
222 <            }
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 <            * 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 <            inline void add( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
230 <                for (unsigned int i = 0; i < Dim; i++)
231 <                    data_[i] = v1.data_[i] + v2.data_[i];
232 <            }
224 >    /**
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 >    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 <            * 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 <                    data_[i] -= v1.data_[i];
241 <            }
234 >    /**
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 <            inline void sub( const Vector<Real, Dim>& v1, const Vector  &v2 ){
249 <                for (unsigned int i = 0; i < Dim; i++)
250 <                    data_[i] = v1.data_[i] - v2.data_[i];
251 <            }
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 >    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  
253 <            /**
254 <            * Sets the value of this vector to the scalar multiplication of itself (*this *= s).
255 <            * @param s the scalar value
256 <            */
257 <            inline void mul( Real s ) {
258 <                for (unsigned int i = 0; i < Dim; i++)
259 <                   data_[i] *= s;
260 <            }
253 >    /**
254 >     * Sets the value of this vector to the scalar multiplication of itself (*this *= s).
255 >     * @param s the scalar value
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 <                    data_[i] = s * v1.data_[i];
271 <            }
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  
256            /**
257            * Sets the value of this vector to the scalar division of itself  (*this /= s ).
258            * @param s the scalar value
259            */            
260            inline void div( Real s) {
261                for (unsigned int i = 0; i < Dim; i++)            
262                    data_[i] /= s;
263            }
264
265            /**
266            * Sets the value of this vector to the scalar division of vector v1  (*this = v1 / s ).
267            * @param v1 the source vector
268            * @param s the scalar value
269            */                        
270            inline void div( const Vector<Real, Dim>& v1, Real s ) {
271                for (unsigned int i = 0; i < Dim; i++)
272                    data_[i] = v1.data_[i] / s;
273            }
274
275            /** @see #add */
276            inline Vector<Real, Dim>& operator +=( const Vector<Real, Dim>& v1 ) {
277                add(v1);
278                return *this;
279            }
280
281            /** @see #sub */
282            inline Vector<Real, Dim>& operator -=( const Vector<Real, Dim>& v1 ) {
283                sub(v1);
284                return *this;
285            }
286
287            /** @see #mul */
288            inline Vector<Real, Dim>& operator *=( Real s) {
289                mul(s);
290                return *this;
291            }
292
293            /** @see #div */
294            inline Vector<Real, Dim>& operator /=( Real s ) {
295                div(s);
296                return *this;
297            }
298
299            /**
300             * Returns the length of this vector.
301             * @return the length of this vector
302             */
303             inline Real length() {
304                return sqrt(lengthSquare());  
305            }
306            
307            /**
308             * Returns the squared length of this vector.
309             * @return the squared length of this vector
310             */
311             inline Real lengthSquare() {
312                return dot(*this, *this);
313            }
314            
315            /** Normalizes this vector in place */
316            inline void normalize() {
317                Real len;
318
319                len = length();
320                
321                //if (len < oopse:epsilon)
322                //  throw();
323                
324                *this /= len;
325            }
326
327            /**
328             * Tests if this vector is normalized
329             * @return true if this vector is normalized, otherwise return false
330             */
331            inline bool isNormalized() {
332                return equal(lengthSquare(), 1.0);
333            }          
334            
335        protected:
336            Real data_[Dim];
337        
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<Real, Dim> tmp(v1);
344        tmp.negate();
345        return tmp;
346    }
347
273      /**
274 <     * Return the sum of two vectors  (v1 - v2).
275 <     * @return the sum of two vectors
276 <     * @param v1 the first vector
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 <    template<typename Real, unsigned int Dim>    
283 <    inline Vector<Real, Dim> operator +(const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) {
284 <        Vector<Real, Dim> result;
357 <        
358 <        result.add(v1, v2);
359 <        return result;        
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      /**
288 <     * Return the difference of two vectors  (v1 - v2).
364 <     * @return the difference of two vectors
365 <     * @param v1 the first vector
366 <     * @param v2 the second vector
367 <     */  
368 <    template<typename Real, unsigned int Dim>    
369 <    Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) {
370 <        Vector<Real, Dim> result;
371 <        result.sub(v1, v2);
372 <        return result;        
373 <    }
374 <    
375 <    /**
376 <     * Returns the vaule of scalar multiplication of this vector v1 (v1 * r).
377 <     * @return  the vaule of scalar multiplication of this vector
378 <     * @param v1 the source vector
288 >     * Sets the value of this vector to the scalar division of itself  (*this /= s ).
289       * @param s the scalar value
290 <     */
291 <    template<typename Real, unsigned int Dim>                
292 <    Vector<Real, Dim> operator * ( const Vector<Real, Dim>& v1, Real s) {      
293 <        Vector<Real, Dim> result;
384 <        result.mul(v1,s);
385 <        return result;          
290 >     */            
291 >    inline void div( Real s) {
292 >      for (unsigned int i = 0; i < Dim; i++)            
293 >        this->data_[i] /= s;
294      }
387    
388    /**
389     * Returns the vaule of scalar multiplication of this vector v1 (v1 * r).
390     * @return  the vaule of scalar multiplication of this vector
391     * @param s the scalar value
392     * @param v1 the source vector
393     */  
394    template<typename Real, unsigned int Dim>
395    Vector<Real, Dim> operator * ( Real s, const Vector<Real, Dim>& v1 ) {
396        Vector<Real, Dim> result;
397        result.mul(v1, s);
398        return result;          
399    }
295  
296      /**
297 <     * Returns the  value of division of a vector by a scalar.
403 <     * @return  the vaule of scalar division of this vector
297 >     * Sets the value of this vector to the scalar division of vector v1  (*this = v1 / s ).
298       * @param v1 the source vector
299       * @param s the scalar value
300 <     */
301 <    template<typename Real, unsigned int Dim>    
302 <    Vector<Real, Dim> operator / ( const Vector<Real, Dim>& v1, Real s) {      
303 <        Vector<Real, Dim> result;
410 <        result.div( v1,s);
411 <        return result;          
300 >     */                        
301 >    inline void div( const Vector<Real, Dim>& v1, Real s ) {
302 >      for (unsigned int i = 0; i < Dim; i++)
303 >        this->data_[i] = v1.data_[i] / s;
304      }
305 <    
305 >
306      /**
307 <     * Returns the dot product of two Vectors
308 <     * @param v1 first vector
309 <     * @param v2 second vector
310 <     * @return the dot product of v1 and v2
307 >     * Sets the elements of this vector to the division of
308 >     * elements of two other vectors.  Not to be confused with scalar
309 >     * division (div)
310 >     *
311 >     * (*this.data_[i] =  v1.data_[i] / v2.data_[i]).
312 >     * @param v1 the first vector            
313 >     * @param v2 the second vector
314       */
315 <    template<typename Real, unsigned int Dim>    
316 <    inline Real dot( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
317 <        Real tmp;
318 <        tmp = 0;
315 >    inline void Vdiv( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) {
316 >      for (unsigned int i = 0; i < Dim; i++)
317 >        this->data_[i] = v1.data_[i] / v2.data_[i];
318 >    }
319  
425        for (unsigned int i = 0; i < Dim; i++)
426            tmp += v1[i] * v2[i];
320  
321 <        return tmp;
321 >    /** @see #add */
322 >    inline Vector<Real, Dim>& operator +=( const Vector<Real, Dim>& v1 ) {
323 >      add(v1);
324 >      return *this;
325      }
326  
327 +    /** @see #sub */
328 +    inline Vector<Real, Dim>& operator -=( const Vector<Real, Dim>& v1 ) {
329 +      sub(v1);
330 +      return *this;
331 +    }
332 +
333 +    /** @see #mul */
334 +    inline Vector<Real, Dim>& operator *=( Real s) {
335 +      mul(s);
336 +      return *this;
337 +    }
338 +
339 +    /** @see #div */
340 +    inline Vector<Real, Dim>& operator /=( Real s ) {
341 +      div(s);
342 +      return *this;
343 +    }
344 +
345      /**
346 <     * Returns the distance between  two Vectors
347 <     * @param v1 first vector
348 <     * @param v2 second vector
349 <     * @return the distance between v1 and v2
350 <     */
351 <    template<typename Real, unsigned int Dim>    
352 <    inline Real distance( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
353 <        Vector<Real, Dim> tempVector = v1 - v2;
354 <        return tempVector.length();
346 >     * Returns the sum of all elements of this vector.
347 >     * @return the sum of all elements of this vector
348 >     */
349 >    inline Real sum() {
350 >      Real tmp;
351 >      tmp = 0;
352 >      for (unsigned int i = 0; i < Dim; i++)
353 >        tmp += this->data_[i];
354 >      return tmp;  
355      }
356  
357      /**
358 <     * Returns the squared distance between  two Vectors
359 <     * @param v1 first vector
446 <     * @param v2 second vector
447 <     * @return the squared distance between v1 and v2
358 >     * Returns the product of all elements of this vector.
359 >     * @return the product of all elements of this vector
360       */
361 <    template<typename Real, unsigned int Dim>
362 <    inline Real distanceSquare( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
363 <        Vector<Real, Dim> tempVector = v1 - v2;
364 <        return tempVector.lengthSquare();
361 >    inline Real componentProduct() {
362 >      Real tmp;
363 >      tmp = 1;
364 >      for (unsigned int i = 0; i < Dim; i++)
365 >        tmp *= this->data_[i];
366 >      return tmp;  
367      }
368 +            
369 +    /**
370 +     * Returns the length of this vector.
371 +     * @return the length of this vector
372 +     */
373 +    inline Real length() {
374 +      return sqrt(lengthSquare());  
375 +    }
376 +            
377 +    /**
378 +     * Returns the squared length of this vector.
379 +     * @return the squared length of this vector
380 +     */
381 +    inline Real lengthSquare() {
382 +      return dot(*this, *this);
383 +    }
384 +            
385 +    /** Normalizes this vector in place */
386 +    inline void normalize() {
387 +      Real len;
388  
389 +      len = length();
390 +                
391 +      //if (len < OpenMD::NumericConstant::epsilon)
392 +      //  throw();
393 +                
394 +      *this /= len;
395 +    }
396 +
397      /**
398 <     * Write to an output stream
398 >     * Tests if this vector is normalized
399 >     * @return true if this vector is normalized, otherwise return false
400       */
401 <    template<typename Real, unsigned int Dim>
402 <    std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v) {
401 >    inline bool isNormalized() {
402 >      return equal(lengthSquare(), (RealType)1);
403 >    }          
404  
405 <        o << "[ ";
405 >    unsigned int size() {return Dim;}
406 >  protected:
407 >    Real data_[Dim];
408          
409 <        for (unsigned int i = 0 ; i< Dim; i++) {
464 <            o << v[i];
409 >  };
410  
411 <            if (i  != Dim -1) {
412 <                o<< ", ";
413 <            }
414 <        }
411 >  /** unary minus*/
412 >  template<typename Real, unsigned int Dim>    
413 >  inline Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1){
414 >    Vector<Real, Dim> tmp(v1);
415 >    tmp.negate();
416 >    return tmp;
417 >  }
418  
419 <        o << " ]";
420 <        return o;        
419 >  /**
420 >   * Return the sum of two vectors  (v1 - v2).
421 >   * @return the sum of two vectors
422 >   * @param v1 the first vector
423 >   * @param v2 the second vector
424 >   */  
425 >  template<typename Real, unsigned int Dim>    
426 >  inline Vector<Real, Dim> operator +(const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) {
427 >    Vector<Real, Dim> result;
428 >        
429 >    result.add(v1, v2);
430 >    return result;        
431 >  }
432 >
433 >  /**
434 >   * Return the difference of two vectors  (v1 - v2).
435 >   * @return the difference of two vectors
436 >   * @param v1 the first vector
437 >   * @param v2 the second vector
438 >   */  
439 >  template<typename Real, unsigned int Dim>    
440 >  Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) {
441 >    Vector<Real, Dim> result;
442 >    result.sub(v1, v2);
443 >    return result;        
444 >  }
445 >    
446 >  /**
447 >   * Returns the vaule of scalar multiplication of this vector v1 (v1 * r).
448 >   * @return  the vaule of scalar multiplication of this vector
449 >   * @param v1 the source vector
450 >   * @param s the scalar value
451 >   */
452 >  template<typename Real, unsigned int Dim>                
453 >  Vector<Real, Dim> operator * ( const Vector<Real, Dim>& v1, Real s) {      
454 >    Vector<Real, Dim> result;
455 >    result.mul(v1,s);
456 >    return result;          
457 >  }
458 >    
459 >  /**
460 >   * Returns the vaule of scalar multiplication of this vector v1 (v1 * r).
461 >   * @return  the vaule of scalar multiplication of this vector
462 >   * @param s the scalar value
463 >   * @param v1 the source vector
464 >   */  
465 >  template<typename Real, unsigned int Dim>
466 >  Vector<Real, Dim> operator * ( Real s, const Vector<Real, Dim>& v1 ) {
467 >    Vector<Real, Dim> result;
468 >    result.mul(v1, s);
469 >    return result;          
470 >  }
471 >
472 >  /**
473 >   * Returns the  value of division of a vector by a scalar.
474 >   * @return  the vaule of scalar division of this vector
475 >   * @param v1 the source vector
476 >   * @param s the scalar value
477 >   */
478 >  template<typename Real, unsigned int Dim>    
479 >  Vector<Real, Dim> operator / ( const Vector<Real, Dim>& v1, Real s) {      
480 >    Vector<Real, Dim> result;
481 >    result.div( v1,s);
482 >    return result;          
483 >  }
484 >    
485 >  /**
486 >   * Returns the dot product of two Vectors
487 >   * @param v1 first vector
488 >   * @param v2 second vector
489 >   * @return the dot product of v1 and v2
490 >   */
491 >  template<typename Real, unsigned int Dim>    
492 >  inline Real dot( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
493 >    Real tmp;
494 >    tmp = 0;
495 >
496 >    for (unsigned int i = 0; i < Dim; i++)
497 >      tmp += v1[i] * v2[i];
498 >
499 >    return tmp;
500 >  }
501 >
502 >
503 >  
504 >
505 >  /**
506 >   * Returns the wide dot product of three Vectors.  Compare with
507 >   * Rapaport's VWDot function.
508 >   *
509 >   * @param v1 first vector
510 >   * @param v2 second vector
511 >   * @param v3 third vector
512 >   * @return the wide dot product of v1, v2, and v3.
513 >   */
514 >  template<typename Real, unsigned int Dim>    
515 >  inline Real dot( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2, const Vector<Real, Dim>& v3 ) {
516 >    Real tmp;
517 >    tmp = 0;
518 >
519 >    for (unsigned int i = 0; i < Dim; i++)
520 >      tmp += v1[i] * v2[i] * v3[i];
521 >
522 >    return tmp;
523 >  }
524 >
525 >
526 >  /**
527 >   * Returns the distance between  two Vectors
528 >   * @param v1 first vector
529 >   * @param v2 second vector
530 >   * @return the distance between v1 and v2
531 >   */  
532 >  template<typename Real, unsigned int Dim>    
533 >  inline Real distance( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
534 >    Vector<Real, Dim> tempVector = v1 - v2;
535 >    return tempVector.length();
536 >  }
537 >
538 >  /**
539 >   * Returns the squared distance between  two Vectors
540 >   * @param v1 first vector
541 >   * @param v2 second vector
542 >   * @return the squared distance between v1 and v2
543 >   */
544 >  template<typename Real, unsigned int Dim>
545 >  inline Real distanceSquare( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
546 >    Vector<Real, Dim> tempVector = v1 - v2;
547 >    return tempVector.lengthSquare();
548 >  }
549 >
550 >  /**
551 >   * Write to an output stream
552 >   */
553 >  template<typename Real, unsigned int Dim>
554 >  std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v) {
555 >
556 >    o << "[ ";
557 >        
558 >    for (unsigned int i = 0 ; i< Dim; i++) {
559 >      o << v[i];
560 >
561 >      if (i  != Dim -1) {
562 >        o<< ", ";
563 >      }
564      }
565 +
566 +    o << " ]";
567 +    return o;        
568 +  }
569      
570   }
571   #endif

Comparing trunk/src/math/Vector.hpp (property svn:keywords):
Revision 151 by tim, Mon Oct 25 22:46:19 2004 UTC vs.
Revision 1615 by gezelter, Fri Aug 26 17:55:44 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines