519
|
1 // f-syl.cc -*- C++ -*- |
37
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1993, 1994, 1995 John W. Eaton |
37
|
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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
37
|
21 |
|
22 */ |
|
23 |
|
24 // Written by A. S. Hodel <scotte@eng.auburn.edu> |
|
25 |
240
|
26 #ifdef HAVE_CONFIG_H |
1192
|
27 #include <config.h> |
37
|
28 #endif |
|
29 |
1352
|
30 #include "CMatrix.h" |
|
31 #include "CmplxSCHUR.h" |
453
|
32 #include "dMatrix.h" |
|
33 #include "dbleSCHUR.h" |
234
|
34 #include "f77-uscore.h" |
37
|
35 |
1352
|
36 #include "defun-dld.h" |
|
37 #include "error.h" |
|
38 #include "gripes.h" |
|
39 #include "help.h" |
37
|
40 #include "tree-const.h" |
|
41 #include "user-prefs.h" |
636
|
42 #include "utils.h" |
37
|
43 |
47
|
44 extern "C" |
|
45 { |
1255
|
46 int F77_FCN (dtrsyl, DTRSYL) (const char*, const char*, const int&, |
|
47 const int&, const int&, const double*, |
|
48 const int&, const double*, const int&, |
|
49 const double*, const int&, double&, |
|
50 int&, long, long); |
37
|
51 |
1255
|
52 int F77_FCN (ztrsyl, ZTRSYL) (const char*, const char*, const int&, |
|
53 const int&, const int&, |
|
54 const Complex*, const int&, |
|
55 const Complex*, const int&, |
|
56 const Complex*, const int&, double&, |
|
57 int&, long, long); |
47
|
58 } |
37
|
59 |
701
|
60 DEFUN_DLD_BUILTIN ("syl", Fsyl, Ssyl, 4, 1, |
519
|
61 "X = syl (A, B, C): solve the Sylvester equation A X + X B + C = 0") |
37
|
62 { |
497
|
63 Octave_object retval; |
37
|
64 |
712
|
65 int nargin = args.length (); |
|
66 |
|
67 if (nargin != 3 || nargout > 1) |
519
|
68 { |
|
69 print_usage ("syl"); |
|
70 return retval; |
|
71 } |
|
72 |
712
|
73 tree_constant arg_a = args(0); |
|
74 tree_constant arg_b = args(1); |
|
75 tree_constant arg_c = args(2); |
636
|
76 |
|
77 int a_nr = arg_a.rows (); |
|
78 int a_nc = arg_a.columns (); |
37
|
79 |
636
|
80 int b_nr = arg_b.rows (); |
|
81 int b_nc = arg_b.columns (); |
|
82 |
|
83 int c_nr = arg_c.rows (); |
|
84 int c_nc = arg_c.columns (); |
718
|
85 |
|
86 int arg_a_is_empty = empty_arg ("syl", a_nr, a_nc); |
|
87 int arg_b_is_empty = empty_arg ("syl", b_nr, b_nc); |
|
88 int arg_c_is_empty = empty_arg ("syl", c_nr, c_nc); |
|
89 |
|
90 if (arg_a_is_empty > 0 && arg_b_is_empty > 0 && arg_c_is_empty > 0) |
|
91 return Matrix (); |
|
92 else if (arg_a_is_empty || arg_b_is_empty || arg_c_is_empty) |
636
|
93 return retval; |
37
|
94 |
1357
|
95 // Arguments are not empty, so check for correct dimensions. |
37
|
96 |
636
|
97 if (a_nr != a_nc || b_nr != b_nc) |
|
98 { |
|
99 gripe_square_matrix_required ("syl: first two parameters:"); |
|
100 return retval; |
|
101 } |
|
102 else if (a_nr != c_nr || b_nr != c_nc) |
|
103 { |
|
104 gripe_nonconformant (); |
|
105 return retval; |
|
106 } |
37
|
107 |
1357
|
108 // Dimensions look o.k., let's solve the problem. |
37
|
109 |
636
|
110 if (arg_a.is_complex_type () |
|
111 || arg_b.is_complex_type () |
|
112 || arg_c.is_complex_type ()) |
37
|
113 { |
1357
|
114 // Do everything in complex arithmetic; |
37
|
115 |
636
|
116 ComplexMatrix ca = arg_a.complex_matrix_value (); |
|
117 |
|
118 if (error_state) |
|
119 return retval; |
|
120 |
|
121 ComplexMatrix cb = arg_b.complex_matrix_value (); |
|
122 |
|
123 if (error_state) |
|
124 return retval; |
|
125 |
|
126 ComplexMatrix cc = arg_c.complex_matrix_value (); |
|
127 |
|
128 if (error_state) |
|
129 return retval; |
|
130 |
1357
|
131 // Compute Schur decompositions |
37
|
132 |
|
133 ComplexSCHUR as (ca, "U"); |
|
134 ComplexSCHUR bs (cb, "U"); |
|
135 |
1357
|
136 // Transform cc to new coordinates. |
37
|
137 |
|
138 ComplexMatrix ua = as.unitary_matrix (); |
|
139 ComplexMatrix sch_a = as.schur_matrix (); |
|
140 ComplexMatrix ub = bs.unitary_matrix (); |
|
141 ComplexMatrix sch_b = bs.schur_matrix (); |
|
142 |
46
|
143 ComplexMatrix cx = ua.hermitian () * cc * ub; |
37
|
144 |
1357
|
145 // Solve the sylvester equation, back-transform, and return |
|
146 // the solution. |
37
|
147 |
|
148 double scale; |
|
149 int info; |
|
150 |
1255
|
151 F77_FCN (ztrsyl, ZTRSYL) ("N", "N", 1, a_nr, b_nr, |
|
152 sch_a.fortran_vec (), a_nr, |
|
153 sch_b.fortran_vec (), b_nr, |
|
154 cx.fortran_vec (), a_nr, scale, |
|
155 info, 1L, 1L); |
37
|
156 |
|
157 cx = -ua * cx * ub.hermitian (); |
|
158 |
636
|
159 retval = cx; |
37
|
160 } |
|
161 else |
|
162 { |
1357
|
163 // Do everything in real arithmetic. |
37
|
164 |
636
|
165 Matrix ca = arg_a.matrix_value (); |
|
166 |
|
167 if (error_state) |
|
168 return retval; |
|
169 |
|
170 Matrix cb = arg_b.matrix_value (); |
|
171 |
|
172 if (error_state) |
|
173 return retval; |
|
174 |
|
175 Matrix cc = arg_c.matrix_value (); |
|
176 |
|
177 if (error_state) |
|
178 return retval; |
|
179 |
1357
|
180 // Compute Schur decompositions. |
37
|
181 |
|
182 SCHUR as (ca, "U"); |
|
183 SCHUR bs (cb, "U"); |
|
184 |
1357
|
185 // Transform cc to new coordinates. |
37
|
186 |
|
187 Matrix ua = as.unitary_matrix (); |
|
188 Matrix sch_a = as.schur_matrix (); |
|
189 Matrix ub = bs.unitary_matrix (); |
|
190 Matrix sch_b = bs.schur_matrix (); |
|
191 |
46
|
192 Matrix cx = ua.transpose () * cc * ub; |
37
|
193 |
1357
|
194 // Solve the sylvester equation, back-transform, and return |
|
195 // the solution. |
37
|
196 |
|
197 double scale; |
|
198 int info; |
|
199 |
1255
|
200 F77_FCN (dtrsyl, DTRSYL) ("N", "N", 1, a_nr, b_nr, |
|
201 sch_a.fortran_vec (), a_nr, |
|
202 sch_b.fortran_vec (), b_nr, |
|
203 cx.fortran_vec (), a_nr, scale, |
|
204 info, 1L, 1L); |
37
|
205 |
|
206 if (info) |
|
207 error ("syl: trouble in dtrsyl info = %d", info); |
|
208 |
|
209 cx = -ua*cx*ub.transpose (); |
|
210 |
636
|
211 retval = cx; |
37
|
212 } |
636
|
213 |
37
|
214 return retval; |
|
215 } |
|
216 |
|
217 /* |
|
218 ;;; Local Variables: *** |
|
219 ;;; mode: C++ *** |
|
220 ;;; page-delimiter: "^/\\*" *** |
|
221 ;;; End: *** |
|
222 */ |