-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix3.cpp
More file actions
319 lines (273 loc) · 7.21 KB
/
Copy pathmatrix3.cpp
File metadata and controls
319 lines (273 loc) · 7.21 KB
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
#include <iostream>
#include <iomanip>
#include "matrix3.h"
#include <float.h>
#include <cmath>
Matrix3::Matrix3(double m11, double m12, double m13, double m21,
double m22, double m23, double m31, double m32, double m33)
{
m[0][0]=m11; m[0][1]=m12; m[0][2]=m13;
m[1][0]=m21; m[1][1]=m22; m[1][2]=m23;
m[2][0]=m31; m[2][1]=m32; m[2][2]=m33;
}
Matrix3::Matrix3(double md)
{
m[0][0]=m[1][1]=m[2][2]=md;
m[0][1]=m[0][2]=m[1][0]=m[1][2]=m[2][0]=m[2][1]=0.0;
}
Matrix3::Matrix3(double m1, double m2, double m3)
{
m[0][0]=m1;
m[1][1]=m2;
m[2][2]=m3;
m[0][1]=m[0][2]=m[1][0]=m[1][2]=m[2][0]=m[2][1]=0.0;
}
Matrix3::Matrix3()
{
m[0][0]=m[1][1]=m[2][2]=0.0;
m[0][1]=m[0][2]=m[1][0]=m[1][2]=m[2][0]=m[2][1]=0.0;
}
Matrix3::~Matrix3()
{
}
std::ostream& operator << (std::ostream& s, Matrix3 matrix)
{
s << "#MATRIX:\n";
s << std::setw(8) << std::setprecision(7) << matrix.m[0][0] << " ";
s << std::setw(8) << std::setprecision(7) << matrix.m[0][1] << " ";
s << std::setw(8) << std::setprecision(7) << matrix.m[0][2] << std::endl;
s << std::setw(8) << std::setprecision(7) << matrix.m[1][0] << " ";
s << std::setw(8) << std::setprecision(7) << matrix.m[1][1] << " ";
s << std::setw(8) << std::setprecision(7) << matrix.m[1][2] << std::endl;
s << std::setw(8) << std::setprecision(7) << matrix.m[2][0] << " ";
s << std::setw(8) << std::setprecision(7) << matrix.m[2][1] << " ";
s << std::setw(8) << std::setprecision(7) << matrix.m[2][2] << std::endl;
return s;
}
Matrix3 operator + (const Matrix3& m1, const Matrix3& m2)
{
Matrix3 m0(0.0);
int i,j;
for (i=0; i<3; i++) for (j=0; j<3; j++)
m0.m[i][j]=m1.m[i][j]+m2.m[i][j];
return m0;
}
Matrix3 operator - (const Matrix3& m1, const Matrix3& m2)
{
Matrix3 m0(0.0);
int i,j;
for (i=0; i<3; i++) for (j=0; j<3; j++)
m0.m[i][j]=m1.m[i][j]-m2.m[i][j];
return m0;
}
Matrix3 operator * (const Matrix3& m1, const Matrix3& m2)
{
Matrix3 m0(0.0);
int i,j;
for (i=0; i<3; i++) for (j=0; j<3; j++)
m0.m[i][j]=m1.m[i][0]*m2.m[0][j]+m1.m[i][1]*m2.m[1][j]+m1.m[i][2]*m2.m[2][j];
return m0;
}
Matrix3 operator * (const Matrix3& m1, const double e)
{
Matrix3 m0(0.0);
int i,j;
for (i=0; i<3; i++) for (j=0; j<3; j++)
m0.m[i][j]=e*m1.m[i][j];
return m0;
}
Matrix3 operator * (const double e, const Matrix3& m1)
{
Matrix3 m0(0.0);
int i,j;
for (i=0; i<3; i++) for (j=0; j<3; j++)
m0.m[i][j]=e*m1.m[i][j];
return m0;
}
Matrix3 operator / (const Matrix3& m1, const double e)
{
Matrix3 m0(0.0);
int i,j;
for (i=0; i<3; i++) for (j=0; j<3; j++)
m0.m[i][j]=m1.m[i][j]/e;
return m0;
}
double& Matrix3::operator () (int row, int col)
{
return m[row-1][col-1];
}
double det(const Matrix3& mat)
{
double d;
d=mat.m[0][0]*(mat.m[1][1]*mat.m[2][2]-mat.m[2][1]*mat.m[1][2]);
d-=mat.m[0][1]*(mat.m[1][0]*mat.m[2][2]-mat.m[2][0]*mat.m[1][2]);
d+=mat.m[0][2]*(mat.m[1][0]*mat.m[2][1]-mat.m[2][0]*mat.m[1][1]);
return d;
}
Matrix3 inverse(const Matrix3& mat)
{
double d;
Matrix3 inv;
d=det(mat);
inv.m[0][0]=(mat.m[1][1]*mat.m[2][2]-mat.m[2][1]*mat.m[1][2])/d;
inv.m[0][1]=-(mat.m[0][1]*mat.m[2][2]-mat.m[2][1]*mat.m[0][2])/d;
inv.m[0][2]=(mat.m[0][1]*mat.m[1][2]-mat.m[1][1]*mat.m[0][2])/d;
inv.m[1][0]=-(mat.m[1][0]*mat.m[2][2]-mat.m[2][0]*mat.m[1][2])/d;
inv.m[1][1]=(mat.m[0][0]*mat.m[2][2]-mat.m[2][0]*mat.m[0][2])/d;
inv.m[1][2]=-(mat.m[0][0]*mat.m[1][2]-mat.m[1][0]*mat.m[0][2])/d;
inv.m[2][0]=(mat.m[1][0]*mat.m[2][1]-mat.m[2][0]*mat.m[1][1])/d;
inv.m[2][1]=-(mat.m[0][0]*mat.m[2][1]-mat.m[2][0]*mat.m[0][1])/d;
inv.m[2][2]=(mat.m[0][0]*mat.m[1][1]-mat.m[1][0]*mat.m[0][1])/d;
return inv;
}
Matrix3 transpose(const Matrix3& mat)
{
Matrix3 tr(0.0);
tr.m[0][0]=mat.m[0][0]; tr.m[1][1]=mat.m[1][1]; tr.m[2][2]=mat.m[2][2];
tr.m[0][1]=mat.m[1][0]; tr.m[0][2]=mat.m[2][0];
tr.m[1][0]=mat.m[0][1]; tr.m[1][2]=mat.m[2][1];
tr.m[2][0]=mat.m[0][2]; tr.m[2][1]=mat.m[1][2];
return tr;
}
bool Jacobi(const Matrix3& mat, Matrix3& d, Matrix3& v, int nstep)
{
int i,p,q; // indices
int iter=0;
double offsum; // sum of off-diagonals
double b[3],z[3]; // used for the summation of t*a[p][q]
double tresh; // absolute threshold for the off-diagonals == 0
double rtrs; // threshold relative to the diagonals
double t,th,c,s,tau; // t, theta, cos, sin, tau of the Jacobi method
double x,y; // working variables
Matrix3 a(mat); // working matrix
v=Matrix3(1.0); // eigenvectors
d=Matrix3(0.0); // eigenvalues
b[0]=d.m[0][0]=a.m[0][0];
b[1]=d.m[1][1]=a.m[1][1];
b[2]=d.m[2][2]=a.m[2][2];
z[0]=z[1]=z[2]=0.0;
while (iter < nstep)
{
offsum=fabs(a.m[0][1])+fabs(a.m[0][2])+fabs(a.m[1][2]); // symmetric!
if (offsum < DBL_EPSILON) return true;
if (iter < 4) tresh=offsum/45.0; else tresh=0.0;
for (p=0; p<2; p++)
{
for (q=p+1; q<3; q++)
{
rtrs=100.0*fabs(a.m[p][q]);
if ((iter > 4)
&& (rtrs < DBL_EPSILON*fabs(d.m[p][p]))
&& (rtrs < DBL_EPSILON*fabs(d.m[q][q])))
a.m[p][q]=0.0;
else if (fabs(a.m[p][q]) > tresh)
{
x=d.m[q][q]-d.m[p][p];
if (rtrs < DBL_EPSILON*fabs(x))
t=a.m[p][q]/x;
else
{
th=0.5*x/(a.m[p][q]);
t=1.0/(fabs(th)+sqrt(1.0+th*th));
if (th < 0.0) t=-t;
}
c=1.0/sqrt(1.0+t*t);
s=t*c;
tau=s/(1.0+c);
x=t*a.m[p][q];
z[p]-=x; z[q]+=x;
d.m[p][p]-=x; d.m[q][q]+=x;
a.m[p][q]=0.0;
for (i=0; i<3; i++)
{
if (i<p)
{
x=a.m[i][p]; y=a.m[i][q];
a.m[i][p]=x-s*(y+tau*x);
a.m[i][q]=y+s*(x-y*tau);
} else if ((i>p) && (i<q))
{
x=a.m[p][i]; y=a.m[i][q];
a.m[p][i]=x-s*(y+tau*x);
a.m[i][q]=y+s*(x-y*tau);
} else if (i>q)
{
x=a.m[p][i]; y=a.m[q][i];
a.m[p][i]=x-s*(y+tau*x);
a.m[q][i]=y+s*(x-y*tau);
}
}
for (i=0; i<3; i++)
{
x=v.m[i][p]; y=v.m[i][q];
v.m[i][p]=x-s*(y+tau*x);
v.m[i][q]=y+s*(x-y*tau);
}
}
}
}
for (i=0; i<3; i++)
{
b[i]+=z[i]; d.m[i][i]=b[i]; z[i]=0.0;
}
iter++;
}
return false;
}
Vector3::Vector3(double v1, double v2, double v3)
{
v[0]=v1; v[1]=v2; v[2]=v3;
}
Vector3::Vector3(double v1)
{
v[0]=v1; v[1]=v1; v[2]=v1;
}
Vector3::Vector3()
{
v[0]=0.0; v[1]=0.0; v[2]=0.0;
}
Vector3::~Vector3()
{
}
std::ostream& operator << (std::ostream& s, Vector3 vector)
{
s << "#VECTOR: ";
s << std::setw(8) << std::setprecision(7) << vector.v[0] << " ";
s << std::setw(8) << std::setprecision(7) << vector.v[1] << " ";
s << std::setw(8) << std::setprecision(7) << vector.v[2] << std::endl;
return s;
}
Vector3 operator + (const Vector3& v1, const Vector3& v2)
{
return Vector3(v1.v[0]+v2.v[0],v1.v[1]+v2.v[1],v1.v[2]+v2.v[2]);
}
Vector3 operator - (const Vector3& v1, const Vector3& v2)
{
return Vector3(v1.v[0]-v2.v[0],v1.v[1]-v2.v[1],v1.v[2]-v2.v[2]);
}
Vector3 operator * (const Vector3& v1, const double e)
{
return(Vector3(v1.v[0]*e,v1.v[1]*e,v1.v[2]*e));
}
Vector3 operator * (const double e, const Vector3& v1)
{
return(Vector3(v1.v[0]*e,v1.v[1]*e,v1.v[2]*e));
}
Vector3 operator / (const Vector3& v1, const double e)
{
return(Vector3(v1.v[0]/e,v1.v[1]/e,v1.v[2]/e));
}
Vector3 operator * (Matrix3 m1, const Vector3& v1)
{
Vector3 v0(0.0);
double x,y,z;
x=v1.v[0]; y=v1.v[1]; z=v1.v[2];
v0.v[0]=m1(1,1)*x+m1(1,2)*y+m1(1,3)*z;
v0.v[1]=m1(2,1)*x+m1(2,2)*y+m1(2,3)*z;
v0.v[2]=m1(3,1)*x+m1(3,2)*y+m1(3,3)*z;
return v0;
}
double& Vector3::operator () (int i)
{
return v[i-1];
}