inline void Mult const VLD ld VLD res res resize size for size_t 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
inline void Mult(const VLD& a, ld x, VLD& res)
{
res.resize(a.size());
for (size_t i = 0; i < a.size(); ++i)
res[i] = a[i] * x;
}
inline void Divide(const VLD& a, ld x, VLD& res)
{
res.resize(a.size());
for (size_t i = 0; i < a.size(); ++i)
res[i] = a[i] / x;
}
inline void PmAlgo(
const VVLD& A, VLD& eigVec, ld& eigVal,
const function<ld(const VLD&)> norm = getNorm, const ld EPS = 1e-5, const int maxIters = 10000)
{
int n = A.size();
VLD y(n, 1), yk(n);
VLD x(n), xk(n);
VLD lambda(n, 1e100), lambdak(n);
Divide(y, norm(y), x);
bool finded = false;
for (int iter = 0; iter < maxIters && !finded; ++iter)
{
MatrixMultiply(A, x, yk);
Divide(yk, norm(yk), xk);
ld maxdelta = 0;
for (int i = 0; i < n; ++i){
lambdak[i] = yk[i] / x[i];
maxdelta = max(maxdelta, fabsl(lambda[i] - lambdak[i]));
}
finded = (maxdelta <= EPS);
swap(x, xk);
swap(y, yk);
swap(lambda, lambdak);
}
eigVal = 0;
for (ld t : lambda) eigVal += t;
eigVal /= n;
eigVec = x;
}