-
Notifications
You must be signed in to change notification settings - Fork 1
/
libKrylov.cpp
202 lines (147 loc) · 3.54 KB
/
libKrylov.cpp
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <cmath>
#include <iostream>
#include <cassert>
#include <omp.h>
#include "Vector.h"
#include "Matrix.h"
template <typename T, typename U>
typename std::common_type<T, U>::type
dot(const Vector<T> &lhs,
const Vector<U> &rhs)
{
int i;
int M = lhs.len();
typename std::common_type<T, U>::type sum = 0;
#pragma omp parallel for private(i) reduction(+ : sum)
for (i = 0; i < M; i++)
{
sum += (lhs[i] * rhs[i]);
}
return sum;
}
template <typename T, typename U>
auto mult(const Matrix<T> &lhs,
const Vector<U> &rhs)
{
int i, j;
int M = lhs.cols();
int N = lhs.rows();
Vector<typename std::common_type<T, U>::type> sum(M);
#pragma omp parallel private(i, j)
{
#pragma omp for schedule(static)
for (int i = 0; i < N; i++)
{
#pragma omp parallel shared(sum, lhs, rhs)
{
#pragma omp for schedule(static)
for (int j = 0; j < M; j++)
{
sum[i] = sum[i] + (lhs(i, j) * rhs[j]);
}
}
}
}
return sum;
}
template <typename T>
Matrix<T> lanczos(const Matrix<T> A, int N)
{
assert(A.rows() == A.cols());
int n = A.rows();
Vector<T> q_old(n);
Vector<T> q(n);
q[n - 1] = 1;
Vector<T> r = q;
Matrix<T> R(N - 1, N - 1);
Matrix<int> I(n, n);
I.identity();
I.print();
double b = 1;
double a = 0;
for (int i = 0; i < N; i++)
{
q = r / b;
a = dot(q, mult(A, q));
auto d = q_old * b;
auto e = A - (I * a);
auto f = mult(e, q);
r = f - d;
R(i, i) = a;
b = r.norm();
if (b < 1e-8)
return R.resize(i);
if (i < N - 1)
{
R(i, i + 1) = b;
R(i + 1, i) = b;
}
q_old = q;
}
return R;
}
template <typename T>
Matrix<T> arnoldi(const Matrix<T> A, int N)
{
assert(A.rows() == A.cols());
int n = A.rows();
Vector<T> q(n);
q[n - 1] = 1;
Vector<T> r = q;
Matrix<T> H(N + 1, N);
Matrix<T> Q(n, N + 1);
for (int k = 0; k < n; k++)
{
Q(k, 0) = q[k];
}
double b = 1;
for (int i = 0; i < N; i++)
{
r = mult(A, q);
for (int j = 0; j < i + 1; j++)
{
for (int k = 0; k < n; k++)
{
q[k] = Q(k, j);
}
H(j, i) = dot(q, r);
r = r - q * H(j, i);
}
b = r.norm();
std::cout << b << std::endl;
H(i + 1, i) = b;
if (b < 1e-8)
{
// return Q.resize(n-1,i);
return H;
}
for (int k = 0; k < n; k++)
{
Q(k, i + 1) = r[k] / b;
}
}
return Q; //, H;
}
template <typename T, typename U>
typename std::common_type<T, U>::type
power(const Matrix<T> A, Vector<U> q_old, int N)
{
Vector<typename std::common_type<T, U>::type> z(A.cols());
auto q = q_old;
typename std::common_type<T, U>::type lambda;
typename std::common_type<T, U>::type lambda_old;
lambda = 0;
for (int k = 0; k < N; k++)
{
lambda_old = lambda;
z = mult(A, q_old);
q = z / z.norm();
lambda = dot(q_old, z);
q_old = q;
if (fabs(lambda - lambda_old) < 1e-8)
{
return lambda;
}
}
return lambda;
}