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