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