-
Notifications
You must be signed in to change notification settings - Fork 7
/
bartelsStewart.m
235 lines (187 loc) · 6.07 KB
/
bartelsStewart.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
function X = bartelsStewart(A, B, C, D, E, split)
%BARTELSSTEWART Solve generalized Sylvester matrix equation.
% BARTELSSTEWART(A, B, C, D, E) solves the generalized Sylvester equation
%
% AXB^T + CXD^T = E
%
% using the Bartels--Stewart algorithm [1,2].
%
% BARTELSSTEWART(A, [], [], D, E) assumes B = I and C = I. This allows more
% efficient computation than passing identity matrices. Similarly,
% BARTELSSTEWART(A, B, [], [], E) assumes C = B and D = A.
%
% BARTELSSTEWART(A, B, C, D, E, SPLIT) also takes information SPLIT which
% states whether or not the problem may be decoupled into even and odd modes
% for efficiency.
%
% References:
% [1] R. H. Bartels & G. W. Stewart, Solution of the matrix equation
% AX +XB = C, Comm. ACM, 15, 820–826, 1972.
%
% [2] J. D. Gardiner, A. J. Laub, J. J. Amato, & C. B. Moler, Solution of the
% Sylvester matrix equation AXB^T + CXD^T = E, ACM Transactions on
% Mathematical Software (TOMS), 18(2), 223-231, 1992.
% Copyright 2014
% Written by Alex Townsend, Sept 2012. ([email protected])
% Modified by Nick Hale, Nov 2014. ([email protected])
% Parse inputs:
if ( nargin < 6 )
split = [false ; false];
end
% Fixed tolerance:
tol = 10*eps;
% If the RHS is zero then the solution is the zero solution (assuming
% uniqueness).
if ( norm(E) < tol )
X = zeros(size(E));
return
end
% Treat the standard Lyapunov and Sylvester equations as special cases:
if ( isempty(B) && isempty(C) )
X = lyap(A, D.', -E);
return
end
% Matrices must be full for QZ():
A = full(A); B = full(B); C = full(C); D = full(D);
% Solution will be a m by n matrix.
[m, n] = size(E);
Y = zeros(m, n);
% Look for problems of the form AXB^T + BXA^T = E
if ( isempty(C) && isempty(D) )
AEQ = true;
C = B;
else
AEQ = false;
end
if ( isempty(C) )
% [Z1, P] = schur(A, 'complex');
[Z1, P] = schur(A);
Q1 = Z1';
S = eye(m);
elseif ( split(2) )
% If the equation is even/odd in the x-direction then we can split the
% problem into two subproblems. This is equivalent to qz(A,C), but faster.
[P, S, Q1, Z1] = qzSplit(A, C);
else
[P, S, Q1, Z1] = qz(A, C);
end
if ( AEQ )
T = P;
R = S;
Q2 = Q1;
Z2 = Z1;
elseif ( isempty(B) )
% [Z2, T] = schur(D, 'complex');
[Z2, T] = schur(D);
Q2 = Z2';
R = eye(n);
elseif ( split(1) )
% If the PDE is even/odd in the y-direction then we can split (further) into
% double as many subproblems.
[T, R, Q2, Z2] = qzSplit(D, B);
else
[T, R, Q2, Z2] = qz(D, B);
end
% Now use the generalised Bartels--Stewart solver found in Gardiner et al.
% (1992). The Sylvester matrix equation now contains quasi upper-triangular
% matrices and we can do a backwards substitution.
% transform the righthand side.
F = Q1*E*Q2.';
% Initialise S*Y and P*Y factors:
PY = zeros(m, n);
SY = zeros(m, n);
% Do a backwards substitution type algorithm to construct the solution.
k = n;
% Construct columns n,n-1,...,3,2,1 of the transformed solution.
while ( k > 0 )
% There are two cases, either the subdiagonal contains a zero, i.e.,
% T(k,k-1) = 0 and then it is simply a backwards substitution, or T(k,k-1)
% ~= 0 and we solve a 2mx2m system.
if ( k == 1 || T(k,k-1) == 0 )
% Simple case (Usually end up here).
jj = (k+1):n;
rhs = F(:,k) - PY(:,jj)*R(k,jj).' - SY(:,jj)*T(k,jj).';
% rhs = F(:,k) - PY*R(k,:).' - SY*T(k,:).';
% Find the kth column of the transformed solution.
tmp = (P + (T(k,k)/R(k,k))*S); % <- Divide both sides by R_kk for speed.
rhs = rhs/R(k,k);
Y(:,k) = tmp \ rhs;
% Store S*Y and P*Y factors:
PY(:,k) = P*Y(:,k);
SY(:,k) = S*Y(:,k);
% Go to next column:
k = k - 1;
else
% This is a straight copy from the Gardiner et al. paper, and just
% solves for two columns at once. (Works because of quasi-triangular
% matrices.)
% Operator reduction.
jj = (k+1):n;
rhs1 = F(:,k-1) - PY(:,jj)*R(k-1,jj).' - SY(:,jj)*T(k-1,jj).';
rhs2 = F(:,k) - PY(:,jj)*R(k,jj).' - SY(:,jj)*T(k,jj).';
% 2 by 2 system.
SM = [R(k-1,k-1)*P + T(k-1,k-1)*S , R(k-1,k)*P + T(k-1,k)*S ;
T(k,k-1)*S , R(k,k)*P + T(k,k)*S ];
% Solve.
% UM = SM \ [rhs1 ; rhs2];
% Solve (permute the columns and rows):
idx = reshape([(1:m) ; (m+1:2*m)], 2*m, 1);
rhs = [rhs1 ; rhs2];
UM = SM(idx,idx) \ rhs(idx);
UM(idx) = UM;
% Store S*Y and P*Y factors:
Y(:,k-1:k) = reshape(UM, m, 2);
PY(:,k-1:k) = P*Y(:,k-1:k);
SY(:,k-1:k) = S*Y(:,k-1:k);
% We solved for two columns so go two columns farther.
k = k - 2;
end
end
% We have now computed the transformed solution so we just transform it back.
X = Z1*Y*Z2.';
end
function [P, S, Q1, Z1] = qzSplit(A, C)
%QZSPLIT A faster QZ factorisation for problems that decouple.
% QZSPLIT() is equivalent to standard QZ, except we take account of symmetry
% to reduce the computational requirements.
% Matrix size (square):
n = size(A, 1);
% Do the QZ by splitting the problem into two subproblems.
% Odd part:
odd = 1:2:n;
A1 = A(odd, odd);
C1 = C(odd, odd);
[P1, S1, Q1, Z1] = qz(A1, C1);
% Even part:
even = 2:2:n;
A2 = A(even, even);
C2 = C(even, even);
[P2, S2, Q2, Z2] = qz(A2, C2);
% Recombine:
[P, S, Q1, Z1] = qzRecombine(P1, P2, S1, S2, Q1, Q2, Z1, Z2);
end
function [P, S, Q, Z] = qzRecombine(P1, P2, S1, S2, Q1, Q2, Z1, Z2)
%QZRECOMBINE Recombine subproblems to form the QZ factorization.
% Determine the indexing:
hf1 = size(P1, 1);
n = 2*hf1 - 1;
top = 1:hf1;
bot = (hf1+1):n;
odd = 1:2:n;
even = 2:2:n;
% Push the subproblem back together:
P = blkdiag(P1, P2);
% P = zeros(n);
% P(top,top) = P1;
% P(bot,bot) = P2;
S = blkdiag(S1, S2);
% S = zeros(n);
% S(top,top) = S1;
% S(bot,bot) = S2;
Q = zeros(n);
Q(top,odd) = Q1;
Q(bot,even) = Q2;
Z = zeros(n);
Z(odd, top) = Z1;
Z(even,bot) = Z2;
end