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