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 |
|
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 |
1192
|
27 #include <config.h> |
37
|
28 #endif |
|
29 |
453
|
30 #include "dMatrix.h" |
|
31 #include "CMatrix.h" |
|
32 #include "dbleSCHUR.h" |
|
33 #include "CmplxSCHUR.h" |
234
|
34 #include "f77-uscore.h" |
37
|
35 |
|
36 #include "tree-const.h" |
|
37 #include "user-prefs.h" |
|
38 #include "gripes.h" |
|
39 #include "error.h" |
636
|
40 #include "utils.h" |
544
|
41 #include "help.h" |
519
|
42 #include "defun-dld.h" |
37
|
43 |
47
|
44 extern "C" |
|
45 { |
1245
|
46 int F77_FCN (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&, int&, |
47
|
50 long, long); |
37
|
51 |
1245
|
52 int F77_FCN (ztrsyl) (const char*, const char*, const int&, |
|
53 const int&, const int&, const Complex*, |
|
54 const int&, const Complex*, const int&, |
|
55 const Complex*, const int&, double&, int&, |
47
|
56 long, long); |
|
57 } |
37
|
58 |
701
|
59 DEFUN_DLD_BUILTIN ("syl", Fsyl, Ssyl, 4, 1, |
519
|
60 "X = syl (A, B, C): solve the Sylvester equation A X + X B + C = 0") |
37
|
61 { |
497
|
62 Octave_object retval; |
37
|
63 |
712
|
64 int nargin = args.length (); |
|
65 |
|
66 if (nargin != 3 || nargout > 1) |
519
|
67 { |
|
68 print_usage ("syl"); |
|
69 return retval; |
|
70 } |
|
71 |
712
|
72 tree_constant arg_a = args(0); |
|
73 tree_constant arg_b = args(1); |
|
74 tree_constant arg_c = args(2); |
636
|
75 |
|
76 int a_nr = arg_a.rows (); |
|
77 int a_nc = arg_a.columns (); |
37
|
78 |
636
|
79 int b_nr = arg_b.rows (); |
|
80 int b_nc = arg_b.columns (); |
|
81 |
|
82 int c_nr = arg_c.rows (); |
|
83 int c_nc = arg_c.columns (); |
718
|
84 |
|
85 int arg_a_is_empty = empty_arg ("syl", a_nr, a_nc); |
|
86 int arg_b_is_empty = empty_arg ("syl", b_nr, b_nc); |
|
87 int arg_c_is_empty = empty_arg ("syl", c_nr, c_nc); |
|
88 |
|
89 if (arg_a_is_empty > 0 && arg_b_is_empty > 0 && arg_c_is_empty > 0) |
|
90 return Matrix (); |
|
91 else if (arg_a_is_empty || arg_b_is_empty || arg_c_is_empty) |
636
|
92 return retval; |
37
|
93 |
|
94 // Arguments are not empty, so check for correct dimensions. |
|
95 |
636
|
96 if (a_nr != a_nc || b_nr != b_nc) |
|
97 { |
|
98 gripe_square_matrix_required ("syl: first two parameters:"); |
|
99 return retval; |
|
100 } |
|
101 else if (a_nr != c_nr || b_nr != c_nc) |
|
102 { |
|
103 gripe_nonconformant (); |
|
104 return retval; |
|
105 } |
37
|
106 |
|
107 // Dimensions look o.k., let's solve the problem. |
|
108 |
636
|
109 if (arg_a.is_complex_type () |
|
110 || arg_b.is_complex_type () |
|
111 || arg_c.is_complex_type ()) |
37
|
112 { |
|
113 |
|
114 // Do everything in complex arithmetic; |
|
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 |
37
|
131 // Compute Schur decompositions |
|
132 |
|
133 ComplexSCHUR as (ca, "U"); |
|
134 ComplexSCHUR bs (cb, "U"); |
|
135 |
|
136 // Transform cc to new coordinates. |
|
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 |
|
145 // Solve the sylvester equation, back-transform, and return the solution. |
|
146 |
|
147 double scale; |
|
148 int info; |
|
149 |
1245
|
150 F77_FCN (ztrsyl) ("N", "N", 1, a_nr, b_nr, |
|
151 sch_a.fortran_vec (), a_nr, |
|
152 sch_b.fortran_vec (), b_nr, |
|
153 cx.fortran_vec (), a_nr, scale, info, |
37
|
154 1L, 1L); |
|
155 |
|
156 cx = -ua * cx * ub.hermitian (); |
|
157 |
636
|
158 retval = cx; |
37
|
159 } |
|
160 else |
|
161 { |
|
162 |
|
163 // Do everything in real arithmetic; |
|
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 |
37
|
180 // Compute Schur decompositions. |
|
181 |
|
182 SCHUR as (ca, "U"); |
|
183 SCHUR bs (cb, "U"); |
|
184 |
|
185 // Transform cc to new coordinates. |
|
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 |
|
194 // Solve the sylvester equation, back-transform, and return the solution. |
|
195 |
|
196 double scale; |
|
197 int info; |
|
198 |
1245
|
199 F77_FCN (dtrsyl) ("N", "N", 1, a_nr, b_nr, |
|
200 sch_a.fortran_vec (), a_nr, |
|
201 sch_b.fortran_vec (), b_nr, |
|
202 cx.fortran_vec (), a_nr, scale, info, |
37
|
203 1L, 1L); |
|
204 |
|
205 if (info) |
|
206 error ("syl: trouble in dtrsyl info = %d", info); |
|
207 |
|
208 cx = -ua*cx*ub.transpose (); |
|
209 |
636
|
210 retval = cx; |
37
|
211 } |
636
|
212 |
37
|
213 return retval; |
|
214 } |
|
215 |
|
216 /* |
|
217 ;;; Local Variables: *** |
|
218 ;;; mode: C++ *** |
|
219 ;;; page-delimiter: "^/\\*" *** |
|
220 ;;; End: *** |
|
221 */ |