PeTar
N-body code for collisional gravitational systems
matrix3.hpp
Go to the documentation of this file.
1
#pragma once
2
3
#include<cmath>
4
5
template
<
class
T>
6
class
Matrix3
{
7
public
:
8
T
xx
,
xy
,
xz
,
yx
,
yy
,
yz
,
zx
,
zy
,
zz
;
9
Matrix3
() :
xx
(T(0)),
xy
(T(0)),
xz
(T(0)),
yx
(T(0)),
yy
(T(0)),
yz
(T(0)),
zx
(T(0)),
zy
(T(0)),
zz
(T(0)) {}
10
11
Matrix3
(
const
T c) :
xx
(c),
xy
(c),
xz
(c),
yx
(c),
yy
(c),
yz
(c),
zx
(c),
zy
(c),
zz
(c) {}
12
13
Matrix3
(
const
Matrix3
& c) :
xx
(c.
xx
),
xy
(c.
xy
),
xz
(c.
xz
),
yx
(c.
yx
),
yy
(c.
yy
),
yz
(c.
yz
),
zx
(c.
zx
),
zy
(c.
zy
),
zz
(c.
zz
) {}
14
15
16
Matrix3
(
const
T _xx,
const
T _xy,
const
T _xz,
17
const
T _yx,
const
T _yy,
const
T _yz,
18
const
T _zx,
const
T _zy,
const
T _zz)
19
:
xx
(_xx),
xy
(_xy),
xz
(_xz),
yx
(_yx),
yy
(_yy),
yz
(_yz),
zx
(_zx),
zy
(_zy),
zz
(_zz) {}
20
21
template
<
typename
U>
22
operator
Matrix3<U>
()
const
{
23
return
Matrix3<U>
(
static_cast<
U
>
(
xx
),
static_cast<
U
>
(
xy
),
static_cast<
U
>
(
xz
),
24
static_cast<
U
>
(
yx
),
static_cast<
U
>
(
yy
),
static_cast<
U
>
(
yz
),
25
static_cast<
U
>
(
zx
),
static_cast<
U
>
(
zy
),
static_cast<
U
>
(
zz
) );
26
}
27
28
void
rotation
(
const
T I,
const
T OMEGA,
const
T omega){
29
const
T cosomega = cos(omega);
30
const
T sinomega = sin(omega);
31
const
T cosOMEGA = cos(OMEGA);
32
const
T sinOMEGA = sin(OMEGA);
33
const
T cosinc = cos(I);
34
const
T sininc = sin(I);
35
xx
= cosomega*cosOMEGA - sinomega*sinOMEGA*cosinc;
36
xy
= -sinomega*cosOMEGA - cosomega*sinOMEGA*cosinc;
37
xz
= sinOMEGA*sininc;
38
39
yx
= cosomega*sinOMEGA + sinomega*cosOMEGA*cosinc;
40
yy
= -sinomega*sinOMEGA + cosomega*cosOMEGA*cosinc;
41
yz
= -cosOMEGA*sininc;
42
43
zx
= sinomega*sininc;
44
zy
= cosomega*sininc;
45
zz
= cosinc;
46
}
47
48
49
template
<
class
Tvec>
50
Tvec
operator *
(
const
Tvec & vec)
const
{
51
Tvec ret;
52
ret.x =
xx
*vec.x +
xy
*vec.y +
xz
*vec.z;
53
ret.y =
yx
*vec.x +
yy
*vec.y +
yz
*vec.z;
54
ret.z =
zx
*vec.x +
zy
*vec.y +
zz
*vec.z;
55
return
ret;
56
}
57
/*
58
template<class Tvec>
59
friend Tvec operator * (const Tvec & vec, const Matrix3 & mat){
60
Tvec ret;
61
ret.x = mat.xx*vec.x + mat.xy*vec.y + mat.xz*vec.z;
62
ret.y = mat.yx*vec.x + mat.yy*vec.y + mat.yz*vec.z;
63
ret.z = mat.zx*vec.x + mat.zy*vec.y + mat.zz*vec.z;
64
return ret;
65
}
66
*/
67
68
friend
std::ostream &
operator <<
(std::ostream & c,
Matrix3
& mtmp){
69
c<<std::setprecision(15)<<mtmp.
xx
<<
" "
<<mtmp.
xy
<<
" "
<<mtmp.
xz
<<std::endl;
70
c<<std::setprecision(15)<<mtmp.
yx
<<
" "
<<mtmp.
yy
<<
" "
<<mtmp.
yz
<<std::endl;
71
c<<std::setprecision(15)<<mtmp.
zx
<<
" "
<<mtmp.
zy
<<
" "
<<mtmp.
zz
<<std::endl;
72
return
c;
73
}
74
75
/*
76
const T * operator[](const int & i)const{return m[i];}
77
78
T * operator[](const int & i){return m[i];}
79
80
81
82
Matrix3& operator=(const Matrix3 &c)const{
83
m[0][0] = c.m[0][0]; m[0][1] = c.m[0][1]; m[0][2] = c.m[0][2];
84
m[1][0] = c.m[1][0]; m[1][1] = c.m[1][1]; m[1][2] = c.m[1][2];
85
m[2][0] = c.m[2][0]; m[2][1] = c.m[2][1]; m[2][2] = c.m[2][2];
86
return *this;
87
}
88
89
Matrix3 & operator+(const Matrix3 &a)const{
90
Matrix3 x;
91
x.m[0][0]=m[0][0]+a.m[0][0]; x.m[0][1]=m[0][1]+a.m[0][1]; x.m[0][2]=m[0][2]+a.m[0][2];
92
x.m[1][0]=m[1][0]+a.m[1][0]; x.m[1][1]=m[1][1]+a.m[1][1]; x.m[1][2]=m[1][2]+a.m[1][2];
93
x.m[2][0]=m[2][0]+a.m[2][0]; x.m[2][1]=m[2][1]+a.m[2][1]; x.m[2][2]=m[2][2]+a.m[2][2];
94
return x;
95
}
96
97
matrix3& operator-(const matrix3 &a)const{
98
matrix3 x;
99
x.m[0][0]=m[0][0]-a.m[0][0]; x.m[0][1]=m[0][1]-a.m[0][1]; x.m[0][2]=m[0][2]-a.m[0][2];
100
x.m[1][0]=m[1][0]-a.m[1][0]; x.m[1][1]=m[1][1]-a.m[1][1]; x.m[1][2]=m[1][2]-a.m[1][2];
101
x.m[2][0]=m[2][0]-a.m[2][0]; x.m[2][1]=m[2][1]-a.m[2][1]; x.m[2][2]=m[2][2]-a.m[2][2];
102
return x;
103
}
104
105
106
matrix3& operator*(const matrix3 &a)const{
107
matrix3 x;
108
x.m[0][0] = m[0][0]*a.m[0][0] + m[0][1]*a.m[1][0] + m[0][2]*a.m[2][0];
109
x.m[0][1] = m[0][0]*a.m[0][1] + m[0][1]*a.m[1][1] + m[0][2]*a.m[2][1];
110
x.m[0][2] = m[0][0]*a.m[0][2] + m[0][1]*a.m[1][2] + m[0][2]*a.m[2][2];
111
112
x.m[1][0] = m[1][0]*a.m[0][0] + m[1][1]*a.m[1][0] + m[1][2]*a.m[2][0];
113
x.m[1][1] = m[1][0]*a.m[0][1] + m[1][1]*a.m[1][1] + m[1][2]*a.m[2][1];
114
x.m[1][2] = m[1][0]*a.m[0][2] + m[1][1]*a.m[1][2] + m[1][2]*a.m[2][2];
115
116
x.m[2][0] = m[2][0]*a.m[0][0] + m[2][1]*a.m[1][0] + m[2][2]*a.m[2][0];
117
x.m[2][1] = m[2][0]*a.m[0][1] + m[2][1]*a.m[1][1] + m[2][2]*a.m[2][1];
118
x.m[2][2] = m[2][0]*a.m[0][2] + m[2][1]*a.m[1][2] + m[2][2]*a.m[2][2];
119
return x;
120
}
121
122
123
vector3<T>& operator * (const vector3<T> &a)const{
124
vector3<T> x;
125
x[0] = m[0][0]*a[0] + m[0][1]*a[1] + m[0][2]*a[2];
126
x[1] = m[1][0]*a[0] + m[1][1]*a[1] + m[1][2]*a[2];
127
x[2] = m[2][0]*a[0] + m[2][1]*a[1] + m[2][2]*a[2];
128
return x;
129
}
130
131
matrix3& operator * (const T &s) const{
132
matrix3 x;
133
x.m[0][0]=m[0][0]*s; x.m[0][1]=m[0][1]*s; x.m[0][2]=m[0][2]*s;
134
x.m[1][0]=m[1][0]*s; x.m[1][1]=m[1][1]*s; x.m[1][2]=m[1][2]*s;
135
x.m[2][0]=m[2][0]*s; x.m[2][1]=m[2][1]*s; x.m[2][2]=m[2][2]*s;
136
return x;
137
}
138
139
matrix3& transposed()const{
140
matrix3 mtmp;
141
for(int i=0; i<3; i++){
142
for(int j=0; j<3; j++){
143
mtmp.m[j][i] = m[i][j];
144
}
145
}
146
return mtmp;
147
}
148
149
void unit(){
150
m[0][1] = m[0][2] = m[1][0] = m[1][2] = m[2][0] = m[2][1] = 0.0;
151
m[0][0] = m[1][1] = m[2][2] = 1.0;
152
}
153
154
matrix3& operator += (const matrix3 &a){
155
m[0][0] += a.m[0][0]; m[0][1] += a.m[0][1]; m[0][2] += a.m[0][2];
156
m[1][0] += a.m[1][0]; m[1][1] += a.m[1][1]; m[1][2] += a.m[1][2];
157
m[2][0] += a.m[2][0]; m[2][1] += a.m[2][1]; m[2][2] +=a.m[2][2];
158
return *this;
159
}
160
161
matrix3& operator -= (const matrix3 &a){
162
m[0][0] -= a.m[0][0]; m[0][1] -= a.m[0][1]; m[0][2] -= a.m[0][2];
163
m[1][0] -= a.m[1][0]; m[1][1] -= a.m[1][1]; m[1][2] -= a.m[1][2];
164
m[2][0] -= a.m[2][0]; m[2][1] -= a.m[2][1]; m[2][2] -=a.m[2][2];
165
return *this;
166
}
167
168
matrix3& operator *= (const T &a){
169
m[0][0] *= a; m[0][1] *= a; m[0][2] *= a;
170
m[1][0] *= a; m[1][1] *= a; m[1][2] *= a;
171
m[2][0] *= a; m[2][1] *= a; m[2][2] *= a;
172
return *this;
173
}
174
175
T& determinant()const{
176
T det = 0.0;
177
det = m[0][0] * m[1][1] * m[2][2]
178
+ m[0][1] * m[1][2] * m[2][0]
179
+ m[0][2] * m[1][0] * m[2][1]
180
- m[0][2] * m[1][1] * m[2][0]
181
- m[0][1] * m[1][0] * m[2][2]
182
- m[0][0] * m[1][2] * m[2][1];
183
return det;
184
}
185
186
friend matrix3 outer_product(const vector3<T> &a, const vector3<T> &b){
187
matrix3 x;
188
x[0][0]=a[0]*b[0]; x[0][1]=a[0]*b[1]; x[0][2]=a[0]*b[2];
189
x[1][0]=a[1]*b[0]; x[1][1]=a[1]*b[1]; x[1][2]=a[1]*b[2];
190
x[2][0]=a[2]*b[0]; x[2][1]=a[2]*b[1]; x[2][2]=a[2]*b[2];
191
return x;
192
}
193
194
friend matrix3 quadrupole(const vector3<T> &a){
195
matrix3 x=3.0*outer_product(a,a);
196
T a_squared=a*a;
197
matrix3 delta;
198
delta.unit();
199
return x-a_squared*delta;
200
}
201
202
friend matrix3 inertiamoment(const T mass, const vector3<T> &pos){
203
matrix3 x = mass * outer_product(pos, pos);
204
return x;
205
}
206
207
friend vector3<T> operator*(const vector3<T> &a, const matrix3 &c){
208
vector3<T> x;
209
x[0]=a[0]*c.m[0][0] + a[1]*c.m[1][0] + a[2]*c.m[2][0];
210
x[1]=a[0]*c.m[0][1] + a[1]*c.m[1][1] + a[2]*c.m[2][1];
211
x[2]=a[0]*c.m[0][2] + a[1]*c.m[1][2] + a[2]*c.m[2][2];
212
return x;
213
}
214
215
friend matrix3 operator * (const T &s, const matrix3 &c){
216
matrix3 x;
217
x[0][0]=c.m[0][0]*s; x[0][1]=c.m[0][1]*s; x[0][2]=c.m[0][2]*s;
218
x[1][0]=c.m[1][0]*s; x[1][1]=c.m[1][1]*s; x[1][2]=c.m[1][2]*s;
219
x[2][0]=c.m[2][0]*s; x[2][1]=c.m[2][1]*s; x[2][2]=c.m[2][2]*s;
220
return x;
221
}
222
*/
223
224
225
};
Matrix3::yx
T yx
Definition:
matrix3.hpp:8
Matrix3::yz
T yz
Definition:
matrix3.hpp:8
petar.hpp
PIKG::S32
int32_t S32
Definition:
pikg_vector.hpp:24
Matrix3::zy
T zy
Definition:
matrix3.hpp:8
Matrix3::xy
T xy
Definition:
matrix3.hpp:8
PIKG::F64
double F64
Definition:
pikg_vector.hpp:17
Matrix3::yy
T yy
Definition:
matrix3.hpp:8
Matrix3::rotation
void rotation(const T I, const T OMEGA, const T omega)
Definition:
matrix3.hpp:28
Matrix3::xz
T xz
Definition:
matrix3.hpp:8
Matrix3::operator<<
friend std::ostream & operator<<(std::ostream &c, Matrix3 &mtmp)
Definition:
matrix3.hpp:68
Matrix3::Matrix3
Matrix3(const T c)
Definition:
matrix3.hpp:11
main
int main(int argc, char *argv[])
Definition:
main.cc:6
Matrix3::operator*
Tvec operator*(const Tvec &vec) const
Definition:
matrix3.hpp:50
Matrix3::Matrix3
Matrix3(const Matrix3 &c)
Definition:
matrix3.hpp:13
Matrix3
Definition:
matrix3.hpp:6
Matrix3::zx
T zx
Definition:
matrix3.hpp:8
Matrix3::xx
T xx
Definition:
matrix3.hpp:8
Matrix3::zz
T zz
Definition:
matrix3.hpp:8
Matrix3::Matrix3
Matrix3()
Definition:
matrix3.hpp:9
Matrix3::Matrix3
Matrix3(const T _xx, const T _xy, const T _xz, const T _yx, const T _yy, const T _yz, const T _zx, const T _zy, const T _zz)
Definition:
matrix3.hpp:16
src
matrix3.hpp
Generated by
1.8.17