<T>LAPACK 0.1.1
C++ Template Linear Algebra PACKage
Loading...
Searching...
No Matches
gghrd.hpp
Go to the documentation of this file.
1
5//
6// Copyright (c) 2021-2023, University of Colorado Denver. All rights reserved.
7//
8// This file is part of <T>LAPACK.
9// <T>LAPACK is free software: you can redistribute it and/or modify it under
10// the terms of the BSD 3-Clause license. See the accompanying LICENSE file.
11
12#ifndef TLAPACK_GGHRD_HH
13#define TLAPACK_GGHRD_HH
14
16#include "tlapack/blas/rot.hpp"
17#include "tlapack/blas/rotg.hpp"
18
19namespace tlapack {
20
38template <TLAPACK_SMATRIX A_t,
42int gghrd(bool wantq,
43 bool wantz,
46 A_t& A,
47 B_t& B,
48 Q_t& Q,
49 Z_t& Z)
50{
51 using T = type_t<A_t>;
52 using idx_t = size_type<A_t>;
54
55 // constants
56 const idx_t n = ncols(A);
57
58 // check arguments
59 tlapack_check(ilo >= 0 && ilo < n);
60 tlapack_check(ihi > ilo && ihi <= n);
61 tlapack_check(n == nrows(A));
62 tlapack_check(n == ncols(B));
63 tlapack_check(n == nrows(B));
64 tlapack_check(n == ncols(Q));
65 tlapack_check(n == nrows(Q));
66 tlapack_check(n == ncols(Z));
67 tlapack_check(n == nrows(Z));
68
69 // quick return
70 if (n <= 1) return 0;
71
72 // Zero out lower triangle of B
73 for (idx_t j = 0; j < n; ++j)
74 for (idx_t i = j + 1; i < n; ++i)
75 B(i, j) = (T)0;
76
77 for (idx_t j = ilo; j + 2 < ihi; ++j) {
78 // Apply sequence of rotations
79 for (idx_t i = ihi - 1; i > j + 1; --i) {
80 //
81 // Rotate rows i-1 and i to eliminate A(i,j)
82 //
84 T s;
85 rotg(A(i - 1, j), A(i, j), c, s);
86 A(i, j) = (T)0;
87 {
88 auto a1 = slice(A, i - 1, range(j + 1, n));
89 auto a2 = slice(A, i, range(j + 1, n));
90 rot(a1, a2, c, s);
91 }
92 {
93 auto b1 = slice(B, i - 1, range(i - 1, n));
94 auto b2 = slice(B, i, range(i - 1, n));
95 rot(b1, b2, c, s);
96 }
97 if (wantq) {
98 auto q1 = slice(Q, range(0, n), i - 1);
99 auto q2 = slice(Q, range(0, n), i);
100 rot(q1, q2, c, conj(s));
101 }
102 //
103 // The previous step introduced fill-in in B, remove it now
104 //
105 rotg(B(i, i), B(i, i - 1), c, s);
106 B(i, i - 1) = (T)0;
107 {
108 auto a1 = slice(A, range(0, ihi), i);
109 auto a2 = slice(A, range(0, ihi), i - 1);
110 rot(a1, a2, c, s);
111 }
112 {
113 auto b1 = slice(B, range(0, i), i);
114 auto b2 = slice(B, range(0, i), i - 1);
115 rot(b1, b2, c, s);
116 }
117 if (wantz) {
118 auto z1 = slice(Z, range(0, n), i);
119 auto z2 = slice(Z, range(0, n), i - 1);
120 rot(z1, z2, c, s);
121 }
122 }
123 }
124
125 return 0;
126}
127
128} // namespace tlapack
129
130#endif // TLAPACK_GGHRD_HH
constexpr T conj(const T &x) noexcept
Extends std::conj() to real datatypes.
Definition utils.hpp:100
#define TLAPACK_SMATRIX
Macro for tlapack::concepts::SliceableMatrix compatible with C++17.
Definition concepts.hpp:899
void rotg(T &a, T &b, T &c, T &s)
Construct plane rotation that eliminates b, such that:
Definition rotg.hpp:39
void rot(vectorX_t &x, vectorY_t &y, const c_type &c, const s_type &s)
Apply plane rotation:
Definition rot.hpp:44
int gghrd(bool wantq, bool wantz, size_type< A_t > ilo, size_type< A_t > ihi, A_t &A, B_t &B, Q_t &Q, Z_t &Z)
Reduces a pair of real square matrices (A, B) to generalized upper Hessenberg form using unitary tran...
Definition gghrd.hpp:42
#define tlapack_check(cond)
Throw an error if cond is false.
Definition exceptionHandling.hpp:98
typename traits::real_type_traits< Types..., int >::type real_type
The common real type of the list of types.
Definition scalar_type_traits.hpp:113