Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
ops/least_squares.hpp
Go to the documentation of this file.
1#pragma once
2
10
11namespace uni20::linalg
12{
14template <RealOrComplex S> struct LeastSquaresResult
15{
17 std::optional<DenseMatrix<S>> solution = {};
19 std::vector<make_real_t<S>> residual_norms = {};
20};
21
29template <KernelBackendSelector Backend, RankedTensorView<2> A, RankedTensorView<2> B>
30 requires RealOrComplex<tensor_element_t<A>> && std::same_as<tensor_element_t<A>, tensor_element_t<B>>
31[[nodiscard]] auto least_squares_with_info(Backend&& backend, A const& a, B const& b, Precision p,
33{
34 using S = tensor_element_t<A>;
35 require_precision<S>(p);
36 (void)p.bit_count();
37 ERROR_IF(a.extent(0) < a.extent(1) || a.extent(0) != b.extent(0),
38 "least squares requires m >= n and matching RHS rows");
40 LeastSquaresResult<S> result;
41 result.solution.emplace(uninitialized, a.extent(1), b.extent(1), p);
42 result.residual_norms.resize(b.extent(1));
43 auto ad = mdspec_of(a);
44 auto bd = mdspec_of(b);
45 auto xd = mdspec_of(*result.solution);
46 detail::dispatch_at_precision<S>(std::forward<Backend>(backend), least_squares_op{}, p, xd, ad, bd,
47 std::span(result.residual_norms), result.info, options);
48 if (!result.info.succeeded())
49 {
50 result.solution.reset();
51 result.residual_norms.clear();
52 }
53 return result;
54}
55
57template <KernelBackendSelector Backend, RankedTensorView<2> A, RankedTensorView<2> B>
58 requires RealOrComplex<tensor_element_t<A>> && std::same_as<tensor_element_t<A>, tensor_element_t<B>>
59[[nodiscard]] auto least_squares_with_info(Backend&& backend, A const& a, B const& b,
61{
62 return least_squares_with_info(std::forward<Backend>(backend), a, b, common_default_precision(a, b), options);
63}
64
66template <RankedTensorView<2> A, RankedTensorView<2> B>
67 requires RealOrComplex<tensor_element_t<A>> && std::same_as<tensor_element_t<A>, tensor_element_t<B>>
68[[nodiscard]] auto least_squares_with_info(A const& a, B const& b, Precision p,
70{
72 options);
73}
74
76template <RankedTensorView<2> A, RankedTensorView<2> B>
77 requires RealOrComplex<tensor_element_t<A>> && std::same_as<tensor_element_t<A>, tensor_element_t<B>>
78[[nodiscard]] auto least_squares_with_info(A const& a, B const& b,
80{
81 return least_squares_with_info(a, b, common_default_precision(a, b), options);
82}
83
85template <class... Args>
86 requires requires(Args&&... args) { least_squares_with_info(std::forward<Args>(args)...); }
87[[nodiscard]] auto least_squares(Args&&... args)
88{
89 auto result = least_squares_with_info(std::forward<Args>(args)...);
90 ERROR_IF(!result.info.succeeded(), "least squares failed", static_cast<int>(result.info.status));
91 return std::move(*result.solution);
92}
93} // namespace uni20::linalg
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
Concept for types that are either real or complex.
Definition scalar_concepts.hpp:87
typename make_real< T >::type make_real_t
Alias for the underlying real-valued type of T.
Definition scalar_traits.hpp:168
void require_options(LeastSquaresOptions< R > const &options)
Definition least_squares_common.hpp:26
Definition linalg/async.hpp:28
constexpr auto select_backend_for(Operation const &operation)
Resolve the immutable backend selector for Tensor operand types.
Definition backend_selector.hpp:201
auto least_squares_with_info(Backend &&backend, A const &a, B const &b, Precision p, LeastSquaresOptions< make_real_t< tensor_element_t< A > > > const &options={})
Minimize each column's Euclidean residual for m-by-n A with m >= n.
Definition ops/least_squares.hpp:31
auto least_squares(Args &&... args)
Return a preserving least-squares solution; numerical failure uses the ordinary terminal error policy...
Definition ops/least_squares.hpp:87
Definition data_table.cpp:8
constexpr uninitialized_t uninitialized
Explicit allocation tag for overwrite outputs and scratch space.
Definition common/initialization.hpp:22
constexpr Precision common_default_precision(A const &a, B const &b)
Select a binary operation's default without inspecting element precisions.
Definition tensor/precision.hpp:43
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
Completed numerical attempt; success does not certify rank or conditioning.
Definition least_squares_info.hpp:26
Full-rank QR rejection policy, independent of any physical convergence criterion.
Definition least_squares_info.hpp:37
Preserving least-squares result; solution and residuals are published only on success.
Definition ops/least_squares.hpp:15
std::vector< make_real_t< S > > residual_norms
Recomputed Euclidean residual norm per RHS, in working precision; not an error bound.
Definition ops/least_squares.hpp:19
LeastSquaresInfo info
Definition ops/least_squares.hpp:16
std::optional< DenseMatrix< S > > solution
Definition ops/least_squares.hpp:17
#define ERROR_IF(cond,...)
Definition trace.hpp:389