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 { |
|
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*, |
|
50 long, long); |
37
|
51 |
47
|
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*, |
|
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 int one = 1; |
|
150 |
636
|
151 F77_FCN (ztrsyl) ("N", "N", &one, &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, &info, |
37
|
155 1L, 1L); |
|
156 |
|
157 cx = -ua * cx * ub.hermitian (); |
|
158 |
636
|
159 retval = cx; |
37
|
160 } |
|
161 else |
|
162 { |
|
163 |
|
164 // Do everything in real arithmetic; |
|
165 |
636
|
166 Matrix ca = arg_a.matrix_value (); |
|
167 |
|
168 if (error_state) |
|
169 return retval; |
|
170 |
|
171 Matrix cb = arg_b.matrix_value (); |
|
172 |
|
173 if (error_state) |
|
174 return retval; |
|
175 |
|
176 Matrix cc = arg_c.matrix_value (); |
|
177 |
|
178 if (error_state) |
|
179 return retval; |
|
180 |
37
|
181 // Compute Schur decompositions. |
|
182 |
|
183 SCHUR as (ca, "U"); |
|
184 SCHUR bs (cb, "U"); |
|
185 |
|
186 // Transform cc to new coordinates. |
|
187 |
|
188 Matrix ua = as.unitary_matrix (); |
|
189 Matrix sch_a = as.schur_matrix (); |
|
190 Matrix ub = bs.unitary_matrix (); |
|
191 Matrix sch_b = bs.schur_matrix (); |
|
192 |
46
|
193 Matrix cx = ua.transpose () * cc * ub; |
37
|
194 |
|
195 // Solve the sylvester equation, back-transform, and return the solution. |
|
196 |
|
197 double scale; |
|
198 int info; |
|
199 int one = 1; |
|
200 |
636
|
201 F77_FCN (dtrsyl) ("N", "N", &one, &a_nr, &b_nr, |
|
202 sch_a.fortran_vec (), &a_nr, |
|
203 sch_b.fortran_vec (), &b_nr, |
|
204 cx.fortran_vec (), &a_nr, &scale, &info, |
37
|
205 1L, 1L); |
|
206 |
|
207 if (info) |
|
208 error ("syl: trouble in dtrsyl info = %d", info); |
|
209 |
|
210 cx = -ua*cx*ub.transpose (); |
|
211 |
636
|
212 retval = cx; |
37
|
213 } |
636
|
214 |
37
|
215 return retval; |
|
216 } |
|
217 |
|
218 /* |
|
219 ;;; Local Variables: *** |
|
220 ;;; mode: C++ *** |
|
221 ;;; page-delimiter: "^/\\*" *** |
|
222 ;;; End: *** |
|
223 */ |