GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
hess.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 "hess.h"
31 
32 #include "defun.h"
33 #include "error.h"
34 #include "errwarn.h"
35 #include "ovl.h"
36 
37 DEFUN (hess, args, nargout,
38  doc: /* -*- texinfo -*-
39 @deftypefn {} {@var{H} =} hess (@var{A})
40 @deftypefnx {} {[@var{P}, @var{H}] =} hess (@var{A})
41 @cindex Hessenberg decomposition
42 Compute the Hessenberg decomposition of the matrix @var{A}.
43 
44 The Hessenberg decomposition is
45 @tex
46 $$
47 A = PHP^T
48 $$
49 where $P$ is a square unitary matrix ($P^TP = I$), and $H$
50 is upper Hessenberg ($H_{i,j} = 0, \forall i > j+1$).
51 @end tex
52 @ifnottex
53 @code{@var{P} * @var{H} * @var{P}' = @var{A}} where @var{P} is a square
54 unitary matrix (@code{@var{P}' * @var{P} = I}, using complex-conjugate
55 transposition) and @var{H} is upper Hessenberg
56 (@code{@var{H}(i, j) = 0 forall i > j+1)}.
57 @end ifnottex
58 
59 The Hessenberg decomposition is usually used as the first step in an
60 eigenvalue computation, but has other applications as well
61 (see @nospell{Golub, Nash, and Van Loan},
62 IEEE Transactions on Automatic Control, 1979).
63 @seealso{eig, chol, lu, qr, qz, schur, svd}
64 @end deftypefn */)
65 {
66  if (args.length () != 1)
67  print_usage ();
68 
69  octave_value arg = args(0);
70 
71  if (arg.isempty ())
72  return octave_value_list (2, Matrix ());
73 
74  if (arg.rows () != arg.columns ())
75  err_square_matrix_required ("hess", "A");
76 
78 
79  if (arg.is_single_type ())
80  {
81  if (arg.isreal ())
82  {
83  FloatMatrix tmp = arg.float_matrix_value ();
84 
86 
87  if (nargout <= 1)
88  retval = ovl (result.hess_matrix ());
89  else
90  retval = ovl (result.unitary_hess_matrix (),
91  result.hess_matrix ());
92  }
93  else if (arg.iscomplex ())
94  {
96 
98 
99  if (nargout <= 1)
100  retval = ovl (result.hess_matrix ());
101  else
102  retval = ovl (result.unitary_hess_matrix (),
103  result.hess_matrix ());
104  }
105  }
106  else
107  {
108  if (arg.isreal ())
109  {
110  Matrix tmp = arg.matrix_value ();
111 
112  octave::math::hess<Matrix> result (tmp);
113 
114  if (nargout <= 1)
115  retval = ovl (result.hess_matrix ());
116  else
117  retval = ovl (result.unitary_hess_matrix (),
118  result.hess_matrix ());
119  }
120  else if (arg.iscomplex ())
121  {
122  ComplexMatrix ctmp = arg.complex_matrix_value ();
123 
124  octave::math::hess<ComplexMatrix> result (ctmp);
125 
126  if (nargout <= 1)
127  retval = ovl (result.hess_matrix ());
128  else
129  retval = ovl (result.unitary_hess_matrix (),
130  result.hess_matrix ());
131  }
132  else
133  err_wrong_type_arg ("hess", arg);
134  }
135 
136  return retval;
137 }
138 
139 /*
140 %!test
141 %! a = [1, 2, 3; 5, 4, 6; 8, 7, 9];
142 %! [p, h] = hess (a);
143 %! assert (p * h * p', a, sqrt (eps));
144 
145 %!test
146 %! a = single ([1, 2, 3; 5, 4, 6; 8, 7, 9]);
147 %! [p, h] = hess (a);
148 %! assert (p * h * p', a, sqrt (eps ("single")));
149 
150 %!error hess ()
151 %!error hess ([1, 2; 3, 4], 2)
152 %!error <must be a square matrix> hess ([1, 2; 3, 4; 5, 6])
153 */
Definition: dMatrix.h:42
Definition: mx-defs.h:74
T hess_matrix(void) const
Definition: hess.h:76
T unitary_hess_matrix(void) const
Definition: hess.h:78
bool isreal(void) const
Definition: ov.h:691
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_wrong_type_arg(const char *name, const char *s)
Definition: errwarn.cc:166
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