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