<T>LAPACK 0.1.1
C++ Template Linear Algebra PACKage
Loading...
Searching...
No Matches
geqrf.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_GEQRF_HH
13#define TLAPACK_GEQRF_HH
14
19
20namespace tlapack {
24struct GeqrfOpts {
25 size_t nb = 32;
26};
27
40template <class T, TLAPACK_SMATRIX A_t, TLAPACK_SVECTOR tau_t>
41constexpr WorkInfo geqrf_worksize(const A_t& A,
42 const tau_t& tau,
43 const GeqrfOpts& opts = {})
44{
45 using idx_t = size_type<A_t>;
46 using range = pair<idx_t, idx_t>;
47 using work_t = matrix_type<A_t, tau_t>;
48
49 // constants
50 const idx_t m = nrows(A);
51 const idx_t n = ncols(A);
52 const idx_t k = min(m, n);
53 const idx_t nb = min((idx_t)opts.nb, k);
54
55 auto&& A11 = cols(A, range(0, nb));
56 auto&& tauw1 = slice(tau, range(0, nb));
57 WorkInfo workinfo = geqr2_worksize<T>(A11, tauw1);
58
59 if (n > nb) {
60 auto&& TT1 = slice(A, range(0, nb), range(0, nb));
61 auto&& A12 = slice(A, range(0, m), range(nb, n));
62 workinfo.minMax(larfb_worksize<T>(LEFT_SIDE, CONJ_TRANS, FORWARD,
63 COLUMNWISE_STORAGE, A11, TT1, A12));
64 if constexpr (is_same_v<T, type_t<work_t>>)
65 workinfo += WorkInfo(nb, nb);
66 }
67
68 return workinfo;
69}
70
79template <TLAPACK_SMATRIX A_t, TLAPACK_SVECTOR tau_t, TLAPACK_WORKSPACE work_t>
81{
82 using idx_t = size_type<A_t>;
83 using range = pair<idx_t, idx_t>;
84
85 // constants
86 const idx_t m = nrows(A);
87 const idx_t n = ncols(A);
88 const idx_t k = min(m, n);
89 const idx_t nb = min((idx_t)opts.nb, k);
90
91 // check arguments
92 tlapack_check((idx_t)size(tau) >= k);
93
94 // Matrix TT
95 auto [TT, work2] = (n > nb) ? reshape(work, nb, nb) : reshape(work, 0, 0);
96
97 // Main computational loop
98 for (idx_t j = 0; j < k; j += nb) {
99 const idx_t ib = min(nb, k - j);
100
101 // Compute the QR factorization of the current block A(j:m,j:j+ib)
102 auto A11 = slice(A, range(j, m), range(j, j + ib));
103 auto tauw1 = slice(tau, range(j, j + ib));
104
105 geqr2_work(A11, tauw1, work);
106
107 if (j + ib < n) {
108 // Form the triangular factor of the block reflector H = H(j)
109 // H(j+1) . . . H(j+ib-1)
110 auto TT1 = slice(TT, range(0, ib), range(0, ib));
111 larft(FORWARD, COLUMNWISE_STORAGE, A11, tauw1, TT1);
112
113 // Apply H to A(j:m,j+ib:n) from the left
114 auto A12 = slice(A, range(j, m), range(j + ib, n));
115 larfb_work(LEFT_SIDE, CONJ_TRANS, FORWARD, COLUMNWISE_STORAGE, A11,
116 TT1, A12, work2);
117 }
118 }
119
120 return 0;
121}
122
157template <TLAPACK_SMATRIX A_t, TLAPACK_SVECTOR tau_t>
158int geqrf(A_t& A, tau_t& tau, const GeqrfOpts& opts = {})
159{
160 using work_t = matrix_type<A_t, tau_t>;
161 using T = type_t<work_t>;
162 Create<work_t> new_matrix;
163
164 // Allocate or get workspace
165 WorkInfo workinfo = geqrf_worksize<T>(A, tau, opts);
166 std::vector<T> work_;
167 auto work = new_matrix(work_, workinfo.m, workinfo.n);
168
169 return geqrf_work(A, tau, work, opts);
170}
171} // namespace tlapack
172#endif // TLAPACK_GEQRF_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 larfb_work(side_t side, trans_t trans, direction_t direction, storage_t storeMode, const matrixV_t &V, const matrixT_t &Tmatrix, matrixC_t &C, work_t &work)
Applies a block reflector or its conjugate transpose to a m-by-n matrix C, from either the left or ...
Definition larfb.hpp:111
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
int geqrf_work(A_t &A, tau_t &tau, work_t &work, const GeqrfOpts &opts={})
Computes a QR factorization of an m-by-n matrix A using a blocked algorithm. Workspace is provided ...
Definition geqrf.hpp:80
int geqr2_work(matrix_t &A, vector_t &tau, work_t &work)
Computes a QR factorization of a matrix A. Workspace is provided as an argument.
Definition geqr2.hpp:60
#define tlapack_check(cond)
Throw an error if cond is false.
Definition exceptionHandling.hpp:98
constexpr WorkInfo geqrf_worksize(const A_t &A, const tau_t &tau, const GeqrfOpts &opts={})
Worspace query of geqrf()
Definition geqrf.hpp:41
Applies a Householder block reflector to a matrix.
Forms the triangular factor T of a block reflector.
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
Options struct for geqrf.
Definition geqrf.hpp:24
size_t nb
Block size.
Definition geqrf.hpp:25
Output information in the workspace query.
Definition workspace.hpp:16