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