All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Vector.h
Go to the documentation of this file.
1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2  Copyright (c) 2013 The plumed team
3  (see the PEOPLE file at the root of the distribution for a list of names)
4 
5  See http://www.plumed-code.org for more information.
6 
7  This file is part of plumed, version 2.0.
8 
9  plumed is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  plumed is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 #ifndef __PLUMED_tools_Vector_h
23 #define __PLUMED_tools_Vector_h
24 
25 #include <cmath>
26 
27 namespace PLMD{
28 
29 /**
30 \ingroup TOOLBOX
31 Class implementing fixed size vectors of doubles
32 
33 \tparam n The number of elements of the vector.
34 
35 This class implements a vector of doubles with size fixed at
36 compile time. It is useful for small fixed size objects (e.g.
37 3d vectors) as it does not waste space to store the vector size.
38 Moreover, as the compiler knows the size, it can be completely
39 opimized inline.
40 All the methods are inlined for better optimization.
41 Vector elements are initialized to zero by default. Notice that
42 this means that constructor is a bit slow. This point might change
43 in future if we find performance issues.
44 Accepts both [] and () syntax for access.
45 Several functions are declared as friends even if not necessary so as to
46 properly appear in Doxygen documentation.
47 
48 Aliases are defined to simplify common declarations (Vector, Vector2d, Vector3d, Vector4d).
49 Also notice that some operations are only available for 3 dimensional vectors.
50 
51 Example of usage
52 \verbatim
53 #include "Vector.h"
54 
55 using namespace PLMD;
56 
57 int main(){
58  VectorGeneric<3> v1;
59  v1[0]=3.0;
60 // use equivalently () and [] syntax:
61  v1(1)=5.0;
62 // initialize with components
63  VectorGeneric<3> v2=VectorGeneric<3>(1.0,2.0,3.0);
64  VectorGeneric<3> v3=crossProduct(v1,v2);
65  double d=dotProduct(v1,v2);
66  v3+=v1;
67  v2=v1+2.0*v3;
68 }
69 \endverbatim
70 
71 */
72 
73 template <unsigned n>
75  double d[n];
76 public:
77 /// Create it with preassigned components.
78 /// Only available for sizes 2, 3 and 4
79  VectorGeneric(double,double);
80  VectorGeneric(double,double,double);
81  VectorGeneric(double,double,double,double);
82 /// create it null
83  VectorGeneric();
84 /// set it to zero
85  void zero();
86 /// array-like access [i]
87  double & operator[](unsigned i);
88 /// array-like access [i]
89  const double & operator[](unsigned i)const;
90 /// parenthesis access (i)
91  double & operator()(unsigned i);
92 /// parenthesis access (i)
93  const double & operator()(unsigned i)const;
94 /// increment
96 /// decrement
98 /// multiply
99  VectorGeneric& operator *=(double s);
100 /// divide
101  VectorGeneric& operator /=(double s);
102 /// sign +
103  VectorGeneric operator +()const;
104 /// sign -
105  VectorGeneric operator -()const;
106 /// return v1+v2
107  template<unsigned m>
109 /// return v1-v2
110  template<unsigned m>
112 /// return s*v
113  template<unsigned m>
114  friend VectorGeneric<m> operator*(double,const VectorGeneric<m>&);
115 /// return v*s
116  template<unsigned m>
117  friend VectorGeneric<m> operator*(const VectorGeneric<m>&,double);
118 /// return v/s
119  template<unsigned m>
120  friend VectorGeneric<m> operator/(const VectorGeneric<m>&,double);
121 /// return v2-v1
122  template<unsigned m>
123  friend VectorGeneric<m> delta(const VectorGeneric<m>&v1,const VectorGeneric<m>&v2);
124 /// return v1 .scalar. v2
125  template<unsigned m>
126  friend double dotProduct(const VectorGeneric<m>&,const VectorGeneric<m>&);
127 /// return v1 .vector. v2
128 /// Only available for size 3
130 /// compute the squared modulo
131  double modulo2()const;
132 /// Compute the modulo.
133 /// Shortcut for sqrt(v.modulo2())
134  double modulo()const;
135 /// friend version of modulo2 (to simplify some syntax)
136  template<unsigned m>
137  friend double modulo2(const VectorGeneric<m>&);
138 /// friend version of modulo (to simplify some syntax)
139  template<unsigned m>
140  friend double modulo(const VectorGeneric<m>&);
141 };
142 
143 template<>
144 inline
145 VectorGeneric<2>:: VectorGeneric(double x0,double x1){
146  d[0]=x0;
147  d[1]=x1;
148 }
149 
150 template<>
151 inline
152 VectorGeneric<3>:: VectorGeneric(double x0,double x1,double x2){
153  d[0]=x0;
154  d[1]=x1;
155  d[2]=x2;
156 }
157 
158 template<>
159 inline
160 VectorGeneric<4>:: VectorGeneric(double x0,double x1,double x2,double x3){
161  d[0]=x0;
162  d[1]=x1;
163  d[2]=x2;
164  d[3]=x3;
165 }
166 
167 template <unsigned n>
169  for(unsigned i=0;i<n;i++) d[i]=0.0;
170 }
171 
172 template <unsigned n>
174  for(unsigned i=0;i<n;i++) d[i]=0.0;
175 }
176 
177 template <unsigned n>
178 double & VectorGeneric<n>::operator[](unsigned i){
179  return d[i];
180 }
181 
182 template <unsigned n>
183 const double & VectorGeneric<n>::operator[](unsigned i)const{
184  return d[i];
185 }
186 
187 template <unsigned n>
188 double & VectorGeneric<n>::operator()(unsigned i){
189  return d[i];
190 }
191 
192 template <unsigned n>
193 const double & VectorGeneric<n>::operator()(unsigned i)const{
194  return d[i];
195 }
196 
197 template <unsigned n>
199  for(unsigned i=0;i<n;i++) d[i]+=b.d[i];
200  return *this;
201 }
202 
203 template <unsigned n>
205  for(unsigned i=0;i<n;i++) d[i]-=b.d[i];
206  return *this;
207 }
208 
209 template <unsigned n>
211  for(unsigned i=0;i<n;i++) d[i]*=s;
212  return *this;
213 }
214 
215 template <unsigned n>
217  double x=1.0/s;
218  return (*this)*=x;
219 }
220 
221 template <unsigned n>
223  return *this;
224 }
225 
226 template <unsigned n>
229  for(unsigned i=0;i<n;i++) r[i]=-d[i];
230  return r;
231 }
232 
233 template <unsigned n>
235  VectorGeneric<n> v(v1);
236  return v+=v2;
237 }
238 
239 template <unsigned n>
241  VectorGeneric<n> v(v1);
242  return v-=v2;
243 }
244 
245 template <unsigned n>
247  VectorGeneric<n> vv(v);
248  return vv*=s;
249 }
250 
251 template <unsigned n>
253  return s*v;
254 }
255 
256 template <unsigned n>
258  return v*(1.0/s);
259 }
260 
261 template <unsigned n>
263  return v2-v1;
264 }
265 
266 template <unsigned n>
268  double r=0.0;
269  for(unsigned i=0;i<n;i++) r+=d[i]*d[i];
270  return r;
271 }
272 
273 template <unsigned n>
274 double dotProduct(const VectorGeneric<n>& v1,const VectorGeneric<n>& v2){
275  double r=0.0;
276  for(unsigned i=0;i<n;i++) r+=v1[i]*v2[i];
277  return r;
278 }
279 
280 // faster, specialized version for 2d
281 inline
282 double dotProduct(const VectorGeneric<2>& v1,const VectorGeneric<2>& v2){
283  return v1[0]*v2[0]+v1[1]*v2[1];
284 }
285 
286 template<>
287 inline
289  return d[0]*d[0]+d[1]*d[1];
290 }
291 
292 // faster, specialized version for 3d
293 inline
294 double dotProduct(const VectorGeneric<3>& v1,const VectorGeneric<3>& v2){
295  return v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2];
296 }
297 
298 template<>
299 inline
301  return d[0]*d[0]+d[1]*d[1]+d[2]*d[2];
302 }
303 
304 // faster, specialized version for 4d
305 inline
306 double dotProduct(const VectorGeneric<4>& v1,const VectorGeneric<4>& v2){
307  return v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2]+v1[3]*v2[3];
308 }
309 
310 template<>
311 inline
313  return d[0]*d[0]+d[1]*d[1]+d[2]*d[2]+d[3]*d[3];
314 }
315 
316 inline
318  return VectorGeneric<3>(
319  v1[1]*v2[2]-v1[2]*v2[1],
320  v1[2]*v2[0]-v1[0]*v2[2],
321  v1[0]*v2[1]-v1[1]*v2[0]);
322 }
323 
324 template<unsigned n>
326  return sqrt(modulo2());
327 }
328 
329 template<unsigned n>
330 double modulo2(const VectorGeneric<n>&v){
331  return v.modulo2();
332 }
333 
334 template<unsigned n>
335 double modulo(const VectorGeneric<n>&v){
336  return v.modulo();
337 }
338 
339 
340 /// \ingroup TOOLBOX
341 /// Alias for two dimensional vectors
343 /// \ingroup TOOLBOX
344 /// Alias for three dimensional vectors
346 /// \ingroup TOOLBOX
347 /// Alias for four dimensional vectors
349 /// \ingroup TOOLBOX
350 /// Alias for three dimensional vectors
351 typedef Vector3d Vector;
352 
353 }
354 
355 #endif
356 
VectorGeneric & operator-=(const VectorGeneric &b)
decrement
Definition: Vector.h:204
VectorGeneric & operator/=(double s)
divide
Definition: Vector.h:216
double modulo() const
Compute the modulo.
Definition: Vector.h:325
VectorGeneric< 3 > crossProduct(const VectorGeneric< 3 > &v1, const VectorGeneric< 3 > &v2)
Definition: Vector.h:317
Class implementing fixed size vectors of doubles.
Definition: Vector.h:74
friend VectorGeneric< m > operator*(double, const VectorGeneric< m > &)
return s*v
VectorGeneric & operator*=(double s)
multiply
Definition: Vector.h:210
TensorGeneric< n, m > operator*(const TensorGeneric< n, m > &t1, double s)
Definition: Tensor.h:288
double & operator[](unsigned i)
array-like access [i]
Definition: Vector.h:178
void const char const char int * n
Definition: Matrix.h:42
void zero()
set it to zero
Definition: Vector.h:173
TensorGeneric< n, m > operator/(const TensorGeneric< n, m > &t1, double s)
Definition: Tensor.h:300
VectorGeneric< 3 > Vector3d
Alias for three dimensional vectors.
Definition: Vector.h:345
VectorGeneric< 4 > Vector4d
Alias for four dimensional vectors.
Definition: Vector.h:348
TensorGeneric< n, m > operator-(const TensorGeneric< n, m > &t1, const TensorGeneric< n, m > &t2)
Definition: Tensor.h:281
VectorGeneric< n > delta(const VectorGeneric< n > &v1, const VectorGeneric< n > &v2)
Definition: Vector.h:262
T dotProduct(const std::vector< T > &A, const std::vector< T > &B)
Calculate the dot product between two vectors.
Definition: Matrix.h:54
double modulo2() const
compute the squared modulo
Definition: Vector.h:267
friend VectorGeneric< 3 > crossProduct(const VectorGeneric< 3 > &, const VectorGeneric< 3 > &)
return v1 .vector.
Definition: Vector.h:317
double & operator()(unsigned i)
parenthesis access (i)
Definition: Vector.h:188
double modulo2(const VectorGeneric< n > &v)
Definition: Vector.h:330
double d[n]
Definition: Vector.h:75
TensorGeneric< n, m > operator+(const TensorGeneric< n, m > &t1, const TensorGeneric< n, m > &t2)
Definition: Tensor.h:274
VectorGeneric operator+() const
sign +
Definition: Vector.h:222
VectorGeneric & operator+=(const VectorGeneric &b)
increment
Definition: Vector.h:198
friend VectorGeneric< m > operator/(const VectorGeneric< m > &, double)
return v/s
VectorGeneric operator-() const
sign -
Definition: Vector.h:227
double modulo(const VectorGeneric< n > &v)
Definition: Vector.h:335
Vector3d Vector
Alias for three dimensional vectors.
Definition: Vector.h:351
VectorGeneric< 2 > Vector2d
Alias for two dimensional vectors.
Definition: Vector.h:342
VectorGeneric()
create it null
Definition: Vector.h:168
friend VectorGeneric< m > delta(const VectorGeneric< m > &v1, const VectorGeneric< m > &v2)
return v2-v1
friend double dotProduct(const VectorGeneric< m > &, const VectorGeneric< m > &)
return v1 .scalar. v2