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