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