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