-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix3f.cpp
More file actions
241 lines (195 loc) · 4.79 KB
/
matrix3f.cpp
File metadata and controls
241 lines (195 loc) · 4.79 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
#include "Matrix3f.h"
void Matrix3f::disp(){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
std::cout << a[i][j] << ' ';
}
std::cout << '\n';
}
}
void Matrix3f::chg_row(int x, int y){
int temp[3] = {a[x][0], a[x][1], a[x][2]};
for(int i = 0; i < 3; i++){
a[x][i] = a[y][i];
a[y][i] = temp[i];
}
}
void Matrix3f::mult_row(int x, float c){
for(int i = 0; i < 3; i++){
a[x][i] *= c;
}
}
void Matrix3f::add_row(int x, int y, float c){
for(int i = 0; i < 3; i++){
a[x][i] += c * a[y][i];
}
}
void Matrix3f::get_eigenvektors(){
float eig[3];
float p = a[0][1] * a[0][1] + a[0][2] * a[0][2] + a[1][2] * a[1][2];
float q, r, phi;
Matrix3f B; Matrix3f I;
for (int i = 0; i < 3; i++) I.set(i, i, 1);
if ( p == 0 ){
eig[0] = a[0][0];
eig[1] = a[1][1];
eig[2] = a[2][2];
}
else {
q = (a[0][0] + a[1][1] + a[2][2]) / 3;
p = (a[0][0] - q) * (a[0][0] - q) +
(a[1][1] - q) * (a[1][1] - q) +
(a[2][2] - q) * (a[2][2] - q) + 2 * q;
p = sqrt( p / 6 );
B = ((*this) - I * q);
B = B * (1 / p);
r = B.get_det();
if (r <= -1)
phi = M_PI / 3;
else if (r >= 1)
phi = 0;
else
phi = acos(r) / 3;
eig[0] = q + 2 * p * cos(phi);
eig[2] = q + 2 * p * cos(phi + M_PI * (2/3));
eig[1] = 3 * q - eig[0] - eig[2];
}
std::cout << eig[0] << ' ' << eig[1] << ' ' << eig[2] << ' ';
for (int i = 0; i < 3; i++) {
Matrix3f temp = (*this);
temp.set(0, 0, temp.get(0, 0) - eig[i]);
temp.set(1, 1, temp.get(1, 1) - eig[i]);
temp.set(2, 2, temp.get(2, 2) - eig[i]);
temp = temp.gauss();
std::cout << "Temp " << i << ":\n" << temp << "\n";
}
}
float Matrix3f::get_det(){
return a[0][0] * a[1][1] * a[2][2] + a[0][1] * a[1][2] * a[2][0] + a[0][2] * a[1][0] * a[2][1] -
a[0][2] * a[1][1] * a[2][0] - a[0][1] * a[1][0] * a[2][2] - a[0][0] * a[1][2] * a[2][1];
}
Matrix3f Matrix3f::gauss(){
Matrix3f * temp = new Matrix3f;
temp = this;
for (int i = 0; i < 3; i++){
if(temp->get(i, i) == 0){
for (int j = i; j < 3; j++){
if(temp->get(j, i) != 0){
temp->chg_row(i, j);
break;
}
}
if(temp->get(i, i) == 0){
std::cout << "Parameter solotion!!\n";
break;
}
}
float mult_val = temp->get(i, i);
temp->mult_row(i, 1 / mult_val );
for (int j = 0; j < 3; j++){
if(i != j){
float mult_val = -temp->get(j, i);
temp->add_row(j, i, mult_val);
}
}
}
std::cout << "Temp " << ":\n" << *temp << "\n";
std::cout << "This " << ":\n" << *this << "\n";
return *temp;
}
Matrix3f Matrix3f::get_inverse(){
Matrix3f temp = (*this) , inverse;
for (int i = 0; i < 3; i++) inverse.set(i, i, 1);
for (int i = 0; i < 3; i++){
if(temp.get(i, i) == 0){
for (int j = i; j < 3; j++){
if(temp.get(j, i) != 0){
temp.chg_row(i, j);
inverse.chg_row(i, j);
std::cout << "Change row " << i << " and " << j << ".\n";
std::cout << temp << '\n';
std::cout << inverse << '\n';
break;
}
}
if(temp.get(i, i) == 0){
std::cout << "Singularity!\n";
break;
}
}
float mult_val = temp.get(i, i);
temp.mult_row(i, 1 / mult_val );
inverse.mult_row(i, 1 / mult_val );
std::cout << "Divide row " << i << " by " << mult_val << ".\n";
std::cout << temp << '\n';
std::cout << inverse << '\n';
for (int j = 0; j < 3; j++){
if(i != j){
float mult_val = -temp.get(j, i);
temp.add_row(j, i, mult_val);
inverse.add_row(j, i, mult_val);
std::cout << "Multiply row " << i << " by " << mult_val << " and adding it to " << j << ".\n";
std::cout << temp << '\n';
std::cout << inverse << '\n';
}
}
}
return inverse;
}
Matrix3f Matrix3f::operator= (Matrix3f param){
Matrix3f temp;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
temp.set(i, j, param.get(i, j));
}
}
return temp;
}
Matrix3f Matrix3f::operator+ (Matrix3f param){
Matrix3f temp;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
temp.set(i, j, a[i][j] + param.get(i, j));
}
}
return temp;
}
Matrix3f Matrix3f::operator- (Matrix3f param){
Matrix3f temp;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
temp.set(i, j, a[i][j] - param.get(i, j));
}
}
return temp;
}
Matrix3f Matrix3f::operator* (float param){
Matrix3f temp;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
temp.set(i, j, a[i][j] * param);
}
}
return temp;
}
Matrix3f Matrix3f::operator* (Matrix3f param){
Matrix3f temp;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
temp.set(i, j, a[i][0] * param.get(0,j) +
a[i][1] * param.get(1,j) +
a[i][2] * param.get(2,j) );
}
}
return temp;
}
std::ostream& operator << ( std::ostream& os, Matrix3f m ){
for(int i = 0; i < 3; i++){
os << "(";
for(int j = 0; j < 3; j++){
os << m.get(i, j) << ",\t";
}
os << ") \n";
}
return os;
}