void SolutionSlau obrMatrix std vector std vector double matrix std ve

 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
void SolutionSlau::obrMatrix(std::vector<std::vector<double> > matrix, std::vector<std::vector<double> > &res)
{
int size = A.size();
for (int j = 0; j < size; ++j)
{
if (A[j][j] == 0)
{
for (int i = j + 1; i < size; ++i)
{
if (A[i][j] != 0)
{
std::vector<double> temp = A[j];
A[j] = A[i];
A[i] = temp;
temp = res[j];
res[j] = res[i];
res[i] = temp;
break;
}
}
}
double t = A[j][j];
for (int i = 0; i < size; ++i)
{
A[j][i] /= t;
res[j][i] /= t;
}
for (int i = j + 1; i < size; ++i)
{
double t = A[i][j];
for (int k = 0; k < size; ++k)
{
A[i][k] -= A[j][k] * t;
res[i][k] -= t*res[j][k];
}
}
}
for (int j = size - 1; j >= 0; --j)
{
for (int i = j - 1; i >= 0; --i)
{
double t = A[i][j];
for (int k = 0; k < size; ++k)
{
A[i][k] -= A[j][k] * A[i][j];
res[i][k] -= t*res[j][k];
}
}
}
}