GNU Octave  6.2.0
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-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include "defun.h"
31 #include "error.h"
32 #include "errwarn.h"
33 #include "ovl.h"
34 
35 DEFUN (sylvester, args, ,
36  doc: /* -*- texinfo -*-
37 @deftypefn {} {@var{X} =} sylvester (@var{A}, @var{B}, @var{C})
38 Solve the Sylvester equation.
39 
40 The Sylvester equation is defined as:
41 @tex
42 $$
43  A X + X B = C
44 $$
45 @end tex
46 @ifnottex
47 
48 @example
49 A X + X B = C
50 @end example
51 
52 @end ifnottex
53 The solution is computed using standard @sc{lapack} subroutines.
54 
55 For example:
56 
57 @example
58 @group
59 sylvester ([1, 2; 3, 4], [5, 6; 7, 8], [9, 10; 11, 12])
60  @result{} [ 0.50000, 0.66667; 0.66667, 0.50000 ]
61 @end group
62 @end example
63 @end deftypefn */)
64 {
65  if (args.length () != 3)
66  print_usage ();
67 
69 
70  octave_value arg_a = args(0);
71  octave_value arg_b = args(1);
72  octave_value arg_c = args(2);
73 
74  octave_idx_type a_nr = arg_a.rows ();
75  octave_idx_type a_nc = arg_a.columns ();
76 
77  octave_idx_type b_nr = arg_b.rows ();
78  octave_idx_type b_nc = arg_b.columns ();
79 
80  octave_idx_type c_nr = arg_c.rows ();
81  octave_idx_type c_nc = arg_c.columns ();
82 
83  bool isfloat = arg_a.is_single_type ()
84  || arg_b.is_single_type ()
85  || arg_c.is_single_type ();
86 
87  if (arg_a.isempty () || arg_b.isempty () || arg_c.isempty ())
88  {
89  if (isfloat)
90  return ovl (FloatMatrix ());
91  else
92  return ovl (Matrix ());
93  }
94 
95  // Arguments are not empty, so check for correct dimensions.
96 
97  if (a_nr != a_nc)
98  err_square_matrix_required ("sylvester", "A");
99  if (b_nr != b_nc)
100  err_square_matrix_required ("sylvester", "B");
101  if (a_nr != c_nr || b_nr != c_nc)
103 
104  if (isfloat)
105  {
106  if (arg_a.iscomplex ()
107  || arg_b.iscomplex ()
108  || arg_c.iscomplex ())
109  {
110  // Do everything in complex arithmetic;
111 
115 
116  retval = Sylvester (ca, cb, cc);
117  }
118  else
119  {
120  // Do everything in real arithmetic.
121 
122  FloatMatrix ca = arg_a.float_matrix_value ();
123  FloatMatrix cb = arg_b.float_matrix_value ();
124  FloatMatrix cc = arg_c.float_matrix_value ();
125 
126  retval = Sylvester (ca, cb, cc);
127  }
128  }
129  else
130  {
131  if (arg_a.iscomplex ()
132  || arg_b.iscomplex ()
133  || arg_c.iscomplex ())
134  {
135  // Do everything in complex arithmetic;
136 
137  ComplexMatrix ca = arg_a.complex_matrix_value ();
138  ComplexMatrix cb = arg_b.complex_matrix_value ();
139  ComplexMatrix cc = arg_c.complex_matrix_value ();
140 
141  retval = Sylvester (ca, cb, cc);
142  }
143  else
144  {
145  // Do everything in real arithmetic.
146 
147  Matrix ca = arg_a.matrix_value ();
148  Matrix cb = arg_b.matrix_value ();
149  Matrix cc = arg_c.matrix_value ();
150 
151  retval = Sylvester (ca, cb, cc);
152  }
153  }
154 
155  return retval;
156 }
157 
158 /*
159 %!assert (sylvester ([1, 2; 3, 4], [5, 6; 7, 8], [9, 10; 11, 12]), [1/2, 2/3; 2/3, 1/2], sqrt (eps))
160 %!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")))
161 
162 ## Test input validation
163 %!error sylvester ()
164 %!error sylvester (1)
165 %!error sylvester (1,2)
166 %!error sylvester (1, 2, 3, 4)
167 %!error <A must be a square matrix> sylvester (ones (2,3), ones (2,2), ones (2,2))
168 %!error <B must be a square matrix> sylvester (ones (2,2), ones (2,3), ones (2,2))
169 %!error <nonconformant matrices> sylvester (ones (2,2), ones (2,2), ones (3,3))
170 */
ComplexMatrix Sylvester(const ComplexMatrix &a, const ComplexMatrix &b, const ComplexMatrix &c)
Definition: CMatrix.cc:3224
Definition: dMatrix.h:42
ComplexMatrix complex_matrix_value(bool frc_str_conv=false) const
Definition: ov.h:824
octave_idx_type rows(void) const
Definition: ov.h:504
octave_idx_type columns(void) const
Definition: ov.h:506
FloatMatrix float_matrix_value(bool frc_str_conv=false) const
Definition: ov.h:809
bool isempty(void) const
Definition: ov.h:557
bool is_single_type(void) const
Definition: ov.h:651
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:806
FloatComplexMatrix float_complex_matrix_value(bool frc_str_conv=false) const
Definition: ov.h:828
bool iscomplex(void) const
Definition: ov.h:694
OCTINTERP_API void print_usage(void)
Definition: defun.cc:53
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:56
void err_square_matrix_required(const char *fcn, const char *name)
Definition: errwarn.cc:122
void err_nonconformant(const char *op, octave_idx_type op1_len, octave_idx_type op2_len)
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:211