22 std::vector<SolveError<make_real_t<S>>>
columns = {};
25namespace refinement_detail
31template <RealOrComplex To, RealOrComplex From>
38 return To(convert<R>(
value.real(),
p), convert<R>(
value.imag(),
p));
40 else if constexpr (has_runtime_precision_v<To>)
41 return scalar_at_precision<To>(
value,
p);
44 require_precision<To>(
p);
45 return static_cast<To
>(
value);
51template <RealOrComplex To, HostMatrix A>
58 auto values = access.mdspan();
60 for (std::size_t j = 0; j < std::size_t(input.extent(1)); ++j)
61 for (std::size_t i = 0; i < std::size_t(input.extent(0)); ++i)
63 From
value(values[i, j]);
69 auto converted = convert<To>(
value,
p);
75 output[i, j] = std::move(converted);
80template <RealOrComplex S>
85 auto const zero = scalar_at_precision<R>(0,
p);
86 auto const szero = scalar_at_precision<S>(0,
p);
90 std::vector<R> row_sums(n, zero);
92 for (std::size_t k = 0; k < n; ++k)
93 for (std::size_t i = 0; i < n; ++i)
95 for (
auto const&
sum : row_sums)
96 a_norm = std::max(a_norm,
sum);
102 for (std::size_t j = 0; j < count; ++j)
104 R x_norm = zero, b_norm = zero, r_norm = zero, componentwise = zero;
105 std::vector<R> denominator(n, zero);
106 for (std::size_t i = 0; i < n; ++i)
108 residual[i, j] = b[i, j];
110 b_norm = std::max(b_norm, denominator[i]);
111 x_norm = std::max(x_norm, R(
math::abs(x[i, j])));
115 for (std::size_t k = 0; k < n; ++k)
118 for (std::size_t i = 0; i < n; ++i)
120 residual[i, j] -= a[i, k] * x[k, j];
121 denominator[i] +=
math::abs(a[i, k]) * x_magnitude;
124 for (std::size_t i = 0; i < n; ++i)
132 r_norm = std::max(r_norm, magnitude);
135 R ratio = denominator[i] == 0 ? zero : R(magnitude / denominator[i]);
136 if (denominator[i] == 0 && residual[i, j] != szero)
141 componentwise = std::max(componentwise, ratio);
143 R denominator_norm = a_norm * x_norm + b_norm;
149 R normwise = denominator_norm == 0 ? zero : R(r_norm / denominator_norm);
150 result.
columns.push_back({r_norm, normwise, componentwise});
152 result.
residual.emplace(std::move(residual));
166template <
RealOrComplex S, refinement_detail::HostMatrix A, refinement_detail::HostMatrix B,
167 refinement_detail::HostMatrix X>
172 require_precision<S>(
p);
174 ERROR_IF(a.extent(0) != a.extent(1) || b.extent(0) != a.extent(0) || x.extent(0) != a.extent(1) ||
175 x.extent(1) != b.extent(1),
176 "solve diagnostics require square A and matching RHS/solution shapes");
178 if (a.extent(0) == 0 || b.extent(1) == 0)
180 auto zero = scalar_at_precision<make_real_t<S>>(0,
p);
181 result.
residual.emplace(a.extent(0), b.extent(1),
p);
182 result.
columns.assign(b.extent(1), {zero, zero, zero});
185 auto av = refinement_detail::pack<S>(a,
p, result.
info);
186 if (!av)
return result;
187 auto bv = refinement_detail::pack<S>(b,
p, result.
info);
188 if (!bv)
return result;
189 auto xv = refinement_detail::pack<S>(x,
p, result.
info);
190 if (!xv)
return result;
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
Concept for complex scalar types.
Definition scalar_concepts.hpp:75
Mdspec metadata supporting host read acquisition.
Definition access.hpp:688
TensorView whose readable mdspec has a specified static rank.
Definition tensor/concepts.hpp:149
Concept for types that are either real or complex.
Definition scalar_concepts.hpp:87
Definition solve_diagnostics.hpp:28
locale_t value
Definition data_table.cpp:49
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.
SolveDiagnosticsResult< S > evaluate(DenseMatrix< S > const &a, DenseMatrix< S > const &b, DenseMatrix< S > const &x, Precision p)
Definition solve_diagnostics.hpp:81
To convert(From const &value, Precision p)
Definition solve_diagnostics.hpp:33
Definition linalg/async.hpp:28
@ nonfinite_input
An input coefficient or RHS component was nonfinite; both workspaces are preserved.
@ nonfinite_result
A factor, solution, reduction, or required magnitude became nonfinite.
SolveDiagnosticsResult< S > solve_diagnostics_with_info(A const &a, B const &b, X const &x, Precision p)
Evaluate b - A*x and normwise/componentwise backward error in scalar S at p.
Definition solve_diagnostics.hpp:170
constexpr detail::scalar_math::abs_fn abs
Absolute value or complex magnitude, preserving the selected scalar implementation.
Definition math.hpp:459
Definition data_table.cpp:8
constexpr uninitialized_t uninitialized
Explicit allocation tag for overwrite outputs and scratch space.
Definition common/initialization.hpp:22
void sum(BackendSelector selector, async::Async< OutputTensor > &output, async::Async< InputTensor > const &input)
Schedule a full sum into an explicit async scalar tensor.
Definition linalg/async/reductions.hpp:309
auto acquire_host_read_access_sync(Mdspan const &mdspan)
Acquire a directly host-accessible read-only mdspan.
Definition access.hpp:616
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
Recomputed residual and per-RHS diagnostics, published together only on success.
Definition solve_diagnostics.hpp:19
SolveInfo info
Definition solve_diagnostics.hpp:20
std::optional< DenseMatrix< S > > residual
Definition solve_diagnostics.hpp:21
std::vector< SolveError< make_real_t< S > > > columns
Definition solve_diagnostics.hpp:22
Diagnostics written by a completed numerical attempt, independent of dispatch.
Definition solve_info.hpp:41
SolveStatus status
Definition solve_info.hpp:42
bool succeeded() const noexcept
Report successful completion of the numerical operation.
Definition solve_info.hpp:49
General-purpose owning tensors with runtime extents.
#define ERROR_IF(cond,...)
Definition trace.hpp:389