11namespace gpr::detail {
16inline Eigen::VectorXd triangularPrecisionLowerBounds(
17 const Eigen::Ref<const Eigen::MatrixXd>& lower)
19 const Eigen::Index order = lower.rows();
20 if (order == 0 || lower.cols() != order || !lower.allFinite() ||
21 (lower.diagonal().array() <= 0.0).any())
25 volatile double normal_min = std::numeric_limits<double>::min();
26 volatile double half_min = normal_min * 0.5;
27 if (!(half_min > 0.0))
return {};
28 const double infinity = std::numeric_limits<double>::infinity();
29 const auto down = [&](
double value) {
30 return std::nextafter(value, -infinity);
32 const auto up = [&](
double value) {
33 return std::nextafter(value, infinity);
35 Eigen::VectorXd bounds = Eigen::VectorXd::Zero(order);
36 Eigen::VectorXd lo(order), hi(order);
37 for (Eigen::Index column = 0; column < order; ++column) {
38 for (Eigen::Index row = column; row < order; ++row) {
39 double sum_lo = 0.0, sum_hi = 0.0;
40 for (Eigen::Index k = column; k < row; ++k) {
41 const double coefficient = lower(row, k);
42 const double product_lo =
43 coefficient * (coefficient >= 0.0 ? lo(k) : hi(k));
44 const double product_hi =
45 coefficient * (coefficient >= 0.0 ? hi(k) : lo(k));
46 sum_lo = down(sum_lo + down(product_lo));
47 sum_hi = up(sum_hi + up(product_hi));
49 const double rhs = row == column ? 1.0 : 0.0;
50 lo(row) = down(down(rhs - sum_hi) / lower(row, row));
51 hi(row) = up(up(rhs - sum_lo) / lower(row, row));
52 if (!std::isfinite(lo(row)) || !std::isfinite(hi(row)))
return {};
53 const double minimum = lo(row) > 0.0 ? lo(row)
54 : hi(row) < 0.0 ? -hi(row)
56 const double square = std::max(0.0, down(minimum * minimum));
57 bounds(column) = std::max(0.0, down(bounds(column) + square));
60 return bounds.allFinite() ? bounds : Eigen::VectorXd{};
65inline bool precisionBoundsCertifyUnitCalibration(
const Eigen::VectorXd& alpha,
66 const Eigen::VectorXd& bounds)
68 if (alpha.size() < 2 || alpha.size() != bounds.size() ||
69 !alpha.allFinite() || !bounds.allFinite())
71 const double limit = std::sqrt(0.4549364231195727) *
72 (1.0 - 128.0 * std::numeric_limits<double>::epsilon());
73 Eigen::Index below = 0;
74 for (Eigen::Index i = 0; i < alpha.size(); ++i) {
75 if (bounds(i) > 0.0 &&
76 std::abs(alpha(i)) / std::sqrt(bounds(i)) < limit)
79 return below > alpha.size() / 2;