519
|
1 // f-balance.cc -*- C++ -*- |
16
|
2 /* |
|
3 |
453
|
4 Copyright (C) 1993, 1994 John W. Eaton |
16
|
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 |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
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 |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 // Written by A. S. Hodel <scotte@eng.auburn.edu> |
|
25 |
240
|
26 #ifdef HAVE_CONFIG_H |
|
27 #include "config.h" |
16
|
28 #endif |
|
29 |
453
|
30 #include "dMatrix.h" |
|
31 #include "CMatrix.h" |
|
32 #include "dbleAEPBAL.h" |
|
33 #include "CmplxAEPBAL.h" |
|
34 #include "dbleAEPBAL.h" |
|
35 #include "CmplxAEPBAL.h" |
|
36 #include "dbleGEPBAL.h" |
16
|
37 |
|
38 #include "tree-const.h" |
|
39 #include "user-prefs.h" |
49
|
40 #include "gripes.h" |
16
|
41 #include "error.h" |
519
|
42 #include "defun-dld.h" |
16
|
43 |
519
|
44 DEFUN_DLD ("balance", Fbalance, Sbalance, 4, 4, |
|
45 "AA = balance (A [, OPT]) or [[DD,] AA] = balance (A [, OPT])\n\ |
|
46 \n\ |
|
47 generalized eigenvalue problem:\n\ |
|
48 \n\ |
|
49 [cc, dd, aa, bb] = balance (a, b [, opt])\n\ |
|
50 \n\ |
|
51 where OPT is an optional single character argument as follows: \n\ |
|
52 \n\ |
|
53 N: no balancing; arguments copied, transformation(s) set to identity\n\ |
|
54 P: permute argument(s) to isolate eigenvalues where possible\n\ |
|
55 S: scale to improve accuracy of computed eigenvalues\n\ |
|
56 B: (default) permute and scale, in that order. Rows/columns\n\ |
|
57 of a (and b) that are isolated by permutation are not scaled\n\ |
|
58 \n\ |
|
59 [DD, AA] = balance (A, OPT) returns aa = dd\a*dd,\n\ |
|
60 \n\ |
|
61 [CC, DD, AA, BB] = balance (A, B, OPT) returns AA (BB) = CC*A*DD (CC*B*DD)") |
16
|
62 { |
497
|
63 Octave_object retval; |
16
|
64 |
506
|
65 int nargin = args.length (); |
|
66 |
519
|
67 if (nargin < 2 || nargin > 4 || nargout < 0 || nargout > 4) |
|
68 { |
|
69 print_usage ("balance"); |
|
70 return retval; |
|
71 } |
|
72 |
|
73 char *bal_job; |
|
74 int my_nargin; // # args w/o optional string arg |
|
75 |
16
|
76 // determine if balancing option is listed |
|
77 // set my_nargin to the number of matrix inputs |
516
|
78 if (args(nargin-1).const_type () == tree_constant_rep::string_constant) |
|
79 { |
|
80 bal_job = args(nargin-1).string_value (); |
|
81 my_nargin = nargin-2; |
|
82 } |
16
|
83 else |
516
|
84 { |
|
85 bal_job = "B"; |
|
86 my_nargin = nargin-1; |
|
87 } |
16
|
88 |
497
|
89 tree_constant arg = args(1).make_numeric (); |
16
|
90 int a_nr = arg.rows (); |
|
91 int a_nc = arg.columns (); |
|
92 |
|
93 // Check argument 1 dimensions. |
|
94 |
|
95 if (a_nr == 0 || a_nc == 0) |
|
96 { |
|
97 int flag = user_pref.propagate_empty_matrices; |
|
98 if (flag != 0) |
|
99 { |
516
|
100 if (flag < 0) |
|
101 warning ("balance: argument is empty matrix"); |
|
102 |
16
|
103 Matrix m; |
497
|
104 retval.resize (2); |
516
|
105 retval(0) = m; |
|
106 retval(1) = m; |
16
|
107 } |
|
108 else |
|
109 error ("balance: empty matrix is invalid as argument"); |
|
110 |
|
111 return retval; |
|
112 } |
|
113 |
|
114 if (a_nr != a_nc) |
|
115 { |
|
116 gripe_square_matrix_required ("balance"); |
|
117 return retval; |
|
118 } |
|
119 |
|
120 // Extract argument 1 parameter for both AEP and GEP. |
|
121 |
|
122 Matrix aa; |
|
123 ComplexMatrix caa; |
|
124 if (arg.is_complex_type ()) |
|
125 { |
|
126 if (arg.is_matrix_type ()) |
|
127 caa=arg.complex_matrix_value (); |
|
128 else |
|
129 { |
|
130 caa.resize (1, 1); |
|
131 caa.elem (0, 0) = arg.complex_value (); |
|
132 } |
|
133 } |
|
134 else |
|
135 { |
|
136 if (arg.is_matrix_type ()) |
|
137 aa = arg.matrix_value (); |
|
138 else |
|
139 { |
|
140 double d = arg.double_value (); |
|
141 aa.resize (1, 1); |
|
142 aa.elem (0, 0) = d; |
|
143 } |
|
144 } |
|
145 |
|
146 // Treat AEP/ GEP cases. |
|
147 |
|
148 switch (my_nargin) |
|
149 { |
|
150 case 1: |
|
151 |
|
152 // Algebraic eigenvalue problem. |
|
153 |
497
|
154 retval.resize (nargout ? nargout : 1); |
16
|
155 if (arg.is_complex_type ()) |
|
156 { |
|
157 ComplexAEPBALANCE result (caa, bal_job); |
|
158 |
497
|
159 if (nargout == 0 || nargout == 1) |
516
|
160 retval(0) = result.balanced_matrix (); |
16
|
161 else |
|
162 { |
516
|
163 retval(0) = result.balancing_matrix (); |
|
164 retval(1) = result.balanced_matrix (); |
16
|
165 } |
|
166 } |
|
167 else |
|
168 { |
|
169 AEPBALANCE result (aa, bal_job); |
|
170 |
497
|
171 if (nargout == 0 || nargout == 1) |
516
|
172 retval(0) = result.balanced_matrix (); |
16
|
173 else |
|
174 { |
516
|
175 retval(0) = result.balancing_matrix (); |
|
176 retval(1) = result.balanced_matrix (); |
16
|
177 } |
|
178 } |
|
179 break; |
|
180 case 2: |
|
181 |
|
182 // Generalized eigenvalue problem. |
|
183 |
|
184 { |
497
|
185 retval.resize (nargout ? nargout : 1); |
16
|
186 |
|
187 // 1st we have to check argument 2 dimensions and type... |
|
188 |
497
|
189 tree_constant brg = args(2).make_numeric (); |
16
|
190 int b_nr = brg.rows (); |
|
191 int b_nc = brg.columns (); |
|
192 |
|
193 // Check argument 2 dimensions -- must match arg 1. |
|
194 |
|
195 if ((b_nr != b_nc) || (b_nr != a_nr)) |
|
196 { |
|
197 gripe_nonconformant (); |
|
198 return retval; |
|
199 } |
|
200 |
|
201 // Now, extract the second matrix... |
|
202 // Extract argument 1 parameter for both AEP and GEP. |
|
203 |
|
204 Matrix bb; |
|
205 ComplexMatrix cbb; |
|
206 if (brg.is_complex_type ()) |
|
207 { |
|
208 if (brg.is_matrix_type ()) |
|
209 cbb = brg.complex_matrix_value (); |
|
210 else |
|
211 { |
|
212 cbb.resize (1, 1); |
|
213 cbb.elem (0, 0) = brg.complex_value (); |
|
214 } |
|
215 } |
|
216 else |
|
217 { |
516
|
218 if (brg.is_matrix_type ()) |
|
219 bb = brg.matrix_value (); |
16
|
220 else |
|
221 { |
|
222 double d = brg.double_value (); |
|
223 bb.resize (1, 1); |
|
224 bb.elem (0, 0) = d; |
|
225 } |
|
226 } |
|
227 |
|
228 // Both matrices loaded, now let's check what kind of arithmetic: |
|
229 |
|
230 if (arg.is_complex_type () || brg.is_complex_type ()) |
|
231 { |
|
232 if (arg.is_real_type ()) |
|
233 caa = aa; |
|
234 else if (brg.is_real_type ()) |
|
235 cbb = bb; |
|
236 |
|
237 // Compute magnitudes of elements for balancing purposes. |
|
238 // Surely there's a function I can call someplace! |
|
239 |
|
240 for (int i = 0; i < a_nr; i++) |
|
241 for (int j = 0; j < a_nr; j++) |
|
242 { |
|
243 aa.elem (i, j) = abs (caa.elem (i, j)); |
|
244 bb.elem (i, j) = abs (cbb.elem (i, j)); |
|
245 } |
|
246 } |
|
247 |
240
|
248 GEPBALANCE result (aa, bb, bal_job); |
16
|
249 |
|
250 if (arg.is_complex_type () || brg.is_complex_type ()) |
|
251 { |
|
252 caa = result.left_balancing_matrix () * caa |
|
253 * result.right_balancing_matrix (); |
|
254 |
|
255 cbb = result.left_balancing_matrix () * cbb |
|
256 * result.right_balancing_matrix (); |
|
257 |
|
258 switch (nargout) |
|
259 { |
503
|
260 case 0: |
16
|
261 case 1: |
|
262 warning ("balance: should use two output arguments"); |
516
|
263 retval(0) = caa; |
16
|
264 break; |
|
265 case 2: |
516
|
266 retval(0) = caa; |
|
267 retval(1) = cbb; |
16
|
268 break; |
|
269 case 4: |
516
|
270 retval(0) = result.left_balancing_matrix (); |
|
271 retval(1) = result.right_balancing_matrix (); |
|
272 retval(2) = caa; |
|
273 retval(3) = cbb; |
16
|
274 break; |
|
275 default: |
503
|
276 error ("balance: invalid number of output arguments"); |
16
|
277 break; |
|
278 } |
|
279 } |
|
280 else |
|
281 { |
|
282 switch (nargout) |
|
283 { |
503
|
284 case 0: |
|
285 case 1: |
|
286 warning ("balance: should use two output arguments"); |
516
|
287 retval(0) = result.balanced_a_matrix (); |
503
|
288 break; |
16
|
289 case 2: |
516
|
290 retval(0) = result.balanced_a_matrix (); |
|
291 retval(1) = result.balanced_b_matrix (); |
16
|
292 break; |
|
293 case 4: |
516
|
294 retval(0) = result.left_balancing_matrix (); |
|
295 retval(1) = result.right_balancing_matrix (); |
|
296 retval(2) = result.balanced_a_matrix (); |
|
297 retval(3) = result.balanced_b_matrix (); |
16
|
298 break; |
|
299 default: |
503
|
300 error ("balance: invalid number of output arguments"); |
16
|
301 break; |
|
302 } |
|
303 } |
|
304 } |
|
305 break; |
|
306 default: |
|
307 error ("balance requires one (AEP) or two (GEP) numeric arguments"); |
|
308 break; |
|
309 } |
|
310 return retval; |
|
311 } |
|
312 |
|
313 /* |
|
314 ;;; Local Variables: *** |
|
315 ;;; mode: C++ *** |
|
316 ;;; page-delimiter: "^/\\*" *** |
|
317 ;;; End: *** |
|
318 */ |