<T>LAPACK 0.1.2
C++ Template Linear Algebra PACKage
Loading...
Searching...
No Matches
geqrt3.hpp
Go to the documentation of this file.
1
7// Copyright (c) 2025, University of Colorado Denver. All rights reserved.
8//
9// This file is part of <T>LAPACK.
10// <T>LAPACK is free software: you can redistribute it and/or modify it under
11// the terms of the BSD 3-Clause license. See the accompanying LICENSE file.
12
13#ifndef TLAPACK_GEQRT3_HH
14#define TLAPACK_GEQRT3_HH
15
16#include "tlapack/blas/gemm.hpp"
17#include "tlapack/blas/trmm.hpp"
22
23namespace tlapack {
32struct Geqrt3Opts {
33 bool isw = false;
34 size_t nx = 1;
35};
36
52template <TLAPACK_MATRIX matrix_a, TLAPACK_MATRIX matrix_h>
54{
55 using std::size_t;
56 using idx_t = size_type<matrix_a>;
58 using T = type_t<matrix_a>;
59
60 // constants
61 const idx_t m = nrows(A);
62 const idx_t n = ncols(A);
63
64 auto info = 0;
65 if (m < n) {
66 std::cout << "Error: m < n" << std::endl;
67 info = -1;
68 }
69
70 if (info != 0) {
71 return;
72 }
73
74 if (n == 1) {
75 // Turn the single column into a vector
76 auto a_vector = col(A, 0);
77
78 // Populate matrix T with an elementary reflector
80 }
81 else if (n <= opts.nx) {
82 auto tau = diag(Tmatrix);
83
84 geqrf(A, tau);
85
87 }
88 else {
89 // Define slice sizes
90 idx_t n1 = n / 2;
91 idx_t n2 = n - n1;
92 idx_t m1 = n1;
93 idx_t m2 = n2 + n1;
94 idx_t m3 = m;
95
96 // slices
97 auto A1 = slice(A, range(0, m), range(0, n1));
98 auto A11 = slice(A, range(0, m1), range(0, n1));
99 auto A12 = slice(A, range(0, m1), range(n1, n));
100 auto A21 = slice(A, range(m1, m2), range(0, n1));
101 auto A22 = slice(A, range(m1, m2), range(n1, n));
102 auto A22_32 = slice(A, range(m1, m3), range(n1, n));
103 auto A31 = slice(A, range(m2, m3), range(0, n1));
104 auto A32 = slice(A, range(m2, m3), range(n1, n));
105 auto T11 = slice(Tmatrix, range(0, n1), range(0, n1));
106 auto T12 = slice(Tmatrix, range(0, n1), range(n1, n));
107 auto T22 = slice(Tmatrix, range(n1, n), range(n1, n));
108
109 // step 1: Compute the QR factorization of A1
110 geqrt3(A1, T11, Geqrt3Opts{.isw = false, .nx = opts.nx});
111
112 // step 2: Copy A12 into T12
113 // no additional flops, just copy
115
116 // step 3: T12 = A11ᴴ * T12
117
119 T12);
120
121 // step 4: T12 = T12 + (A21ᴴ * A22)
122
123 gemm(Op::ConjTrans, Op::NoTrans, T(1.0), A21, A22, T(1.0), T12);
124
125 // T12 = T12 + (A31ᴴ * A32)
126 gemm(Op::ConjTrans, Op::NoTrans, T(1.0), A31, A32, T(1.0), T12);
127
128 // step 5: T12 = T11ᴴ * T12
130 T12);
131
132 // step 6: A22 = A22 - (A21 * T12)
133 gemm(Op::NoTrans, Op::NoTrans, T(-1.0), A21, T12, T(1.0), A22);
134
135 // A32 = A32 - (A31 * T12)
136 gemm(Op::NoTrans, Op::NoTrans, T(-1.0), A31, T12, T(1.0), A32);
137
138 // step 7:T12 = A11 * T12
140 T12);
141
142 // step 8: A12 = A12 - T12
143 for (idx_t j = 0; j < n2; ++j) {
144 for (idx_t i = 0; i < m1; ++i) {
145 A12(i, j) -= T12(i, j);
146 }
147 }
148 // step 9: Compute the QR factorization of A22_32
150
151 if (!opts.isw) {
152 // step 10: manually compute T12 = A21ᴴ
153 for (idx_t j = 0; j < n2; ++j) {
154 for (idx_t i = 0; i < m1; ++i) {
155 if constexpr (is_complex<T>)
156 T12(i, j) = std::conj(A21(j, i));
157 else
158 T12(i, j) = A21(j, i);
159 }
160 }
161
162 // step 11: T12 = T12 * T22ᴴ
164 T12);
165
166 // step 12: T12 = T12 + A31ᴴ * A32
167 gemm(Op::ConjTrans, Op::NoTrans, T(1.0), A31, A32, T(1.0), T12);
168
169 // step 13: T12 = T12 * T11
171 T11, T12);
172
173 // step 14: T12 = T12 * T22
175 T22, T12);
176 }
177 }
178}
179} // namespace tlapack
180#endif // TLAPACK_GEQRT3_HH
int geqrf(A_t &A, tau_t &tau, const GeqrfOpts &opts={})
Computes a QR factorization of an m-by-n matrix A using a blocked algorithm.
Definition geqrf.hpp:158
int larft(direction_t direction, storage_t storeMode, const matrixV_t &V, const vector_t &tau, matrixT_t &T)
Forms the triangular factor T of a block reflector H of order n, which is defined as a product of k e...
Definition larft.hpp:92
void larfg(storage_t storeMode, type_t< vector_t > &alpha, vector_t &x, type_t< vector_t > &tau)
Generates a elementary Householder reflection.
Definition larfg.hpp:73
void lacpy(uplo_t uplo, const matrixA_t &A, matrixB_t &B)
Copies a matrix from A to B.
Definition lacpy.hpp:38
void trmm(Side side, Uplo uplo, Op trans, Diag diag, const alpha_t &alpha, const matrixA_t &A, matrixB_t &B)
Triangular matrix-matrix multiply:
Definition trmm.hpp:72
void gemm(Op transA, Op transB, const alpha_t &alpha, const matrixA_t &A, const matrixB_t &B, const beta_t &beta, matrixC_t &C)
General matrix-matrix multiply:
Definition gemm.hpp:61
void geqrt3(matrix_a &A, matrix_h &Tmatrix, const Geqrt3Opts &opts={})
Recursive QR factorization using compact WY Householder representation.
Definition geqrt3.hpp:53
Forms the triangular factor T of a block reflector.
Sort the numbers in D in increasing order (if ID = 'I') or in decreasing order (if ID = 'D' ).
Definition arrayTraits.hpp:15
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
@ Unit
The main diagonal is assumed to consist of 1's.
@ NonUnit
The main diagonal is not assumed to consist of 1's.
@ Right
right side
@ Left
left side
@ Forward
Forward direction.
constexpr auto diag(T &A, int diagIdx=0) noexcept
Get the Diagonal of an Eigen Matrix.
Definition eigen.hpp:576
@ NoTrans
no transpose
@ ConjTrans
conjugate transpose
@ Columnwise
Columnwise storage.
@ General
0 <= i <= m, 0 <= j <= n.
@ Upper
0 <= i <= j, 0 <= j <= n.
@ Lower
0 <= i <= m, 0 <= j <= i.
By toggling isw to true, the geqrt3 routine will stop its loop before computing the the upper right b...
Definition geqrt3.hpp:32