2928
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 1999, 2000, 2002, 2005, 2006, 2007 |
|
4 John W. Eaton |
2928
|
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 |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
2928
|
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 |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
2928
|
21 |
|
22 */ |
|
23 |
3911
|
24 // Author: A. S. Hodel <scotte@eng.auburn.edu> |
2928
|
25 |
|
26 #ifdef HAVE_CONFIG_H |
|
27 #include <config.h> |
|
28 #endif |
|
29 |
|
30 #include "defun-dld.h" |
|
31 #include "error.h" |
|
32 #include "gripes.h" |
|
33 #include "oct-obj.h" |
|
34 #include "utils.h" |
|
35 |
|
36 DEFUN_DLD (syl, args, nargout, |
3548
|
37 "-*- texinfo -*-\n\ |
3372
|
38 @deftypefn {Loadable Function} {@var{x} =} syl (@var{a}, @var{b}, @var{c})\n\ |
|
39 Solve the Sylvester equation\n\ |
|
40 @iftex\n\ |
|
41 @tex\n\ |
|
42 $$\n\ |
|
43 A X + X B + C = 0\n\ |
|
44 $$\n\ |
|
45 @end tex\n\ |
|
46 @end iftex\n\ |
|
47 @ifinfo\n\ |
|
48 \n\ |
|
49 @example\n\ |
|
50 A X + X B + C = 0\n\ |
|
51 @end example\n\ |
|
52 @end ifinfo\n\ |
|
53 using standard @sc{Lapack} subroutines. For example,\n\ |
|
54 \n\ |
|
55 @example\n\ |
|
56 @group\n\ |
|
57 syl ([1, 2; 3, 4], [5, 6; 7, 8], [9, 10; 11, 12])\n\ |
|
58 @result{} [ -0.50000, -0.66667; -0.66667, -0.50000 ]\n\ |
|
59 @end group\n\ |
|
60 @end example\n\ |
|
61 @end deftypefn") |
2928
|
62 { |
4233
|
63 octave_value retval; |
2928
|
64 |
|
65 int nargin = args.length (); |
|
66 |
|
67 if (nargin != 3 || nargout > 1) |
|
68 { |
5823
|
69 print_usage (); |
2928
|
70 return retval; |
|
71 } |
|
72 |
|
73 octave_value arg_a = args(0); |
|
74 octave_value arg_b = args(1); |
|
75 octave_value arg_c = args(2); |
|
76 |
5275
|
77 octave_idx_type a_nr = arg_a.rows (); |
|
78 octave_idx_type a_nc = arg_a.columns (); |
2928
|
79 |
5275
|
80 octave_idx_type b_nr = arg_b.rows (); |
|
81 octave_idx_type b_nc = arg_b.columns (); |
2928
|
82 |
5275
|
83 octave_idx_type c_nr = arg_c.rows (); |
|
84 octave_idx_type c_nc = arg_c.columns (); |
2928
|
85 |
|
86 int arg_a_is_empty = empty_arg ("syl", a_nr, a_nc); |
|
87 int arg_b_is_empty = empty_arg ("syl", b_nr, b_nc); |
|
88 int arg_c_is_empty = empty_arg ("syl", c_nr, c_nc); |
|
89 |
|
90 if (arg_a_is_empty > 0 && arg_b_is_empty > 0 && arg_c_is_empty > 0) |
4233
|
91 return octave_value (Matrix ()); |
2928
|
92 else if (arg_a_is_empty || arg_b_is_empty || arg_c_is_empty) |
|
93 return retval; |
|
94 |
|
95 // Arguments are not empty, so check for correct dimensions. |
|
96 |
|
97 if (a_nr != a_nc || b_nr != b_nc) |
|
98 { |
|
99 gripe_square_matrix_required ("syl: first two parameters:"); |
|
100 return retval; |
|
101 } |
|
102 else if (a_nr != c_nr || b_nr != c_nc) |
|
103 { |
|
104 gripe_nonconformant (); |
|
105 return retval; |
|
106 } |
|
107 |
|
108 // Dimensions look o.k., let's solve the problem. |
|
109 |
|
110 if (arg_a.is_complex_type () |
|
111 || arg_b.is_complex_type () |
|
112 || arg_c.is_complex_type ()) |
|
113 { |
|
114 // Do everything in complex arithmetic; |
|
115 |
|
116 ComplexMatrix ca = arg_a.complex_matrix_value (); |
|
117 |
|
118 if (error_state) |
|
119 return retval; |
|
120 |
|
121 ComplexMatrix cb = arg_b.complex_matrix_value (); |
|
122 |
|
123 if (error_state) |
|
124 return retval; |
|
125 |
|
126 ComplexMatrix cc = arg_c.complex_matrix_value (); |
|
127 |
|
128 if (error_state) |
|
129 return retval; |
|
130 |
|
131 retval = Sylvester (ca, cb, cc); |
|
132 } |
|
133 else |
|
134 { |
|
135 // Do everything in real arithmetic. |
|
136 |
|
137 Matrix ca = arg_a.matrix_value (); |
|
138 |
|
139 if (error_state) |
|
140 return retval; |
|
141 |
|
142 Matrix cb = arg_b.matrix_value (); |
|
143 |
|
144 if (error_state) |
|
145 return retval; |
|
146 |
|
147 Matrix cc = arg_c.matrix_value (); |
|
148 |
|
149 if (error_state) |
|
150 return retval; |
|
151 |
|
152 retval = Sylvester (ca, cb, cc); |
|
153 } |
|
154 |
|
155 return retval; |
|
156 } |
|
157 |
|
158 /* |
|
159 ;;; Local Variables: *** |
|
160 ;;; mode: C++ *** |
|
161 ;;; End: *** |
|
162 */ |