-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolver.cpp
More file actions
48 lines (46 loc) · 853 Bytes
/
Solver.cpp
File metadata and controls
48 lines (46 loc) · 853 Bytes
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
#include "Solver.h"
#include <cmath>
using std::fabs;
Solver::Solver(const Mat4& m,const myVec4d& v)
{
this->mat = m;
this->vec = v;
}
myVec4d Solver::solve(double eps)//使用类里的矩阵,解非齐次线性方程组
{
myVec4d ret;
for (int i = 0; i < 4; i++)
{
int j = 0;
while (j < 4 && fabs(mat.mat[i][j]) < eps)
{
j++;
}
if (j == 4) continue;
for (int k = 0; k < 4; k++)
{
if (k != i)
{
double rate = mat.mat[k][j] / mat.mat[i][j];
for (int l = 0; l < 4; l++)
mat.mat[k][l] -= mat.mat[i][l] * rate;
vec.data[k] -= vec.data[i] * rate;
}
}
}
for (int i = 0; i < 4; i++)
{
int j = 0;
while (j < 4 && fabs(mat.mat[i][j]) < eps)
{
j++;
}
if (j == 4)
{
return myVec4d(0.0, 0.0, 0.0, -1.0);
}
ret.data[i] = vec.data[i] / mat.mat[i][j];
}
ret.data[4] = 1.0;
return ret;
}