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