GNU Octave  4.2.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-2017 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
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any 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, see
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 @tex
39 \$\$
40  A X + X B = C
41 \$\$
42 @end tex
43 @ifnottex
44
45 @example
46 A X + X B = C
47 @end example
48
49 @end ifnottex
50 using standard @sc{lapack} subroutines.
51
52 For example:
53
54 @example
55 @group
56 sylvester ([1, 2; 3, 4], [5, 6; 7, 8], [9, 10; 11, 12])
57  @result{} [ 0.50000, 0.66667; 0.66667, 0.50000 ]
58 @end group
59 @end example
60 @end deftypefn */)
61 {
62  if (args.length () != 3)
63  print_usage ();
64
66
70
73
76
79
80  bool isfloat = arg_a.is_single_type ()
81  || arg_b.is_single_type ()
82  || arg_c.is_single_type ();
83
84  if (arg_a.is_empty () || arg_b.is_empty () || arg_c.is_empty ())
85  {
86  if (isfloat)
87  return ovl (FloatMatrix ());
88  else
89  return ovl (Matrix ());
90  }
91
92  // Arguments are not empty, so check for correct dimensions.
93
94  if (a_nr != a_nc)
95  err_square_matrix_required ("sylvester", "A");
96  if (b_nr != b_nc)
97  err_square_matrix_required ("sylvester", "B");
98  if (a_nr != c_nr || b_nr != c_nc)
100
101  if (isfloat)
102  {
103  if (arg_a.is_complex_type ()
104  || arg_b.is_complex_type ()
105  || arg_c.is_complex_type ())
106  {
107  // Do everything in complex arithmetic;
108
112
113  retval = Sylvester (ca, cb, cc);
114  }
115  else
116  {
117  // Do everything in real arithmetic.
118
119  FloatMatrix ca = arg_a.float_matrix_value ();
120  FloatMatrix cb = arg_b.float_matrix_value ();
121  FloatMatrix cc = arg_c.float_matrix_value ();
122
123  retval = Sylvester (ca, cb, cc);
124  }
125  }
126  else
127  {
128  if (arg_a.is_complex_type ()
129  || arg_b.is_complex_type ()
130  || arg_c.is_complex_type ())
131  {
132  // Do everything in complex arithmetic;
133
134  ComplexMatrix ca = arg_a.complex_matrix_value ();
135  ComplexMatrix cb = arg_b.complex_matrix_value ();
136  ComplexMatrix cc = arg_c.complex_matrix_value ();
137
138  retval = Sylvester (ca, cb, cc);
139  }
140  else
141  {
142  // Do everything in real arithmetic.
143
144  Matrix ca = arg_a.matrix_value ();
145  Matrix cb = arg_b.matrix_value ();
146  Matrix cc = arg_c.matrix_value ();
147
148  retval = Sylvester (ca, cb, cc);
149  }
150  }
151
152  return retval;
153 }
154
155 /*
156 %!assert (sylvester ([1, 2; 3, 4], [5, 6; 7, 8], [9, 10; 11, 12]), [1/2, 2/3; 2/3, 1/2], sqrt (eps))
157 %!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")))
158
159 %% Test input validation
160 %!error sylvester ()
161 %!error sylvester (1)
162 %!error sylvester (1,2)
163 %!error sylvester (1, 2, 3, 4)
164 %!error <A must be a square matrix> sylvester (ones (2,3), ones (2,2), ones (2,2))
165 %!error <B must be a square matrix> sylvester (ones (2,2), ones (2,3), ones (2,2))
166 %!error <nonconformant matrices> sylvester (ones (2,2), ones (2,2), ones (3,3))
167 */
octave_value arg_c
Definition: sylvester.cc:69
octave_idx_type c_nr
Definition: sylvester.cc:77
octave_idx_type rows(void) const
Definition: ov.h:489
FloatComplexMatrix float_complex_matrix_value(bool frc_str_conv=false) const
Definition: ov.h:809
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).is_integer_type())
nd group nd example nd deftypefn *octave_value retval
Definition: sylvester.cc:61
OCTINTERP_API void print_usage(void)
Definition: defun.cc:52
ComplexMatrix Sylvester(const ComplexMatrix &a, const ComplexMatrix &b, const ComplexMatrix &c)
Definition: CMatrix.cc:3298
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:46
octave_idx_type b_nr
Definition: sylvester.cc:74
void err_square_matrix_required(const char *fcn, const char *name)
Definition: errwarn.cc:112
octave_idx_type a_nc
Definition: sylvester.cc:72
octave_value arg_b
Definition: sylvester.cc:68
JNIEnv void * args
Definition: ov-java.cc:67
octave_idx_type columns(void) const
Definition: ov.h:491
octave_idx_type a_nr
Definition: sylvester.cc:71
bool is_complex_type(void) const
Definition: ov.h:670
void err_nonconformant(const char *op, octave_idx_type op1_len, octave_idx_type op2_len)
Definition: dMatrix.h:37
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:787
bool is_empty(void) const
Definition: ov.h:542
octave_value arg_a
Definition: sylvester.cc:67
octave_idx_type c_nc
Definition: sylvester.cc:78
ComplexMatrix complex_matrix_value(bool frc_str_conv=false) const
Definition: ov.h:805
octave_idx_type b_nc
Definition: sylvester.cc:75
FloatMatrix float_matrix_value(bool frc_str_conv=false) const
Definition: ov.h:790
bool isfloat
Definition: sylvester.cc:80
bool is_single_type(void) const
Definition: ov.h:627