void SolutionSlau degreeMethod double lMax double lMax2 std vector dou

 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
void SolutionSlau::degreeMethod(double &lMax, double &lMax2, std::vector<double> &xMax, std::vector<double> &xMax2)
{
int size = A.size();
std::vector<double> yCur(size, 1);
std::vector<double> xCur(size, 1);
std::vector<double> yNext(size);
std::vector<double> xNext(size);
std::vector<double> lambda(size, 0);
std::vector<double> lambdaNext(size);
for (int k = 0; k < 100000; ++k)
{
for (int i = 0; i < size; ++i)
{
yNext[i] = 0;
for (int j = 0; j < size; j++){
yNext[i] += A[i][j] * xCur[j];
}
lambdaNext[i] = yNext[i] / xCur[i];
}
double n = 0;
for (int i = 0; i < size; ++i)
n = std::max(n, fabs(yNext[i]));
for (int i = 0; i < size; ++i)
xNext[i] = yNext[i] / n;
std::swap(xNext, xCur);
std::swap(yNext, yCur);
std::swap(lambda, lambdaNext);
double t = 0;
for (int i = 0; i < size; ++i)
t = std::max(t, fabs(lambda[i] - lambdaNext[i]));
if (t < eps)
break;
}
xMax = xCur;
lMax = 0;
for (int i = 0; i < size; ++i)
lMax += lambda[i];
lMax /= size;
}