<T>LAPACK 0.1.2
C++ Template Linear Algebra PACKage
Loading...
Searching...
No Matches
unmq.hpp
Go to the documentation of this file.
1
6//
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_UNMQ_HH
14#define TLAPACK_UNMQ_HH
15
19
20namespace tlapack {
21
25struct UnmqOpts {
26 size_t nb = 32;
27};
28
71template <class T,
81 direction_t direction,
83 const matrixV_t& V,
84 const vector_t& tau,
85 const matrixC_t& C,
86 const UnmqOpts& opts = {})
87{
88 using idx_t = size_type<matrixC_t>;
91
92 // Constants
93 const idx_t m = nrows(C);
94 const idx_t n = ncols(C);
95 const idx_t k = size(tau);
96 const idx_t nQ = (side == Side::Left) ? m : n;
97 const idx_t nb = min((idx_t)opts.nb, k);
98
99 // Local workspace sizes
100 WorkInfo workinfo =
101 (is_same_v<T, type_t<work_t>>) ? WorkInfo(nb, nb) : WorkInfo(0);
102
103 auto&& Vi = (storeMode == StoreV::Columnwise)
104 ? slice(V, range{0, nQ}, range{0, nb})
105 : slice(V, range{0, nb}, range{0, nQ});
106 auto&& matrixTi = slice(V, range{0, nb}, range{0, nb});
107
108 // larfb:
110 matrixTi, C);
111
112 return workinfo;
113}
114
123template <TLAPACK_SMATRIX matrixV_t,
133 direction_t direction,
135 const matrixV_t& V,
136 const vector_t& tau,
137 matrixC_t& C,
138 work_t& work,
139 const UnmqOpts& opts = {})
140{
141 using idx_t = size_type<matrixC_t>;
143
144 // constants
145 const idx_t m = nrows(C);
146 const idx_t n = ncols(C);
147 const idx_t k = size(tau);
148 const idx_t nQ = (side == Side::Left) ? m : n;
149 const idx_t nb = min((idx_t)opts.nb, k);
150
151 // check arguments
157 direction != Direction::Forward);
159 ? ((ncols(V) == k) && (nrows(V) == nQ))
160 : ((nrows(V) == k) && (ncols(V) == nQ)));
161
162 // quick return
163 if (m <= 0 || n <= 0 || k <= 0) return 0;
164
165 // Matrix matrixT
166 auto [matrixT, work1] = reshape(work, nb, nb);
167
168 // const expressions
169 const bool positiveIncLeft =
171 ? ((direction == Direction::Backward) ? (trans == Op::NoTrans)
172 : (trans != Op::NoTrans))
173 : ((direction == Direction::Forward) ? (trans == Op::NoTrans)
174 : (trans != Op::NoTrans));
175 const bool positiveInc =
177 const idx_t i0 = (positiveInc) ? 0 : ((k - 1) / nb) * nb;
178 const idx_t iN = (positiveInc) ? ((k - 1) / nb + 1) * nb : -nb;
179 const idx_t inc = (positiveInc) ? nb : -nb;
180
182 for (idx_t i = i0; i != iN; i += inc) {
183 const idx_t ib = min(nb, k - i);
184 const auto rangev = (direction == Direction::Forward)
185 ? range{i, nQ}
186 : range{0, nQ - k + i + ib};
187 const auto Vi = slice(V, rangev, range{i, i + ib});
188 const auto taui = slice(tau, range{i, i + ib});
189 auto matrixTi = slice(matrixT, range{0, ib}, range{0, ib});
190
191 // Form the triangular factor of the block reflector
192 larft(direction, COLUMNWISE_STORAGE, Vi, taui, matrixTi);
193
194 // H or H**H is applied to either C[i:m,0:n] or C[0:m,i:n]
195 auto Ci = (side == Side::Left) ? slice(C, rangev, range{0, n})
196 : slice(C, range{0, m}, rangev);
198 Ci, work1);
199 }
200 }
201 else {
202 for (idx_t i = i0; i != iN; i += inc) {
203 const idx_t ib = min(nb, k - i);
204 const auto rangev = (direction == Direction::Forward)
205 ? range{i, nQ}
206 : range{0, nQ - k + i + ib};
207 const auto Vi = slice(V, range{i, i + ib}, rangev);
208 const auto taui = slice(tau, range{i, i + ib});
209 auto matrixTi = slice(matrixT, range{0, ib}, range{0, ib});
210
211 // Form the triangular factor of the block reflector
212 larft(direction, ROWWISE_STORAGE, Vi, taui, matrixTi);
213
214 // H or H**H is applied to either C[i:m,0:n] or C[0:m,i:n]
215 auto Ci = (side == Side::Left) ? slice(C, rangev, range{0, n})
216 : slice(C, range{0, m}, rangev);
219 direction, ROWWISE_STORAGE, Vi, matrixTi, Ci, work1);
220 }
221 }
222
223 return 0;
224}
225
275template <TLAPACK_SMATRIX matrixV_t,
284 direction_t direction,
286 const matrixV_t& V,
287 const vector_t& tau,
288 matrixC_t& C,
289 const UnmqOpts& opts = {})
290{
291 using idx_t = size_type<matrixC_t>;
293 using T = type_t<work_t>;
294
295 // Functor
297
298 // constants
299 const idx_t m = nrows(C);
300 const idx_t n = ncols(C);
301 const idx_t k = size(tau);
302
303 // quick return
304 if (m <= 0 || n <= 0 || k <= 0) return 0;
305
306 // Allocates workspace
307 WorkInfo workinfo =
308 unmq_worksize<T>(side, trans, direction, storeMode, V, tau, C, opts);
309 std::vector<T> work_;
310 auto work = new_matrix(work_, workinfo.m, workinfo.n);
311
312 return unmq_work(side, trans, direction, storeMode, V, tau, C, work, opts);
313}
314
315} // namespace tlapack
316
317#endif // TLAPACK_UNMQ_HH
#define TLAPACK_SVECTOR
Macro for tlapack::concepts::SliceableVector compatible with C++17.
Definition concepts.hpp:909
#define TLAPACK_STOREV
Macro for tlapack::concepts::StoreV compatible with C++17.
Definition concepts.hpp:936
#define TLAPACK_SIDE
Macro for tlapack::concepts::Side compatible with C++17.
Definition concepts.hpp:927
#define TLAPACK_SMATRIX
Macro for tlapack::concepts::SliceableMatrix compatible with C++17.
Definition concepts.hpp:899
#define TLAPACK_DIRECTION
Macro for tlapack::concepts::Direction compatible with C++17.
Definition concepts.hpp:930
#define TLAPACK_OP
Macro for tlapack::concepts::Op compatible with C++17.
Definition concepts.hpp:933
#define TLAPACK_WORKSPACE
Macro for tlapack::concepts::Workspace compatible with C++17.
Definition concepts.hpp:912
int unmq(side_t side, trans_t trans, direction_t direction, storage_t storeMode, const matrixV_t &V, const vector_t &tau, matrixC_t &C, const UnmqOpts &opts={})
Applies unitary matrix Q to a matrix C.
Definition unmq.hpp:282
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 unmq_work(side_t side, trans_t trans, direction_t direction, storage_t storeMode, const matrixV_t &V, const vector_t &tau, matrixC_t &C, work_t &work, const UnmqOpts &opts={})
Applies unitary matrix Q to a matrix C. Workspace is provided as an argument.
Definition unmq.hpp:131
#define tlapack_check(cond)
Throw an error if cond is false.
Definition exceptionHandling.hpp:98
#define tlapack_check_false(cond)
Throw an error if cond is true.
Definition exceptionHandling.hpp:113
constexpr WorkInfo unmq_worksize(side_t side, trans_t trans, direction_t direction, storage_t storeMode, const matrixV_t &V, const vector_t &tau, const matrixC_t &C, const UnmqOpts &opts={})
Worspace query of unmq()
Definition unmq.hpp:79
Concept for types that represent tlapack::Direction.
Concept for types that represent tlapack::Op.
Applies a Householder block reflector to a matrix.
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
@ Right
right side
@ Left
left side
constexpr internal::RowwiseStorage ROWWISE_STORAGE
Rowwise storage.
Definition types.hpp:416
@ Forward
Forward direction.
@ Backward
Backward direction.
@ Trans
transpose
@ NoTrans
no transpose
@ ConjTrans
conjugate transpose
constexpr internal::ColumnwiseStorage COLUMNWISE_STORAGE
Columnwise storage.
Definition types.hpp:414
@ Columnwise
Columnwise storage.
constexpr bool is_complex
True if T is a complex scalar type.
Definition scalar_type_traits.hpp:192
constexpr internal::NoTranspose NO_TRANS
no transpose
Definition types.hpp:260
Options struct for unmq.
Definition unmq.hpp:25
size_t nb
Block size.
Definition unmq.hpp:26
Output information in the workspace query.
Definition workspace.hpp:16