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 |
|
46 function [Uret,H,nu] = krylov(A,V,k,eps1,pflg); |
|
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 = []; |
|
84 nu = 0; |
|
85 return; |
3211
|
86 endif |
|
87 |
3240
|
88 # identify trivial null space |
3457
|
89 abm = max (abs ([A, V]')); |
|
90 nzidx = find (abm != 0); |
|
91 zidx = find (abm == 0); |
3240
|
92 |
|
93 # set up vector of pivot points |
|
94 pivot_vec = 1:na; |
3225
|
95 |
3240
|
96 iter = 0; |
3273
|
97 alpha = []; |
3240
|
98 nh = 0; |
3457
|
99 while (length(alpha) < na) && (columns(V) > 0) && (iter < k) |
3240
|
100 iter++; |
3211
|
101 |
3457
|
102 ## get orthogonal basis of V |
3240
|
103 jj = 1; |
3457
|
104 while (jj <= columns (V) && length (alpha) < na) |
|
105 ## index of next Householder reflection |
|
106 nu = length(alpha)+1; |
3273
|
107 |
3240
|
108 short_pv = pivot_vec(nu:na); |
|
109 q = V(:,jj); |
|
110 short_q = q(short_pv); |
3211
|
111 |
3457
|
112 if (norm (short_q) < eps1) |
|
113 ## insignificant column; delete |
|
114 nv = columns (V); |
|
115 if (jj != nv) |
|
116 [V(:,jj), V(:,nv)] = swap (V(:,jj), V(:,nv)); |
|
117 ## XXX FIXME XXX -- H columns should be swapped too. Not done |
|
118 ## since Block Hessenberg structure is lost anyway. |
3240
|
119 endif |
|
120 V = V(:,1:(nv-1)); |
3457
|
121 ## one less reflection |
|
122 nu--; |
3240
|
123 else |
3457
|
124 ## new householder reflection |
|
125 if (pflg) |
|
126 ## locate max magnitude element in short_q |
|
127 asq = abs (short_q); |
|
128 maxv = max (asq); |
|
129 maxidx = find (asq == maxv); |
3240
|
130 pivot_idx = short_pv(maxidx(1)); |
3273
|
131 |
3457
|
132 ## see if need to change the pivot list |
|
133 if (pivot_idx != pivot_vec(nu)) |
3240
|
134 swapidx = maxidx(1) + (nu-1); |
3457
|
135 [pivot_vec(nu), pivot_vec(swapidx)] = ... |
|
136 swap (pivot_vec(nu), pivot_vec(swapidx)); |
3240
|
137 endif |
|
138 endif |
3273
|
139 |
3457
|
140 ## isolate portion of vector for reflection |
3240
|
141 idx = pivot_vec(nu:na); |
|
142 jdx = pivot_vec(1:nu); |
|
143 |
3457
|
144 [hv, av, z] = housh (q(idx), 1, 0); |
3240
|
145 alpha(nu) = av; |
|
146 U(idx,nu) = hv; |
3211
|
147 |
3240
|
148 # reduce V per the reflection |
|
149 V(idx,:) = V(idx,:) - av*hv*(hv' * V(idx,:)); |
|
150 if(iter > 1) |
3457
|
151 ## XXX FIXME XXX -- not done correctly for block case |
3240
|
152 H(nu,nu-1) = V(pivot_vec(nu),jj); |
|
153 endif |
|
154 |
3457
|
155 ## advance to next column of V |
|
156 jj++; |
3240
|
157 endif |
|
158 endwhile |
3211
|
159 |
3457
|
160 ## check for oversize V (due to full rank) |
|
161 if ((columns (V) > na) && (length (alpha) == na)) |
|
162 ## trim to size |
3273
|
163 V = V(:,1:na); |
3457
|
164 elseif (columns(V) > na) |
3273
|
165 krylov_V = V |
|
166 krylov_na = na |
3457
|
167 krylov_length_alpha = length (alpha) |
|
168 error ("This case should never happen; submit a bug report"); |
3273
|
169 endif |
|
170 |
3457
|
171 if (columns (V) > 0) |
|
172 ## construct next Q and multiply |
|
173 Q = zeros (size (V)); |
|
174 for kk = 1:columns (Q) |
3240
|
175 Q(pivot_vec(nu-columns(Q)+kk),kk) = 1; |
|
176 endfor |
3273
|
177 |
3457
|
178 ## apply Householder reflections |
3240
|
179 for ii = nu:-1:1 |
|
180 idx = pivot_vec(ii:na); |
|
181 hv = U(idx,ii); |
|
182 av = alpha(ii); |
|
183 Q(idx,:) = Q(idx,:) - av*hv*(hv'*Q(idx,:)); |
|
184 endfor |
3211
|
185 endif |
|
186 |
3457
|
187 ## multiply to get new vector; |
3240
|
188 V = A*Q; |
3457
|
189 ## project off of previous vectors |
|
190 nu = length (alpha); |
|
191 for i = 1:nu |
|
192 hv = U(:,i); |
|
193 av = alpha(i); |
3240
|
194 V = V - av*hv*(hv'*V); |
|
195 H(i,nu-columns(V)+(1:columns(V))) = V(pivot_vec(i),:); |
|
196 end |
|
197 |
3211
|
198 endwhile |
|
199 |
3457
|
200 ## Back out complete U matrix |
|
201 ## back out U matrix ; |
|
202 j1 = columns (U); |
|
203 for i = j1:-1:1; |
3240
|
204 idx = pivot_vec(i:na); |
|
205 hv = U(idx,i); |
|
206 av = alpha(i); |
3457
|
207 U(:,i) = zeros (na, 1); |
3240
|
208 U(idx(1),i) = 1; |
|
209 U(idx,i:j1) = U(idx,i:j1)-av*hv*(hv'*U(idx,i:j1)); |
3211
|
210 endfor |
3273
|
211 |
3457
|
212 nu = length (alpha); |
3240
|
213 Uret = U; |
3457
|
214 if (max (max (abs (Uret(zidx,:)))) > 0) |
|
215 warning ("krylov: trivial null space corrupted; set pflg = 1 or eps1 > %e", |
|
216 eps1); |
3225
|
217 endif |
|
218 |
3211
|
219 endfunction |