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