3431
|
1 ## Copyright (C) 1996, 1998 Auburn University. All rights reserved. |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by the |
|
7 ## Free Software Foundation; either version 2, or (at your option) any |
|
8 ## later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but WITHOUT |
|
11 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 ## for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301 USA. |
3431
|
19 |
|
20 ## -*- texinfo -*- |
5016
|
21 ## @deftypefn {Function File} {@var{x} =} zgscal (@var{f}, @var{z}, @var{n}, @var{m}, @var{p}) |
3431
|
22 ## Generalized conjugate gradient iteration to |
|
23 ## solve zero-computation generalized eigenvalue problem balancing equation |
5016
|
24 ## @math{fx=z}; called by @command{zgepbal}. |
3431
|
25 ## @end deftypefn |
|
26 |
|
27 ## References: |
|
28 ## ZGEP: Hodel, "Computation of Zeros with Balancing," 1992, submitted to LAA |
|
29 ## Generalized CG: Golub and Van Loan, "Matrix Computations, 2nd ed" 1989 |
|
30 |
|
31 ## Author: A. S. Hodel <a.s.hodel@eng.auburn.edu> |
|
32 ## Created: July 24, 1992 |
|
33 ## Conversion to Octave R. Bruce Tenison July 3, 1994 |
|
34 |
|
35 function x = zgscal (a, b, c, d, z, n, m, p) |
|
36 |
|
37 ## initialize parameters: |
|
38 ## Givens rotations, diagonalized 2x2 block of F, gcg vector initialization |
|
39 |
|
40 nmp = n+m+p; |
|
41 |
|
42 ## x_0 = x_{-1} = 0, r_0 = z |
|
43 x = zeros(nmp,1); |
|
44 xk1 = x; |
|
45 xk2 = x; |
|
46 rk1 = z; |
|
47 k = 0; |
|
48 |
|
49 ## construct balancing least squares problem |
|
50 F = eye(nmp); |
|
51 for kk=1:nmp |
|
52 F(1:nmp,kk) = zgfmul(a,b,c,d,F(:,kk)); |
|
53 endfor |
|
54 |
|
55 [U,H,k1] = krylov(F,z,nmp,1e-12,1); |
4030
|
56 if(!issquare(H)) |
3431
|
57 if(columns(H) != k1) |
|
58 error("zgscal(tzero): k1=%d, columns(H)=%d",k1,columns(H)); |
|
59 elseif(rows(H) != k1+1) |
|
60 error("zgscal: k1=%d, rows(H) = %d",k1,rows(H)); |
|
61 elseif ( norm(H(k1+1,:)) > 1e-12*norm(H,"inf") ) |
|
62 zgscal_last_row_of_H = H(k1+1,:) |
|
63 error("zgscal: last row of H nonzero (norm(H)=%e)",norm(H,"inf")) |
|
64 endif |
|
65 H = H(1:k1,1:k1); |
|
66 U = U(:,1:k1); |
|
67 endif |
|
68 |
|
69 ## tridiagonal H can still be rank deficient, so do permuted qr |
|
70 ## factorization |
|
71 [qq,rr,pp] = qr(H); # H = qq*rr*pp' |
|
72 nn = rank(rr); |
|
73 qq = qq(:,1:nn); |
|
74 rr = rr(1:nn,:); # rr may not be square, but "\" does least |
|
75 xx = U*pp*(rr\qq'*(U'*z)); # squares solution, so this works |
|
76 ## xx1 = pinv(F)*z; |
|
77 ## zgscal_x_xx1_err = [xx,xx1,xx-xx1] |
|
78 return; |
|
79 |
|
80 ## the rest of this is left from the original zgscal; |
|
81 ## I've had some numerical problems with the GCG algorithm, |
|
82 ## so for now I'm solving it with the krylov routine. |
|
83 |
|
84 ## initialize residual error norm |
|
85 rnorm = norm(rk1,1); |
|
86 |
|
87 xnorm = 0; |
|
88 fnorm = 1e-12 * norm([a,b;c,d],1); |
|
89 |
|
90 ## dummy defines for MATHTOOLS compiler |
|
91 gamk2 = 0; omega1 = 0; ztmz2 = 0; |
|
92 |
|
93 ## do until small changes to x |
|
94 len_x = length(x); |
|
95 while ((k < 2*len_x) & (xnorm> 0.5) & (rnorm>fnorm))|(k == 0) |
|
96 k = k+1; |
|
97 |
|
98 ## solve F_d z_{k-1} = r_{k-1} |
|
99 zk1= zgfslv(n,m,p,rk1); |
|
100 |
|
101 ## Generalized CG iteration |
|
102 ## gamk1 = (zk1'*F_d*zk1)/(zk1'*F*zk1); |
|
103 ztMz1 = zk1'*rk1; |
|
104 gamk1 = ztMz1/(zk1'*zgfmul(a,b,c,d,zk1)); |
|
105 |
|
106 if(rem(k,len_x) == 1) omega = 1; |
|
107 else omega = 1/(1-gamk1*ztMz1/(gamk2*omega1*ztmz2)); |
|
108 endif |
|
109 |
|
110 ## store x in xk2 to save space |
|
111 xk2 = xk2 + omega*(gamk1*zk1 + xk1 - xk2); |
|
112 |
|
113 ## compute new residual error: rk = z - F xk, check end conditions |
|
114 rk1 = z - zgfmul(a,b,c,d,xk2); |
|
115 rnorm = norm(rk1); |
|
116 xnorm = max(abs(xk1 - xk2)); |
|
117 |
|
118 ## printf("zgscal: k=%d, gamk1=%e, gamk2=%e, \nztMz1=%e ztmz2=%e\n", ... |
|
119 ## k,gamk1, gamk2, ztMz1, ztmz2); |
|
120 ## xk2_1_zk1 = [xk2 xk1 zk1] |
|
121 ## ABCD = [a,b;c,d] |
|
122 ## prompt |
|
123 |
|
124 ## get ready for next iteration |
|
125 gamk2 = gamk1; |
|
126 omega1 = omega; |
|
127 ztmz2 = ztMz1; |
|
128 [xk1,xk2] = swap(xk1,xk2); |
|
129 endwhile |
|
130 x = xk2; |
|
131 |
|
132 ## check convergence |
|
133 if (xnorm> 0.5 & rnorm>fnorm) |
|
134 warning("zgscal(tzero): GCG iteration failed; solving with pinv"); |
|
135 |
|
136 ## perform brute force least squares; construct F |
|
137 Am = eye(nmp); |
|
138 for ii=1:nmp |
|
139 Am(:,ii) = zgfmul(a,b,c,d,Am(:,ii)); |
|
140 endfor |
|
141 |
|
142 ## now solve with qr factorization |
|
143 x = pinv(Am)*z; |
|
144 endif |
|
145 endfunction |