GNU Octave  4.4.1
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) 1994-2018 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
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include "Array.h"
28 #include "CMatrix.h"
29 #include "dMatrix.h"
30 #include "fCMatrix.h"
31 #include "fMatrix.h"
32 #include "hess.h"
33 #include "lo-error.h"
34 #include "lo-lapack-proto.h"
35 
36 namespace octave
37 {
38  namespace math
39  {
40  template <>
43  {
44  F77_INT a_nr = to_f77_int (a.rows ());
45  F77_INT a_nc = to_f77_int (a.cols ());
46 
47  if (a_nr != a_nc)
48  (*current_liboctave_error_handler) ("hess: requires square matrix");
49 
50  char job = 'N';
51  char side = 'R';
52 
53  F77_INT n = a_nc;
54  F77_INT lwork = 32 * n;
55  F77_INT info;
56  F77_INT ilo;
57  F77_INT ihi;
58 
59  hess_mat = a;
60  double *h = hess_mat.fortran_vec ();
61 
63  double *pscale = scale.fortran_vec ();
64 
65  F77_XFCN (dgebal, DGEBAL, (F77_CONST_CHAR_ARG2 (&job, 1),
66  n, h, n, ilo, ihi, pscale, info
67  F77_CHAR_ARG_LEN (1)));
68 
69  Array<double> tau (dim_vector (n-1, 1));
70  double *ptau = tau.fortran_vec ();
71 
72  Array<double> work (dim_vector (lwork, 1));
73  double *pwork = work.fortran_vec ();
74 
75  F77_XFCN (dgehrd, DGEHRD, (n, ilo, ihi, h, n, ptau, pwork,
76  lwork, info));
77 
78  unitary_hess_mat = hess_mat;
79  double *z = unitary_hess_mat.fortran_vec ();
80 
81  F77_XFCN (dorghr, DORGHR, (n, ilo, ihi, z, n, ptau, pwork,
82  lwork, info));
83 
84  F77_XFCN (dgebak, DGEBAK, (F77_CONST_CHAR_ARG2 (&job, 1),
85  F77_CONST_CHAR_ARG2 (&side, 1),
86  n, ilo, ihi, pscale, n, z,
87  n, info
88  F77_CHAR_ARG_LEN (1)
89  F77_CHAR_ARG_LEN (1)));
90 
91  // If someone thinks of a more graceful way of doing
92  // this (or faster for that matter :-)), please let
93  // me know!
94 
95  if (n > 2)
96  for (F77_INT j = 0; j < a_nc; j++)
97  for (F77_INT i = j+2; i < a_nr; i++)
98  hess_mat.elem (i, j) = 0;
99 
100  return info;
101  }
102 
103  template <>
106  {
107  F77_INT a_nr = to_f77_int (a.rows ());
108  F77_INT a_nc = to_f77_int (a.cols ());
109 
110  if (a_nr != a_nc)
111  (*current_liboctave_error_handler) ("hess: requires square matrix");
112 
113  char job = 'N';
114  char side = 'R';
115 
116  F77_INT n = a_nc;
117  F77_INT lwork = 32 * n;
118  F77_INT info;
119  F77_INT ilo;
120  F77_INT ihi;
121 
122  hess_mat = a;
123  float *h = hess_mat.fortran_vec ();
124 
125  Array<float> scale (dim_vector (n, 1));
126  float *pscale = scale.fortran_vec ();
127 
128  F77_XFCN (sgebal, SGEBAL, (F77_CONST_CHAR_ARG2 (&job, 1),
129  n, h, n, ilo, ihi, pscale, info
130  F77_CHAR_ARG_LEN (1)));
131 
132  Array<float> tau (dim_vector (n-1, 1));
133  float *ptau = tau.fortran_vec ();
134 
135  Array<float> work (dim_vector (lwork, 1));
136  float *pwork = work.fortran_vec ();
137 
138  F77_XFCN (sgehrd, SGEHRD, (n, ilo, ihi, h, n, ptau, pwork,
139  lwork, info));
140 
141  unitary_hess_mat = hess_mat;
142  float *z = unitary_hess_mat.fortran_vec ();
143 
144  F77_XFCN (sorghr, SORGHR, (n, ilo, ihi, z, n, ptau, pwork,
145  lwork, info));
146 
147  F77_XFCN (sgebak, SGEBAK, (F77_CONST_CHAR_ARG2 (&job, 1),
148  F77_CONST_CHAR_ARG2 (&side, 1),
149  n, ilo, ihi, pscale, n, z,
150  n, info
151  F77_CHAR_ARG_LEN (1)
152  F77_CHAR_ARG_LEN (1)));
153 
154  // If someone thinks of a more graceful way of doing
155  // this (or faster for that matter :-)), please let
156  // me know!
157 
158  if (n > 2)
159  for (F77_INT j = 0; j < a_nc; j++)
160  for (F77_INT i = j+2; i < a_nr; i++)
161  hess_mat.elem (i, j) = 0;
162 
163  return info;
164  }
165 
166  template <>
169  {
170  F77_INT a_nr = to_f77_int (a.rows ());
171  F77_INT a_nc = to_f77_int (a.cols ());
172 
173  if (a_nr != a_nc)
174  (*current_liboctave_error_handler) ("hess: requires square matrix");
175 
176  char job = 'N';
177  char side = 'R';
178 
179  F77_INT n = a_nc;
180  F77_INT lwork = 32 * n;
181  F77_INT info;
182  F77_INT ilo;
183  F77_INT ihi;
184 
185  hess_mat = a;
186  Complex *h = hess_mat.fortran_vec ();
187 
188  Array<double> scale (dim_vector (n, 1));
189  double *pscale = scale.fortran_vec ();
190 
191  F77_XFCN (zgebal, ZGEBAL, (F77_CONST_CHAR_ARG2 (&job, 1),
192  n, F77_DBLE_CMPLX_ARG (h), n, ilo, ihi, pscale, info
193  F77_CHAR_ARG_LEN (1)));
194 
195  Array<Complex> tau (dim_vector (n-1, 1));
196  Complex *ptau = tau.fortran_vec ();
197 
198  Array<Complex> work (dim_vector (lwork, 1));
199  Complex *pwork = work.fortran_vec ();
200 
201  F77_XFCN (zgehrd, ZGEHRD, (n, ilo, ihi, F77_DBLE_CMPLX_ARG (h), n,
202  F77_DBLE_CMPLX_ARG (ptau), F77_DBLE_CMPLX_ARG (pwork), lwork, info));
203 
204  unitary_hess_mat = hess_mat;
205  Complex *z = unitary_hess_mat.fortran_vec ();
206 
207  F77_XFCN (zunghr, ZUNGHR, (n, ilo, ihi, F77_DBLE_CMPLX_ARG (z), n,
208  F77_DBLE_CMPLX_ARG (ptau), F77_DBLE_CMPLX_ARG (pwork),
209  lwork, info));
210 
211  F77_XFCN (zgebak, ZGEBAK, (F77_CONST_CHAR_ARG2 (&job, 1),
212  F77_CONST_CHAR_ARG2 (&side, 1),
213  n, ilo, ihi, pscale, n, F77_DBLE_CMPLX_ARG (z), n, info
214  F77_CHAR_ARG_LEN (1)
215  F77_CHAR_ARG_LEN (1)));
216 
217  // If someone thinks of a more graceful way of
218  // doing this (or faster for that matter :-)),
219  // please let me know!
220 
221  if (n > 2)
222  for (F77_INT j = 0; j < a_nc; j++)
223  for (F77_INT i = j+2; i < a_nr; i++)
224  hess_mat.elem (i, j) = 0;
225 
226  return info;
227  }
228 
229  template <>
232  {
233  F77_INT a_nr = to_f77_int (a.rows ());
234  F77_INT a_nc = to_f77_int (a.cols ());
235 
236  if (a_nr != a_nc)
237  {
238  (*current_liboctave_error_handler) ("hess: requires square matrix");
239  return -1;
240  }
241 
242  char job = 'N';
243  char side = 'R';
244 
245  F77_INT n = a_nc;
246  F77_INT lwork = 32 * n;
247  F77_INT info;
248  F77_INT ilo;
249  F77_INT ihi;
250 
251  hess_mat = a;
252  FloatComplex *h = hess_mat.fortran_vec ();
253 
254  Array<float> scale (dim_vector (n, 1));
255  float *pscale = scale.fortran_vec ();
256 
257  F77_XFCN (cgebal, CGEBAL, (F77_CONST_CHAR_ARG2 (&job, 1),
258  n, F77_CMPLX_ARG (h), n, ilo, ihi, pscale, info
259  F77_CHAR_ARG_LEN (1)));
260 
261  Array<FloatComplex> tau (dim_vector (n-1, 1));
262  FloatComplex *ptau = tau.fortran_vec ();
263 
264  Array<FloatComplex> work (dim_vector (lwork, 1));
265  FloatComplex *pwork = work.fortran_vec ();
266 
267  F77_XFCN (cgehrd, CGEHRD, (n, ilo, ihi, F77_CMPLX_ARG (h), n,
268  F77_CMPLX_ARG (ptau), F77_CMPLX_ARG (pwork), lwork, info));
269 
270  unitary_hess_mat = hess_mat;
271  FloatComplex *z = unitary_hess_mat.fortran_vec ();
272 
273  F77_XFCN (cunghr, CUNGHR, (n, ilo, ihi, F77_CMPLX_ARG (z), n,
274  F77_CMPLX_ARG (ptau), F77_CMPLX_ARG (pwork),
275  lwork, info));
276 
277  F77_XFCN (cgebak, CGEBAK, (F77_CONST_CHAR_ARG2 (&job, 1),
278  F77_CONST_CHAR_ARG2 (&side, 1),
279  n, ilo, ihi, pscale, n, F77_CMPLX_ARG (z), n, info
280  F77_CHAR_ARG_LEN (1)
281  F77_CHAR_ARG_LEN (1)));
282 
283  // If someone thinks of a more graceful way of
284  // doing this (or faster for that matter :-)),
285  // please let me know!
286 
287  if (n > 2)
288  for (F77_INT j = 0; j < a_nc; j++)
289  for (F77_INT i = j+2; i < a_nr; i++)
290  hess_mat.elem (i, j) = 0;
291 
292  return info;
293  }
294  }
295 }
#define F77_DBLE_CMPLX_ARG(x)
Definition: f77-fcn.h:315
const T * fortran_vec(void) const
Definition: Array.h:584
#define F77_XFCN(f, F, args)
Definition: f77-fcn.h:41
octave_idx_type a_nc
Definition: sylvester.cc:74
calling an anonymous function involves an overhead quite comparable to the overhead of an m file function Passing a handle to a built in function is because the interpreter is not involved in the internal loop For a
Definition: cellfun.cc:400
double h
Definition: graphics.cc:11808
octave_idx_type a_nr
Definition: sylvester.cc:73
Definition: dMatrix.h:36
#define F77_CMPLX_ARG(x)
Definition: f77-fcn.h:309
octave_f77_int_type F77_INT
Definition: f77-fcn.h:305
void scale(Matrix &m, double x, double y, double z)
Definition: graphics.cc:5442
for i
Definition: data.cc:5264
std::complex< float > FloatComplex
Definition: oct-cmplx.h:32
octave_idx_type init(const T &a)
std::complex< double > Complex
Definition: oct-cmplx.h:31
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87