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