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" |
636
|
42 #include "utils.h" |
544
|
43 #include "help.h" |
519
|
44 #include "defun-dld.h" |
16
|
45 |
701
|
46 DEFUN_DLD_BUILTIN ("balance", Fbalance, Sbalance, 4, 4, |
519
|
47 "AA = balance (A [, OPT]) or [[DD,] AA] = balance (A [, OPT])\n\ |
|
48 \n\ |
|
49 generalized eigenvalue problem:\n\ |
|
50 \n\ |
|
51 [cc, dd, aa, bb] = balance (a, b [, opt])\n\ |
|
52 \n\ |
|
53 where OPT is an optional single character argument as follows: \n\ |
|
54 \n\ |
|
55 N: no balancing; arguments copied, transformation(s) set to identity\n\ |
|
56 P: permute argument(s) to isolate eigenvalues where possible\n\ |
|
57 S: scale to improve accuracy of computed eigenvalues\n\ |
|
58 B: (default) permute and scale, in that order. Rows/columns\n\ |
|
59 of a (and b) that are isolated by permutation are not scaled\n\ |
|
60 \n\ |
|
61 [DD, AA] = balance (A, OPT) returns aa = dd\a*dd,\n\ |
|
62 \n\ |
|
63 [CC, DD, AA, BB] = balance (A, B, OPT) returns AA (BB) = CC*A*DD (CC*B*DD)") |
16
|
64 { |
497
|
65 Octave_object retval; |
16
|
66 |
506
|
67 int nargin = args.length (); |
|
68 |
712
|
69 if (nargin < 1 || nargin > 3 || nargout < 0 || nargout > 4) |
519
|
70 { |
|
71 print_usage ("balance"); |
|
72 return retval; |
|
73 } |
|
74 |
|
75 char *bal_job; |
|
76 int my_nargin; // # args w/o optional string arg |
|
77 |
636
|
78 // Determine if balancing option is listed. Set my_nargin to the |
|
79 // number of matrix inputs. |
|
80 |
610
|
81 if (args(nargin-1).is_string ()) |
516
|
82 { |
|
83 bal_job = args(nargin-1).string_value (); |
712
|
84 my_nargin = nargin-1; |
516
|
85 } |
16
|
86 else |
516
|
87 { |
|
88 bal_job = "B"; |
712
|
89 my_nargin = nargin; |
516
|
90 } |
16
|
91 |
712
|
92 tree_constant arg_a = args(0); |
636
|
93 |
|
94 int a_nr = arg_a.rows (); |
|
95 int a_nc = arg_a.columns (); |
16
|
96 |
|
97 // Check argument 1 dimensions. |
|
98 |
636
|
99 if (empty_arg ("balance", a_nr, a_nc) < 0) |
|
100 return retval; |
16
|
101 |
|
102 if (a_nr != a_nc) |
|
103 { |
|
104 gripe_square_matrix_required ("balance"); |
|
105 return retval; |
|
106 } |
|
107 |
|
108 // Extract argument 1 parameter for both AEP and GEP. |
|
109 |
|
110 Matrix aa; |
|
111 ComplexMatrix caa; |
636
|
112 if (arg_a.is_complex_type ()) |
|
113 caa = arg_a.complex_matrix_value (); |
16
|
114 else |
636
|
115 aa = arg_a.matrix_value (); |
|
116 |
|
117 if (error_state) |
|
118 return retval; |
16
|
119 |
|
120 // Treat AEP/ GEP cases. |
|
121 |
|
122 switch (my_nargin) |
|
123 { |
|
124 case 1: |
636
|
125 |
16
|
126 // Algebraic eigenvalue problem. |
|
127 |
636
|
128 if (arg_a.is_complex_type ()) |
16
|
129 { |
|
130 ComplexAEPBALANCE result (caa, bal_job); |
|
131 |
497
|
132 if (nargout == 0 || nargout == 1) |
516
|
133 retval(0) = result.balanced_matrix (); |
16
|
134 else |
|
135 { |
636
|
136 retval(1) = result.balanced_matrix (); |
516
|
137 retval(0) = result.balancing_matrix (); |
16
|
138 } |
|
139 } |
|
140 else |
|
141 { |
|
142 AEPBALANCE result (aa, bal_job); |
|
143 |
497
|
144 if (nargout == 0 || nargout == 1) |
516
|
145 retval(0) = result.balanced_matrix (); |
16
|
146 else |
|
147 { |
636
|
148 retval(1) = result.balanced_matrix (); |
516
|
149 retval(0) = result.balancing_matrix (); |
16
|
150 } |
|
151 } |
|
152 break; |
636
|
153 |
16
|
154 case 2: |
636
|
155 { |
16
|
156 // Generalized eigenvalue problem. |
|
157 |
|
158 // 1st we have to check argument 2 dimensions and type... |
|
159 |
712
|
160 tree_constant arg_b = args(1); |
636
|
161 |
|
162 int b_nr = arg_b.rows (); |
|
163 int b_nc = arg_b.columns (); |
16
|
164 |
|
165 // Check argument 2 dimensions -- must match arg 1. |
|
166 |
636
|
167 if (b_nr != b_nc || b_nr != a_nr) |
16
|
168 { |
|
169 gripe_nonconformant (); |
|
170 return retval; |
|
171 } |
|
172 |
|
173 // Now, extract the second matrix... |
|
174 // Extract argument 1 parameter for both AEP and GEP. |
|
175 |
|
176 Matrix bb; |
|
177 ComplexMatrix cbb; |
636
|
178 if (arg_b.is_complex_type ()) |
|
179 cbb = arg_b.complex_matrix_value (); |
16
|
180 else |
636
|
181 bb = arg_b.matrix_value (); |
|
182 |
|
183 if (error_state) |
|
184 return retval; |
|
185 |
16
|
186 // Both matrices loaded, now let's check what kind of arithmetic: |
|
187 |
636
|
188 if (arg_a.is_complex_type () || arg_b.is_complex_type ()) |
16
|
189 { |
636
|
190 if (arg_a.is_real_type ()) |
16
|
191 caa = aa; |
636
|
192 |
|
193 if (arg_b.is_real_type ()) |
16
|
194 cbb = bb; |
|
195 |
|
196 // Compute magnitudes of elements for balancing purposes. |
|
197 // Surely there's a function I can call someplace! |
|
198 |
|
199 for (int i = 0; i < a_nr; i++) |
636
|
200 for (int j = 0; j < a_nc; j++) |
16
|
201 { |
|
202 aa.elem (i, j) = abs (caa.elem (i, j)); |
|
203 bb.elem (i, j) = abs (cbb.elem (i, j)); |
|
204 } |
|
205 } |
|
206 |
240
|
207 GEPBALANCE result (aa, bb, bal_job); |
16
|
208 |
636
|
209 if (arg_a.is_complex_type () || arg_b.is_complex_type ()) |
16
|
210 { |
|
211 caa = result.left_balancing_matrix () * caa |
|
212 * result.right_balancing_matrix (); |
|
213 |
|
214 cbb = result.left_balancing_matrix () * cbb |
|
215 * result.right_balancing_matrix (); |
|
216 |
|
217 switch (nargout) |
|
218 { |
503
|
219 case 0: |
16
|
220 case 1: |
|
221 warning ("balance: should use two output arguments"); |
516
|
222 retval(0) = caa; |
16
|
223 break; |
636
|
224 |
16
|
225 case 2: |
636
|
226 retval(1) = cbb; |
516
|
227 retval(0) = caa; |
16
|
228 break; |
636
|
229 |
16
|
230 case 4: |
636
|
231 retval(3) = cbb; |
516
|
232 retval(2) = caa; |
636
|
233 retval(1) = result.right_balancing_matrix (); |
|
234 retval(0) = result.left_balancing_matrix (); |
16
|
235 break; |
636
|
236 |
16
|
237 default: |
503
|
238 error ("balance: invalid number of output arguments"); |
16
|
239 break; |
|
240 } |
|
241 } |
|
242 else |
|
243 { |
|
244 switch (nargout) |
|
245 { |
503
|
246 case 0: |
|
247 case 1: |
|
248 warning ("balance: should use two output arguments"); |
516
|
249 retval(0) = result.balanced_a_matrix (); |
503
|
250 break; |
636
|
251 |
16
|
252 case 2: |
636
|
253 retval(1) = result.balanced_b_matrix (); |
516
|
254 retval(0) = result.balanced_a_matrix (); |
16
|
255 break; |
636
|
256 |
16
|
257 case 4: |
636
|
258 retval(3) = result.balanced_b_matrix (); |
516
|
259 retval(2) = result.balanced_a_matrix (); |
636
|
260 retval(1) = result.right_balancing_matrix (); |
|
261 retval(0) = result.left_balancing_matrix (); |
16
|
262 break; |
636
|
263 |
16
|
264 default: |
503
|
265 error ("balance: invalid number of output arguments"); |
16
|
266 break; |
|
267 } |
|
268 } |
|
269 } |
|
270 break; |
636
|
271 |
16
|
272 default: |
|
273 error ("balance requires one (AEP) or two (GEP) numeric arguments"); |
|
274 break; |
|
275 } |
636
|
276 |
16
|
277 return retval; |
|
278 } |
|
279 |
|
280 /* |
|
281 ;;; Local Variables: *** |
|
282 ;;; mode: C++ *** |
|
283 ;;; page-delimiter: "^/\\*" *** |
|
284 ;;; End: *** |
|
285 */ |