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