37
|
1 // tc-syl.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1993 John W. Eaton |
|
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 |
|
26 #ifdef __GNUG__ |
|
27 #pragma implementation |
|
28 #endif |
|
29 |
|
30 #include "Matrix.h" |
|
31 |
|
32 #include "tree-const.h" |
|
33 #include "user-prefs.h" |
|
34 #include "gripes.h" |
|
35 #include "error.h" |
|
36 |
47
|
37 extern "C" |
|
38 { |
|
39 int F77_FCN (dtrsyl) (const char*, const char*, const int*, |
|
40 const int*, const int*, const double*, |
|
41 const int*, const double*, const int*, |
|
42 const double*, const int*, double*, int*, |
|
43 long, long); |
37
|
44 |
47
|
45 int F77_FCN (ztrsyl) (const char*, const char*, const int*, |
|
46 const int*, const int*, const Complex*, |
|
47 const int*, const Complex*, const int*, |
|
48 const Complex*, const int*, double*, int*, |
|
49 long, long); |
|
50 } |
37
|
51 |
|
52 // Local function: check for empty matrix arguments. Probably should make |
|
53 // this available elsewhere, since tc-xxx functions do this a lot. |
|
54 |
|
55 static inline int |
|
56 empty_arg (tree_constant& arg) |
|
57 { |
|
58 return (arg.rows () == 0 || arg.columns () == 0); |
|
59 } |
|
60 |
|
61 |
|
62 // Local function: construct return vector of empty matrices. Return |
|
63 // empty matrices and/or gripe when appropriate. Probably should make |
|
64 // this available elsewhere, since tc-xxx functions do this a lot. |
|
65 |
|
66 tree_constant * |
|
67 empty_tree (int nargout, char* fcn_name) |
|
68 { |
|
69 tree_constant *retval = NULL_TREE_CONST; |
|
70 |
|
71 // Got an empty argument, check if should gripe/return empty values. |
|
72 |
|
73 int flag = user_pref.propagate_empty_matrices; |
|
74 if (flag != 0) |
|
75 { |
|
76 if (flag < 0) |
|
77 gripe_empty_arg (fcn_name, 0); |
|
78 |
|
79 Matrix m; |
|
80 retval = new tree_constant [nargout+1]; |
|
81 for (int i = 0; i < nargout; i++) |
|
82 retval[i] = tree_constant (m); |
|
83 } |
|
84 else |
|
85 gripe_empty_arg (fcn_name, 1); |
|
86 |
|
87 return retval; |
|
88 } |
|
89 |
|
90 // Return value of tree_constant argument as ComplexMatrix. |
|
91 |
|
92 ComplexMatrix |
|
93 ComplexMatrixLoad (tree_constant& arg) |
|
94 { |
|
95 ComplexMatrix retval; |
|
96 |
|
97 // Set argument size for scalar (for later). |
|
98 |
|
99 switch (arg.const_type ()) |
|
100 { |
|
101 case tree_constant_rep::scalar_constant: |
|
102 retval.resize (1, 1); |
|
103 { |
|
104 double real_val = arg.double_value (); |
|
105 retval.elem (0, 0) = real_val; |
|
106 } |
|
107 break; |
|
108 case tree_constant_rep::complex_scalar_constant: |
|
109 retval.resize (1, 1); |
|
110 retval.elem (0, 0) = arg.complex_value (); |
|
111 break; |
|
112 case tree_constant_rep::matrix_constant: |
|
113 { |
|
114 Matrix tmp = arg.matrix_value (); |
|
115 retval = tmp; |
|
116 } |
|
117 break; |
|
118 case tree_constant_rep::complex_matrix_constant: |
|
119 retval = arg.complex_matrix_value (); |
|
120 break; |
|
121 default: |
|
122 panic_impossible (); |
|
123 break; |
|
124 } |
|
125 return retval; |
|
126 } |
|
127 |
|
128 #ifdef WITH_DLD |
|
129 tree_constant * |
|
130 builtin_syl_2 (tree_constant *args, int nargin, int nargout) |
|
131 { |
43
|
132 return syl (args, nargin, nargout); |
37
|
133 } |
|
134 #endif |
|
135 |
|
136 tree_constant * |
|
137 syl (tree_constant *args, int nargin, int nargout) |
|
138 { |
|
139 tree_constant *retval = NULL_TREE_CONST; |
|
140 |
|
141 tree_constant arga = args[1].make_numeric (); |
|
142 tree_constant argb = args[2].make_numeric (); |
|
143 tree_constant argc = args[3].make_numeric (); |
|
144 |
|
145 if (empty_arg (arga) || empty_arg (argb) || empty_arg (argc)) |
|
146 retval = empty_tree (nargout, "syl"); |
|
147 else |
|
148 { |
|
149 |
|
150 // Arguments are not empty, so check for correct dimensions. |
|
151 |
|
152 int a_rows = arga.rows (); |
|
153 int a_cols = arga.columns (); |
|
154 int b_rows = argb.rows (); |
|
155 int b_cols = argb.columns (); |
|
156 int c_rows = argc.rows (); |
|
157 int c_cols = argc.columns (); |
|
158 |
|
159 if ((a_rows != a_cols) || (b_rows != b_cols)) |
|
160 { |
|
161 gripe_square_matrix_required ("syl: first two parameters:"); |
|
162 return retval; |
|
163 } |
|
164 else if ((a_rows != c_rows) || (b_rows != c_cols)) |
|
165 { |
|
166 gripe_nonconformant (); |
|
167 return retval; |
|
168 } |
|
169 |
|
170 // Dimensions look o.k., let's solve the problem. |
|
171 |
|
172 retval = new tree_constant[nargout+1]; |
|
173 |
|
174 if (arga.is_complex_type () || argb.is_complex_type () |
|
175 || argc.is_complex_type ()) |
|
176 { |
|
177 |
|
178 // Do everything in complex arithmetic; |
|
179 |
|
180 ComplexMatrix ca = ComplexMatrixLoad (arga); |
|
181 ComplexMatrix cb = ComplexMatrixLoad (argb); |
|
182 ComplexMatrix cc = ComplexMatrixLoad (argc); |
|
183 |
|
184 // Compute Schur decompositions |
|
185 |
|
186 ComplexSCHUR as (ca, "U"); |
|
187 ComplexSCHUR bs (cb, "U"); |
|
188 |
|
189 // Transform cc to new coordinates. |
|
190 |
|
191 ComplexMatrix ua = as.unitary_matrix (); |
|
192 ComplexMatrix sch_a = as.schur_matrix (); |
|
193 ComplexMatrix ub = bs.unitary_matrix (); |
|
194 ComplexMatrix sch_b = bs.schur_matrix (); |
|
195 |
46
|
196 ComplexMatrix cx = ua.hermitian () * cc * ub; |
37
|
197 |
|
198 // Solve the sylvester equation, back-transform, and return the solution. |
|
199 |
|
200 double scale; |
|
201 int info; |
|
202 int one = 1; |
|
203 |
|
204 F77_FCN (ztrsyl) ("N", "N", &one, &a_rows, &b_rows, |
|
205 sch_a.fortran_vec (), &a_rows, |
|
206 sch_b.fortran_vec (), &b_rows, |
|
207 cx.fortran_vec (), &a_rows, &scale, &info, |
|
208 1L, 1L); |
|
209 |
|
210 cx = -ua * cx * ub.hermitian (); |
|
211 |
|
212 retval[0] = tree_constant (cx); |
|
213 } |
|
214 else |
|
215 { |
|
216 |
|
217 // Do everything in real arithmetic; |
|
218 |
|
219 Matrix ca = arga.to_matrix (); |
|
220 Matrix cb = argb.to_matrix (); |
|
221 Matrix cc = argc.to_matrix (); |
|
222 |
|
223 // Compute Schur decompositions. |
|
224 |
|
225 SCHUR as (ca, "U"); |
|
226 SCHUR bs (cb, "U"); |
|
227 |
|
228 // Transform cc to new coordinates. |
|
229 |
|
230 Matrix ua = as.unitary_matrix (); |
|
231 Matrix sch_a = as.schur_matrix (); |
|
232 Matrix ub = bs.unitary_matrix (); |
|
233 Matrix sch_b = bs.schur_matrix (); |
|
234 |
46
|
235 Matrix cx = ua.transpose () * cc * ub; |
37
|
236 |
|
237 // Solve the sylvester equation, back-transform, and return the solution. |
|
238 |
|
239 double scale; |
|
240 int info; |
|
241 int one = 1; |
|
242 |
|
243 F77_FCN (dtrsyl) ("N", "N", &one, &a_rows, &b_rows, |
|
244 sch_a.fortran_vec (), &a_rows, |
|
245 sch_b.fortran_vec (), &b_rows, |
|
246 cx.fortran_vec (), &a_rows, &scale, &info, |
|
247 1L, 1L); |
|
248 |
|
249 if (info) |
|
250 error ("syl: trouble in dtrsyl info = %d", info); |
|
251 |
|
252 cx = -ua*cx*ub.transpose (); |
|
253 |
|
254 retval[0] = tree_constant (cx); |
|
255 } |
|
256 } |
|
257 return retval; |
|
258 } |
|
259 |
|
260 /* |
|
261 ;;; Local Variables: *** |
|
262 ;;; mode: C++ *** |
|
263 ;;; page-delimiter: "^/\\*" *** |
|
264 ;;; End: *** |
|
265 */ |