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 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <string> |
|
28 |
|
29 #include "CmplxSCHUR.h" |
|
30 #include "dbleSCHUR.h" |
|
31 |
|
32 #include "defun-dld.h" |
|
33 #include "error.h" |
|
34 #include "gripes.h" |
|
35 #include "oct-obj.h" |
|
36 #include "utils.h" |
|
37 |
|
38 DEFUN_DLD (schur, args, nargout, |
3548
|
39 "-*- texinfo -*-\n\ |
3372
|
40 @deftypefn {Loadable Function} {@var{s} =} schur (@var{a})\n\ |
|
41 @deftypefnx {Loadable Function} {[@var{u}, @var{s}] =} schur (@var{a}, @var{opt})\n\ |
|
42 @cindex Schur decomposition\n\ |
|
43 The Schur decomposition is used to compute eigenvalues of a\n\ |
|
44 square matrix, and has applications in the solution of algebraic\n\ |
|
45 Riccati equations in control (see @code{are} and @code{dare}).\n\ |
|
46 @code{schur} always returns\n\ |
|
47 @iftex\n\ |
|
48 @tex\n\ |
|
49 $S = U^T A U$\n\ |
|
50 @end tex\n\ |
|
51 @end iftex\n\ |
|
52 @ifinfo\n\ |
|
53 @code{s = u' * a * u}\n\ |
|
54 @end ifinfo\n\ |
|
55 where\n\ |
|
56 @iftex\n\ |
|
57 @tex\n\ |
|
58 $U$\n\ |
|
59 @end tex\n\ |
|
60 @end iftex\n\ |
|
61 @ifinfo\n\ |
|
62 @code{u}\n\ |
|
63 @end ifinfo\n\ |
|
64 is a unitary matrix\n\ |
|
65 @iftex\n\ |
|
66 @tex\n\ |
|
67 ($U^T U$ is identity)\n\ |
|
68 @end tex\n\ |
|
69 @end iftex\n\ |
|
70 @ifinfo\n\ |
|
71 (@code{u'* u} is identity)\n\ |
|
72 @end ifinfo\n\ |
|
73 and\n\ |
|
74 @iftex\n\ |
|
75 @tex\n\ |
|
76 $S$\n\ |
|
77 @end tex\n\ |
|
78 @end iftex\n\ |
|
79 @ifinfo\n\ |
|
80 @code{s}\n\ |
|
81 @end ifinfo\n\ |
|
82 is upper triangular. The eigenvalues of\n\ |
|
83 @iftex\n\ |
|
84 @tex\n\ |
|
85 $A$ (and $S$)\n\ |
|
86 @end tex\n\ |
|
87 @end iftex\n\ |
|
88 @ifinfo\n\ |
|
89 @code{a} (and @code{s})\n\ |
|
90 @end ifinfo\n\ |
|
91 are the diagonal elements of\n\ |
|
92 @iftex\n\ |
|
93 @tex\n\ |
|
94 $S$\n\ |
|
95 @end tex\n\ |
|
96 @end iftex\n\ |
|
97 @ifinfo\n\ |
|
98 @code{s}\n\ |
|
99 @end ifinfo\n\ |
|
100 If the matrix\n\ |
|
101 @iftex\n\ |
|
102 @tex\n\ |
|
103 $A$\n\ |
|
104 @end tex\n\ |
|
105 @end iftex\n\ |
|
106 @ifinfo\n\ |
|
107 @code{a}\n\ |
|
108 @end ifinfo\n\ |
|
109 is real, then the real Schur decomposition is computed, in which the\n\ |
|
110 matrix\n\ |
|
111 @iftex\n\ |
|
112 @tex\n\ |
|
113 $U$\n\ |
|
114 @end tex\n\ |
|
115 @end iftex\n\ |
|
116 @ifinfo\n\ |
|
117 @code{u}\n\ |
|
118 @end ifinfo\n\ |
|
119 is orthogonal and\n\ |
|
120 @iftex\n\ |
|
121 @tex\n\ |
|
122 $S$\n\ |
|
123 @end tex\n\ |
|
124 @end iftex\n\ |
|
125 @ifinfo\n\ |
|
126 @code{s}\n\ |
|
127 @end ifinfo\n\ |
|
128 is block upper triangular\n\ |
|
129 with blocks of size at most\n\ |
|
130 @iftex\n\ |
|
131 @tex\n\ |
|
132 $2\\times 2$\n\ |
|
133 @end tex\n\ |
|
134 @end iftex\n\ |
|
135 @ifinfo\n\ |
|
136 @code{2 x 2}\n\ |
|
137 @end ifinfo\n\ |
3600
|
138 along the diagonal. The diagonal elements of\n\ |
3372
|
139 @iftex\n\ |
|
140 @tex\n\ |
|
141 $S$\n\ |
|
142 @end tex\n\ |
|
143 @end iftex\n\ |
|
144 @ifinfo\n\ |
|
145 @code{s}\n\ |
|
146 @end ifinfo\n\ |
|
147 (or the eigenvalues of the\n\ |
|
148 @iftex\n\ |
|
149 @tex\n\ |
|
150 $2\\times 2$\n\ |
|
151 @end tex\n\ |
|
152 @end iftex\n\ |
|
153 @ifinfo\n\ |
|
154 @code{2 x 2}\n\ |
|
155 @end ifinfo\n\ |
|
156 blocks, when\n\ |
|
157 appropriate) are the eigenvalues of\n\ |
|
158 @iftex\n\ |
|
159 @tex\n\ |
|
160 $A$\n\ |
|
161 @end tex\n\ |
|
162 @end iftex\n\ |
|
163 @ifinfo\n\ |
|
164 @code{a}\n\ |
|
165 @end ifinfo\n\ |
|
166 and\n\ |
|
167 @iftex\n\ |
|
168 @tex\n\ |
|
169 $S$.\n\ |
|
170 @end tex\n\ |
|
171 @end iftex\n\ |
|
172 @ifinfo\n\ |
|
173 @code{s}.\n\ |
|
174 @end ifinfo\n\ |
2928
|
175 \n\ |
3372
|
176 The eigenvalues are optionally ordered along the diagonal according to\n\ |
|
177 the value of @code{opt}. @code{opt = \"a\"} indicates that all\n\ |
|
178 eigenvalues with negative real parts should be moved to the leading\n\ |
|
179 block of\n\ |
|
180 @iftex\n\ |
|
181 @tex\n\ |
|
182 $S$\n\ |
|
183 @end tex\n\ |
|
184 @end iftex\n\ |
|
185 @ifinfo\n\ |
|
186 @code{s}\n\ |
|
187 @end ifinfo\n\ |
|
188 (used in @code{are}), @code{opt = \"d\"} indicates that all eigenvalues\n\ |
|
189 with magnitude less than one should be moved to the leading block of\n\ |
|
190 @iftex\n\ |
|
191 @tex\n\ |
|
192 $S$\n\ |
|
193 @end tex\n\ |
|
194 @end iftex\n\ |
|
195 @ifinfo\n\ |
|
196 @code{s}\n\ |
|
197 @end ifinfo\n\ |
|
198 (used in @code{dare}), and @code{opt = \"u\"}, the default, indicates that\n\ |
|
199 no ordering of eigenvalues should occur. The leading\n\ |
|
200 @iftex\n\ |
|
201 @tex\n\ |
|
202 $k$\n\ |
|
203 @end tex\n\ |
|
204 @end iftex\n\ |
|
205 @ifinfo\n\ |
|
206 @code{k}\n\ |
|
207 @end ifinfo\n\ |
|
208 columns of\n\ |
|
209 @iftex\n\ |
|
210 @tex\n\ |
|
211 $U$\n\ |
|
212 @end tex\n\ |
|
213 @end iftex\n\ |
|
214 @ifinfo\n\ |
|
215 @code{u}\n\ |
|
216 @end ifinfo\n\ |
|
217 always span the\n\ |
|
218 @iftex\n\ |
|
219 @tex\n\ |
|
220 $A$-invariant\n\ |
|
221 @end tex\n\ |
|
222 @end iftex\n\ |
|
223 @ifinfo\n\ |
|
224 @code{a}-invariant\n\ |
|
225 @end ifinfo\n\ |
|
226 subspace corresponding to the\n\ |
|
227 @iftex\n\ |
|
228 @tex\n\ |
|
229 $k$\n\ |
|
230 @end tex\n\ |
|
231 @end iftex\n\ |
|
232 @ifinfo\n\ |
|
233 @code{k}\n\ |
|
234 @end ifinfo\n\ |
|
235 leading eigenvalues of\n\ |
|
236 @iftex\n\ |
|
237 @tex\n\ |
|
238 $S$.\n\ |
|
239 @end tex\n\ |
|
240 @end iftex\n\ |
|
241 @ifinfo\n\ |
|
242 @code{s}.\n\ |
|
243 @end ifinfo\n\ |
|
244 @end deftypefn") |
2928
|
245 { |
|
246 octave_value_list retval; |
|
247 |
|
248 int nargin = args.length (); |
|
249 |
|
250 if (nargin < 1 || nargin > 2 || nargout > 2) |
|
251 { |
|
252 print_usage ("schur"); |
|
253 return retval; |
|
254 } |
|
255 |
|
256 octave_value arg = args(0); |
|
257 |
3523
|
258 std::string ord; |
2928
|
259 |
|
260 if (nargin == 2) |
|
261 { |
|
262 ord = args(1).string_value (); |
|
263 |
|
264 if (error_state) |
|
265 { |
|
266 error ("schur: expecting string as second argument"); |
|
267 return retval; |
|
268 } |
|
269 } |
|
270 |
|
271 char ord_char = ord.empty () ? 'U' : ord[0]; |
|
272 |
|
273 if (ord_char != 'U' && ord_char != 'A' && ord_char != 'D' |
|
274 && ord_char != 'u' && ord_char != 'a' && ord_char != 'd') |
|
275 { |
|
276 warning ("schur: incorrect ordered schur argument `%c'", |
|
277 ord.c_str ()); |
|
278 return retval; |
|
279 } |
|
280 |
|
281 int nr = arg.rows (); |
|
282 int nc = arg.columns (); |
|
283 |
|
284 int arg_is_empty = empty_arg ("schur", nr, nc); |
|
285 |
|
286 if (arg_is_empty < 0) |
|
287 return retval; |
|
288 else if (arg_is_empty > 0) |
|
289 return octave_value_list (2, Matrix ()); |
|
290 |
|
291 if (nr != nc) |
|
292 { |
|
293 gripe_square_matrix_required ("schur"); |
|
294 return retval; |
|
295 } |
|
296 |
|
297 if (arg.is_real_type ()) |
|
298 { |
|
299 Matrix tmp = arg.matrix_value (); |
|
300 |
|
301 if (! error_state) |
|
302 { |
|
303 SCHUR result (tmp, ord); |
|
304 |
|
305 if (nargout == 0 || nargout == 1) |
|
306 { |
|
307 retval(0) = result.schur_matrix (); |
|
308 } |
|
309 else |
|
310 { |
|
311 retval(1) = result.schur_matrix (); |
|
312 retval(0) = result.unitary_matrix (); |
|
313 } |
|
314 } |
|
315 } |
|
316 else if (arg.is_complex_type ()) |
|
317 { |
|
318 ComplexMatrix ctmp = arg.complex_matrix_value (); |
|
319 |
|
320 if (! error_state) |
|
321 { |
|
322 ComplexSCHUR result (ctmp, ord); |
|
323 |
|
324 if (nargout == 0 || nargout == 1) |
|
325 { |
|
326 retval(0) = result.schur_matrix (); |
|
327 } |
|
328 else |
|
329 { |
|
330 retval(1) = result.schur_matrix (); |
|
331 retval(0) = result.unitary_matrix (); |
|
332 } |
|
333 } |
|
334 } |
|
335 else |
|
336 { |
|
337 gripe_wrong_type_arg ("schur", arg); |
|
338 } |
|
339 |
|
340 return retval; |
|
341 } |
|
342 |
|
343 /* |
|
344 ;;; Local Variables: *** |
|
345 ;;; mode: C++ *** |
|
346 ;;; End: *** |
|
347 */ |