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> |
3523
|
33 #include <iostream> |
3183
|
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 { |
3887
|
62 int F77_FUNC (dggbal, DGGBAL) (const char* JOB, const int& N, |
3185
|
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 |
3887
|
68 int F77_FUNC (dggbak, DGGBAK) (const char* JOB, const char* SIDE, |
3185
|
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 |
3887
|
74 int F77_FUNC (dgghrd, DGGHRD) (const char* COMPQ, const char* COMPZ, |
3185
|
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 |
3887
|
82 int F77_FUNC (dhgeqz, DHGEQZ) (const char* JOB, const char* COMPQ, |
3185
|
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 |
3887
|
93 int F77_FUNC (dlag2, DLAG2) (double* A, const int& LDA, double* B, |
3185
|
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. |
3887
|
100 int F77_FUNC (dsubsp, DSUBSP) (const int& NMAX, const int& N, double* A, |
3185
|
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) |
3887
|
108 int F77_FUNC (dtgevc, DTGEVC) (const char* SIDE, const char* HOWMNY, |
3185
|
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 |
3887
|
117 int F77_FUNC (xdlamch, XDLAMCH) (const char* cmach, double& retval, long); |
3185
|
118 |
3887
|
119 int F77_FUNC (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 |
3538
|
154 std::cout << "qz: fin: retval=" << retval << std::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\ |
3500
|
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 |
3538
|
243 std::cout << "qz: nargin = " << nargin << ", nargout = " << nargout << std::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 { |
3427
|
253 error ("qz: invalid number of output arguments for form [3] call"); |
3185
|
254 return retval; |
|
255 } |
3183
|
256 |
3185
|
257 #ifdef DEBUG |
3538
|
258 std::cout << "qz: determine ordering option" << std::endl; |
3185
|
259 #endif |
3183
|
260 |
|
261 // Determine ordering option |
3523
|
262 std::string ord_job; |
3183
|
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 { |
3427
|
282 error ("qz: invalid order option"); |
3185
|
283 return retval; |
|
284 } |
|
285 |
|
286 // overflow constant required by dlag2 |
3887
|
287 F77_FUNC (xdlamch, XDLAMCH) ("S", safmin, 1L); |
3183
|
288 |
3185
|
289 #ifdef DEBUG_EIG |
3538
|
290 std::cout << "qz: initial value of safmin=" << setiosflags (std::ios::scientific) |
|
291 << safmin << std::endl; |
3185
|
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 |
3538
|
299 std::cout << "qz: DANGER WILL ROBINSON: safmin is 0!" << std::endl; |
3185
|
300 #endif |
3183
|
301 |
3887
|
302 F77_FUNC (xdlamch, XDLAMCH) ("E", safmin, 1L); |
3185
|
303 |
|
304 #ifdef DEBUG_EIG |
3538
|
305 std::cout << "qz: safmin set to " << setiosflags (std::ios::scientific) |
|
306 << safmin << std::endl; |
3185
|
307 #endif |
|
308 } |
3183
|
309 } |
|
310 |
3185
|
311 #ifdef DEBUG |
3538
|
312 std::cout << "qz: check argument 1" << std::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 |
3531
|
319 std::cout << "argument 1 dimensions: (" << nn << "," << args(0).columns () << ")" |
3538
|
320 << std::endl; |
3185
|
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 |
3538
|
354 std::cout << "qz: check argument 2" << std::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') |
3538
|
415 std::cout << "qz: performing balancing; QQ=" << std::endl << QQ << std::endl; |
3185
|
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') |
3538
|
443 std::cout << "qz: balancing done; QQ=" << std::endl << QQ << std::endl; |
3185
|
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') |
3538
|
463 std::cout << "qz: balancing done; ZZ=" << std::endl << ZZ << std::endl; |
3185
|
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 ()) |
3587
|
480 caa = ComplexMatrix (aa); |
3185
|
481 |
|
482 if (args(1).is_real_type ()) |
3587
|
483 cbb = ComplexMatrix (bb); |
3185
|
484 |
|
485 if (compq == 'V') |
3587
|
486 CQ = ComplexMatrix (QQ); |
3185
|
487 |
|
488 if (compz == 'V') |
3587
|
489 CZ = ComplexMatrix (ZZ); |
3185
|
490 |
|
491 error ("complex case not done yet"); |
|
492 return retval; |
|
493 } |
3183
|
494 else // real matrices case |
3185
|
495 { |
|
496 #ifdef DEBUG |
3538
|
497 std::cout << "qz: peforming qr decomposition of bb" << std::endl; |
3185
|
498 #endif |
3183
|
499 |
3185
|
500 // compute the QR factorization of bb |
|
501 QR bqr (bb); |
|
502 |
|
503 #ifdef DEBUG |
3538
|
504 std::cout << "qz: qr (bb) done; now peforming qz decomposition" << std::endl; |
3185
|
505 #endif |
3183
|
506 |
3185
|
507 bb = bqr.R (); |
|
508 |
|
509 #ifdef DEBUG |
3538
|
510 std::cout << "qz: extracted bb" << std::endl; |
3185
|
511 #endif |
3183
|
512 |
3185
|
513 aa = (bqr.Q ()).transpose ()*aa; |
|
514 |
|
515 #ifdef DEBUG |
3538
|
516 std::cout << "qz: updated aa " << std::endl; |
|
517 std::cout << "bqr.Q () = " << std::endl << bqr.Q () << std::endl; |
3183
|
518 |
3185
|
519 if (compq == 'V') |
3538
|
520 std::cout << "QQ =" << QQ << std::endl; |
3185
|
521 #endif |
3183
|
522 |
3185
|
523 if (compq == 'V') |
|
524 QQ = QQ*bqr.Q (); |
3183
|
525 |
3185
|
526 #ifdef DEBUG |
3538
|
527 std::cout << "qz: precursors done..." << std::endl; |
3185
|
528 #endif |
3183
|
529 |
3185
|
530 #ifdef DEBUG |
3538
|
531 std::cout << "qz: compq = " << compq << ", compz = " << compz << std::endl; |
3185
|
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 |
3538
|
577 std::cout << "qz: ordering eigenvalues: ord_job = " << ord_job[0] << std::endl; |
3185
|
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: |
3427
|
603 // invalid order option (should never happen, since we |
3185
|
604 // checked the options at the top). |
|
605 panic_impossible (); |
|
606 break; |
3550
|
607 } |
3183
|
608 |
3550
|
609 int ndim, fail; |
3185
|
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 |
3538
|
619 std::cout << "qz: calling dsubsp: aa=" << std::endl; |
3531
|
620 octave_print_internal (std::cout, aa, 0); |
3538
|
621 std::cout << std::endl << "bb=" << std::endl; |
3531
|
622 octave_print_internal (std::cout, bb, 0); |
3185
|
623 if (compz == 'V') |
|
624 { |
3538
|
625 std::cout << std::endl << "ZZ=" << std::endl; |
3531
|
626 octave_print_internal (std::cout, ZZ, 0); |
3185
|
627 } |
3538
|
628 std::cout << std::endl; |
|
629 std::cout << "alphar = " << std::endl; |
3531
|
630 octave_print_internal (std::cout, (Matrix) alphar, 0); |
3538
|
631 std::cout << std::endl << "alphai = " << std::endl; |
3531
|
632 octave_print_internal (std::cout, (Matrix) alphai, 0); |
3538
|
633 std::cout << std::endl << "beta = " << std::endl; |
3531
|
634 octave_print_internal (std::cout, (Matrix) betar, 0); |
3538
|
635 std::cout << std::endl; |
3185
|
636 #endif |
|
637 |
3550
|
638 Array<int> ind (nn); |
|
639 |
3185
|
640 F77_XFCN (dsubsp, DSUBSP, |
|
641 (nn, nn, aa.fortran_vec(), bb.fortran_vec(), |
3550
|
642 ZZ.fortran_vec(), sort_test, eps, ndim, fail, |
|
643 ind.fortran_vec ())); |
3185
|
644 |
|
645 #ifdef DEBUG |
3538
|
646 std::cout << "qz: back from dsubsp: aa=" << std::endl; |
3531
|
647 octave_print_internal (std::cout, aa, 0); |
3538
|
648 std::cout << std::endl << "bb=" << std::endl; |
3531
|
649 octave_print_internal (std::cout, bb, 0); |
3185
|
650 if (compz == 'V') |
|
651 { |
3538
|
652 std::cout << std::endl << "ZZ=" << std::endl; |
3531
|
653 octave_print_internal (std::cout, ZZ, 0); |
3185
|
654 } |
3538
|
655 std::cout << std::endl; |
3185
|
656 #endif |
|
657 |
|
658 // manually update alphar, alphai, betar |
|
659 static int jj; |
|
660 |
|
661 jj=0; |
|
662 while (jj < nn) |
|
663 { |
|
664 #ifdef DEBUG_EIG |
3538
|
665 std::cout << "computing gen eig #" << jj << std::endl; |
3185
|
666 #endif |
|
667 |
|
668 static int zcnt; // number of zeros in this block |
|
669 |
|
670 if (jj == (nn-1)) |
|
671 zcnt = 1; |
|
672 else if (aa(jj+1,jj) == 0) |
|
673 zcnt = 1; |
|
674 else zcnt = 2; |
|
675 |
|
676 if (zcnt == 1) // real zero |
|
677 { |
|
678 #ifdef DEBUG_EIG |
3538
|
679 std::cout << " single gen eig:" << std::endl; |
|
680 std::cout << " alphar(" << jj << ") = " << aa(jj,jj) << std::endl; |
|
681 std::cout << " betar( " << jj << ") = " << bb(jj,jj) << std::endl; |
|
682 std::cout << " alphai(" << jj << ") = 0" << std::endl; |
3185
|
683 #endif |
|
684 |
|
685 alphar(jj) = aa(jj,jj); |
|
686 alphai(jj) = 0; |
|
687 betar(jj) = bb(jj,jj); |
|
688 } |
|
689 else |
|
690 { |
|
691 // complex conjugate pair |
|
692 #ifdef DEBUG_EIG |
3538
|
693 std::cout << "qz: calling dlag2:" << std::endl; |
3531
|
694 std::cout << "safmin=" |
3538
|
695 << setiosflags (std::ios::scientific) << safmin << std::endl; |
3185
|
696 |
|
697 for (int idr = jj; idr <= jj+1; idr++) |
|
698 { |
|
699 for (int idc = jj; idc <= jj+1; idc++) |
|
700 { |
3531
|
701 std::cout << "aa(" << idr << "," << idc << ")=" |
3538
|
702 << aa(idr,idc) << std::endl; |
3531
|
703 std::cout << "bb(" << idr << "," << idc << ")=" |
3538
|
704 << bb(idr,idc) << std::endl; |
3185
|
705 } |
|
706 } |
|
707 #endif |
|
708 |
|
709 double scale1, scale2, wr1, wr2, wi; |
|
710 F77_XFCN (dlag2, DLAG2, |
|
711 (&aa(jj,jj), nn, &bb(jj,jj), nn, safmin, |
|
712 scale1, scale2, wr1, wr2, wi)); |
|
713 |
|
714 #ifdef DEBUG_EIG |
3531
|
715 std::cout << "dlag2 returns: scale1=" << scale1 |
3538
|
716 << "\tscale2=" << scale2 << std::endl |
3185
|
717 << "\twr1=" << wr1 << "\twr2=" << wr2 |
3538
|
718 << "\twi=" << wi << std::endl; |
3185
|
719 #endif |
|
720 |
|
721 // just to be safe, check if it's a real pair |
|
722 if (wi == 0) |
|
723 { |
|
724 alphar(jj) = wr1; |
|
725 alphai(jj) = 0; |
|
726 betar(jj) = scale1; |
|
727 alphar(jj+1) = wr2; |
|
728 alphai(jj+1) = 0; |
|
729 betar(jj+1) = scale2; |
|
730 } |
|
731 else |
|
732 { |
|
733 alphar(jj) = alphar(jj+1)=wr1; |
|
734 alphai(jj) = -(alphai(jj+1) = wi); |
|
735 betar(jj) = betar(jj+1) = scale1; |
|
736 } |
|
737 } |
|
738 |
|
739 // advance past this block |
|
740 jj += zcnt; |
|
741 } |
|
742 |
|
743 #ifdef DEBUG_SORT |
3538
|
744 std::cout << "qz: back from dsubsp: aa=" << std::endl; |
3531
|
745 octave_print_internal (std::cout, aa, 0); |
3538
|
746 std::cout << std::endl << "bb=" << std::endl; |
3531
|
747 octave_print_internal (std::cout, bb, 0); |
3185
|
748 |
|
749 if (compz == 'V') |
|
750 { |
3538
|
751 std::cout << std::endl << "ZZ=" << std::endl; |
3531
|
752 octave_print_internal (std::cout, ZZ, 0); |
3185
|
753 } |
3538
|
754 std::cout << std::endl << "qz: ndim=" << ndim << std::endl |
|
755 << "fail=" << fail << std::endl; |
|
756 std::cout << "alphar = " << std::endl; |
3531
|
757 octave_print_internal (std::cout, (Matrix) alphar, 0); |
3538
|
758 std::cout << std::endl << "alphai = " << std::endl; |
3531
|
759 octave_print_internal (std::cout, (Matrix) alphai, 0); |
3538
|
760 std::cout << std::endl << "beta = " << std::endl; |
3531
|
761 octave_print_internal (std::cout, (Matrix) betar, 0); |
3538
|
762 std::cout << std::endl; |
3185
|
763 #endif |
|
764 } |
3183
|
765 } |
3185
|
766 |
3183
|
767 // compute generalized eigenvalues? |
|
768 ComplexColumnVector gev; |
3185
|
769 |
|
770 if (nargout < 2 || nargout == 7 || (nargin == 3 && nargout == 4)) |
3183
|
771 { |
3185
|
772 if (complex_case) |
|
773 { |
|
774 error ("complex case not yet implemented"); |
|
775 return retval; |
|
776 } |
|
777 else |
|
778 { |
|
779 #ifdef DEBUG |
3538
|
780 std::cout << "qz: computing generalized eigenvalues" << std::endl; |
3185
|
781 #endif |
3183
|
782 |
3185
|
783 // return finite generalized eigenvalues |
|
784 int cnt = 0; |
|
785 |
|
786 for (int ii = 0; ii < nn; ii++) |
|
787 if (betar(ii) != 0) |
|
788 cnt++; |
|
789 |
|
790 ComplexColumnVector tmp(cnt); |
|
791 |
3671
|
792 cnt = 0; |
3185
|
793 for (int ii = 0; ii < nn; ii++) |
|
794 if (betar(ii) != 0) |
3671
|
795 tmp(cnt++) = Complex(alphar(ii), alphai(ii))/betar(ii); |
3185
|
796 gev = tmp; |
|
797 } |
3183
|
798 } |
|
799 |
|
800 // right, left eigenvector matrices |
3185
|
801 if (nargout >= 5) |
3183
|
802 { |
3185
|
803 char side = (nargout == 5 ? 'R' : 'B'); // which side to compute? |
|
804 char howmny = 'B'; // compute all of them and backtransform |
|
805 int *select = NULL; // dummy pointer; select is not used. |
|
806 int m; |
|
807 |
|
808 if (complex_case) |
|
809 { |
|
810 error ("complex type not yet implemented"); |
|
811 return retval; |
|
812 } |
|
813 else |
|
814 { |
|
815 #ifdef DEBUG |
3538
|
816 std::cout << "qz: computing generalized eigenvectors" << std::endl; |
3185
|
817 #endif |
|
818 |
|
819 VL = QQ; |
|
820 VR = ZZ; |
|
821 |
|
822 F77_XFCN (dtgevc, DTGEVC, |
|
823 (&side, &howmny, select, nn, aa.fortran_vec(), |
|
824 nn, bb.fortran_vec(), nn, VL.fortran_vec(), nn, |
|
825 VR.fortran_vec(), nn, nn, m, work.fortran_vec(), |
|
826 info, 1L, 1L)); |
|
827 |
|
828 if (f77_exception_encountered) |
|
829 { |
|
830 error ("unrecoverable error in qz (dtgevc)"); |
|
831 return retval; |
|
832 } |
3183
|
833 |
3185
|
834 // now construct the complex form of VV, WW |
|
835 int jj = 0; |
|
836 |
|
837 while (jj < nn) |
|
838 { |
|
839 // see if real or complex eigenvalue |
|
840 int cinc = 2; // column increment; assume complex eigenvalue |
|
841 |
|
842 if (jj == (nn-1)) |
|
843 cinc = 1; // single column |
|
844 else if (aa(jj+1,jj) == 0) |
|
845 cinc = 1; |
|
846 |
|
847 // now copy the eigenvector (s) to CVR, CVL |
|
848 if (cinc == 1) |
|
849 { |
|
850 for (int ii = 0; ii < nn; ii++) |
|
851 CVR(ii,jj) = VR(ii,jj); |
|
852 |
|
853 if (side == 'B') |
|
854 for (int ii = 0; ii < nn; ii++) |
|
855 CVL(ii,jj) = VL(ii,jj); |
|
856 } |
|
857 else |
|
858 { |
|
859 // double column; complex vector |
|
860 |
|
861 for (int ii = 0; ii < nn; ii++) |
|
862 { |
|
863 CVR(ii,jj) = Complex (VR(ii,jj), VR(ii,jj+1)); |
|
864 CVR(ii,jj+1) = Complex (VR(ii,jj), -VR(ii,jj+1)); |
|
865 } |
3183
|
866 |
3185
|
867 if (side == 'B') |
|
868 for (int ii = 0; ii < nn; ii++) |
|
869 { |
|
870 CVL(ii,jj) = Complex (VL(ii,jj), VL(ii,jj+1)); |
|
871 CVL(ii,jj+1) = Complex (VL(ii,jj), -VL(ii,jj+1)); |
|
872 } |
|
873 } |
|
874 |
|
875 // advance to next eigenvectors (if any) |
|
876 jj += cinc; |
|
877 } |
|
878 } |
3183
|
879 } |
3185
|
880 |
|
881 switch (nargout) |
|
882 { |
|
883 case 7: |
|
884 retval(6) = gev; |
|
885 |
|
886 case 6: // return eigenvectors |
|
887 retval(5) = CVL; |
|
888 |
|
889 case 5: // return eigenvectors |
|
890 retval(4) = CVR; |
|
891 |
|
892 case 4: |
|
893 if (nargin == 3) |
|
894 { |
|
895 #ifdef DEBUG |
3538
|
896 std::cout << "qz: sort: retval(3) = gev = " << std::endl; |
3531
|
897 octave_print_internal (std::cout, gev); |
3538
|
898 std::cout << std::endl; |
3185
|
899 #endif |
|
900 retval(3) = gev; |
|
901 } |
|
902 else |
|
903 retval(3) = ZZ; |
|
904 |
|
905 case 3: |
|
906 if (nargin == 3) |
|
907 retval(2) = ZZ; |
|
908 else |
|
909 retval(2) = QQ; |
|
910 |
|
911 case 2: |
|
912 #ifdef DEBUG |
3538
|
913 std::cout << "qz: retval (1) = bb = " << std::endl; |
3531
|
914 octave_print_internal (std::cout, bb, 0); |
3538
|
915 std::cout << std::endl << "qz: retval(0) = aa = " <<std::endl; |
3531
|
916 octave_print_internal (std::cout, aa, 0); |
3538
|
917 std::cout << std::endl; |
3185
|
918 #endif |
|
919 retval(1) = bb; |
|
920 retval(0) = aa; |
|
921 break; |
|
922 |
|
923 case 1: |
|
924 case 0: |
|
925 #ifdef DEBUG |
3538
|
926 std::cout << "qz: retval(0) = gev = " << gev << std::endl; |
3185
|
927 #endif |
|
928 retval(0) = gev; |
|
929 break; |
|
930 |
|
931 default: |
|
932 error ("qz: too many return arguments."); |
|
933 break; |
3183
|
934 } |
|
935 |
3185
|
936 #ifdef DEBUG |
3538
|
937 std::cout << "qz: exiting (at long last)" << std::endl; |
3185
|
938 #endif |
3183
|
939 |
|
940 return retval; |
|
941 } |
|
942 |
|
943 /* |
|
944 ;;; Local Variables: *** |
|
945 ;;; mode: C++ *** |
|
946 ;;; End: *** |
|
947 */ |