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