2928
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2005, 2006, |
|
4 2007 John W. Eaton |
2928
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
2928
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
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\ |
7096
|
80 Compute @code{aa = dd \\ a * dd} in which @code{aa} is a matrix whose\n\ |
|
81 row and column norms are roughly equal in magnitude, and\n\ |
|
82 @code{dd} = @code{p * d}, in which @code{p} is a permutation\n\ |
3372
|
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\ |
7096
|
87 If four output values are requested, compute @code{aa = cc*a*dd} and\n\ |
|
88 @code{bb = cc*b*dd)}, in which @code{aa} and @code{bb} have non-zero\n\ |
3372
|
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\ |
7096
|
93 The eigenvalue balancing option @code{opt} may be one of:\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 { |
5823
|
123 print_usage (); |
2928
|
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 else |
|
258 { |
|
259 // real matrices case |
3181
|
260 |
3185
|
261 F77_XFCN (dggbal, DGGBAL, |
4552
|
262 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
263 nn, aa.fortran_vec (), nn, bb.fortran_vec (), |
|
264 nn, ilo, ihi, lscale.fortran_vec (), |
|
265 rscale.fortran_vec (), work.fortran_vec (), info |
|
266 F77_CHAR_ARG_LEN (1))); |
3185
|
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 |
5275
|
274 for (octave_idx_type ii = 0; ii < nn; ii++) |
|
275 for (octave_idx_type 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, |
4552
|
284 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
285 F77_CONST_CHAR_ARG2 ("L", 1), |
4628
|
286 nn, ilo, ihi, lscale.data (), rscale.data (), |
|
287 nn, Pl.fortran_vec (), nn, info |
4552
|
288 F77_CHAR_ARG_LEN (1) |
|
289 F77_CHAR_ARG_LEN (1))); |
3185
|
290 |
|
291 // then right |
|
292 F77_XFCN (dggbak, DGGBAK, |
4552
|
293 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
294 F77_CONST_CHAR_ARG2 ("R", 1), |
4628
|
295 nn, ilo, ihi, lscale.data (), rscale.data (), |
|
296 nn, Pr.fortran_vec (), nn, info |
4552
|
297 F77_CHAR_ARG_LEN (1) |
|
298 F77_CHAR_ARG_LEN (1))); |
3185
|
299 |
|
300 switch (nargout) |
|
301 { |
|
302 case 0: |
|
303 case 1: |
|
304 warning ("balance: used GEP, should have two output arguments"); |
|
305 if (complex_case) |
|
306 retval(0) = caa; |
|
307 else |
|
308 retval(0) = aa; |
|
309 break; |
2928
|
310 |
3185
|
311 case 2: |
|
312 if (complex_case) |
|
313 { |
|
314 retval(1) = cbb; |
|
315 retval(0) = caa; |
|
316 } |
|
317 else |
|
318 { |
|
319 retval(1) = bb; |
|
320 retval(0) = aa; |
|
321 } |
|
322 break; |
3181
|
323 |
3185
|
324 case 4: |
|
325 if (complex_case) |
|
326 { |
|
327 retval(3) = cbb; |
|
328 retval(2) = caa; |
|
329 } |
|
330 else |
|
331 { |
|
332 retval(3) = bb; |
|
333 retval(2) = aa; |
|
334 } |
|
335 retval(1) = Pr; |
|
336 retval(0) = Pl.transpose (); // so that aa_bal = cc*aa*dd, etc. |
|
337 break; |
|
338 |
|
339 default: |
|
340 error ("balance: invalid number of output arguments"); |
|
341 break; |
|
342 } |
2928
|
343 } |
3185
|
344 |
2928
|
345 return retval; |
|
346 } |
|
347 |
|
348 /* |
|
349 ;;; Local Variables: *** |
|
350 ;;; mode: C++ *** |
|
351 ;;; End: *** |
|
352 */ |