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