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