GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
trexc.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2025-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 "lapack-proto.h"
31
32#include "defun.h"
33#include "error.h"
34#include "ovl.h"
35
37
38DEFUN (trexc, args, ,
39 doc: /* -*- texinfo -*-
40@deftypefn {} {[@var{U}, @var{T}] =} trexc (@var{U}, @var{T}, @var{M})
41Reorder the Schur factorization using @sc{LAPACK} function @code{ZTREXC}.
42
43Given a unitary matrix @var{U} and an upper triangular Schur form @var{T}
44from the Schur decomposition @code{A = U*T*U'}, this function reorders
45the diagonal elements of @var{T} according to the swap specifications
46in @var{M}.
47
48Each row of @var{M} contains two indices @code{[IFST, ILST]} specifying
49that the diagonal element at position @code{IFST} should be moved to
50position @code{ILST}. The swaps are performed sequentially.
51
52@seealso{schur, ordschur}
53@end deftypefn */)
54{
55 if (args.length () != 3)
56 print_usage ();
57
58 ComplexMatrix U = args(0).complex_matrix_value ();
59 ComplexMatrix T = args(1).complex_matrix_value ();
60 Matrix M = args(2).matrix_value ();
61
62 F77_INT n = octave::to_f77_int (T.rows ());
63
64 if (T.rows () != T.cols ())
65 error ("trexc: T must be a square matrix");
66
67 if (U.rows () != n || U.cols () != n)
68 error ("trexc: U must have the same dimensions as T");
69
70 if (M.cols () != 2)
71 error ("trexc: M must have exactly 2 columns [IFST, ILST]");
72
73 F77_INT ldt = n;
74 F77_INT ldq = n;
75 F77_INT info = 0;
76
77 // Process each swap specified in M
78 octave_idx_type n_swaps = M.rows ();
79
80 for (octave_idx_type k = 0; k < n_swaps; k++)
81 {
82 F77_INT ifst = octave::to_f77_int (static_cast<int> (M(k, 0)));
83 F77_INT ilst = octave::to_f77_int (static_cast<int> (M(k, 1)));
84
85 // Validate indices
86 if (ifst < 1 || ifst > n || ilst < 1 || ilst > n)
87 error ("trexc: indices in M must be between 1 and %d", n);
88
89 F77_XFCN (ztrexc, ZTREXC,
90 (F77_CONST_CHAR_ARG ("V"),
91 n,
93 ldt,
95 ldq,
96 ifst,
97 ilst,
98 info
99 F77_CHAR_ARG_LEN (1)));
100
101 if (info != 0)
102 error ("trexc: ZTREXC failed with info = %d", info);
103 }
104
105 return ovl (U, T);
106}
107
108OCTAVE_END_NAMESPACE(octave)
109
110/*
111%!test
112%! A = [1 2 3; 0 4 5; 0 0 6];
113%! [U, T] = schur (A, "complex");
114%! M = [1, 2];
115%! [U2, T2] = trexc (U, T, M);
116%! assert (U2 * T2 * U2', A, 4*eps);
117
118%!test
119%! v = randn ("state");
120%! randn ("state", 42);
121%! A = randn (4) + 1i * randn (4);
122%! randn ("state", v);
123%! [U, T] = schur (A, "complex");
124%! M = [1, 3; 2, 4];
125%! [U2, T2] = trexc (U, T, M);
126%! assert (norm (U2 * T2 * U2' - A, "fro"), 0, 65 * eps (norm (A, "fro")));
127
128%!error <must be a square matrix> trexc (eye (2), [1 2; 3 4; 5 6], [1 2])
129%!error <must have exactly 2 columns> trexc (eye (2), eye (2), [1 2 3])
130*/
T * fortran_vec()
Size of the specified dimension.
Definition Array-base.h:695
octave_idx_type rows() const
Definition Array-base.h:485
octave_idx_type cols() const
Definition Array-base.h:495
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void print_usage()
Definition defun-int.h:72
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition defun.h:56
void error(const char *fmt,...)
Definition error.cc:1008
#define F77_DBLE_CMPLX_ARG(x)
Definition f77-fcn.h:316
#define F77_XFCN(f, F, args)
Definition f77-fcn.h:45
octave_f77_int_type F77_INT
Definition f77-fcn.h:306
F77_RET_T const F77_INT const F77_INT const F77_INT const F77_DBLE const F77_DBLE F77_INT & M
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition ovl.h:217