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