1 ## Copyright (C) 1993-2012 Auburn University. All rights reserved.
3 ## This file is part of Octave.
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {[@var{u}, @var{h}, @var{nu}] =} krylov (@var{A}, @var{V}, @var{k}, @var{eps1}, @var{pflg})
21 ## Construct an orthogonal basis @var{u} of block Krylov subspace
24 ## [v a*v a^2*v @dots{} a^(k+1)*v]
28 ## Using Householder reflections to guard against loss of orthogonality.
30 ## If @var{V} is a vector, then @var{h} contains the Hessenberg matrix
31 ## such that @nospell{@xcode{a*u == u*h+rk*ek'}}, in which @code{rk =
32 ## a*u(:,k)-u*h(:,k)}, and @nospell{@xcode{ek'}} is the vector
33 ## @code{[0, 0, @dots{}, 1]} of length @code{k}. Otherwise, @var{h} is
36 ## If @var{V} is a vector and @var{k} is greater than
37 ## @code{length(A)-1}, then @var{h} contains the Hessenberg matrix such
38 ## that @code{a*u == u*h}.
40 ## The value of @var{nu} is the dimension of the span of the Krylov
41 ## subspace (based on @var{eps1}).
43 ## If @var{b} is a vector and @var{k} is greater than @var{m-1}, then
44 ## @var{h} contains the Hessenberg decomposition of @var{A}.
46 ## The optional parameter @var{eps1} is the threshold for zero. The
47 ## default value is 1e-12.
49 ## If the optional parameter @var{pflg} is nonzero, row pivoting is used
50 ## to improve numerical behavior. The default value is 0.
52 ## Reference: A. Hodel, P. Misra, @cite{Partial Pivoting in the Computation of
53 ## Krylov Subspaces of Large Sparse Systems}, Proceedings of the 42nd IEEE
54 ## Conference on Decision and Control, December 2003.
57 ## Author: A. Scottedward Hodel <a.s.hodel@eng.auburn.edu>
59 function [Uret, H, nu] = krylov (A, V, k, eps1, pflg);
61 if (isa (A, "single") || isa (V, "single"))
67 if (nargin < 3 || nargin > 5)
70 ## Default permutation flag.
75 ## Default tolerance parameter.
83 if (! issquare (A) || isempty (A))
84 error ("krylov: A(%d x %d) must be a non-empty square matrix", rows (A), columns (A));
90 error ("krylov: A(%d x %d), V(%d x %d): argument dimensions do not match",
95 error ("krylov: K must be a scalar integer");
100 ## check for trivial solution.
108 ## Identify trivial null space.
109 abm = max (abs ([A, V]'));
110 zidx = find (abm == 0);
112 ## Set up vector of pivot points.
118 while (length(alpha) < na) && (columns(V) > 0) && (iter < k)
121 ## Get orthogonal basis of V.
123 while (jj <= columns (V) && length (alpha) < na)
124 ## Index of next Householder reflection.
125 nu = length(alpha)+1;
127 short_pv = pivot_vec(nu:na);
129 short_q = q(short_pv);
131 if (norm (short_q) < eps1)
132 ## Insignificant column; delete.
135 [V(:,jj), V(:,nv)] = swap (V(:,jj), V(:,nv));
136 ## FIXME -- H columns should be swapped too. Not done
137 ## since Block Hessenberg structure is lost anyway.
140 ## One less reflection.
143 ## New householder reflection.
145 ## Locate max magnitude element in short_q.
148 maxidx = find (asq == maxv, 1);
149 pivot_idx = short_pv(maxidx);
151 ## See if need to change the pivot list.
152 if (pivot_idx != pivot_vec(nu))
153 swapidx = maxidx + (nu-1);
154 [pivot_vec(nu), pivot_vec(swapidx)] = ...
155 swap (pivot_vec(nu), pivot_vec(swapidx));
159 ## Isolate portion of vector for reflection.
160 idx = pivot_vec(nu:na);
161 jdx = pivot_vec(1:nu);
163 [hv, av, z] = housh (q(idx), 1, 0);
167 ## Reduce V per the reflection.
168 V(idx,:) = V(idx,:) - av*hv*(hv' * V(idx,:));
170 ## FIXME -- not done correctly for block case.
171 H(nu,nu-1) = V(pivot_vec(nu),jj);
174 ## Advance to next column of V.
179 ## Check for oversize V (due to full rank).
180 if ((columns (V) > na) && (length (alpha) == na))
183 elseif (columns(V) > na)
186 krylov_length_alpha = length (alpha);
187 error ("krylov: this case should never happen; submit a bug report");
191 ## Construct next Q and multiply.
192 Q = zeros (size (V));
193 for kk = 1:columns (Q)
194 Q(pivot_vec(nu-columns(Q)+kk),kk) = 1;
197 ## Apply Householder reflections.
199 idx = pivot_vec(ii:na);
202 Q(idx,:) = Q(idx,:) - av*hv*(hv'*Q(idx,:));
206 ## Multiply to get new vector.
208 ## Project off of previous vectors.
213 V = V - av*hv*(hv'*V);
214 H(i,nu-columns(V)+(1:columns(V))) = V(pivot_vec(i),:);
219 ## Back out complete U matrix.
220 ## back out U matrix.
223 idx = pivot_vec(i:na);
226 U(:,i) = zeros (na, 1);
228 U(idx,i:j1) = U(idx,i:j1)-av*hv*(hv'*U(idx,i:j1));
233 if (max (max (abs (Uret(zidx,:)))) > 0)
234 warning ("krylov: trivial null space corrupted; set pflg = 1 or eps1 > %e",
241 function [a1, b1] = swap (a, b)