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