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 { |
|
45 int F77_FCN( dggbal, DGGBAL) (const char* JOB, const int& N, |
|
46 double* A, const int& LDA, double* B, const int& LDB, |
|
47 int& ILO, int & IHI, double* LSCALE, |
|
48 double* RSCALE, double* WORK, int& INFO, long ); |
|
49 |
|
50 int F77_FCN( dggbak, DGGBAK) (const char* JOB, const char* SIDE, |
|
51 const int& N, const int& ILO, const int& IHI, |
|
52 double* LSCALE, double* RSCALE, int& M, |
|
53 double* V, const int& LDV, int& INFO, long, long); |
|
54 |
|
55 int F77_FCN( zggbal, ZGGBAL) ( const char* JOB, const int& N, |
|
56 Complex* A, const int& LDA, Complex* B, const int& LDB, |
|
57 int& ILO, int & IHI, double* LSCALE, |
|
58 double* RSCALE, double* WORK, int& INFO, long ); |
|
59 } |
|
60 |
2928
|
61 DEFUN_DLD (balance, args, nargout, |
|
62 "AA = balance (A [, OPT]) or [[DD,] AA] = balance (A [, OPT])\n\ |
|
63 \n\ |
|
64 generalized eigenvalue problem:\n\ |
|
65 \n\ |
|
66 [cc, dd, aa, bb] = balance (a, b [, opt])\n\ |
|
67 \n\ |
|
68 where OPT is an optional single character argument as follows: \n\ |
|
69 \n\ |
|
70 N: no balancing; arguments copied, transformation(s) set to identity\n\ |
|
71 P: permute argument(s) to isolate eigenvalues where possible\n\ |
|
72 S: scale to improve accuracy of computed eigenvalues\n\ |
|
73 B: (default) permute and scale, in that order. Rows/columns\n\ |
|
74 of a (and b) that are isolated by permutation are not scaled\n\ |
|
75 \n\ |
3181
|
76 [DD, AA] = balance (A, OPT) returns aa = dd*a*dd,\n\ |
2928
|
77 \n\ |
|
78 [CC, DD, AA, BB] = balance (A, B, OPT) returns AA (BB) = CC*A*DD (CC*B*DD)") |
|
79 { |
3181
|
80 |
2928
|
81 octave_value_list retval; |
|
82 |
|
83 int nargin = args.length (); |
|
84 |
|
85 if (nargin < 1 || nargin > 3 || nargout < 0 || nargout > 4) |
|
86 { |
|
87 print_usage ("balance"); |
|
88 return retval; |
|
89 } |
|
90 |
3181
|
91 // determine if it's AEP or GEP |
|
92 int AEPcase = (nargin == 1 ? 1 : args(1).is_string() ); |
2928
|
93 string bal_job; |
|
94 |
3181
|
95 // problem dimension |
|
96 int nn = args(0).rows (); |
2928
|
97 |
3181
|
98 int arg_is_empty = empty_arg ("balance", nn, args(0).columns()); |
2928
|
99 |
|
100 if (arg_is_empty < 0) |
|
101 return retval; |
|
102 if (arg_is_empty > 0) |
|
103 return octave_value_list (2, Matrix ()); |
|
104 |
3181
|
105 if (nn != args(0).columns()) |
|
106 { |
|
107 gripe_square_matrix_required ("balance"); |
|
108 return retval; |
|
109 } |
2928
|
110 |
|
111 // Extract argument 1 parameter for both AEP and GEP. |
|
112 Matrix aa; |
|
113 ComplexMatrix caa; |
3181
|
114 if (args(0).is_complex_type ()) caa = args(0).complex_matrix_value (); |
|
115 else aa = args(0).matrix_value (); |
|
116 if (error_state) return retval; |
2928
|
117 |
|
118 // Treat AEP/GEP cases. |
3181
|
119 if(AEPcase) |
|
120 { |
|
121 // Algebraic eigenvalue problem. |
|
122 if(nargin == 1) |
|
123 bal_job = "B"; |
|
124 else if(args(1).is_string()) |
|
125 bal_job = args(1).string_value(); |
|
126 // the next line should never execute, but better safe than sorry. |
|
127 else error("balance: AEP argument 2 must be a string"); |
2928
|
128 |
3181
|
129 // balance the AEP |
|
130 if (args(0).is_complex_type ()) |
|
131 { |
|
132 ComplexAEPBALANCE result (caa, bal_job); |
|
133 |
|
134 if (nargout == 0 || nargout == 1) |
|
135 retval(0) = result.balanced_matrix (); |
|
136 else |
|
137 { |
|
138 retval(1) = result.balanced_matrix (); |
|
139 retval(0) = result.balancing_matrix (); |
|
140 } |
|
141 } |
|
142 else |
2928
|
143 { |
3181
|
144 AEPBALANCE result (aa, bal_job); |
2928
|
145 |
3181
|
146 if (nargout == 0 || nargout == 1) |
|
147 retval(0) = result.balanced_matrix (); |
|
148 else |
|
149 { |
|
150 retval(1) = result.balanced_matrix (); |
|
151 retval(0) = result.balancing_matrix (); |
|
152 } |
|
153 } |
|
154 } |
|
155 // |
|
156 // end of AEP case, now do GEP case |
|
157 else |
|
158 { |
|
159 // Generalized eigenvalue problem. |
|
160 if(nargin == 2) |
|
161 bal_job = "B"; |
|
162 else if(args(2).is_string()) |
|
163 bal_job = args(2).string_value(); |
|
164 else error("balance: GEP argument 3 must be a string"); |
|
165 |
|
166 if( (nn != args(1).columns()) || (nn != args(1).rows() ) ) |
|
167 { |
|
168 gripe_nonconformant (); |
|
169 return retval; |
|
170 } |
|
171 Matrix bb; |
|
172 ComplexMatrix cbb; |
|
173 if (args(1).is_complex_type ()) cbb = args(1).complex_matrix_value (); |
|
174 else bb = args(1).matrix_value (); |
|
175 if (error_state) return retval; |
2928
|
176 |
3181
|
177 // |
|
178 // Both matrices loaded, now let's check what kind of arithmetic: |
|
179 // first, declare variables used in both the real and complex case |
|
180 int ilo, ihi, info; |
|
181 RowVector lscale(nn), rscale(nn), work(6*nn); |
|
182 char job = bal_job[0]; |
|
183 static int complex_case = (args(0).is_complex_type() |
|
184 || args(1).is_complex_type()); |
|
185 |
|
186 // now balance |
|
187 if (complex_case) |
|
188 { |
|
189 if (args(0).is_real_type ()) caa = aa; |
|
190 if (args(1).is_real_type ()) cbb = bb; |
|
191 |
|
192 F77_XFCN( zggbal, ZGGBAL, ( &job, nn, caa.fortran_vec(), nn, |
|
193 cbb.fortran_vec(), nn, ilo, ihi, lscale.fortran_vec(), |
|
194 rscale.fortran_vec(), work.fortran_vec(), info, 1L)); |
|
195 } |
|
196 else // real matrices case |
|
197 { |
|
198 F77_XFCN( dggbal, DGGBAL, (&job, nn, aa.fortran_vec(), |
|
199 nn, bb.fortran_vec() , nn, ilo, ihi, lscale.fortran_vec(), |
|
200 rscale.fortran_vec(), work.fortran_vec(), info , 1L)); |
|
201 |
|
202 if(f77_exception_encountered) |
|
203 (*current_liboctave_error_handler) |
|
204 ("unrecoverable error in balance GEP"); |
|
205 } |
|
206 |
|
207 // Since we just want the balancing matrices, we can use dggbal |
|
208 // for both the real and complex cases; |
|
209 Matrix Pl(nn,nn), Pr(nn,nn); |
|
210 for(int ii=0; ii < nn ; ii++) |
|
211 for( int jj=0; jj < nn ; jj++) |
|
212 Pl(ii,jj) = Pr(ii,jj) = (ii == jj ? 1.0 : 0.0); |
|
213 |
|
214 // left first |
|
215 F77_XFCN( dggbak, DGGBAK, (&job, "L", |
|
216 nn, ilo, ihi, lscale.fortran_vec(), |
|
217 rscale.fortran_vec(), nn, Pl.fortran_vec(), |
|
218 nn, info, 1L, 1L)); |
|
219 |
|
220 if(f77_exception_encountered) |
|
221 (*current_liboctave_error_handler) |
|
222 ("unrecoverable error in balance GEP(L)"); |
|
223 |
|
224 // then right |
|
225 F77_XFCN(dggbak, DGGBAK, (&job, "R", |
|
226 nn, ilo, ihi, lscale.fortran_vec(), |
|
227 rscale.fortran_vec(), nn, Pr.fortran_vec(), |
|
228 nn, info, 1L, 1L)); |
|
229 if(f77_exception_encountered) |
|
230 (*current_liboctave_error_handler) |
|
231 ("unrecoverable error in balance GEP(R)"); |
|
232 |
|
233 switch (nargout) |
|
234 { |
|
235 case 0: |
|
236 case 1: |
|
237 warning ("balance: used GEP, should have two output arguments"); |
|
238 if(complex_case) |
|
239 retval(0) = caa; |
2928
|
240 else |
3181
|
241 retval(0) = aa; |
2928
|
242 break; |
|
243 |
|
244 case 2: |
3181
|
245 if(complex_case) |
2928
|
246 { |
3181
|
247 retval(1) = cbb; |
|
248 retval(0) = caa; |
|
249 } |
|
250 else |
|
251 { |
|
252 retval(1) = bb; |
|
253 retval(0) = aa; |
2928
|
254 } |
|
255 break; |
|
256 |
3181
|
257 case 4: |
|
258 if(complex_case) |
|
259 { |
|
260 retval(3) = cbb; |
|
261 retval(2) = caa; |
|
262 } |
|
263 else |
|
264 { |
|
265 retval(3) = bb; |
|
266 retval(2) = aa; |
|
267 } |
|
268 retval(1) = Pr; |
|
269 retval(0) = Pl.transpose(); // so that aa_bal = cc*aa*dd, etc. |
|
270 break; |
|
271 |
2928
|
272 default: |
3181
|
273 error ("balance: invalid number of output arguments"); |
2928
|
274 break; |
|
275 } |
3181
|
276 } |
2928
|
277 return retval; |
|
278 } |
|
279 |
|
280 /* |
|
281 ;;; Local Variables: *** |
|
282 ;;; mode: C++ *** |
|
283 ;;; End: *** |
|
284 */ |