Loading...
Searching...
No Matches
TrustRegionSegment.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
20
21#ifndef GPRIDGEIZATION_TRUSTREGIONSEGMENT_H_
22#define GPRIDGEIZATION_TRUSTREGIONSEGMENT_H_
23
24#include <Eigen/Dense>
25#include <algorithm>
26#include <cmath>
27#include <utility>
28#include <vector>
29
30namespace gpr {
31namespace optim {
32namespace detail {
33
36inline double maxAtomDisplacement(const Eigen::VectorXd& delta)
37{
38 const Eigen::Index d = delta.size();
39 const Eigen::Index n_atoms = d / 3;
40 double max_disp = 0.0;
41 for (Eigen::Index a = 0; a < n_atoms; ++a) {
42 max_disp = std::max(max_disp, delta.segment<3>(3 * a).norm());
43 }
44 if (3 * n_atoms < d) {
45 max_disp = std::max(max_disp, delta.tail(d - 3 * n_atoms).norm());
46 }
47 return max_disp;
48}
49
52inline bool segmentBallInterval(const Eigen::VectorXd& from,
53 const Eigen::VectorXd& step,
54 const Eigen::VectorXd& center, double radius,
55 double& lo, double& hi)
56{
57 const Eigen::Index d = from.size();
58 lo = 0.0;
59 hi = 1.0;
60 for (Eigen::Index begin = 0; begin < d; begin += 3) {
61 const Eigen::Index width = std::min<Eigen::Index>(3, d - begin);
62 const Eigen::VectorXd u =
63 from.segment(begin, width) - center.segment(begin, width);
64 const Eigen::VectorXd v = step.segment(begin, width);
65 const double a = v.squaredNorm();
66 const double b = u.dot(v);
67 const double c = u.squaredNorm() - radius * radius;
68 if (a == 0.0) {
69 if (c > 0.0) return false;
70 continue;
71 }
72 const double disc = b * b - a * c;
73 if (disc < 0.0) return false;
74 // The two roots of a s^2 + 2 b s + c, without the cancellation of the
75 // textbook form when b dominates the discriminant.
76 const double q = -(b + std::copysign(std::sqrt(disc), b));
77 double r0 = q / a;
78 double r1 = (q != 0.0) ? c / q : r0;
79 if (r0 > r1) std::swap(r0, r1);
80 lo = std::max(lo, r0);
81 hi = std::min(hi, r1);
82 if (lo > hi) return false;
83 }
84 return true;
85}
86
92template <typename Centers>
93double trustCoveredFraction(const Centers& centers, const Eigen::VectorXd& from,
94 const Eigen::VectorXd& to, double radius,
95 double join_tolerance = 1.0e-12)
96{
97 const Eigen::VectorXd step = to - from;
98 std::vector<std::pair<double, double>> intervals;
99 intervals.reserve(static_cast<std::size_t>(centers.rows()));
100 for (Eigen::Index r = 0; r < centers.rows(); ++r) {
101 const Eigen::VectorXd center = centers.row(r).transpose();
102 double lo = 0.0;
103 double hi = 0.0;
104 if (segmentBallInterval(from, step, center, radius, lo, hi)) {
105 intervals.emplace_back(lo, hi);
106 }
107 }
108 std::sort(intervals.begin(), intervals.end());
109 double reach = 0.0;
110 for (const auto& [lo, hi]: intervals) {
111 if (lo > reach + join_tolerance) break;
112 reach = std::max(reach, hi);
113 if (reach >= 1.0 - join_tolerance) return 1.0;
114 }
115 return reach;
116}
117
118} // namespace detail
119} // namespace optim
120} // namespace gpr
121
122#endif // GPRIDGEIZATION_TRUSTREGIONSEGMENT_H_
double maxAtomDisplacement(const Eigen::VectorXd &delta)
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16