31using qr_matrix_extents = stdex::dextents<uni20::index_type, 2>;
33template <
class QTensor,
class RTensor,
class MatrixTensor>
34concept CompatibleQrTensors =
37 detail::DecompositionScalar<uni20::tensor_element_t<MatrixTensor>> &&
41template <uni20::MutableRankedTensorView<2> QTensor, uni20::MutableRankedTensorView<2> RTensor,
42 uni20::MutableRankedTensorView<2> MatrixTensor>
43void prepare_qr(QTensor& q, RTensor& r, MatrixTensor
const& matrix_work)
47 auto const rank = std::min(rows, cols);
52template <KernelBackendSelector BackendSelector, uni20::MutableRankedTensorView<2> QTensor,
53 uni20::MutableRankedTensorView<2> RTensor, uni20::MutableRankedTensorView<2> MatrixTensor>
54void dispatch_qr(BackendSelector&& selector, QTensor& q, RTensor& r, MatrixTensor& matrix_work, Precision
p)
59 detail::dispatch_at_precision<uni20::tensor_element_t<MatrixTensor>>(
60 std::forward<BackendSelector>(selector), qr_op{},
p, q_descriptor, r_descriptor, matrix_descriptor);
62 uni20::detail::record_output_precision(q,
p);
63 uni20::detail::record_output_precision(r,
p);
64 uni20::detail::record_output_precision(matrix_work,
p);
73template <KernelBackendSelector BackendSelector,
class QTensor,
class RTensor,
class MatrixTensor>
74 requires detail::CompatibleQrTensors<QTensor, RTensor, MatrixTensor>
75void qr_factorization(BackendSelector&& selector, QTensor&& q, RTensor&& r, MatrixTensor&& matrix_work,
76 std::optional<Precision> requested = {})
78 auto const p = detail::decomposition_precision(matrix_work, requested);
79 detail::prepare_qr(q, r, matrix_work);
80 detail::dispatch_qr(std::forward<BackendSelector>(selector), q, r, matrix_work,
p);
84template <
class QTensor,
class RTensor,
class MatrixTensor>
85 requires detail::CompatibleQrTensors<QTensor, RTensor, MatrixTensor>
86void qr_factorization(QTensor&& q, RTensor&& r, MatrixTensor&& matrix_work, std::optional<Precision> requested = {})
88 auto const p = detail::decomposition_precision(matrix_work, requested);
89 detail::prepare_qr(q, r, matrix_work);
91 detail::dispatch_qr(selector, q, r, matrix_work,
p);
96template <
class QTensor,
class RTensor>
struct QrResult
109template <
class MatrixTensor>
consteval bool can_use_qr_storage_as_workspace()
111 return std::same_as<std::remove_cvref_t<MatrixTensor>, qr_matrix_tensor_t<MatrixTensor>>;
114template <KernelBackendSelector BackendSelector, uni20::OwningTensor MatrixTensor>
116[[nodiscard]]
auto qr_from_work_matrix(BackendSelector&& selector, MatrixTensor matrix_work,
Precision p)
118 qr_matrix_tensor_t<MatrixTensor> q;
119 qr_matrix_tensor_t<MatrixTensor> r;
121 return QrResult<
decltype(q),
decltype(r)>{.q = std::move(q), .r = std::move(r)};
124template <
class MatrixTensor> [[nodiscard]]
constexpr auto select_qr_backend()
126 return select_backend_for<qr_matrix_tensor_t<MatrixTensor>, qr_matrix_tensor_t<MatrixTensor>,
127 std::remove_cvref_t<MatrixTensor>>(qr_op{});
132template <KernelBackendSelector BackendSelector, uni20::RankedTensorView<2> MatrixTensor>
133 requires detail::DecompositionScalar<uni20::tensor_element_t<MatrixTensor>>
134[[nodiscard]]
auto qr(BackendSelector&& selector, MatrixTensor
const& matrix, std::optional<Precision> requested = {})
136 auto const p = detail::decomposition_precision(matrix, requested);
137 auto matrix_work = uni20::make_tensor<uni20::ColumnMajor>(matrix);
138 return detail::qr_from_work_matrix(std::forward<BackendSelector>(selector), std::move(matrix_work),
p);
142template <uni20::RankedTensorView<2> MatrixTensor>
143 requires detail::DecompositionScalar<uni20::tensor_element_t<MatrixTensor>>
144[[nodiscard]]
auto qr(MatrixTensor
const& matrix, std::optional<Precision> requested = {})
146 auto const p = detail::decomposition_precision(matrix, requested);
147 auto matrix_work = uni20::make_tensor<uni20::ColumnMajor>(matrix);
148 auto selector = detail::select_qr_backend<decltype(matrix_work)>();
149 return detail::qr_from_work_matrix(std::move(selector), std::move(matrix_work),
p);
155template <KernelBackendSelector BackendSelector,
class MatrixTensor>
157 detail::DecompositionScalar<uni20::tensor_element_t<MatrixTensor>> &&
158 (!std::is_lvalue_reference_v<MatrixTensor>) && (!std::is_const_v<std::remove_reference_t<MatrixTensor>>)
159[[nodiscard]]
auto qr(BackendSelector&& selector, MatrixTensor&& matrix, std::optional<Precision> requested = {})
161 auto const p = detail::decomposition_precision(matrix, requested);
162 if constexpr (detail::can_use_qr_storage_as_workspace<MatrixTensor>())
164 return detail::qr_from_work_matrix(std::forward<BackendSelector>(selector), std::forward<MatrixTensor>(matrix),
p);
168 auto matrix_work = uni20::make_tensor<uni20::ColumnMajor>(matrix);
169 return detail::qr_from_work_matrix(std::forward<BackendSelector>(selector), std::move(matrix_work),
p);
174template <
class MatrixTensor>
176 detail::DecompositionScalar<uni20::tensor_element_t<MatrixTensor>> &&
177 (!std::is_lvalue_reference_v<MatrixTensor>) && (!std::is_const_v<std::remove_reference_t<MatrixTensor>>)
178[[nodiscard]]
auto qr(MatrixTensor&& matrix, std::optional<Precision> requested = {})
180 auto const p = detail::decomposition_precision(matrix, requested);
181 using work_type = std::conditional_t<detail::can_use_qr_storage_as_workspace<MatrixTensor>(),
182 std::remove_cvref_t<MatrixTensor>, detail::qr_matrix_tensor_t<MatrixTensor>>;
183 auto selector = detail::select_qr_backend<work_type>();
LAPACK backend for reduced dense real or complex QR factorization.
Exact arithmetic or an explicit binary working precision.
Definition core/precision.hpp:13
General-purpose owning tensor with immediate or deferred mdspan metadata.
Definition basic_tensor.hpp:109
MutableTensorView with a specified static rank.
Definition tensor/concepts.hpp:166
Tensor-level object that owns the storage described by its multidimensional view.
Definition tensor/concepts.hpp:116
Operation-tag backend dispatch over tensor-view operands.
int rows(std::FILE *stream)
Return the terminal row count associated with a C stream.
Definition terminal.cpp:482
Definition linalg/async.hpp:28
constexpr auto select_backend(Operation const &operation, FirstTensor const &, RestTensors const &...)
Select the default backend list for an operation on Tensor operands.
Definition backend_selector.hpp:252
auto qr(BackendSelector selector, async::Async< MatrixTensor > const &matrix, std::optional< Precision > requested={})
Schedule a preserving reduced real or complex QR factorization through an explicit selector.
Definition async/qr.hpp:109
void qr_factorization(BackendSelector &&selector, QTensor &&q, RTensor &&r, MatrixTensor &&matrix_work, std::optional< Precision > requested={})
Compute reduced QR in a destructive input workspace through an explicit selector.
Definition ops/qr.hpp:75
Definition data_table.cpp:8
Output & prepare_output(Output &output, RequiredExtents const &required)
Prepare a mutable tensor output with the specified shape.
Definition tensor/output.hpp:104
constexpr decltype(auto) mdspec_of(Tensor &tensor) noexcept(noexcept(tensor.mdspec()))
Return a tensor's immediate or descriptor-backed multidimensional metadata.
Definition tensor/concepts.hpp:50
std::remove_cv_t< typename tensor_mdspec_t< T >::element_type > tensor_element_t
Scalar value type exposed by a tensor-level object.
Definition tensor/concepts.hpp:126
std::ptrdiff_t index_type
Definition types.hpp:19
Owning reduced factors returned by qr.
Definition ops/qr.hpp:97
QTensor q
Definition ops/qr.hpp:101
RTensor r_tensor_type
Definition ops/qr.hpp:99
RTensor r
Definition ops/qr.hpp:102
QTensor q_tensor_type
Definition ops/qr.hpp:98
Concepts for tensor-level objects that resolve mdspan kernel operands.
Backend-dispatched tensor copies and inferred owning materialization.
Shape preparation contracts for tensor operation outputs.
General-purpose owning tensors with runtime extents.