Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
ops/svd_least_squares.hpp
Go to the documentation of this file.
1#pragma once
2
10
11namespace uni20::linalg
12{
14template <RealOrComplex S> struct SvdLeastSquaresResult
15{
17 std::optional<DenseMatrix<S>> solution = {};
19 std::vector<make_real_t<S>> residual_norms = {};
21 std::optional<SvdLeastSquaresDiagnostics<make_real_t<S>>> diagnostics = {};
22};
23
31template <KernelBackendSelector Backend, RankedTensorView<2> A, RankedTensorView<2> B>
32 requires RealOrComplex<tensor_element_t<A>> && std::same_as<tensor_element_t<A>, tensor_element_t<B>>
33[[nodiscard]] auto
34svd_least_squares_with_info(Backend&& backend, A const& a, B const& b, Precision p,
36{
37 using S = tensor_element_t<A>;
38 require_precision<S>(p);
39 (void)p.bit_count();
40 ERROR_IF(a.extent(0) != b.extent(0), "SVD least squares requires matching RHS rows");
42 SvdLeastSquaresResult<S> result;
43 result.solution.emplace(uninitialized, a.extent(1), b.extent(1), p);
44 result.residual_norms.resize(b.extent(1));
45 result.diagnostics.emplace();
46 auto ad = mdspec_of(a);
47 auto bd = mdspec_of(b);
48 auto xd = mdspec_of(*result.solution);
49 detail::dispatch_at_precision<S>(std::forward<Backend>(backend), svd_least_squares_op{}, p, xd, ad, bd,
50 std::span(result.residual_norms), result.info, *result.diagnostics, options);
51 if (!result.info.succeeded())
52 {
53 result.solution.reset();
54 result.residual_norms.clear();
55 result.diagnostics.reset();
56 }
57 return result;
58}
59
61template <KernelBackendSelector Backend, RankedTensorView<2> A, RankedTensorView<2> B>
62 requires RealOrComplex<tensor_element_t<A>> && std::same_as<tensor_element_t<A>, tensor_element_t<B>>
63[[nodiscard]] auto
64svd_least_squares_with_info(Backend&& backend, A const& a, B const& b,
66{
67 return svd_least_squares_with_info(std::forward<Backend>(backend), a, b, common_default_precision(a, b), options);
68}
69
71template <RankedTensorView<2> A, RankedTensorView<2> B>
72 requires RealOrComplex<tensor_element_t<A>> && std::same_as<tensor_element_t<A>, tensor_element_t<B>>
73[[nodiscard]] auto
74svd_least_squares_with_info(A const& a, B const& b, Precision p,
76{
78 p, options);
79}
80
82template <RankedTensorView<2> A, RankedTensorView<2> B>
83 requires RealOrComplex<tensor_element_t<A>> && std::same_as<tensor_element_t<A>, tensor_element_t<B>>
84[[nodiscard]] auto
85svd_least_squares_with_info(A const& a, B const& b,
87{
88 return svd_least_squares_with_info(a, b, common_default_precision(a, b), options);
89}
90
92template <class... Args>
93 requires requires(Args&&... args) { svd_least_squares_with_info(std::forward<Args>(args)...); }
94[[nodiscard]] auto svd_least_squares(Args&&... args)
95{
96 auto result = svd_least_squares_with_info(std::forward<Args>(args)...);
97 ERROR_IF(!result.info.succeeded(), "SVD least squares failed", static_cast<int>(result.info.status));
98 return std::move(*result.solution);
99}
100} // 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_svd_options(SvdLeastSquaresOptions< R > const &options)
Definition backends/lapack/svd_least_squares.hpp:28
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 svd_least_squares_with_info(Backend &&backend, A const &a, B const &b, Precision p, SvdLeastSquaresOptions< make_real_t< tensor_element_t< A > > > const &options={})
Compute a minimum-norm solution after singular-value truncation for rectangular A.
Definition ops/svd_least_squares.hpp:34
auto svd_least_squares(Args &&... args)
Return a preserving truncated-SVD solution; numerical failure uses the ordinary terminal error policy...
Definition ops/svd_least_squares.hpp:94
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
Singular-value truncation policy for minimum-norm least squares.
Definition svd_least_squares_info.hpp:10
Preserving least-squares result; solution and residuals are published only on success.
Definition ops/svd_least_squares.hpp:15
LeastSquaresInfo info
Definition ops/svd_least_squares.hpp:16
std::optional< SvdLeastSquaresDiagnostics< make_real_t< S > > > diagnostics
Present exactly on success, including rank zero.
Definition ops/svd_least_squares.hpp:21
std::optional< DenseMatrix< S > > solution
Definition ops/svd_least_squares.hpp:17
std::vector< make_real_t< S > > residual_norms
Recomputed Euclidean residual norm per RHS, in working precision; not an error bound.
Definition ops/svd_least_squares.hpp:19
#define ERROR_IF(cond,...)
Definition trace.hpp:389