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