3427
|
1 ## Copyright (C) 1993, 1998, 1999 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. |
3427
|
19 |
3439
|
20 ## -*- texinfo -*- |
5555
|
21 ## @deftypefn {Function File} {[@var{u}, @var{h}, @var{nu}] =} krylov (@var{a}, @var{v}, @var{k}, @var{eps1}, @var{pflg}) |
|
22 ## Construct an orthogonal basis @var{u} of block Krylov subspace |
|
23 ## |
|
24 ## @example |
|
25 ## [v a*v a^2*v ... a^(k+1)*v] |
|
26 ## @end example |
|
27 ## |
|
28 ## @noindent |
|
29 ## Using Householder reflections to guard against loss of orthogonality. |
3427
|
30 ## |
5555
|
31 ## If @var{v} is a vector, then @var{h} contains the Hessenberg matrix |
|
32 ## such that @code{a*u == u*h}. Otherwise, @var{h} is meaningless. |
|
33 ## |
|
34 ## The value of @var{nu} is the dimension of the span of the krylov |
|
35 ## subspace (based on @var{eps1}). |
|
36 ## |
|
37 ## If @var{b} is a vector and @var{k} is greater than @var{m-1}, then |
|
38 ## @var{h} contains the Hessenberg decompostion of @var{a}. |
|
39 ## |
|
40 ## The optional parameter @var{eps1} is the threshold for zero. The |
|
41 ## default value is 1e-12. |
|
42 ## |
|
43 ## If the optional parameter @var{pflg} is nonzero, row pivoting is used |
|
44 ## to improve numerical behavior. The default value is 0. |
3427
|
45 ## |
|
46 ## Reference: Hodel and Misra, "Partial Pivoting in the Computation of |
5555
|
47 ## Krylov Subspaces", to be submitted to Linear Algebra and its |
|
48 ## Applications |
3439
|
49 ## @end deftypefn |
|
50 |
|
51 ## Author: A. Scottedward Hodel <a.s.hodel@eng.auburn.edu> |
3240
|
52 |
4609
|
53 function [Uret, H, nu] = krylov (A, V, k, eps1, pflg); |
3240
|
54 |
|
55 defeps = 1e-12; |
3457
|
56 |
|
57 if (nargin < 3 || nargin > 5) |
6046
|
58 print_usage (); |
3457
|
59 elseif (nargin < 5) |
3240
|
60 pflg = 0; # default permutation flag |
|
61 endif |
3457
|
62 |
3240
|
63 if(nargin < 4) |
|
64 eps1 = defeps; # default tolerance parameter |
|
65 endif |
3273
|
66 |
3457
|
67 if (isempty (eps1)) |
|
68 eps1 = defeps; |
|
69 endif |
|
70 |
4030
|
71 na = issquare (A); |
3457
|
72 if (! na) |
|
73 error ("A(%d x %d) must be square", rows (A), columns (A)); |
3240
|
74 endif |
3225
|
75 |
3457
|
76 [m, kb] = size(V); |
|
77 if (m != na) |
|
78 error("A(%d x %d), V(%d x %d): argument dimensions do not match", |
|
79 na, na, m, kb) |
3240
|
80 endif |
3233
|
81 |
4030
|
82 if (! isscalar (k)) |
3457
|
83 error ("krylov: third argument must be a scalar integer"); |
|
84 endif |
|
85 |
|
86 Vnrm = norm (V, Inf); |
3273
|
87 |
3457
|
88 ## check for trivial solution |
|
89 if (Vnrm == 0) |
|
90 Uret = []; |
4609
|
91 H = []; |
3457
|
92 nu = 0; |
|
93 return; |
3211
|
94 endif |
|
95 |
3240
|
96 # identify trivial null space |
3457
|
97 abm = max (abs ([A, V]')); |
|
98 zidx = find (abm == 0); |
3240
|
99 |
|
100 # set up vector of pivot points |
|
101 pivot_vec = 1:na; |
3225
|
102 |
3240
|
103 iter = 0; |
3273
|
104 alpha = []; |
3240
|
105 nh = 0; |
3457
|
106 while (length(alpha) < na) && (columns(V) > 0) && (iter < k) |
3240
|
107 iter++; |
3211
|
108 |
3457
|
109 ## get orthogonal basis of V |
3240
|
110 jj = 1; |
3457
|
111 while (jj <= columns (V) && length (alpha) < na) |
|
112 ## index of next Householder reflection |
|
113 nu = length(alpha)+1; |
3273
|
114 |
3240
|
115 short_pv = pivot_vec(nu:na); |
|
116 q = V(:,jj); |
|
117 short_q = q(short_pv); |
3211
|
118 |
3457
|
119 if (norm (short_q) < eps1) |
|
120 ## insignificant column; delete |
|
121 nv = columns (V); |
|
122 if (jj != nv) |
|
123 [V(:,jj), V(:,nv)] = swap (V(:,jj), V(:,nv)); |
5775
|
124 ## FIXME -- H columns should be swapped too. Not done |
3457
|
125 ## since Block Hessenberg structure is lost anyway. |
3240
|
126 endif |
|
127 V = V(:,1:(nv-1)); |
3457
|
128 ## one less reflection |
|
129 nu--; |
3240
|
130 else |
3457
|
131 ## new householder reflection |
|
132 if (pflg) |
|
133 ## locate max magnitude element in short_q |
|
134 asq = abs (short_q); |
|
135 maxv = max (asq); |
6024
|
136 maxidx = find (asq == maxv, 1); |
|
137 pivot_idx = short_pv(maxidx); |
3273
|
138 |
3457
|
139 ## see if need to change the pivot list |
|
140 if (pivot_idx != pivot_vec(nu)) |
6024
|
141 swapidx = maxidx + (nu-1); |
3457
|
142 [pivot_vec(nu), pivot_vec(swapidx)] = ... |
|
143 swap (pivot_vec(nu), pivot_vec(swapidx)); |
3240
|
144 endif |
|
145 endif |
3273
|
146 |
3457
|
147 ## isolate portion of vector for reflection |
3240
|
148 idx = pivot_vec(nu:na); |
|
149 jdx = pivot_vec(1:nu); |
|
150 |
3457
|
151 [hv, av, z] = housh (q(idx), 1, 0); |
3240
|
152 alpha(nu) = av; |
|
153 U(idx,nu) = hv; |
3211
|
154 |
3240
|
155 # reduce V per the reflection |
|
156 V(idx,:) = V(idx,:) - av*hv*(hv' * V(idx,:)); |
|
157 if(iter > 1) |
5775
|
158 ## FIXME -- not done correctly for block case |
3240
|
159 H(nu,nu-1) = V(pivot_vec(nu),jj); |
|
160 endif |
|
161 |
3457
|
162 ## advance to next column of V |
|
163 jj++; |
3240
|
164 endif |
|
165 endwhile |
3211
|
166 |
3457
|
167 ## check for oversize V (due to full rank) |
|
168 if ((columns (V) > na) && (length (alpha) == na)) |
|
169 ## trim to size |
3273
|
170 V = V(:,1:na); |
3457
|
171 elseif (columns(V) > na) |
3273
|
172 krylov_V = V |
|
173 krylov_na = na |
3457
|
174 krylov_length_alpha = length (alpha) |
|
175 error ("This case should never happen; submit a bug report"); |
3273
|
176 endif |
|
177 |
3457
|
178 if (columns (V) > 0) |
|
179 ## construct next Q and multiply |
|
180 Q = zeros (size (V)); |
|
181 for kk = 1:columns (Q) |
3240
|
182 Q(pivot_vec(nu-columns(Q)+kk),kk) = 1; |
|
183 endfor |
3273
|
184 |
3457
|
185 ## apply Householder reflections |
3240
|
186 for ii = nu:-1:1 |
|
187 idx = pivot_vec(ii:na); |
|
188 hv = U(idx,ii); |
|
189 av = alpha(ii); |
|
190 Q(idx,:) = Q(idx,:) - av*hv*(hv'*Q(idx,:)); |
|
191 endfor |
3211
|
192 endif |
|
193 |
3457
|
194 ## multiply to get new vector; |
3240
|
195 V = A*Q; |
3457
|
196 ## project off of previous vectors |
|
197 nu = length (alpha); |
|
198 for i = 1:nu |
|
199 hv = U(:,i); |
|
200 av = alpha(i); |
3240
|
201 V = V - av*hv*(hv'*V); |
|
202 H(i,nu-columns(V)+(1:columns(V))) = V(pivot_vec(i),:); |
|
203 end |
|
204 |
3211
|
205 endwhile |
|
206 |
3457
|
207 ## Back out complete U matrix |
|
208 ## back out U matrix ; |
|
209 j1 = columns (U); |
|
210 for i = j1:-1:1; |
3240
|
211 idx = pivot_vec(i:na); |
|
212 hv = U(idx,i); |
|
213 av = alpha(i); |
3457
|
214 U(:,i) = zeros (na, 1); |
3240
|
215 U(idx(1),i) = 1; |
|
216 U(idx,i:j1) = U(idx,i:j1)-av*hv*(hv'*U(idx,i:j1)); |
3211
|
217 endfor |
3273
|
218 |
3457
|
219 nu = length (alpha); |
3240
|
220 Uret = U; |
3457
|
221 if (max (max (abs (Uret(zidx,:)))) > 0) |
|
222 warning ("krylov: trivial null space corrupted; set pflg = 1 or eps1 > %e", |
|
223 eps1); |
3225
|
224 endif |
|
225 |
3211
|
226 endfunction |