void SolutionSlau jacobi std vector double double maxNorm int size siz

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