forked from plumed/plumed2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tensor.h
434 lines (372 loc) · 13.2 KB
/
Tensor.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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_Tensor_h
#define __PLUMED_tools_Tensor_h
#include "MatrixSquareBracketsAccess.h"
#include "Vector.h"
namespace PLMD{
/**
\ingroup TOOLBOX
Class implementing fixed size matrices of doubles
\tparam n The number rows
\tparam m The number columns
This class implements a matrix of doubles with size fixed at
compile time. It is useful for small fixed size objects (e.g.
3x3 tensors) as it does not waste space to store the vector size.
Moreover, as the compiler knows the size, it can be completely
opimized inline.
Matrix 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.
It takes advantage of MatrixSquareBracketsAccess to provide 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 (Tensor, Tensor2d, Tensor3d, Tensor4d).
Also notice that some operations are only available for 3x3 tensors.
Example of usage
\verbatim
#include "Tensor.h"
using namespace PLMD;
int main(){
Tensor a;
TensorGeneric<3,2> b;
TensorGeneric<3,2> c=matmul(a,b);
return 0;
}
\endverbatim
*/
template <unsigned n,unsigned m>
class TensorGeneric:
public MatrixSquareBracketsAccess<TensorGeneric<n,m>,double>
{
double d[n*m];
public:
/// initialize the tensor to zero
TensorGeneric();
/// initialize a tensor as an external product of two Vector
TensorGeneric(const VectorGeneric<n>&v1,const VectorGeneric<m>&v2);
/// initialize a tensor with 4 values, in standard C order
TensorGeneric(double,double,double,double);
/// initialize a tensor with 9 values, in standard C order
TensorGeneric(double,double,double,double,double,double,double,double,double);
/// set it to zero
void zero();
/// access element
double & operator() (unsigned i,unsigned j);
/// access element
const double & operator() (unsigned i,unsigned j)const;
/// increment
TensorGeneric& operator +=(const TensorGeneric<n,m>& b);
/// decrement
TensorGeneric& operator -=(const TensorGeneric<n,m>& b);
/// multiply
TensorGeneric& operator *=(double);
/// divide
TensorGeneric& operator /=(double);
/// return +t
TensorGeneric operator +()const;
/// return -t
TensorGeneric operator -()const;
/// set j-th column
TensorGeneric& setCol(unsigned j,const VectorGeneric<n> & c);
/// set i-th row
TensorGeneric& setRow(unsigned i,const VectorGeneric<m> & r);
/// get j-th column
VectorGeneric<n> getCol(unsigned j)const;
/// get i-th row
VectorGeneric<m> getRow(unsigned i)const;
/// return t1+t2
template<unsigned n_,unsigned m_>
friend TensorGeneric<n_,m_> operator+(const TensorGeneric<n_,m_>&,const TensorGeneric<n_,m_>&);
/// return t1+t2
template<unsigned n_,unsigned m_>
friend TensorGeneric<n_,m_> operator-(const TensorGeneric<n_,m_>&,const TensorGeneric<n_,m_>&);
/// scale the tensor by a factor s
template<unsigned n_,unsigned m_>
friend TensorGeneric<n_,m_> operator*(double,const TensorGeneric<n_,m_>&);
/// scale the tensor by a factor s
template<unsigned n_,unsigned m_>
friend TensorGeneric<n_,m_> operator*(const TensorGeneric<n_,m_>&,double s);
/// scale the tensor by a factor 1/s
template<unsigned n_,unsigned m_>
friend TensorGeneric<n_,m_> operator/(const TensorGeneric<n_,m_>&,double s);
/// returns the determinant
double determinant()const;
/// return an identity tensor
static TensorGeneric<n,n> identity();
/// return the matrix inverse
TensorGeneric inverse()const;
/// return the transpose matrix
TensorGeneric<m,n> transpose()const;
/// matrix-matrix multiplication
template<unsigned n_,unsigned m_,unsigned l_>
friend TensorGeneric<n_,l_> matmul(const TensorGeneric<n_,m_>&,const TensorGeneric<m_,l_>&);
/// matrix-vector multiplication
template<unsigned n_,unsigned m_>
friend VectorGeneric<n_> matmul(const TensorGeneric<n_,m_>&,const VectorGeneric<m_>&);
/// vector-matrix multiplication
template<unsigned n_,unsigned m_>
friend VectorGeneric<n_> matmul(const VectorGeneric<m_>&,const TensorGeneric<m_,n_>&);
/// matrix-matrix-matrix multiplication
template<unsigned n_,unsigned m_,unsigned l_,unsigned i_>
friend TensorGeneric<n_,i_> matmul(const TensorGeneric<n_,m_>&,const TensorGeneric<m_,l_>&,const TensorGeneric<l_,i_>&);
/// matrix-matrix-vector multiplication
template<unsigned n_,unsigned m_,unsigned l_>
friend VectorGeneric<n_> matmul(const TensorGeneric<n_,m_>&,const TensorGeneric<m_,l_>&,const VectorGeneric<l_>&);
/// vector-matrix-matrix multiplication
template<unsigned n_,unsigned m_,unsigned l_>
friend VectorGeneric<l_> matmul(const VectorGeneric<n_>&,const TensorGeneric<n_,m_>&,const TensorGeneric<m_,l_>&);
/// returns the determinant of a tensor
friend double determinant(const TensorGeneric<3,3>&);
/// returns the inverse of a tensor (same as inverse())
friend TensorGeneric<3,3> inverse(const TensorGeneric<3,3>&);
/// returns the transpose of a tensor (same as transpose())
template<unsigned n_,unsigned m_>
friend TensorGeneric<n_,m_> transpose(const TensorGeneric<m_,n_>&);
/// returns the transpose of a tensor (same as TensorGeneric(const VectorGeneric&,const VectorGeneric&))
template<unsigned n_,unsigned m_>
friend TensorGeneric<n_,m_> extProduct(const VectorGeneric<n>&,const VectorGeneric<m>&);
friend TensorGeneric<3,3> dcrossDv1(const VectorGeneric<3>&,const VectorGeneric<3>&);
friend TensorGeneric<3,3> dcrossDv2(const VectorGeneric<3>&,const VectorGeneric<3>&);
};
template<unsigned n,unsigned m>
TensorGeneric<n,m>::TensorGeneric(){
for(unsigned i=0;i<n*m;i++)d[i]=0.0;
}
template<unsigned n,unsigned m>
TensorGeneric<n,m>::TensorGeneric(const VectorGeneric<n>&v1,const VectorGeneric<m>&v2){
for(unsigned i=0;i<n;i++)for(unsigned j=0;j<m;j++)d[i*m+j]=v1[i]*v2[j];
}
template<>
inline
TensorGeneric<2,2>::TensorGeneric(double d00,double d01,double d10,double d11){
d[0]=d00;
d[1]=d01;
d[2]=d10;
d[3]=d11;
}
template<>
inline
TensorGeneric<3,3>::TensorGeneric(double d00,double d01,double d02,double d10,double d11,double d12,double d20,double d21,double d22){
d[0]=d00;
d[1]=d01;
d[2]=d02;
d[3]=d10;
d[4]=d11;
d[5]=d12;
d[6]=d20;
d[7]=d21;
d[8]=d22;
}
template<unsigned n,unsigned m>
void TensorGeneric<n,m>::zero(){
for(unsigned i=0;i<n*m;i++)d[i]=0.0;
}
template<unsigned n,unsigned m>
double & TensorGeneric<n,m>::operator() (unsigned i,unsigned j){
return d[m*i+j];
}
template<unsigned n,unsigned m>
const double & TensorGeneric<n,m>::operator() (unsigned i,unsigned j)const{
return d[m*i+j];
}
template<unsigned n,unsigned m>
TensorGeneric<n,m>& TensorGeneric<n,m>::operator +=(const TensorGeneric<n,m>& b){
for(unsigned i=0;i<n*m;i++)d[i]+=b.d[i];
return *this;
}
template<unsigned n,unsigned m>
TensorGeneric<n,m>& TensorGeneric<n,m>::operator -=(const TensorGeneric<n,m>& b){
for(unsigned i=0;i<n*m;i++)d[i]-=b.d[i];
return *this;
}
template<unsigned n,unsigned m>
TensorGeneric<n,m>& TensorGeneric<n,m>::operator *=(double s){
for(unsigned i=0;i<n*m;i++)d[i]*=s;
return *this;
}
template<unsigned n,unsigned m>
TensorGeneric<n,m>& TensorGeneric<n,m>::operator /=(double s){
return (*this)*=1.0/s;
}
template<unsigned n,unsigned m>
TensorGeneric<n,m> TensorGeneric<n,m>::operator+()const{
return *this;
}
template<unsigned n,unsigned m>
TensorGeneric<n,m> TensorGeneric<n,m>::operator-()const{
TensorGeneric<n,m> r;
for(unsigned i=0;i<n*m;i++)r.d[i]=-d[i];
return r;
}
template<unsigned n,unsigned m>
TensorGeneric<n,m>& TensorGeneric<n,m>::setCol(unsigned j,const VectorGeneric<n> & c){
for(unsigned i=0;i<n;++i) (*this)(i,j)=c(i);
return *this;
}
template<unsigned n,unsigned m>
TensorGeneric<n,m>& TensorGeneric<n,m>::setRow(unsigned i,const VectorGeneric<m> & r){
for(unsigned j=0;j<m;++j) (*this)(i,j)=r(j);
return *this;
}
template<unsigned n,unsigned m>
VectorGeneric<n> TensorGeneric<n,m>::getCol(unsigned j)const{
VectorGeneric<n> v;
for(unsigned i=0;i<n;++i) v(i)=(*this)(i,j);
return v;
}
template<unsigned n,unsigned m>
VectorGeneric<m> TensorGeneric<n,m>::getRow(unsigned i)const{
VectorGeneric<m> v;
for(unsigned j=0;j<m;++j) v(j)=(*this)(i,j);
return v;
}
template<unsigned n,unsigned m>
TensorGeneric<n,m> operator+(const TensorGeneric<n,m>&t1,const TensorGeneric<n,m>&t2){
TensorGeneric<n,m> t(t1);
t+=t2;
return t;
}
template<unsigned n,unsigned m>
TensorGeneric<n,m> operator-(const TensorGeneric<n,m>&t1,const TensorGeneric<n,m>&t2){
TensorGeneric<n,m> t(t1);
t-=t2;
return t;
}
template<unsigned n,unsigned m>
TensorGeneric<n,m> operator*(const TensorGeneric<n,m>&t1,double s){
TensorGeneric<n,m> t(t1);
t*=s;
return t;
}
template<unsigned n,unsigned m>
TensorGeneric<n,m> operator*(double s,const TensorGeneric<n,m>&t1){
return t1*s;
}
template<unsigned n,unsigned m>
TensorGeneric<n,m> operator/(const TensorGeneric<n,m>&t1,double s){
return t1*(1.0/s);
}
template<>
inline
double TensorGeneric<3,3>::determinant()const{
return
d[0]*d[4]*d[8]
+ d[1]*d[5]*d[6]
+ d[2]*d[3]*d[7]
- d[0]*d[5]*d[7]
- d[1]*d[3]*d[8]
- d[2]*d[4]*d[6];
}
template<unsigned n,unsigned m>
inline
TensorGeneric<n,n> TensorGeneric<n,m>::identity(){
TensorGeneric<n,n> t;
for(unsigned i=0;i<n;i++) t(i,i)=1.0;
return t;
}
template<unsigned n,unsigned m>
TensorGeneric<m,n> TensorGeneric<n,m>::transpose()const{
TensorGeneric<m,n> t;
for(unsigned i=0;i<n;i++)for(unsigned j=0;j<m;j++) t(i,j)=(*this)(j,i);
return t;
}
template<>
inline
TensorGeneric<3,3> TensorGeneric<3,3>::inverse()const{
TensorGeneric t;
double invdet=1.0/determinant();
for(unsigned i=0;i<3;i++) for(unsigned j=0;j<3;j++)
t(j,i)=invdet*( (*this)((i+1)%3,(j+1)%3)*(*this)((i+2)%3,(j+2)%3)
-(*this)((i+1)%3,(j+2)%3)*(*this)((i+2)%3,(j+1)%3));
return t;
}
template<unsigned n,unsigned m,unsigned l>
TensorGeneric<n,l> matmul(const TensorGeneric<n,m>&a,const TensorGeneric<m,l>&b){
TensorGeneric<n,l> t;
for(unsigned i=0;i<n;i++) for(unsigned j=0;j<l;j++) for(unsigned k=0;k<m;k++) {
t(i,j)+=a(i,k)*b(k,j);
}
return t;
}
template<unsigned n,unsigned m>
VectorGeneric<n> matmul(const TensorGeneric<n,m>&a,const VectorGeneric<m>&b){
VectorGeneric<n> t;
for(unsigned i=0;i<n;i++) for(unsigned j=0;j<m;j++) t(i)+=a(i,j)*b(j);
return t;
}
template<unsigned n,unsigned m>
VectorGeneric<n> matmul(const VectorGeneric<m>&a,const TensorGeneric<m,n>&b){
VectorGeneric<n> t;
for(unsigned i=0;i<n;i++) for(unsigned j=0;j<m;j++) t(i)+=a(j)*b(j,i);
return t;
}
template<unsigned n,unsigned m,unsigned l,unsigned i>
TensorGeneric<n,i> matmul(const TensorGeneric<n,m>&a,const TensorGeneric<m,l>&b,const TensorGeneric<l,i>&c){
return matmul(matmul(a,b),c);
}
template<unsigned n,unsigned m,unsigned l>
VectorGeneric<n> matmul(const TensorGeneric<n,m>&a,const TensorGeneric<m,l>&b,const VectorGeneric<l>&c){
return matmul(matmul(a,b),c);
}
template<unsigned n,unsigned m,unsigned l>
VectorGeneric<l> matmul(const VectorGeneric<n>&a,const TensorGeneric<n,m>&b,const TensorGeneric<m,l>&c){
return matmul(matmul(a,b),c);
}
inline
double determinant(const TensorGeneric<3,3>&t){
return t.determinant();
}
inline
TensorGeneric<3,3> inverse(const TensorGeneric<3,3>&t){
return t.inverse();
}
template<unsigned n,unsigned m>
TensorGeneric<n,m> transpose(const TensorGeneric<m,n>&t){
return t.transpose();
}
template<unsigned n,unsigned m>
TensorGeneric<n,m> extProduct(const VectorGeneric<n>&v1,const VectorGeneric<m>&v2){
return TensorGeneric<n,m>(v1,v2);
}
inline
TensorGeneric<3,3> dcrossDv1(const VectorGeneric<3>&v1,const VectorGeneric<3>&v2){
(void) v1; // this is to avoid warnings. still the syntax of this function is a bit dummy...
return TensorGeneric<3,3>(
0.0, v2[2],-v2[1],
-v2[2], 0.0, v2[0],
v2[1],-v2[0], 0.0);
}
inline
TensorGeneric<3,3> dcrossDv2(const VectorGeneric<3>&v1,const VectorGeneric<3>&v2){
(void) v2; // this is to avoid warnings. still the syntax of this function is a bit dummy...
return TensorGeneric<3,3>(
0.0,-v1[2],v1[1],
v1[2],0.0,-v1[0],
-v1[1],v1[0],0.0);
}
/// \ingroup TOOLBOX
typedef TensorGeneric<2,2> Tensor2d;
/// \ingroup TOOLBOX
typedef TensorGeneric<3,3> Tensor3d;
/// \ingroup TOOLBOX
typedef TensorGeneric<4,4> Tensor4d;
/// \ingroup TOOLBOX
typedef Tensor3d Tensor;
}
#endif