GNU Octave 7.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-2022 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
37OCTAVE_NAMESPACE_BEGIN
38
39DEFUN (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
44Compute the Hessenberg decomposition of the matrix @var{A}.
45
46The Hessenberg decomposition is
47@tex
48$$
49A = PHP^T
50$$
51where $P$ is a square unitary matrix ($P^TP = I$), and $H$
52is 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
56unitary matrix (@code{@var{P}' * @var{P} = I}, using complex-conjugate
57transposition) and @var{H} is upper Hessenberg
58(@code{@var{H}(i, j) = 0 forall i > j+1)}.
59@end ifnottex
60
61The Hessenberg decomposition is usually used as the first step in an
62eigenvalue computation, but has other applications as well
63(see @nospell{Golub, Nash, and Van Loan},
64IEEE 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 {
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 {
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
157OCTAVE_NAMESPACE_END
Definition: dMatrix.h:42
Definition: mx-defs.h:45
bool isreal(void) const
Definition: ov.h:783
ComplexMatrix complex_matrix_value(bool frc_str_conv=false) const
Definition: ov.h:916
octave_idx_type rows(void) const
Definition: ov.h:590
octave_idx_type columns(void) const
Definition: ov.h:592
FloatMatrix float_matrix_value(bool frc_str_conv=false) const
Definition: ov.h:901
bool isempty(void) const
Definition: ov.h:646
bool is_single_type(void) const
Definition: ov.h:743
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:898
FloatComplexMatrix float_complex_matrix_value(bool frc_str_conv=false) const
Definition: ov.h:920
bool iscomplex(void) const
Definition: ov.h:786
OCTINTERP_API 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:211