GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
schur.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1994-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 "Array.h"
31 #include "CMatrix.h"
32 #include "dMatrix.h"
33 #include "fCMatrix.h"
34 #include "fMatrix.h"
35 #include "lo-error.h"
36 #include "lo-lapack-proto.h"
37 #include "oct-locbuf.h"
38 #include "schur.h"
39 
40 namespace octave
41 {
42  namespace math
43  {
44  // For real types.
45 
46  static F77_INT
47  select_ana (const double& a, const double&)
48  {
49  return (a < 0.0);
50  }
51 
52  static F77_INT
53  select_dig (const double& a, const double& b)
54  {
55  return (hypot (a, b) < 1.0);
56  }
57 
58  static F77_INT
59  select_ana (const float& a, const float&)
60  {
61  return (a < 0.0);
62  }
63 
64  static F77_INT
65  select_dig (const float& a, const float& b)
66  {
67  return (hypot (a, b) < 1.0);
68  }
69 
70  // For complex types.
71 
72  static F77_INT
73  select_ana (const F77_DBLE_CMPLX& a_arg)
74  {
75  const Complex a = reinterpret_cast<const Complex&> (a_arg);
76  return a.real () < 0.0;
77  }
78 
79  static F77_INT
80  select_dig (const F77_DBLE_CMPLX& a_arg)
81  {
82  const Complex& a = reinterpret_cast<const Complex&> (a_arg);
83  return (abs (a) < 1.0);
84  }
85 
86  static F77_INT
87  select_ana (const F77_CMPLX& a_arg)
88  {
89  const FloatComplex& a = reinterpret_cast<const FloatComplex&> (a_arg);
90  return a.real () < 0.0;
91  }
92 
93  static F77_INT
94  select_dig (const F77_CMPLX& a_arg)
95  {
96  const FloatComplex& a = reinterpret_cast<const FloatComplex&> (a_arg);
97  return (abs (a) < 1.0);
98  }
99 
100  template <>
101  F77_INT
102  schur<Matrix>::init (const Matrix& a, const std::string& ord, bool calc_unitary)
103  {
104  F77_INT a_nr = to_f77_int (a.rows ());
105  F77_INT a_nc = to_f77_int (a.cols ());
106 
107  if (a_nr != a_nc)
108  (*current_liboctave_error_handler) ("schur: requires square matrix");
109 
110  if (a_nr == 0)
111  {
112  schur_mat.clear ();
113  unitary_mat.clear ();
114  return 0;
115  }
116 
117  // Workspace requirements may need to be fixed if any of the
118  // following change.
119 
120  char jobvs;
121  char sense = 'N';
122  char sort = 'N';
123 
124  if (calc_unitary)
125  jobvs = 'V';
126  else
127  jobvs = 'N';
128 
129  char ord_char = (ord.empty () ? 'U' : ord[0]);
130 
131  if (ord_char == 'A' || ord_char == 'D' || ord_char == 'a' || ord_char == 'd')
132  sort = 'S';
133 
134  volatile double_selector selector = nullptr;
135  if (ord_char == 'A' || ord_char == 'a')
136  selector = select_ana;
137  else if (ord_char == 'D' || ord_char == 'd')
138  selector = select_dig;
139 
140  F77_INT n = a_nc;
141  F77_INT lwork = 8 * n;
142  F77_INT liwork = 1;
143  F77_INT info;
144  F77_INT sdim;
145  double rconde;
146  double rcondv;
147 
148  schur_mat = a;
149 
150  if (calc_unitary)
151  unitary_mat.clear (n, n);
152 
153  double *s = schur_mat.fortran_vec ();
154  double *q = unitary_mat.fortran_vec ();
155 
156  Array<double> wr (dim_vector (n, 1));
157  double *pwr = wr.fortran_vec ();
158 
159  Array<double> wi (dim_vector (n, 1));
160  double *pwi = wi.fortran_vec ();
161 
162  Array<double> work (dim_vector (lwork, 1));
163  double *pwork = work.fortran_vec ();
164 
165  // BWORK is not referenced for the non-ordered Schur routine.
166  F77_INT ntmp = (ord_char == 'N' || ord_char == 'n') ? 0 : n;
167  Array<F77_INT> bwork (dim_vector (ntmp, 1));
168  F77_INT *pbwork = bwork.fortran_vec ();
169 
170  Array<F77_INT> iwork (dim_vector (liwork, 1));
171  F77_INT *piwork = iwork.fortran_vec ();
172 
173  F77_XFCN (dgeesx, DGEESX, (F77_CONST_CHAR_ARG2 (&jobvs, 1),
174  F77_CONST_CHAR_ARG2 (&sort, 1),
175  selector,
176  F77_CONST_CHAR_ARG2 (&sense, 1),
177  n, s, n, sdim, pwr, pwi, q, n, rconde, rcondv,
178  pwork, lwork, piwork, liwork, pbwork, info
179  F77_CHAR_ARG_LEN (1)
180  F77_CHAR_ARG_LEN (1)
181  F77_CHAR_ARG_LEN (1)));
182 
183  return info;
184  }
185 
186  template <>
187  F77_INT
188  schur<FloatMatrix>::init (const FloatMatrix& a, const std::string& ord,
189  bool calc_unitary)
190  {
191  F77_INT a_nr = to_f77_int (a.rows ());
192  F77_INT a_nc = to_f77_int (a.cols ());
193 
194  if (a_nr != a_nc)
195  (*current_liboctave_error_handler) ("SCHUR requires square matrix");
196 
197  if (a_nr == 0)
198  {
199  schur_mat.clear ();
200  unitary_mat.clear ();
201  return 0;
202  }
203 
204  // Workspace requirements may need to be fixed if any of the
205  // following change.
206 
207  char jobvs;
208  char sense = 'N';
209  char sort = 'N';
210 
211  if (calc_unitary)
212  jobvs = 'V';
213  else
214  jobvs = 'N';
215 
216  char ord_char = (ord.empty () ? 'U' : ord[0]);
217 
218  if (ord_char == 'A' || ord_char == 'D' || ord_char == 'a' || ord_char == 'd')
219  sort = 'S';
220 
221  volatile float_selector selector = nullptr;
222  if (ord_char == 'A' || ord_char == 'a')
223  selector = select_ana;
224  else if (ord_char == 'D' || ord_char == 'd')
225  selector = select_dig;
226 
227  F77_INT n = a_nc;
228  F77_INT lwork = 8 * n;
229  F77_INT liwork = 1;
230  F77_INT info;
231  F77_INT sdim;
232  float rconde;
233  float rcondv;
234 
235  schur_mat = a;
236 
237  if (calc_unitary)
238  unitary_mat.clear (n, n);
239 
240  float *s = schur_mat.fortran_vec ();
241  float *q = unitary_mat.fortran_vec ();
242 
243  Array<float> wr (dim_vector (n, 1));
244  float *pwr = wr.fortran_vec ();
245 
246  Array<float> wi (dim_vector (n, 1));
247  float *pwi = wi.fortran_vec ();
248 
249  Array<float> work (dim_vector (lwork, 1));
250  float *pwork = work.fortran_vec ();
251 
252  // BWORK is not referenced for the non-ordered Schur routine.
253  F77_INT ntmp = (ord_char == 'N' || ord_char == 'n') ? 0 : n;
254  Array<F77_INT> bwork (dim_vector (ntmp, 1));
255  F77_INT *pbwork = bwork.fortran_vec ();
256 
257  Array<F77_INT> iwork (dim_vector (liwork, 1));
258  F77_INT *piwork = iwork.fortran_vec ();
259 
260  F77_XFCN (sgeesx, SGEESX, (F77_CONST_CHAR_ARG2 (&jobvs, 1),
261  F77_CONST_CHAR_ARG2 (&sort, 1),
262  selector,
263  F77_CONST_CHAR_ARG2 (&sense, 1),
264  n, s, n, sdim, pwr, pwi, q, n, rconde, rcondv,
265  pwork, lwork, piwork, liwork, pbwork, info
266  F77_CHAR_ARG_LEN (1)
267  F77_CHAR_ARG_LEN (1)
268  F77_CHAR_ARG_LEN (1)));
269 
270  return info;
271  }
272 
273  template <>
274  F77_INT
275  schur<ComplexMatrix>::init (const ComplexMatrix& a, const std::string& ord,
276  bool calc_unitary)
277  {
278  F77_INT a_nr = to_f77_int (a.rows ());
279  F77_INT a_nc = to_f77_int (a.cols ());
280 
281  if (a_nr != a_nc)
282  (*current_liboctave_error_handler) ("SCHUR requires square matrix");
283 
284  if (a_nr == 0)
285  {
286  schur_mat.clear ();
287  unitary_mat.clear ();
288  return 0;
289  }
290 
291  // Workspace requirements may need to be fixed if any of the
292  // following change.
293 
294  char jobvs;
295  char sense = 'N';
296  char sort = 'N';
297 
298  if (calc_unitary)
299  jobvs = 'V';
300  else
301  jobvs = 'N';
302 
303  char ord_char = (ord.empty () ? 'U' : ord[0]);
304 
305  if (ord_char == 'A' || ord_char == 'D' || ord_char == 'a' || ord_char == 'd')
306  sort = 'S';
307 
308  volatile complex_selector selector = nullptr;
309  if (ord_char == 'A' || ord_char == 'a')
310  selector = select_ana;
311  else if (ord_char == 'D' || ord_char == 'd')
312  selector = select_dig;
313 
314  F77_INT n = a_nc;
315  F77_INT lwork = 8 * n;
316  F77_INT info;
317  F77_INT sdim;
318  double rconde;
319  double rcondv;
320 
321  schur_mat = a;
322  if (calc_unitary)
323  unitary_mat.clear (n, n);
324 
325  Complex *s = schur_mat.fortran_vec ();
326  Complex *q = unitary_mat.fortran_vec ();
327 
328  Array<double> rwork (dim_vector (n, 1));
329  double *prwork = rwork.fortran_vec ();
330 
331  Array<Complex> w (dim_vector (n, 1));
332  Complex *pw = w.fortran_vec ();
333 
334  Array<Complex> work (dim_vector (lwork, 1));
335  Complex *pwork = work.fortran_vec ();
336 
337  // BWORK is not referenced for non-ordered Schur.
338  F77_INT ntmp = (ord_char == 'N' || ord_char == 'n') ? 0 : n;
339  Array<F77_INT> bwork (dim_vector (ntmp, 1));
340  F77_INT *pbwork = bwork.fortran_vec ();
341 
342  F77_XFCN (zgeesx, ZGEESX, (F77_CONST_CHAR_ARG2 (&jobvs, 1),
343  F77_CONST_CHAR_ARG2 (&sort, 1),
344  selector,
345  F77_CONST_CHAR_ARG2 (&sense, 1),
346  n, F77_DBLE_CMPLX_ARG (s), n, sdim, F77_DBLE_CMPLX_ARG (pw),
347  F77_DBLE_CMPLX_ARG (q), n, rconde, rcondv,
348  F77_DBLE_CMPLX_ARG (pwork), lwork, prwork, pbwork, info
349  F77_CHAR_ARG_LEN (1)
350  F77_CHAR_ARG_LEN (1)
351  F77_CHAR_ARG_LEN (1)));
352 
353  return info;
354  }
355 
356  template <>
358  rsf2csf<ComplexMatrix, Matrix> (const Matrix& s_arg, const Matrix& u_arg)
359  {
360  ComplexMatrix s (s_arg);
361  ComplexMatrix u (u_arg);
362 
363  F77_INT n = to_f77_int (s.rows ());
364 
365  if (s.columns () != n || u.rows () != n || u.columns () != n)
366  (*current_liboctave_error_handler)
367  ("rsf2csf: inconsistent matrix dimensions");
368 
369  if (n > 0)
370  {
371  OCTAVE_LOCAL_BUFFER (double, c, n-1);
372  OCTAVE_LOCAL_BUFFER (double, sx, n-1);
373 
374  F77_XFCN (zrsf2csf, ZRSF2CSF, (n, F77_DBLE_CMPLX_ARG (s.fortran_vec ()),
375  F77_DBLE_CMPLX_ARG (u.fortran_vec ()), c, sx));
376  }
377 
378  return schur<ComplexMatrix> (s, u);
379  }
380 
381  template <>
382  F77_INT
384  const std::string& ord, bool calc_unitary)
385  {
386  F77_INT a_nr = to_f77_int (a.rows ());
387  F77_INT a_nc = to_f77_int (a.cols ());
388 
389  if (a_nr != a_nc)
390  (*current_liboctave_error_handler) ("SCHUR requires square matrix");
391 
392  if (a_nr == 0)
393  {
394  schur_mat.clear ();
395  unitary_mat.clear ();
396  return 0;
397  }
398 
399  // Workspace requirements may need to be fixed if any of the
400  // following change.
401 
402  char jobvs;
403  char sense = 'N';
404  char sort = 'N';
405 
406  if (calc_unitary)
407  jobvs = 'V';
408  else
409  jobvs = 'N';
410 
411  char ord_char = (ord.empty () ? 'U' : ord[0]);
412 
413  if (ord_char == 'A' || ord_char == 'D' || ord_char == 'a' || ord_char == 'd')
414  sort = 'S';
415 
416  volatile float_complex_selector selector = nullptr;
417  if (ord_char == 'A' || ord_char == 'a')
418  selector = select_ana;
419  else if (ord_char == 'D' || ord_char == 'd')
420  selector = select_dig;
421 
422  F77_INT n = a_nc;
423  F77_INT lwork = 8 * n;
424  F77_INT info;
425  F77_INT sdim;
426  float rconde;
427  float rcondv;
428 
429  schur_mat = a;
430  if (calc_unitary)
431  unitary_mat.clear (n, n);
432 
433  FloatComplex *s = schur_mat.fortran_vec ();
434  FloatComplex *q = unitary_mat.fortran_vec ();
435 
436  Array<float> rwork (dim_vector (n, 1));
437  float *prwork = rwork.fortran_vec ();
438 
440  FloatComplex *pw = w.fortran_vec ();
441 
442  Array<FloatComplex> work (dim_vector (lwork, 1));
443  FloatComplex *pwork = work.fortran_vec ();
444 
445  // BWORK is not referenced for non-ordered Schur.
446  F77_INT ntmp = (ord_char == 'N' || ord_char == 'n') ? 0 : n;
447  Array<F77_INT> bwork (dim_vector (ntmp, 1));
448  F77_INT *pbwork = bwork.fortran_vec ();
449 
450  F77_XFCN (cgeesx, CGEESX, (F77_CONST_CHAR_ARG2 (&jobvs, 1),
451  F77_CONST_CHAR_ARG2 (&sort, 1),
452  selector,
453  F77_CONST_CHAR_ARG2 (&sense, 1),
454  n, F77_CMPLX_ARG (s), n, sdim, F77_CMPLX_ARG (pw), F77_CMPLX_ARG (q), n, rconde,
455  rcondv,
456  F77_CMPLX_ARG (pwork), lwork, prwork, pbwork, info
457  F77_CHAR_ARG_LEN (1)
458  F77_CHAR_ARG_LEN (1)
459  F77_CHAR_ARG_LEN (1)));
460 
461  return info;
462  }
463 
464  template <>
467  const FloatMatrix& u_arg)
468  {
469  FloatComplexMatrix s (s_arg);
470  FloatComplexMatrix u (u_arg);
471 
472  F77_INT n = to_f77_int (s.rows ());
473 
474  if (s.columns () != n || u.rows () != n || u.columns () != n)
475  (*current_liboctave_error_handler)
476  ("rsf2csf: inconsistent matrix dimensions");
477 
478  if (n > 0)
479  {
480  OCTAVE_LOCAL_BUFFER (float, c, n-1);
481  OCTAVE_LOCAL_BUFFER (float, sx, n-1);
482 
483  F77_XFCN (crsf2csf, CRSF2CSF, (n, F77_CMPLX_ARG (s.fortran_vec ()),
484  F77_CMPLX_ARG (u.fortran_vec ()), c, sx));
485  }
486 
487  return schur<FloatComplexMatrix> (s, u);
488  }
489 
490  // Instantiations we need.
491 
492  template class schur<ComplexMatrix>;
493 
494  template class schur<FloatComplexMatrix>;
495 
496  template class schur<FloatMatrix>;
497 
498  template class schur<Matrix>;
499  }
500 }
octave_idx_type cols(void) const
Definition: Array.h:423
octave_idx_type rows(void) const
Definition: Array.h:415
void clear(void)
Definition: Array.cc:87
const T * fortran_vec(void) const
Size of the specified dimension.
Definition: Array.h:583
Definition: dMatrix.h:42
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:95
octave_f77_int_type init(const T &a, const std::string &ord, bool calc_unitary)
Definition: mx-defs.h:76
subroutine crsf2csf(n, t, u, c, s)
Definition: crsf2csf.f:24
#define F77_DBLE_CMPLX_ARG(x)
Definition: f77-fcn.h:315
#define F77_CMPLX_ARG(x)
Definition: f77-fcn.h:309
#define F77_XFCN(f, F, args)
Definition: f77-fcn.h:44
double _Complex F77_DBLE_CMPLX
Definition: f77-fcn.h:303
octave_f77_int_type F77_INT
Definition: f77-fcn.h:305
float _Complex F77_CMPLX
Definition: f77-fcn.h:304
F77_INT(* float_complex_selector)(const F77_CMPLX &)
F77_INT(* float_selector)(const F77_REAL &, const F77_REAL &)
F77_INT(* double_selector)(const F77_DBLE &, const F77_DBLE &)
F77_INT(* complex_selector)(const F77_DBLE_CMPLX &)
octave_idx_type n
Definition: mx-inlines.cc:753
std::complex< double > w(std::complex< double > z, double relerr=0)
schur< FloatComplexMatrix > rsf2csf< FloatComplexMatrix, FloatMatrix >(const FloatMatrix &s_arg, const FloatMatrix &u_arg)
Definition: schur.cc:466
static F77_INT select_dig(const double &a, const double &b)
Definition: schur.cc:53
static F77_INT select_ana(const double &a, const double &)
Definition: schur.cc:47
schur< ComplexMatrix > rsf2csf< ComplexMatrix, Matrix >(const Matrix &s_arg, const Matrix &u_arg)
Definition: schur.cc:358
static double wi[256]
Definition: randmtzig.cc:447
std::complex< double > Complex
Definition: oct-cmplx.h:33
std::complex< float > FloatComplex
Definition: oct-cmplx.h:34
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
Definition: oct-locbuf.h:44
static T abs(T x)
Definition: pr-output.cc:1678
subroutine zrsf2csf(n, t, u, c, s)
Definition: zrsf2csf.f:24