void SolutionSlau simpleIterarionMetho std vector double int size size

 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
void SolutionSlau::simpleIterarionMethod(std::vector<double> &x)
{
int size = A.size();
std::vector<double> xNext(size);
double norm;
std::vector<int> max(size);
double maxi;
for (int i = 0; i < size; ++i)
{
maxi = 0;
for (int j = 0; j < size; ++j)
{
if (A[i][j] > maxi)
{
maxi = A[i][j];
max[i] = j;
}
}
}
for (int i = 0; i < size - 1; ++i)
{
for (int j = 0; j < size; ++j)
{
if (i == max[j])
{
std::vector<double> t = A[i];
A[i] = A[j];
A[j] = t;
double temp = b[i];
b[i] = b[j];
b[j] = temp;
break;
}
}
}
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;
double t = b[i];
b[i] = b[j];
b[j] = t;
break;
}
}
}
}
for (int i = 0; i < size; ++i)
{
x[i] = b[i] / A[i][i];
}
do
{
for (int i = 0; i < size; ++i)
{
xNext[i] = b[i];
for (int j = 0; j < size; ++j)
{
if (j != i)
{
xNext[i] -= A[i][j] * x[j];
}
}
xNext[i] /= A[i][i];
}
norm = fabs(xNext[0] - x[0]);
for (int i = 0; i < size; ++i)
{
norm = std::max(fabs(xNext[i] - x[i]), norm);
x[i] = xNext[i];
}
} while (norm > eps);
}