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