2928
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
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 |
3911
|
23 // Author: A. S. Hodel <scotte@eng.auburn.edu> |
2928
|
24 |
|
25 #ifdef HAVE_CONFIG_H |
|
26 #include <config.h> |
|
27 #endif |
|
28 |
|
29 #include <string> |
|
30 |
|
31 #include "CmplxAEPBAL.h" |
|
32 #include "CmplxAEPBAL.h" |
|
33 #include "dbleAEPBAL.h" |
|
34 #include "dbleAEPBAL.h" |
4153
|
35 #include "quit.h" |
2928
|
36 |
|
37 #include "defun-dld.h" |
|
38 #include "error.h" |
3181
|
39 #include "f77-fcn.h" |
2928
|
40 #include "gripes.h" |
|
41 #include "oct-obj.h" |
|
42 #include "utils.h" |
|
43 |
3181
|
44 extern "C" |
|
45 { |
4552
|
46 F77_RET_T |
|
47 F77_FUNC (dggbal, DGGBAL) (F77_CONST_CHAR_ARG_DECL, const int& N, |
|
48 double* A, const int& LDA, double* B, |
|
49 const int& LDB, int& ILO, int& IHI, |
|
50 double* LSCALE, double* RSCALE, |
|
51 double* WORK, int& INFO |
|
52 F77_CHAR_ARG_LEN_DECL); |
3181
|
53 |
4552
|
54 F77_RET_T |
|
55 F77_FUNC (dggbak, DGGBAK) (F77_CONST_CHAR_ARG_DECL, |
|
56 F77_CONST_CHAR_ARG_DECL, |
|
57 const int& N, const int& ILO, |
4566
|
58 const int& IHI, const double* LSCALE, |
|
59 const double* RSCALE, int& M, double* V, |
4552
|
60 const int& LDV, int& INFO |
|
61 F77_CHAR_ARG_LEN_DECL |
|
62 F77_CHAR_ARG_LEN_DECL); |
3181
|
63 |
4552
|
64 F77_RET_T |
|
65 F77_FUNC (zggbal, ZGGBAL) (F77_CONST_CHAR_ARG_DECL, const int& N, |
|
66 Complex* A, const int& LDA, Complex* B, |
|
67 const int& LDB, int& ILO, int& IHI, |
|
68 double* LSCALE, double* RSCALE, |
|
69 double* WORK, int& INFO |
|
70 F77_CHAR_ARG_LEN_DECL); |
3181
|
71 } |
|
72 |
2928
|
73 DEFUN_DLD (balance, args, nargout, |
3548
|
74 "-*- texinfo -*-\n\ |
3372
|
75 @deftypefn {Loadable Function} {@var{aa} =} balance (@var{a}, @var{opt})\n\ |
|
76 @deftypefnx {Loadable Function} {[@var{dd}, @var{aa}] =} balance (@var{a}, @var{opt})\n\ |
3402
|
77 @deftypefnx {Loadable Function} {[@var{cc}, @var{dd}, @var{aa}, @var{bb}] =} balance (@var{a}, @var{b}, @var{opt})\n\ |
2928
|
78 \n\ |
3372
|
79 @code{[dd, aa] = balance (a)} returns @code{aa = dd \\ a * dd}.\n\ |
|
80 @code{aa} is a matrix whose row and column norms are roughly equal in\n\ |
|
81 magnitude, and @code{dd} = @code{p * d}, where @code{p} is a permutation\n\ |
|
82 matrix and @code{d} is a diagonal matrix of powers of two. This allows\n\ |
|
83 the equilibration to be computed without roundoff. Results of\n\ |
|
84 eigenvalue calculation are typically improved by balancing first.\n\ |
2928
|
85 \n\ |
3372
|
86 @code{[cc, dd, aa, bb] = balance (a, b)} returns @code{aa = cc*a*dd} and\n\ |
|
87 @code{bb = cc*b*dd)}, where @code{aa} and @code{bb} have non-zero\n\ |
|
88 elements of approximately the same magnitude and @code{cc} and @code{dd}\n\ |
|
89 are permuted diagonal matrices as in @code{dd} for the algebraic\n\ |
|
90 eigenvalue problem.\n\ |
|
91 \n\ |
|
92 The eigenvalue balancing option @code{opt} is selected as follows:\n\ |
2928
|
93 \n\ |
3372
|
94 @table @asis\n\ |
|
95 @item @code{\"N\"}, @code{\"n\"}\n\ |
|
96 No balancing; arguments copied, transformation(s) set to identity.\n\ |
|
97 \n\ |
|
98 @item @code{\"P\"}, @code{\"p\"}\n\ |
|
99 Permute argument(s) to isolate eigenvalues where possible.\n\ |
|
100 \n\ |
|
101 @item @code{\"S\"}, @code{\"s\"}\n\ |
|
102 Scale to improve accuracy of computed eigenvalues.\n\ |
2928
|
103 \n\ |
3372
|
104 @item @code{\"B\"}, @code{\"b\"}\n\ |
|
105 Permute and scale, in that order. Rows/columns of a (and b)\n\ |
|
106 that are isolated by permutation are not scaled. This is the default\n\ |
|
107 behavior.\n\ |
|
108 @end table\n\ |
2928
|
109 \n\ |
3372
|
110 Algebraic eigenvalue balancing uses standard @sc{Lapack} routines.\n\ |
|
111 \n\ |
|
112 Generalized eigenvalue problem balancing uses Ward's algorithm\n\ |
|
113 (SIAM Journal on Scientific and Statistical Computing, 1981).\n\ |
|
114 @end deftypefn") |
2928
|
115 { |
|
116 octave_value_list retval; |
|
117 |
|
118 int nargin = args.length (); |
|
119 |
|
120 if (nargin < 1 || nargin > 3 || nargout < 0 || nargout > 4) |
|
121 { |
|
122 print_usage ("balance"); |
|
123 return retval; |
|
124 } |
|
125 |
3181
|
126 // determine if it's AEP or GEP |
3185
|
127 int AEPcase = nargin == 1 ? 1 : args(1).is_string (); |
3523
|
128 std::string bal_job; |
2928
|
129 |
3181
|
130 // problem dimension |
|
131 int nn = args(0).rows (); |
2928
|
132 |
3181
|
133 int arg_is_empty = empty_arg ("balance", nn, args(0).columns()); |
2928
|
134 |
|
135 if (arg_is_empty < 0) |
|
136 return retval; |
3185
|
137 |
2928
|
138 if (arg_is_empty > 0) |
|
139 return octave_value_list (2, Matrix ()); |
|
140 |
3181
|
141 if (nn != args(0).columns()) |
3185
|
142 { |
|
143 gripe_square_matrix_required ("balance"); |
|
144 return retval; |
|
145 } |
2928
|
146 |
|
147 // Extract argument 1 parameter for both AEP and GEP. |
|
148 Matrix aa; |
|
149 ComplexMatrix caa; |
3185
|
150 |
|
151 if (args(0).is_complex_type ()) |
|
152 caa = args(0).complex_matrix_value (); |
|
153 else |
|
154 aa = args(0).matrix_value (); |
|
155 |
|
156 if (error_state) |
|
157 return retval; |
2928
|
158 |
|
159 // Treat AEP/GEP cases. |
3185
|
160 if (AEPcase) |
|
161 { |
|
162 // Algebraic eigenvalue problem. |
2928
|
163 |
3185
|
164 if (nargin == 1) |
|
165 bal_job = "B"; |
|
166 else if (args(1).is_string ()) |
|
167 bal_job = args(1).string_value (); |
3181
|
168 else |
3185
|
169 { |
|
170 error ("balance: AEP argument 2 must be a string"); |
|
171 return retval; |
|
172 } |
|
173 |
|
174 // balance the AEP |
|
175 if (args(0).is_complex_type ()) |
|
176 { |
|
177 ComplexAEPBALANCE result (caa, bal_job); |
2928
|
178 |
3185
|
179 if (nargout == 0 || nargout == 1) |
|
180 retval(0) = result.balanced_matrix (); |
|
181 else |
|
182 { |
|
183 retval(1) = result.balanced_matrix (); |
|
184 retval(0) = result.balancing_matrix (); |
|
185 } |
|
186 } |
3181
|
187 else |
3185
|
188 { |
|
189 AEPBALANCE result (aa, bal_job); |
|
190 |
|
191 if (nargout == 0 || nargout == 1) |
|
192 retval(0) = result.balanced_matrix (); |
|
193 else |
|
194 { |
|
195 retval(1) = result.balanced_matrix (); |
|
196 retval(0) = result.balancing_matrix (); |
|
197 } |
|
198 } |
3181
|
199 } |
|
200 else |
|
201 { |
3185
|
202 // Generalized eigenvalue problem. |
|
203 if (nargin == 2) |
|
204 bal_job = "B"; |
|
205 else if (args(2).is_string ()) |
|
206 bal_job = args(2).string_value (); |
|
207 else |
|
208 { |
|
209 error ("balance: GEP argument 3 must be a string"); |
|
210 return retval; |
|
211 } |
|
212 |
|
213 if ((nn != args(1).columns ()) || (nn != args(1).rows ())) |
|
214 { |
|
215 gripe_nonconformant (); |
|
216 return retval; |
|
217 } |
|
218 |
|
219 Matrix bb; |
|
220 ComplexMatrix cbb; |
|
221 |
|
222 if (args(1).is_complex_type ()) |
|
223 cbb = args(1).complex_matrix_value (); |
|
224 else |
|
225 bb = args(1).matrix_value (); |
|
226 |
|
227 if (error_state) |
|
228 return retval; |
|
229 |
|
230 // Both matrices loaded, now let's check what kind of arithmetic: |
|
231 // first, declare variables used in both the real and complex case |
|
232 |
|
233 int ilo, ihi, info; |
|
234 RowVector lscale(nn), rscale(nn), work(6*nn); |
|
235 char job = bal_job[0]; |
|
236 |
|
237 static int complex_case |
|
238 = (args(0).is_complex_type () || args(1).is_complex_type ()); |
|
239 |
|
240 // now balance |
|
241 if (complex_case) |
|
242 { |
|
243 if (args(0).is_real_type ()) |
3587
|
244 caa = ComplexMatrix (aa); |
3185
|
245 |
|
246 if (args(1).is_real_type ()) |
3587
|
247 cbb = ComplexMatrix (bb); |
3181
|
248 |
3185
|
249 F77_XFCN (zggbal, ZGGBAL, |
4552
|
250 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
251 nn, caa.fortran_vec (), nn, cbb.fortran_vec (), |
|
252 nn, ilo, ihi, lscale.fortran_vec (), |
|
253 rscale.fortran_vec (), work.fortran_vec (), info |
|
254 F77_CHAR_ARG_LEN (1))); |
3185
|
255 |
|
256 if (f77_exception_encountered) |
|
257 { |
|
258 error ("unrecoverable error in balance GEP"); |
|
259 return retval; |
|
260 } |
|
261 } |
|
262 else |
|
263 { |
|
264 // real matrices case |
3181
|
265 |
3185
|
266 F77_XFCN (dggbal, DGGBAL, |
4552
|
267 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
268 nn, aa.fortran_vec (), nn, bb.fortran_vec (), |
|
269 nn, ilo, ihi, lscale.fortran_vec (), |
|
270 rscale.fortran_vec (), work.fortran_vec (), info |
|
271 F77_CHAR_ARG_LEN (1))); |
3185
|
272 |
|
273 if (f77_exception_encountered) |
|
274 { |
|
275 error ("unrecoverable error in balance GEP"); |
|
276 return retval; |
|
277 } |
|
278 } |
|
279 |
|
280 // Since we just want the balancing matrices, we can use dggbal |
|
281 // for both the real and complex cases. |
|
282 |
|
283 Matrix Pl(nn,nn), Pr(nn,nn); |
|
284 |
|
285 for (int ii = 0; ii < nn; ii++) |
|
286 for (int jj = 0; jj < nn; jj++) |
4153
|
287 { |
|
288 OCTAVE_QUIT; |
|
289 |
|
290 Pl(ii,jj) = Pr(ii,jj) = (ii == jj ? 1.0 : 0.0); |
|
291 } |
3185
|
292 |
|
293 // left first |
|
294 F77_XFCN (dggbak, DGGBAK, |
4552
|
295 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
296 F77_CONST_CHAR_ARG2 ("L", 1), |
4628
|
297 nn, ilo, ihi, lscale.data (), rscale.data (), |
|
298 nn, Pl.fortran_vec (), nn, info |
4552
|
299 F77_CHAR_ARG_LEN (1) |
|
300 F77_CHAR_ARG_LEN (1))); |
3185
|
301 |
|
302 if (f77_exception_encountered) |
|
303 { |
|
304 error ("unrecoverable error in balance GEP(L)"); |
|
305 return retval; |
|
306 } |
|
307 |
|
308 // then right |
|
309 F77_XFCN (dggbak, DGGBAK, |
4552
|
310 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
311 F77_CONST_CHAR_ARG2 ("R", 1), |
4628
|
312 nn, ilo, ihi, lscale.data (), rscale.data (), |
|
313 nn, Pr.fortran_vec (), nn, info |
4552
|
314 F77_CHAR_ARG_LEN (1) |
|
315 F77_CHAR_ARG_LEN (1))); |
3185
|
316 |
|
317 if (f77_exception_encountered) |
|
318 { |
|
319 error ("unrecoverable error in balance GEP(R)"); |
|
320 return retval; |
|
321 } |
2928
|
322 |
3185
|
323 switch (nargout) |
|
324 { |
|
325 case 0: |
|
326 case 1: |
|
327 warning ("balance: used GEP, should have two output arguments"); |
|
328 if (complex_case) |
|
329 retval(0) = caa; |
|
330 else |
|
331 retval(0) = aa; |
|
332 break; |
2928
|
333 |
3185
|
334 case 2: |
|
335 if (complex_case) |
|
336 { |
|
337 retval(1) = cbb; |
|
338 retval(0) = caa; |
|
339 } |
|
340 else |
|
341 { |
|
342 retval(1) = bb; |
|
343 retval(0) = aa; |
|
344 } |
|
345 break; |
3181
|
346 |
3185
|
347 case 4: |
|
348 if (complex_case) |
|
349 { |
|
350 retval(3) = cbb; |
|
351 retval(2) = caa; |
|
352 } |
|
353 else |
|
354 { |
|
355 retval(3) = bb; |
|
356 retval(2) = aa; |
|
357 } |
|
358 retval(1) = Pr; |
|
359 retval(0) = Pl.transpose (); // so that aa_bal = cc*aa*dd, etc. |
|
360 break; |
|
361 |
|
362 default: |
|
363 error ("balance: invalid number of output arguments"); |
|
364 break; |
|
365 } |
2928
|
366 } |
3185
|
367 |
2928
|
368 return retval; |
|
369 } |
|
370 |
|
371 /* |
|
372 ;;; Local Variables: *** |
|
373 ;;; mode: C++ *** |
|
374 ;;; End: *** |
|
375 */ |