-
Notifications
You must be signed in to change notification settings - Fork 8
/
rk_sylv.m
191 lines (157 loc) · 5.37 KB
/
rk_sylv.m
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
function [Xu, Xv, VA, VB] = rk_sylv(poles, A, B, u, v, k, tol, debug, nrmtype)
%RK_SYLV Approximate the solution of a Sylvester equation AX + XB' = U*V'.
%
% [XU,XV] = RK_SYLV(POLES, A, B, U, V, K) approximates the solution of the
% Sylvester equation in the factored form XU * XV'. The variable POLES
% is a 2 x N matrix containing the poles to use in the rational Krylov
% method. The poles for A are on the first row, the ones for B on the
% second one.
%
% [XU, VA] = RK_SYLV(POLES, A, B, U, V, K, TOL, DEBUG) also returns the
% bases VA and VB, and the optional parameters TOL and DEBUG control
% the stopping criterion and the debugging during the iteration.
%
% The tolerance TOL can also be specified as a function TOL(R, N) that
% takes as input the residual norm and the norm of the solution (R and N,
% respectively), and returns true if the solution can be accepted.
if ~exist('debug', 'var')
debug = false;
end
if ~exist('tol', 'var')
tol = 1e-8;
end
if ~exist('nrmtype', 'var')
nrmtype = 2;
end
if size(u, 2) ~= size(v, 2)
error('RK_SYLV:: different number of columns in the factorization of the rhs');
end
if size(u, 2) == 0
Xu = u;
Xv = v;
VA = u;
VB = v;
return;
end
if ~isstruct(A)
AA = rk_struct(A);
else
AA = A;
end
if ~isstruct(B)
BB = rk_struct(B');
else
BB = B';
end
nrmA = AA.nrm;
nrmB = BB.nrm;
if isfloat(poles) && size(poles, 1) == 1
poles = [poles; poles];
end
% Dimension of the space
sa = size(u, 2);
sb = size(v, 2);
bsa = sa;
bsb = sb;
% tol can be function tol(r, n) that is given the residual and the norm, or
% a scalar. In the latter case, we turn it into a function
if isfloat(tol)
tol_eps = tol;
tol = @(r, nrm) r < tol_eps * nrm;
end
it=1;
% Counter for the vector of poles
counter = 1;
residuals = [];
% rk_krylov = @rat_krylov;
[VA, KA, HA, param_A] = rk_krylov(AA, u, inf);
[VB, KB, HB, param_B] = rk_krylov(BB, v, inf);
Cprojected = (VA(:,1:bsa)' * u) * (VB(:,1:bsb)'*v)';
while max(sa-bsa, sb-bsb) < k
poleA = poles(1, counter);
poleB = poles(2, counter);
[VA, KA, HA, param_A] = rk_krylov(AA, VA, KA, HA, poleA, param_A);
[VB, KB, HB, param_B] = rk_krylov(BB, VB, KB, HB, poleB, param_B);
sa = size(VA, 2);
sb = size(VB, 2);
% We only compute the solution and residual of the projected Lyapunov
% equation in the first three iterations, and then use the fact that
% the convergence is expected to be linear to estimate the number of
% iterations requires to converge. This saves some Lyapunov dense
% solutions, which are relatively expensive.
check_residual_n = 4;
if it < check_residual_n
check_residual = true;
else
if it == check_residual_n
pp = polyfit(1:check_residual_n-1, log(residuals(1:check_residual_n-1)), 1);
if exist('tol_eps', 'var')
r1 = residuals(1) / norm(Y, nrmtype);
needed_iterations = ceil(...
(log(tol_eps) - log(r1)) / pp(1));
needed_iterations = min(20, needed_iterations);
else
j = it + 1;
nrmY = norm(Y, nrmtype);
while ~tol(exp(polyval(pp, j)), nrmY) && j <= 30
j = j + 1;
end
needed_iterations = min(30, j);
needed_iterations = 6;
end
end
check_residual = it >= needed_iterations;
end
%check_residual = true;
if check_residual
if poleA ~= inf
[~, KA2, HA2] = rk_krylov(AA, VA, KA, HA, inf, param_A);
else
KA2 = KA; HA2 = HA;
end
if poleB ~= inf
[~, KB2, HB2] = rk_krylov(BB, VB, KB, HB, inf, param_B);
else
KB2 = KB; HB2 = HB;
end
% Compute the solution and residual of the projected Lyapunov equation
%fprintf('cond KA = %e, cond KB = %e\n', cond(KA2(1:end-bsa,:)), cond(KB2(1:end-bsb,:)))
As = HA2 / KA2(1:end-bsa,:);
Bs = HB2 / KB2(1:end-bsb,:);
Cs = zeros(size(As, 1), size(Bs, 1));
Cs(1:size(u,2), 1:size(v,2)) = Cprojected;
[Y, res] = lyap_galerkin(As, Bs, Cs, bsa, bsb, nrmtype);
% temp=KA2(1:end-bsa, :)\Y;
% fprintf('res = %e\n', norm(temp(end-bsa+1:end, :)))
residuals(it) = res;
% You might want to enable this for debugging purposes
if debug
fprintf('%d Residue: %e\n', size(Y,1), res / norm(Y, nrmtype));
end
if tol(res, norm(Y, nrmtype)) % res < norm(Y) * tol
%fprintf('Needed iterations = %d, it = %d\n', needed_iterations, it);
%pause(1);
break
end
end
it = it + 1;
% Switch to the next poles for the next round
if isfloat(poles)
counter = mod(counter, size(poles, 2)) + 1;
else
counter = counter + 1;
end
end
[UU,SS,VV] = svd(Y);
switch nrmtype
case 2
s = diag(SS);
rk = sum( arrayfun(@(ss) tol(ss, s(1) / (nrmA + nrmB)), s) == 0);
case 'fro'
d = sort(diag(SS));
s = sqrt(cumsum(d.^2));
rk = sum( arrayfun(@(ss) tol(ss, s(end) / (nrmA + nrmB)), s) == 0 );
end
Xu = VA(:,1:size(Y,1)) * UU(:,1:rk) * sqrt(SS(1:rk,1:rk));
Xv = VB(:,1:size(Y,2)) * VV(:,1:rk) * sqrt(SS(1:rk,1:rk));
end