13template <
class S,
class... P>
14concept PrecisionArgs = (has_runtime_precision_v<S> &&
sizeof...(P) == 1 && (std::same_as<P, Precision> && ...)) ||
15 (!has_runtime_precision_v<S> &&
sizeof...(P) == 0);
19template <
class X,
class A,
class B>
23 std::same_as<typename X::value_type, typename A::value_type> &&
24 std::same_as<typename B::value_type, typename A::value_type>;
29 "least squares requires a finite nonnegative diagonal tolerance");
31template <
class Matrix>
bool finite(Matrix
const& a)
33 for (std::size_t j = 0; j < std::size_t(a.extent(1)); ++j)
34 for (std::size_t i = 0; i < std::size_t(a.extent(0)); ++i)
35 if (!
uni20::isfinite(
typename Matrix::value_type(a[i, j])))
return false;
39template <
class Matrix, Real R>
42 for (std::size_t k = 0; k < std::size_t(a.extent(1)); ++k)
47 else if (diagonal == 0)
49 else if (tolerance != 0 && diagonal /
scale <= tolerance)
51 if (!info.succeeded())
return false;
63 auto zero = scalar_at_precision<R>(0,
p);
64 norms.assign(nrhs, zero);
66 std::vector<S> error(m);
67 for (std::size_t j = 0; j < nrhs; ++j)
69 for (std::size_t i = 0; i < m; ++i)
71 for (std::size_t k = 0; k < n; ++k)
73 S
const&
value = solution[k, j];
74 for (std::size_t i = 0; i < m; ++i)
75 error[i] += av[i, k] *
value;
77 for (std::size_t i = 0; i < m; ++i)
91template <
class X,
class A,
class B,
class Factor>
95 using S =
typename A::value_type;
97 require_precision<S>(
p);
100 std::size_t m = a.extent(0), n = a.extent(1), nrhs = b.extent(1);
101 ERROR_IF(m < n || std::size_t(b.extent(0)) != m || std::size_t(x.extent(0)) != n ||
102 std::size_t(x.extent(1)) != nrhs || residuals.size() != nrhs,
103 "incompatible full-rank least-squares shapes");
108 auto as = aa.mdspan();
109 auto bs = ba.mdspan();
117 auto tolerance = scalar_at_precision<R>(options.relative_diagonal_tolerance,
p);
123 auto zero = scalar_at_precision<R>(0,
p);
125 for (std::size_t j = 0; j < n; ++j)
126 for (std::size_t i = 0; i < m; ++i)
137 factor(work, rhs,
p);
147 for (std::size_t j = 0; j < nrhs; ++j)
149 for (std::size_t i = 0; i < n; ++i)
150 solution[i, j] = rhs[i, j];
151 for (std::size_t k = n; k-- > 0;)
153 solution[k, j] /= work[k, k];
154 S
const&
value = solution[k, j];
155 for (std::size_t i = 0; i < k; ++i)
156 solution[i, j] -= work[i, k] *
value;
164 std::vector<R> norms;
171 auto xs = xa.mdspan();
173 std::copy_n(norms.begin(), nrhs, residuals.begin());
RAII leases that resolve device descriptors and tensor views to mdspans.
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
auto extent(size_type axis) const noexcept
Return one tensor extent.
Definition basic_tensor.hpp:720
auto mdspan() noexcept -> mdspan_type
Resolve a writable mdspan over the owned storage.
Definition basic_tensor.hpp:609
Mdspec metadata supporting host read acquisition.
Definition access.hpp:688
Mutable mdspec metadata supporting host write acquisition.
Definition access.hpp:694
Mutable mdspec-like type with a specified static rank.
Definition mdspan/concepts.hpp:487
An mdspec-like type with a specified static rank.
Definition mdspan/concepts.hpp:454
Concept for types that are either real or complex.
Definition scalar_concepts.hpp:87
Definition least_squares_common.hpp:20
Definition least_squares_common.hpp:14
locale_t value
Definition data_table.cpp:49
Operation-tag backend dispatch over tensor-view operands.
typename make_real< T >::type make_real_t
Alias for the underlying real-valued type of T.
Definition scalar_traits.hpp:168
constexpr bool isfinite(I const &x) noexcept
Returns whether an integer scalar is finite.
Definition math.hpp:121
Scalar math helper utilities.
Definition backends/cpu/least_squares.hpp:8
Precision working_precision()
Definition least_squares_common.hpp:16
bool residual_norms(DenseMatrix< S > const &av, DenseMatrix< S > const &bv, DenseMatrix< S > const &solution, std::vector< make_real_t< S > > &norms, Precision p)
Definition least_squares_common.hpp:58
bool finite(Matrix const &a)
Definition least_squares_common.hpp:31
KernelAttempt run(X &x, A &a, B &b, std::span< make_real_t< typename A::value_type > > residuals, LeastSquaresInfo &info, LeastSquaresOptions< make_real_t< typename A::value_type > > const &options, Precision p, Factor factor)
Definition least_squares_common.hpp:92
void require_options(LeastSquaresOptions< R > const &options)
Definition least_squares_common.hpp:26
bool check_diagonal(Matrix const &a, R const &scale, R const &tolerance, LeastSquaresInfo &info)
Definition least_squares_common.hpp:40
void unpack(Tensor const &source, Span &target)
Definition packed_matrix.hpp:33
auto pack(Span const &span, Precision p, bool read=true)
Definition packed_matrix.hpp:19
KernelAttempt
Result of one runtime kernel backend attempt.
Definition kernel_attempt.hpp:27
constexpr detail::scalar_math::abs_fn abs
Absolute value or complex magnitude, preserving the selected scalar implementation.
Definition math.hpp:459
constexpr detail::scalar_math::hypot_fn hypot
Euclidean length with scalar-specific overflow and underflow handling.
Definition math.hpp:507
Definition data_table.cpp:8
constexpr uninitialized_t uninitialized
Explicit allocation tag for overwrite outputs and scratch space.
Definition common/initialization.hpp:22
auto acquire_host_read_access_sync(Mdspan const &mdspan)
Acquire a directly host-accessible read-only mdspan.
Definition access.hpp:616
auto acquire_host_write_access_sync(Mdspan &mdspan)
Acquire a directly host-accessible writable mdspan.
Definition access.hpp:626
Logical host packing for value-converting BLAS and LAPACK adapters.
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
R relative_diagonal_tolerance
Reject nonzero abs(R[k,k])/max(abs(A)) at or below this finite, nonnegative tolerance.
Definition least_squares_info.hpp:41
Multiply one element value by a retained scalar factor.
Definition elementwise_functions.hpp:68
#define ERROR_IF(cond,...)
Definition trace.hpp:389