3183
|
1 /* |
|
2 |
|
3 Copyright (C) 1998 A. S. Hodel |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
3183
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
3183
|
20 |
|
21 */ |
|
22 |
|
23 // Generalized eigenvalue balancing via LAPACK |
3911
|
24 |
|
25 // Author: A. S. Hodel <scotte@eng.auburn.edu> |
3183
|
26 |
|
27 #undef DEBUG |
|
28 #undef DEBUG_SORT |
|
29 #undef DEBUG_EIG |
|
30 |
|
31 #include "config.h" |
|
32 |
|
33 #include <cfloat> |
4051
|
34 #include <cmath> |
|
35 |
3523
|
36 #include <iostream> |
4051
|
37 #include <iomanip> |
3183
|
38 |
|
39 #include "CmplxQRP.h" |
|
40 #include "dbleQR.h" |
4153
|
41 #include "f77-fcn.h" |
|
42 #include "quit.h" |
|
43 |
3183
|
44 #include "defun-dld.h" |
|
45 #include "error.h" |
|
46 #include "gripes.h" |
|
47 #include "oct-obj.h" |
|
48 #include "oct-map.h" |
|
49 #include "ov.h" |
|
50 #include "pager.h" |
3185
|
51 #if defined (DEBUG) || defined (DEBUG_SORT) |
3183
|
52 #include "pr-output.h" |
|
53 #endif |
|
54 #include "symtab.h" |
|
55 #include "utils.h" |
|
56 #include "variables.h" |
|
57 |
5275
|
58 typedef octave_idx_type (*sort_function) (const octave_idx_type& LSIZE, const double& ALPHA, |
3185
|
59 const double& BETA, const double& S, |
|
60 const double& P); |
3183
|
61 |
|
62 extern "C" |
|
63 { |
4552
|
64 F77_RET_T |
|
65 F77_FUNC (dggbal, DGGBAL) (F77_CONST_CHAR_ARG_DECL, |
5275
|
66 const octave_idx_type& N, double* A, const octave_idx_type& LDA, |
|
67 double* B, const octave_idx_type& LDB, octave_idx_type& ILO, |
|
68 octave_idx_type& IHI, double* LSCALE, double* RSCALE, |
|
69 double* WORK, octave_idx_type& INFO |
4552
|
70 F77_CHAR_ARG_LEN_DECL); |
3183
|
71 |
4552
|
72 F77_RET_T |
|
73 F77_FUNC (dggbak, DGGBAK) (F77_CONST_CHAR_ARG_DECL, |
|
74 F77_CONST_CHAR_ARG_DECL, |
5275
|
75 const octave_idx_type& N, const octave_idx_type& ILO, |
|
76 const octave_idx_type& IHI, const double* LSCALE, |
|
77 const double* RSCALE, octave_idx_type& M, double* V, |
|
78 const octave_idx_type& LDV, octave_idx_type& INFO |
4552
|
79 F77_CHAR_ARG_LEN_DECL |
|
80 F77_CHAR_ARG_LEN_DECL); |
3183
|
81 |
4552
|
82 F77_RET_T |
|
83 F77_FUNC (dgghrd, DGGHRD) (F77_CONST_CHAR_ARG_DECL, |
|
84 F77_CONST_CHAR_ARG_DECL, |
5275
|
85 const octave_idx_type& N, const octave_idx_type& ILO, |
|
86 const octave_idx_type& IHI, double* A, |
|
87 const octave_idx_type& LDA, double* B, |
|
88 const octave_idx_type& LDB, double* Q, |
|
89 const octave_idx_type& LDQ, double* Z, |
|
90 const octave_idx_type& LDZ, octave_idx_type& INFO |
4552
|
91 F77_CHAR_ARG_LEN_DECL |
|
92 F77_CHAR_ARG_LEN_DECL); |
3183
|
93 |
4552
|
94 F77_RET_T |
|
95 F77_FUNC (dhgeqz, DHGEQZ) (F77_CONST_CHAR_ARG_DECL, |
|
96 F77_CONST_CHAR_ARG_DECL, |
|
97 F77_CONST_CHAR_ARG_DECL, |
5275
|
98 const octave_idx_type& N, const octave_idx_type& ILO, const octave_idx_type& IHI, |
|
99 double* A, const octave_idx_type& LDA, double* B, |
|
100 const octave_idx_type& LDB, double* ALPHAR, |
4552
|
101 double* ALPHAI, double* BETA, double* Q, |
5275
|
102 const octave_idx_type& LDQ, double* Z, |
|
103 const octave_idx_type& LDZ, double* WORK, |
|
104 const octave_idx_type& LWORK, octave_idx_type& INFO |
4552
|
105 F77_CHAR_ARG_LEN_DECL |
|
106 F77_CHAR_ARG_LEN_DECL |
|
107 F77_CHAR_ARG_LEN_DECL); |
3183
|
108 |
4552
|
109 F77_RET_T |
5275
|
110 F77_FUNC (dlag2, DLAG2) (const double* A, const octave_idx_type& LDA, const double* B, |
|
111 const octave_idx_type& LDB, const double& SAFMIN, |
4552
|
112 double& SCALE1, double& SCALE2, |
|
113 double& WR1, double& WR2, double& WI); |
3183
|
114 |
|
115 // Van Dooren's code (netlib.org: toms/590) for reordering |
|
116 // GEP. Only processes Z, not Q. |
4552
|
117 F77_RET_T |
5275
|
118 F77_FUNC (dsubsp, DSUBSP) (const octave_idx_type& NMAX, const octave_idx_type& N, double* A, |
4552
|
119 double* B, double* Z, sort_function, |
5275
|
120 const double& EPS, octave_idx_type& NDIM, octave_idx_type& FAIL, |
|
121 octave_idx_type* IND); |
3183
|
122 |
|
123 // documentation for DTGEVC incorrectly states that VR, VL are |
|
124 // complex*16; they are declared in DTGEVC as double precision |
|
125 // (probably a cut and paste problem fro ZTGEVC) |
4552
|
126 F77_RET_T |
|
127 F77_FUNC (dtgevc, DTGEVC) (F77_CONST_CHAR_ARG_DECL, |
|
128 F77_CONST_CHAR_ARG_DECL, |
5275
|
129 octave_idx_type* SELECT, const octave_idx_type& N, double* A, |
|
130 const octave_idx_type& LDA, double* B, |
|
131 const octave_idx_type& LDB, double* VL, |
|
132 const octave_idx_type& LDVL, double* VR, |
|
133 const octave_idx_type& LDVR, const octave_idx_type& MM, |
|
134 octave_idx_type& M, double* WORK, octave_idx_type& INFO |
4552
|
135 F77_CHAR_ARG_LEN_DECL |
|
136 F77_CHAR_ARG_LEN_DECL); |
3183
|
137 |
4552
|
138 F77_RET_T |
|
139 F77_FUNC (xdlamch, XDLAMCH) (F77_CONST_CHAR_ARG_DECL, |
|
140 double& retval |
|
141 F77_CHAR_ARG_LEN_DECL); |
3185
|
142 |
4552
|
143 F77_RET_T |
|
144 F77_FUNC (xdlange, XDLANGE) (F77_CONST_CHAR_ARG_DECL, |
5275
|
145 const octave_idx_type&, const octave_idx_type&, const double*, |
|
146 const octave_idx_type&, double*, double& |
4552
|
147 F77_CHAR_ARG_LEN_DECL); |
3183
|
148 } |
|
149 |
|
150 // fcrhp, fin, fout, folhp: |
|
151 // routines for ordering of generalized eigenvalues |
|
152 // return 1 if test is passed, 0 otherwise |
|
153 // fin: |lambda| < 1 |
|
154 // fout: |lambda| >= 1 |
|
155 // fcrhp: real(lambda) >= 0 |
|
156 // folhp: real(lambda) < 0 |
|
157 |
5275
|
158 static octave_idx_type |
|
159 fcrhp (const octave_idx_type& lsize, const double& alpha, |
3185
|
160 const double& beta, const double& s, const double&) |
3183
|
161 { |
3185
|
162 if (lsize == 1) |
3183
|
163 return (alpha*beta >= 0 ? 1 : -1); |
3185
|
164 else |
3183
|
165 return (s >= 0 ? 1 : -1); |
|
166 } |
3185
|
167 |
5275
|
168 static octave_idx_type |
|
169 fin (const octave_idx_type& lsize, const double& alpha, |
3185
|
170 const double& beta, const double&, const double& p) |
3183
|
171 { |
5275
|
172 octave_idx_type retval; |
3183
|
173 |
3185
|
174 if (lsize == 1) |
|
175 retval = (fabs (alpha) < fabs (beta) ? 1 : -1); |
|
176 else |
|
177 retval = (fabs (p) < 1 ? 1 : -1); |
|
178 |
|
179 #ifdef DEBUG |
3538
|
180 std::cout << "qz: fin: retval=" << retval << std::endl; |
3185
|
181 #endif |
|
182 |
3183
|
183 return retval; |
|
184 } |
3185
|
185 |
5275
|
186 static octave_idx_type |
|
187 folhp (const octave_idx_type& lsize, const double& alpha, |
3185
|
188 const double& beta, const double& s, const double&) |
3183
|
189 { |
3185
|
190 if (lsize == 1) |
3183
|
191 return (alpha*beta < 0 ? 1 : -1); |
3185
|
192 else |
3183
|
193 return (s < 0 ? 1 : -1); |
|
194 } |
3185
|
195 |
5275
|
196 static octave_idx_type |
|
197 fout (const octave_idx_type& lsize, const double& alpha, |
3185
|
198 const double& beta, const double&, const double& p) |
3183
|
199 { |
3185
|
200 if (lsize == 1) |
|
201 return (fabs (alpha) >= fabs (beta) ? 1 : -1); |
|
202 else |
|
203 return (fabs (p) >= 1 ? 1 : -1); |
3183
|
204 } |
|
205 |
|
206 DEFUN_DLD (qz, args, nargout, |
3372
|
207 "-*- texinfo -*-\n\ |
|
208 @deftypefn {Loadable Function} {@var{lambda} =} qz (@var{a}, @var{b})\n\ |
|
209 Generalized eigenvalue problem @math{A x = s B x},\n\ |
5016
|
210 @var{QZ} decomposition. There are three ways to call this function:\n\ |
3372
|
211 @enumerate\n\ |
|
212 @item @code{lambda = qz(A,B)}\n\ |
3185
|
213 \n\ |
5016
|
214 Computes the generalized eigenvalues\n\ |
|
215 @iftex\n\ |
|
216 @tex\n\ |
|
217 $\\lambda$\n\ |
|
218 @end tex\n\ |
|
219 @end iftex\n\ |
|
220 @ifinfo\n\ |
|
221 @var{lambda}\n\ |
|
222 @end ifinfo\n\ |
|
223 of @math{(A - s B)}.\n\ |
3500
|
224 @item @code{[AA, BB, Q, Z, V, W, lambda] = qz (A, B)}\n\ |
3185
|
225 \n\ |
3372
|
226 Computes qz decomposition, generalized eigenvectors, and \n\ |
5481
|
227 generalized eigenvalues of @math{(A - sB)}\n\ |
5016
|
228 @iftex\n\ |
|
229 @tex\n\ |
|
230 $$ AV = BV{ \\rm diag }(\\lambda) $$\n\ |
|
231 $$ W^T A = { \\rm diag }(\\lambda)W^T B $$\n\ |
|
232 $$ AA = Q^T AZ, BB = Q^T BZ $$\n\ |
|
233 @end tex\n\ |
|
234 @end iftex\n\ |
|
235 @ifinfo\n\ |
3372
|
236 @example\n\ |
|
237 @group\n\ |
5481
|
238 \n\ |
|
239 A*V = B*V*diag(lambda)\n\ |
|
240 W'*A = diag(lambda)*W'*B\n\ |
|
241 AA = Q'*A*Z, BB = Q'*B*Z\n\ |
|
242 \n\ |
3372
|
243 @end group\n\ |
|
244 @end example\n\ |
5016
|
245 @end ifinfo\n\ |
|
246 with @var{Q} and @var{Z} orthogonal (unitary)= @var{I}\n\ |
3185
|
247 \n\ |
5016
|
248 @item @code{[AA,BB,Z@{, lambda@}] = qz(A,B,opt)}\n\ |
3185
|
249 \n\ |
3372
|
250 As in form [2], but allows ordering of generalized eigenpairs\n\ |
5481
|
251 for (e.g.) solution of discrete time algebraic Riccati equations.\n\ |
|
252 Form 3 is not available for complex matrices, and does not compute\n\ |
|
253 the generalized eigenvectors @var{V}, @var{W}, nor the orthogonal matrix @var{Q}.\n\ |
3372
|
254 @table @var\n\ |
|
255 @item opt\n\ |
5481
|
256 for ordering eigenvalues of the GEP pencil. The leading block\n\ |
|
257 of the revised pencil contains all eigenvalues that satisfy:\n\ |
3372
|
258 @table @code\n\ |
|
259 @item \"N\"\n\ |
5481
|
260 = unordered (default) \n\ |
3183
|
261 \n\ |
3372
|
262 @item \"S\"\n\ |
|
263 = small: leading block has all |lambda| <=1 \n\ |
3185
|
264 \n\ |
3372
|
265 @item \"B\"\n\ |
5481
|
266 = big: leading block has all |lambda| >= 1 \n\ |
3372
|
267 \n\ |
|
268 @item \"-\"\n\ |
5481
|
269 = negative real part: leading block has all eigenvalues\n\ |
|
270 in the open left half-plane\n\ |
3372
|
271 \n\ |
|
272 @item \"+\"\n\ |
5481
|
273 = nonnegative real part: leading block has all eigenvalues\n\ |
|
274 in the closed right half-plane\n\ |
3372
|
275 @end table\n\ |
|
276 @end table\n\ |
|
277 @end enumerate\n\ |
3183
|
278 \n\ |
3372
|
279 Note: qz performs permutation balancing, but not scaling (see balance).\n\ |
5481
|
280 Order of output arguments was selected for compatibility with MATLAB\n\ |
3183
|
281 \n\ |
5333
|
282 @seealso{balance, dare, eig, schur}\n\ |
3372
|
283 @end deftypefn") |
3183
|
284 { |
|
285 octave_value_list retval; |
|
286 int nargin = args.length (); |
|
287 |
3185
|
288 #ifdef DEBUG |
3538
|
289 std::cout << "qz: nargin = " << nargin << ", nargout = " << nargout << std::endl; |
3185
|
290 #endif |
3183
|
291 |
3185
|
292 if (nargin < 2 || nargin > 3 || nargout > 7) |
|
293 { |
5823
|
294 print_usage (); |
3185
|
295 return retval; |
|
296 } |
|
297 else if (nargin == 3 && (nargout < 3 || nargout > 4)) |
|
298 { |
3427
|
299 error ("qz: invalid number of output arguments for form [3] call"); |
3185
|
300 return retval; |
|
301 } |
3183
|
302 |
3185
|
303 #ifdef DEBUG |
3538
|
304 std::cout << "qz: determine ordering option" << std::endl; |
3185
|
305 #endif |
3183
|
306 |
|
307 // Determine ordering option |
3523
|
308 std::string ord_job; |
3183
|
309 static double safmin; |
3185
|
310 |
|
311 if (nargin == 2) |
3183
|
312 ord_job = "N"; |
3185
|
313 else if (!args(2).is_string ()) |
|
314 { |
|
315 error ("qz: argument 3 must be a string"); |
|
316 return retval; |
|
317 } |
|
318 else |
|
319 { |
|
320 ord_job = args(2).string_value (); |
3183
|
321 |
3185
|
322 if (ord_job[0] != 'N' |
|
323 && ord_job[0] != 'S' |
|
324 && ord_job[0] != 'B' |
|
325 && ord_job[0] != '+' |
|
326 && ord_job[0] != '-') |
|
327 { |
3427
|
328 error ("qz: invalid order option"); |
3185
|
329 return retval; |
|
330 } |
|
331 |
|
332 // overflow constant required by dlag2 |
4552
|
333 F77_FUNC (xdlamch, XDLAMCH) (F77_CONST_CHAR_ARG2 ("S", 1), |
|
334 safmin |
|
335 F77_CHAR_ARG_LEN (1)); |
3183
|
336 |
3185
|
337 #ifdef DEBUG_EIG |
3538
|
338 std::cout << "qz: initial value of safmin=" << setiosflags (std::ios::scientific) |
|
339 << safmin << std::endl; |
3185
|
340 #endif |
3183
|
341 |
3185
|
342 // some machines (e.g., DEC alpha) get safmin = 0; |
|
343 // for these, use eps instead to avoid problems in dlag2 |
|
344 if (safmin == 0) |
|
345 { |
|
346 #ifdef DEBUG_EIG |
3538
|
347 std::cout << "qz: DANGER WILL ROBINSON: safmin is 0!" << std::endl; |
3185
|
348 #endif |
3183
|
349 |
4552
|
350 F77_FUNC (xdlamch, XDLAMCH) (F77_CONST_CHAR_ARG2 ("E", 1), |
|
351 safmin |
|
352 F77_CHAR_ARG_LEN (1)); |
3185
|
353 |
|
354 #ifdef DEBUG_EIG |
3538
|
355 std::cout << "qz: safmin set to " << setiosflags (std::ios::scientific) |
|
356 << safmin << std::endl; |
3185
|
357 #endif |
|
358 } |
3183
|
359 } |
|
360 |
3185
|
361 #ifdef DEBUG |
3538
|
362 std::cout << "qz: check argument 1" << std::endl; |
3185
|
363 #endif |
3183
|
364 |
|
365 // Argument 1: check if it's o.k. dimensioned |
5275
|
366 octave_idx_type nn = args(0).rows (); |
3185
|
367 |
|
368 #ifdef DEBUG |
3531
|
369 std::cout << "argument 1 dimensions: (" << nn << "," << args(0).columns () << ")" |
3538
|
370 << std::endl; |
3185
|
371 #endif |
|
372 |
|
373 int arg_is_empty = empty_arg ("qz", nn, args(0).columns ()); |
|
374 |
3183
|
375 if (arg_is_empty < 0) |
3185
|
376 { |
|
377 gripe_empty_arg ("qz: parameter 1", 0); |
|
378 return retval; |
|
379 } |
3183
|
380 else if (arg_is_empty > 0) |
3185
|
381 { |
|
382 gripe_empty_arg ("qz: parameter 1; continuing", 0); |
|
383 return octave_value_list (2, Matrix ()); |
|
384 } |
|
385 else if (args(0).columns () != nn) |
|
386 { |
|
387 gripe_square_matrix_required ("qz"); |
|
388 return retval; |
|
389 } |
3183
|
390 |
|
391 // Argument 1: dimensions look good; get the value |
|
392 Matrix aa; |
|
393 ComplexMatrix caa; |
3185
|
394 |
|
395 if (args(0).is_complex_type ()) |
3183
|
396 caa = args(0).complex_matrix_value (); |
3185
|
397 else |
3183
|
398 aa = args(0).matrix_value (); |
3185
|
399 |
|
400 if (error_state) |
3183
|
401 return retval; |
|
402 |
3185
|
403 #ifdef DEBUG |
3538
|
404 std::cout << "qz: check argument 2" << std::endl; |
3185
|
405 #endif |
3183
|
406 |
|
407 // Extract argument 2 (bb, or cbb if complex) |
3185
|
408 if ((nn != args(1).columns ()) || (nn != args(1).rows ())) |
|
409 { |
|
410 gripe_nonconformant (); |
|
411 return retval; |
|
412 } |
|
413 |
3183
|
414 Matrix bb; |
|
415 ComplexMatrix cbb; |
3185
|
416 |
|
417 if (args(1).is_complex_type ()) |
3183
|
418 cbb = args(1).complex_matrix_value (); |
|
419 else |
|
420 bb = args(1).matrix_value (); |
3185
|
421 |
|
422 if (error_state) |
3183
|
423 return retval; |
|
424 |
|
425 // Both matrices loaded, now let's check what kind of arithmetic: |
|
426 //declared static to avoid compiler warnings about long jumps, vforks. |
3185
|
427 |
|
428 static int complex_case |
|
429 = (args(0).is_complex_type () || args(1).is_complex_type ()); |
3183
|
430 |
3185
|
431 if (nargin == 3 && complex_case) |
|
432 { |
|
433 error ("qz: cannot re-order complex qz decomposition."); |
|
434 return retval; |
|
435 } |
3183
|
436 |
|
437 // first, declare variables used in both the real and complex case |
|
438 Matrix QQ(nn,nn), ZZ(nn,nn), VR(nn,nn), VL(nn,nn); |
|
439 RowVector alphar(nn), alphai(nn), betar(nn); |
|
440 |
3185
|
441 ComplexMatrix CQ(nn,nn), CZ(nn,nn), CVR(nn,nn), CVL(nn,nn); |
5275
|
442 octave_idx_type ilo, ihi, info; |
3185
|
443 char compq = (nargout >= 3 ? 'V' : 'N'); |
|
444 char compz = (nargout >= 4 ? 'V' : 'N'); |
3183
|
445 |
3185
|
446 // initialize Q, Z to identity if we need either of them |
|
447 if (compq == 'V' || compz == 'V') |
5275
|
448 for (octave_idx_type ii = 0; ii < nn; ii++) |
|
449 for (octave_idx_type jj = 0; jj < nn; jj++) |
4153
|
450 { |
|
451 OCTAVE_QUIT; |
|
452 QQ(ii,jj) = ZZ(ii,jj) = (ii == jj ? 1.0 : 0.0); |
|
453 } |
3183
|
454 |
3185
|
455 // always perform permutation balancing |
4552
|
456 const char bal_job = 'P'; |
3183
|
457 RowVector lscale(nn), rscale(nn), work(6*nn); |
|
458 |
3185
|
459 if (complex_case) |
|
460 { |
|
461 error ("Complex case not implemented yet"); |
|
462 return retval; |
|
463 } |
3183
|
464 else |
3185
|
465 { |
|
466 #ifdef DEBUG |
|
467 if (compq == 'V') |
3538
|
468 std::cout << "qz: performing balancing; QQ=" << std::endl << QQ << std::endl; |
3185
|
469 #endif |
3183
|
470 |
3185
|
471 F77_XFCN (dggbal, DGGBAL, |
4552
|
472 (F77_CONST_CHAR_ARG2 (&bal_job, 1), |
|
473 nn, aa.fortran_vec (), nn, bb.fortran_vec (), |
|
474 nn, ilo, ihi, lscale.fortran_vec (), |
|
475 rscale.fortran_vec (), work.fortran_vec (), info |
|
476 F77_CHAR_ARG_LEN (1))); |
3185
|
477 |
|
478 if (f77_exception_encountered) |
|
479 { |
|
480 error ("unrecoverable error in qz (bal)"); |
|
481 return retval; |
|
482 } |
|
483 } |
3183
|
484 |
|
485 // Since we just want the balancing matrices, we can use dggbal |
|
486 // for both the real and complex cases; |
|
487 // left first |
3185
|
488 |
|
489 if (compq == 'V') |
|
490 { |
|
491 F77_XFCN (dggbak, DGGBAK, |
4552
|
492 (F77_CONST_CHAR_ARG2 (&bal_job, 1), |
|
493 F77_CONST_CHAR_ARG2 ("L", 1), |
4566
|
494 nn, ilo, ihi, lscale.data (), rscale.data (), |
|
495 nn, QQ.fortran_vec (), nn, info |
4552
|
496 F77_CHAR_ARG_LEN (1) |
|
497 F77_CHAR_ARG_LEN (1))); |
3183
|
498 |
3185
|
499 #ifdef DEBUG |
|
500 if (compq == 'V') |
3538
|
501 std::cout << "qz: balancing done; QQ=" << std::endl << QQ << std::endl; |
3185
|
502 #endif |
3183
|
503 |
3185
|
504 if (f77_exception_encountered) |
|
505 { |
|
506 error ("unrecoverable error in qz (bal-L)"); |
|
507 return retval; |
|
508 } |
3183
|
509 } |
|
510 |
|
511 // then right |
3185
|
512 if (compz == 'V') |
|
513 { |
4552
|
514 F77_XFCN (dggbak, DGGBAK, |
|
515 (F77_CONST_CHAR_ARG2 (&bal_job, 1), |
|
516 F77_CONST_CHAR_ARG2 ("R", 1), |
4566
|
517 nn, ilo, ihi, lscale.data (), rscale.data (), |
|
518 nn, ZZ.fortran_vec (), nn, info |
4552
|
519 F77_CHAR_ARG_LEN (1) |
|
520 F77_CHAR_ARG_LEN (1))); |
3183
|
521 |
3185
|
522 #ifdef DEBUG |
|
523 if (compz == 'V') |
3538
|
524 std::cout << "qz: balancing done; ZZ=" << std::endl << ZZ << std::endl; |
3185
|
525 #endif |
3183
|
526 |
3185
|
527 if (f77_exception_encountered) |
|
528 { |
|
529 error ("unrecoverable error in qz (bal-R)"); |
|
530 return retval; |
|
531 } |
|
532 } |
3183
|
533 |
|
534 static char qz_job; |
|
535 qz_job = (nargout < 2 ? 'E' : 'S'); |
3185
|
536 |
3183
|
537 if (complex_case) |
3185
|
538 { |
|
539 // complex case |
|
540 if (args(0).is_real_type ()) |
3587
|
541 caa = ComplexMatrix (aa); |
3185
|
542 |
|
543 if (args(1).is_real_type ()) |
3587
|
544 cbb = ComplexMatrix (bb); |
3185
|
545 |
|
546 if (compq == 'V') |
3587
|
547 CQ = ComplexMatrix (QQ); |
3185
|
548 |
|
549 if (compz == 'V') |
3587
|
550 CZ = ComplexMatrix (ZZ); |
3185
|
551 |
|
552 error ("complex case not done yet"); |
|
553 return retval; |
|
554 } |
3183
|
555 else // real matrices case |
3185
|
556 { |
|
557 #ifdef DEBUG |
3538
|
558 std::cout << "qz: peforming qr decomposition of bb" << std::endl; |
3185
|
559 #endif |
3183
|
560 |
3185
|
561 // compute the QR factorization of bb |
|
562 QR bqr (bb); |
|
563 |
|
564 #ifdef DEBUG |
3538
|
565 std::cout << "qz: qr (bb) done; now peforming qz decomposition" << std::endl; |
3185
|
566 #endif |
3183
|
567 |
3185
|
568 bb = bqr.R (); |
|
569 |
|
570 #ifdef DEBUG |
3538
|
571 std::cout << "qz: extracted bb" << std::endl; |
3185
|
572 #endif |
3183
|
573 |
3185
|
574 aa = (bqr.Q ()).transpose ()*aa; |
|
575 |
|
576 #ifdef DEBUG |
3538
|
577 std::cout << "qz: updated aa " << std::endl; |
|
578 std::cout << "bqr.Q () = " << std::endl << bqr.Q () << std::endl; |
3183
|
579 |
3185
|
580 if (compq == 'V') |
3538
|
581 std::cout << "QQ =" << QQ << std::endl; |
3185
|
582 #endif |
3183
|
583 |
3185
|
584 if (compq == 'V') |
|
585 QQ = QQ*bqr.Q (); |
3183
|
586 |
3185
|
587 #ifdef DEBUG |
3538
|
588 std::cout << "qz: precursors done..." << std::endl; |
3185
|
589 #endif |
3183
|
590 |
3185
|
591 #ifdef DEBUG |
3538
|
592 std::cout << "qz: compq = " << compq << ", compz = " << compz << std::endl; |
3185
|
593 #endif |
3183
|
594 |
3185
|
595 // reduce to generalized hessenberg form |
|
596 F77_XFCN (dgghrd, DGGHRD, |
4552
|
597 (F77_CONST_CHAR_ARG2 (&compq, 1), |
|
598 F77_CONST_CHAR_ARG2 (&compz, 1), |
|
599 nn, ilo, ihi, aa.fortran_vec (), |
|
600 nn, bb.fortran_vec (), nn, QQ.fortran_vec (), nn, |
|
601 ZZ.fortran_vec (), nn, info |
|
602 F77_CHAR_ARG_LEN (1) |
|
603 F77_CHAR_ARG_LEN (1))); |
3183
|
604 |
3185
|
605 if (f77_exception_encountered) |
|
606 { |
|
607 error ("unrecoverable error in qz (dgghrd)"); |
|
608 return retval; |
|
609 } |
3183
|
610 |
3185
|
611 // check if just computing generalized eigenvalues or if we're |
|
612 // actually computing the decomposition |
3183
|
613 |
3185
|
614 // reduce to generalized Schur form |
|
615 F77_XFCN (dhgeqz, DHGEQZ, |
4552
|
616 (F77_CONST_CHAR_ARG2 (&qz_job, 1), |
|
617 F77_CONST_CHAR_ARG2 (&compq, 1), |
|
618 F77_CONST_CHAR_ARG2 (&compz, 1), |
|
619 nn, ilo, ihi, aa.fortran_vec (), nn, bb.fortran_vec (), |
|
620 nn, alphar.fortran_vec (), alphai.fortran_vec (), |
|
621 betar.fortran_vec (), QQ.fortran_vec (), nn, |
|
622 ZZ.fortran_vec (), nn, work.fortran_vec (), nn, info |
|
623 F77_CHAR_ARG_LEN (1) |
|
624 F77_CHAR_ARG_LEN (1) |
|
625 F77_CHAR_ARG_LEN (1))); |
3183
|
626 |
3185
|
627 if (f77_exception_encountered) |
|
628 { |
|
629 error ("unrecoverable error in qz (dhgeqz)"); |
|
630 return retval; |
|
631 } |
|
632 } |
3183
|
633 |
|
634 // order the QZ decomposition? |
3185
|
635 if (ord_job[0] != 'N') |
3183
|
636 { |
3185
|
637 if (complex_case) |
|
638 { |
|
639 // probably not needed, but better be safe |
|
640 error ("qz: cannot re-order complex qz decomposition."); |
|
641 return retval; |
|
642 } |
|
643 else |
|
644 { |
|
645 #ifdef DEBUG_SORT |
3538
|
646 std::cout << "qz: ordering eigenvalues: ord_job = " << ord_job[0] << std::endl; |
3185
|
647 #endif |
3183
|
648 |
3185
|
649 // declared static to avoid vfork/long jump compiler complaints |
|
650 static sort_function sort_test; |
|
651 sort_test = NULL; |
3183
|
652 |
3185
|
653 switch (ord_job[0]) |
|
654 { |
|
655 case 'S': |
|
656 sort_test = &fin; |
|
657 break; |
3183
|
658 |
3185
|
659 case 'B': |
|
660 sort_test = &fout; |
|
661 break; |
3183
|
662 |
3185
|
663 case '+': |
|
664 sort_test = &fcrhp; |
|
665 break; |
3183
|
666 |
3185
|
667 case '-': |
|
668 sort_test = &folhp; |
|
669 break; |
|
670 |
|
671 default: |
3427
|
672 // invalid order option (should never happen, since we |
3185
|
673 // checked the options at the top). |
|
674 panic_impossible (); |
|
675 break; |
3550
|
676 } |
3183
|
677 |
5275
|
678 octave_idx_type ndim, fail; |
3185
|
679 double inf_norm; |
|
680 |
|
681 F77_XFCN (xdlange, XDLANGE, |
4552
|
682 (F77_CONST_CHAR_ARG2 ("I", 1), |
4566
|
683 nn, nn, aa.data (), nn, work.fortran_vec (), inf_norm |
4552
|
684 F77_CHAR_ARG_LEN (1))); |
3185
|
685 |
|
686 double eps = DBL_EPSILON*inf_norm*nn; |
|
687 |
|
688 #ifdef DEBUG_SORT |
3538
|
689 std::cout << "qz: calling dsubsp: aa=" << std::endl; |
3531
|
690 octave_print_internal (std::cout, aa, 0); |
3538
|
691 std::cout << std::endl << "bb=" << std::endl; |
3531
|
692 octave_print_internal (std::cout, bb, 0); |
3185
|
693 if (compz == 'V') |
|
694 { |
3538
|
695 std::cout << std::endl << "ZZ=" << std::endl; |
3531
|
696 octave_print_internal (std::cout, ZZ, 0); |
3185
|
697 } |
3538
|
698 std::cout << std::endl; |
|
699 std::cout << "alphar = " << std::endl; |
3531
|
700 octave_print_internal (std::cout, (Matrix) alphar, 0); |
3538
|
701 std::cout << std::endl << "alphai = " << std::endl; |
3531
|
702 octave_print_internal (std::cout, (Matrix) alphai, 0); |
3538
|
703 std::cout << std::endl << "beta = " << std::endl; |
3531
|
704 octave_print_internal (std::cout, (Matrix) betar, 0); |
3538
|
705 std::cout << std::endl; |
3185
|
706 #endif |
|
707 |
5275
|
708 Array<octave_idx_type> ind (nn); |
3550
|
709 |
3185
|
710 F77_XFCN (dsubsp, DSUBSP, |
4552
|
711 (nn, nn, aa.fortran_vec (), bb.fortran_vec (), |
|
712 ZZ.fortran_vec (), sort_test, eps, ndim, fail, |
3550
|
713 ind.fortran_vec ())); |
3185
|
714 |
|
715 #ifdef DEBUG |
3538
|
716 std::cout << "qz: back from dsubsp: aa=" << std::endl; |
3531
|
717 octave_print_internal (std::cout, aa, 0); |
3538
|
718 std::cout << std::endl << "bb=" << std::endl; |
3531
|
719 octave_print_internal (std::cout, bb, 0); |
3185
|
720 if (compz == 'V') |
|
721 { |
3538
|
722 std::cout << std::endl << "ZZ=" << std::endl; |
3531
|
723 octave_print_internal (std::cout, ZZ, 0); |
3185
|
724 } |
3538
|
725 std::cout << std::endl; |
3185
|
726 #endif |
|
727 |
|
728 // manually update alphar, alphai, betar |
|
729 static int jj; |
|
730 |
|
731 jj=0; |
|
732 while (jj < nn) |
|
733 { |
|
734 #ifdef DEBUG_EIG |
3538
|
735 std::cout << "computing gen eig #" << jj << std::endl; |
3185
|
736 #endif |
|
737 |
|
738 static int zcnt; // number of zeros in this block |
|
739 |
|
740 if (jj == (nn-1)) |
|
741 zcnt = 1; |
|
742 else if (aa(jj+1,jj) == 0) |
|
743 zcnt = 1; |
|
744 else zcnt = 2; |
|
745 |
|
746 if (zcnt == 1) // real zero |
|
747 { |
|
748 #ifdef DEBUG_EIG |
3538
|
749 std::cout << " single gen eig:" << std::endl; |
|
750 std::cout << " alphar(" << jj << ") = " << aa(jj,jj) << std::endl; |
|
751 std::cout << " betar( " << jj << ") = " << bb(jj,jj) << std::endl; |
|
752 std::cout << " alphai(" << jj << ") = 0" << std::endl; |
3185
|
753 #endif |
|
754 |
|
755 alphar(jj) = aa(jj,jj); |
|
756 alphai(jj) = 0; |
|
757 betar(jj) = bb(jj,jj); |
|
758 } |
|
759 else |
|
760 { |
|
761 // complex conjugate pair |
|
762 #ifdef DEBUG_EIG |
3538
|
763 std::cout << "qz: calling dlag2:" << std::endl; |
3531
|
764 std::cout << "safmin=" |
3538
|
765 << setiosflags (std::ios::scientific) << safmin << std::endl; |
3185
|
766 |
|
767 for (int idr = jj; idr <= jj+1; idr++) |
|
768 { |
|
769 for (int idc = jj; idc <= jj+1; idc++) |
|
770 { |
3531
|
771 std::cout << "aa(" << idr << "," << idc << ")=" |
3538
|
772 << aa(idr,idc) << std::endl; |
3531
|
773 std::cout << "bb(" << idr << "," << idc << ")=" |
3538
|
774 << bb(idr,idc) << std::endl; |
3185
|
775 } |
|
776 } |
|
777 #endif |
|
778 |
5775
|
779 // FIXME -- probably should be using |
4566
|
780 // fortran_vec instead of &aa(jj,jj) here. |
|
781 |
3185
|
782 double scale1, scale2, wr1, wr2, wi; |
4629
|
783 const double *aa_ptr = aa.data () + jj*nn+jj; |
|
784 const double *bb_ptr = bb.data () + jj*nn+jj; |
3185
|
785 F77_XFCN (dlag2, DLAG2, |
4629
|
786 (aa_ptr, nn, bb_ptr, nn, safmin, |
3185
|
787 scale1, scale2, wr1, wr2, wi)); |
|
788 |
|
789 #ifdef DEBUG_EIG |
3531
|
790 std::cout << "dlag2 returns: scale1=" << scale1 |
3538
|
791 << "\tscale2=" << scale2 << std::endl |
3185
|
792 << "\twr1=" << wr1 << "\twr2=" << wr2 |
3538
|
793 << "\twi=" << wi << std::endl; |
3185
|
794 #endif |
|
795 |
|
796 // just to be safe, check if it's a real pair |
|
797 if (wi == 0) |
|
798 { |
|
799 alphar(jj) = wr1; |
|
800 alphai(jj) = 0; |
|
801 betar(jj) = scale1; |
|
802 alphar(jj+1) = wr2; |
|
803 alphai(jj+1) = 0; |
|
804 betar(jj+1) = scale2; |
|
805 } |
|
806 else |
|
807 { |
|
808 alphar(jj) = alphar(jj+1)=wr1; |
|
809 alphai(jj) = -(alphai(jj+1) = wi); |
|
810 betar(jj) = betar(jj+1) = scale1; |
|
811 } |
|
812 } |
|
813 |
|
814 // advance past this block |
|
815 jj += zcnt; |
|
816 } |
|
817 |
|
818 #ifdef DEBUG_SORT |
3538
|
819 std::cout << "qz: back from dsubsp: aa=" << std::endl; |
3531
|
820 octave_print_internal (std::cout, aa, 0); |
3538
|
821 std::cout << std::endl << "bb=" << std::endl; |
3531
|
822 octave_print_internal (std::cout, bb, 0); |
3185
|
823 |
|
824 if (compz == 'V') |
|
825 { |
3538
|
826 std::cout << std::endl << "ZZ=" << std::endl; |
3531
|
827 octave_print_internal (std::cout, ZZ, 0); |
3185
|
828 } |
3538
|
829 std::cout << std::endl << "qz: ndim=" << ndim << std::endl |
|
830 << "fail=" << fail << std::endl; |
|
831 std::cout << "alphar = " << std::endl; |
3531
|
832 octave_print_internal (std::cout, (Matrix) alphar, 0); |
3538
|
833 std::cout << std::endl << "alphai = " << std::endl; |
3531
|
834 octave_print_internal (std::cout, (Matrix) alphai, 0); |
3538
|
835 std::cout << std::endl << "beta = " << std::endl; |
3531
|
836 octave_print_internal (std::cout, (Matrix) betar, 0); |
3538
|
837 std::cout << std::endl; |
3185
|
838 #endif |
|
839 } |
3183
|
840 } |
3185
|
841 |
3183
|
842 // compute generalized eigenvalues? |
|
843 ComplexColumnVector gev; |
3185
|
844 |
|
845 if (nargout < 2 || nargout == 7 || (nargin == 3 && nargout == 4)) |
3183
|
846 { |
3185
|
847 if (complex_case) |
|
848 { |
|
849 error ("complex case not yet implemented"); |
|
850 return retval; |
|
851 } |
|
852 else |
|
853 { |
|
854 #ifdef DEBUG |
3538
|
855 std::cout << "qz: computing generalized eigenvalues" << std::endl; |
3185
|
856 #endif |
3183
|
857 |
3185
|
858 // return finite generalized eigenvalues |
|
859 int cnt = 0; |
|
860 |
|
861 for (int ii = 0; ii < nn; ii++) |
|
862 if (betar(ii) != 0) |
|
863 cnt++; |
|
864 |
|
865 ComplexColumnVector tmp(cnt); |
|
866 |
3671
|
867 cnt = 0; |
3185
|
868 for (int ii = 0; ii < nn; ii++) |
|
869 if (betar(ii) != 0) |
3671
|
870 tmp(cnt++) = Complex(alphar(ii), alphai(ii))/betar(ii); |
3185
|
871 gev = tmp; |
|
872 } |
3183
|
873 } |
|
874 |
|
875 // right, left eigenvector matrices |
3185
|
876 if (nargout >= 5) |
3183
|
877 { |
3185
|
878 char side = (nargout == 5 ? 'R' : 'B'); // which side to compute? |
|
879 char howmny = 'B'; // compute all of them and backtransform |
5275
|
880 octave_idx_type *select = NULL; // dummy pointer; select is not used. |
|
881 octave_idx_type m; |
3185
|
882 |
|
883 if (complex_case) |
|
884 { |
|
885 error ("complex type not yet implemented"); |
|
886 return retval; |
|
887 } |
|
888 else |
|
889 { |
|
890 #ifdef DEBUG |
3538
|
891 std::cout << "qz: computing generalized eigenvectors" << std::endl; |
3185
|
892 #endif |
|
893 |
|
894 VL = QQ; |
|
895 VR = ZZ; |
|
896 |
|
897 F77_XFCN (dtgevc, DTGEVC, |
4552
|
898 (F77_CONST_CHAR_ARG2 (&side, 1), |
|
899 F77_CONST_CHAR_ARG2 (&howmny, 1), |
|
900 select, nn, aa.fortran_vec (), nn, bb.fortran_vec (), |
|
901 nn, VL.fortran_vec (), nn, VR.fortran_vec (), nn, nn, |
|
902 m, work.fortran_vec (), info |
|
903 F77_CHAR_ARG_LEN (1) |
|
904 F77_CHAR_ARG_LEN (1))); |
3185
|
905 |
|
906 if (f77_exception_encountered) |
|
907 { |
|
908 error ("unrecoverable error in qz (dtgevc)"); |
|
909 return retval; |
|
910 } |
3183
|
911 |
3185
|
912 // now construct the complex form of VV, WW |
|
913 int jj = 0; |
|
914 |
|
915 while (jj < nn) |
|
916 { |
4153
|
917 OCTAVE_QUIT; |
|
918 |
3185
|
919 // see if real or complex eigenvalue |
|
920 int cinc = 2; // column increment; assume complex eigenvalue |
|
921 |
|
922 if (jj == (nn-1)) |
|
923 cinc = 1; // single column |
|
924 else if (aa(jj+1,jj) == 0) |
|
925 cinc = 1; |
|
926 |
|
927 // now copy the eigenvector (s) to CVR, CVL |
|
928 if (cinc == 1) |
|
929 { |
|
930 for (int ii = 0; ii < nn; ii++) |
|
931 CVR(ii,jj) = VR(ii,jj); |
|
932 |
|
933 if (side == 'B') |
|
934 for (int ii = 0; ii < nn; ii++) |
|
935 CVL(ii,jj) = VL(ii,jj); |
|
936 } |
|
937 else |
|
938 { |
|
939 // double column; complex vector |
|
940 |
|
941 for (int ii = 0; ii < nn; ii++) |
|
942 { |
|
943 CVR(ii,jj) = Complex (VR(ii,jj), VR(ii,jj+1)); |
|
944 CVR(ii,jj+1) = Complex (VR(ii,jj), -VR(ii,jj+1)); |
|
945 } |
3183
|
946 |
3185
|
947 if (side == 'B') |
|
948 for (int ii = 0; ii < nn; ii++) |
|
949 { |
|
950 CVL(ii,jj) = Complex (VL(ii,jj), VL(ii,jj+1)); |
|
951 CVL(ii,jj+1) = Complex (VL(ii,jj), -VL(ii,jj+1)); |
|
952 } |
|
953 } |
|
954 |
|
955 // advance to next eigenvectors (if any) |
|
956 jj += cinc; |
|
957 } |
|
958 } |
3183
|
959 } |
3185
|
960 |
|
961 switch (nargout) |
|
962 { |
|
963 case 7: |
|
964 retval(6) = gev; |
|
965 |
|
966 case 6: // return eigenvectors |
|
967 retval(5) = CVL; |
|
968 |
|
969 case 5: // return eigenvectors |
|
970 retval(4) = CVR; |
|
971 |
|
972 case 4: |
|
973 if (nargin == 3) |
|
974 { |
|
975 #ifdef DEBUG |
3538
|
976 std::cout << "qz: sort: retval(3) = gev = " << std::endl; |
3531
|
977 octave_print_internal (std::cout, gev); |
3538
|
978 std::cout << std::endl; |
3185
|
979 #endif |
|
980 retval(3) = gev; |
|
981 } |
|
982 else |
|
983 retval(3) = ZZ; |
|
984 |
|
985 case 3: |
|
986 if (nargin == 3) |
|
987 retval(2) = ZZ; |
|
988 else |
|
989 retval(2) = QQ; |
|
990 |
|
991 case 2: |
|
992 #ifdef DEBUG |
3538
|
993 std::cout << "qz: retval (1) = bb = " << std::endl; |
3531
|
994 octave_print_internal (std::cout, bb, 0); |
3538
|
995 std::cout << std::endl << "qz: retval(0) = aa = " <<std::endl; |
3531
|
996 octave_print_internal (std::cout, aa, 0); |
3538
|
997 std::cout << std::endl; |
3185
|
998 #endif |
|
999 retval(1) = bb; |
|
1000 retval(0) = aa; |
|
1001 break; |
|
1002 |
|
1003 case 1: |
|
1004 case 0: |
|
1005 #ifdef DEBUG |
3538
|
1006 std::cout << "qz: retval(0) = gev = " << gev << std::endl; |
3185
|
1007 #endif |
|
1008 retval(0) = gev; |
|
1009 break; |
|
1010 |
|
1011 default: |
|
1012 error ("qz: too many return arguments."); |
|
1013 break; |
3183
|
1014 } |
|
1015 |
3185
|
1016 #ifdef DEBUG |
3538
|
1017 std::cout << "qz: exiting (at long last)" << std::endl; |
3185
|
1018 #endif |
3183
|
1019 |
|
1020 return retval; |
|
1021 } |
|
1022 |
|
1023 /* |
|
1024 ;;; Local Variables: *** |
|
1025 ;;; mode: C++ *** |
|
1026 ;;; End: *** |
|
1027 */ |