GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
sylvester.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 // Author: A. S. Hodel <scotte@eng.auburn.edu>
24 
25 #if defined (HAVE_CONFIG_H)
26 # include "config.h"
27 #endif
28 
29 #include "defun.h"
30 #include "error.h"
31 #include "errwarn.h"
32 #include "ovl.h"
33 
34 DEFUN (sylvester, args, ,
35  doc: /* -*- texinfo -*-
36 @deftypefn {} {@var{X} =} sylvester (@var{A}, @var{B}, @var{C})
37 Solve the Sylvester equation.
38 
39 The Sylvester equation is defined as:
40 @tex
41 $$
42  A X + X B = C
43 $$
44 @end tex
45 @ifnottex
46 
47 @example
48 A X + X B = C
49 @end example
50 
51 @end ifnottex
52 The solution is computed using standard @sc{lapack} subroutines.
53 
54 For example:
55 
56 @example
57 @group
58 sylvester ([1, 2; 3, 4], [5, 6; 7, 8], [9, 10; 11, 12])
59  @result{} [ 0.50000, 0.66667; 0.66667, 0.50000 ]
60 @end group
61 @end example
62 @end deftypefn */)
63 {
64  if (args.length () != 3)
65  print_usage ();
66 
68 
69  octave_value arg_a = args(0);
70  octave_value arg_b = args(1);
71  octave_value arg_c = args(2);
72 
75 
78 
81 
83  || arg_b.is_single_type ()
84  || arg_c.is_single_type ();
85 
86  if (arg_a.isempty () || arg_b.isempty () || arg_c.isempty ())
87  {
88  if (isfloat)
89  return ovl (FloatMatrix ());
90  else
91  return ovl (Matrix ());
92  }
93 
94  // Arguments are not empty, so check for correct dimensions.
95 
96  if (a_nr != a_nc)
97  err_square_matrix_required ("sylvester", "A");
98  if (b_nr != b_nc)
99  err_square_matrix_required ("sylvester", "B");
100  if (a_nr != c_nr || b_nr != c_nc)
102 
103  if (isfloat)
104  {
105  if (arg_a.iscomplex ()
106  || arg_b.iscomplex ()
107  || arg_c.iscomplex ())
108  {
109  // Do everything in complex arithmetic;
110 
114 
115  retval = Sylvester (ca, cb, cc);
116  }
117  else
118  {
119  // Do everything in real arithmetic.
120 
124 
125  retval = Sylvester (ca, cb, cc);
126  }
127  }
128  else
129  {
130  if (arg_a.iscomplex ()
131  || arg_b.iscomplex ()
132  || arg_c.iscomplex ())
133  {
134  // Do everything in complex arithmetic;
135 
139 
140  retval = Sylvester (ca, cb, cc);
141  }
142  else
143  {
144  // Do everything in real arithmetic.
145 
146  Matrix ca = arg_a.matrix_value ();
147  Matrix cb = arg_b.matrix_value ();
148  Matrix cc = arg_c.matrix_value ();
149 
150  retval = Sylvester (ca, cb, cc);
151  }
152  }
153 
154  return retval;
155 }
156 
157 /*
158 %!assert (sylvester ([1, 2; 3, 4], [5, 6; 7, 8], [9, 10; 11, 12]), [1/2, 2/3; 2/3, 1/2], sqrt (eps))
159 %!assert (sylvester (single ([1, 2; 3, 4]), single ([5, 6; 7, 8]), single ([9, 10; 11, 12])), single ([1/2, 2/3; 2/3, 1/2]), sqrt (eps ("single")))
160 
161 ## Test input validation
162 %!error sylvester ()
163 %!error sylvester (1)
164 %!error sylvester (1,2)
165 %!error sylvester (1, 2, 3, 4)
166 %!error <A must be a square matrix> sylvester (ones (2,3), ones (2,2), ones (2,2))
167 %!error <B must be a square matrix> sylvester (ones (2,2), ones (2,3), ones (2,2))
168 %!error <nonconformant matrices> sylvester (ones (2,2), ones (2,2), ones (3,3))
169 */
octave_value arg_c
Definition: sylvester.cc:71
octave_idx_type c_nr
Definition: sylvester.cc:79
bool isempty(void) const
Definition: ov.h:529
nd group nd example nd deftypefn *octave_value retval
Definition: sylvester.cc:63
OCTINTERP_API void print_usage(void)
Definition: defun.cc:54
ComplexMatrix Sylvester(const ComplexMatrix &a, const ComplexMatrix &b, const ComplexMatrix &c)
Definition: CMatrix.cc:3417
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:53
octave_idx_type b_nr
Definition: sylvester.cc:76
FloatMatrix float_matrix_value(bool frc_str_conv=false) const
Definition: ov.h:837
void err_square_matrix_required(const char *fcn, const char *name)
Definition: errwarn.cc:118
octave_idx_type a_nc
Definition: sylvester.cc:74
octave_value arg_b
Definition: sylvester.cc:70
FloatComplexMatrix float_complex_matrix_value(bool frc_str_conv=false) const
Definition: ov.h:856
octave_idx_type a_nr
Definition: sylvester.cc:73
octave_idx_type columns(void) const
Definition: ov.h:474
bool is_single_type(void) const
Definition: ov.h:651
octave_idx_type rows(void) const
Definition: ov.h:472
void err_nonconformant(const char *op, octave_idx_type op1_len, octave_idx_type op2_len)
Definition: dMatrix.h:36
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).isinteger())
octave_value arg_a
Definition: sylvester.cc:69
octave_idx_type c_nc
Definition: sylvester.cc:80
octave_idx_type b_nc
Definition: sylvester.cc:77
bool isfloat
Definition: sylvester.cc:82
bool iscomplex(void) const
Definition: ov.h:710
ComplexMatrix complex_matrix_value(bool frc_str_conv=false) const
Definition: ov.h:852
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:834