Loading...
Searching...
No Matches
gpr::numerics Namespace Reference

Functions

double posteriorIndeterminateBand (double scale) noexcept
bool exceedsThreshold (double value, double threshold) noexcept
bool withinThreshold (double value, double threshold) noexcept
bool clearlyGreater (double candidate, double incumbent) noexcept
double quantizeToPosteriorBand (double value) noexcept
void perturbPosterior (double &energy, Eigen::VectorXd &gradient)
double exp (double x) noexcept
double log (double x) noexcept
double lgamma (double x) noexcept
double erf (double x) noexcept
double erfc (double x) noexcept

Function Documentation

◆ clearlyGreater()

bool gpr::numerics::clearlyGreater ( double candidate,
double incumbent )
inlinenoexcept

Strict argmax with a scale-aware tie. The lower index keeps the incumbent unless candidate is larger by more than the band of the larger magnitude. +inf beats a finite incumbent; NaN does not.

Definition at line 49 of file DecisionBand.h.

50{
51 if (!std::isfinite(candidate)) {
52 return std::isinf(candidate) && candidate > 0.0 &&
53 !(std::isinf(incumbent) && incumbent > 0.0);
54 }
55 if (!std::isfinite(incumbent)) return !(incumbent > 0.0);
56 const double scale = std::max(std::abs(candidate), std::abs(incumbent));
57 return candidate > incumbent + posteriorIndeterminateBand(scale);
58}
double posteriorIndeterminateBand(double scale) noexcept

◆ erf()

double gpr::numerics::erf ( double x)
noexcept

Definition at line 88 of file PortableMath.cpp.

89{
90 const double magnitude = std::abs(x);
91 if (std::isnan(x) || x == 0.0) return x;
92 if (magnitude < 0.84375) {
93 if (magnitude < 0x1p-28) {
94 return magnitude < 0x1p-1015
95 ? 0.0625 * (16.0 * x +
96 (16.0 * fdlibm::kNearZeroCorrection) * x)
97 : x + fdlibm::kNearZeroCorrection * x;
98 }
99 return x + x * nearZeroRatio(x);
100 }
101 const bool negative = std::signbit(x);
102 if (magnitude < 1.25) {
103 const double ratio = nearOneRatio(magnitude);
104 return negative ? -fdlibm::kErfOneAnchor - ratio
105 : fdlibm::kErfOneAnchor + ratio;
106 }
107 if (magnitude >= 6.0) return negative ? -1.0 : 1.0;
108 const double tail = positiveErfcTail(magnitude, 0x4006db6eU);
109 return negative ? tail - 1.0 : 1.0 - tail;
110}
@ x
Definition Enums.h:28

◆ erfc()

double gpr::numerics::erfc ( double x)
noexcept

Definition at line 112 of file PortableMath.cpp.

113{
114 const double magnitude = std::abs(x);
115 if (std::isnan(x)) return x;
116 if (magnitude < 0.84375) {
117 if (magnitude < 0x1p-56) return 1.0 - x;
118 const double scaled = x * nearZeroRatio(x);
119 return x < 0.25 ? 1.0 - (x + scaled) : 0.5 - (scaled + (x - 0.5));
120 }
121 const bool negative = std::signbit(x);
122 if (magnitude < 1.25) {
123 const double ratio = nearOneRatio(magnitude);
124 return negative ? 1.0 + (fdlibm::kErfOneAnchor + ratio)
125 : (1.0 - fdlibm::kErfOneAnchor) - ratio;
126 }
127 if (magnitude >= 28.0 || (negative && magnitude >= 6.0)) {
128 return negative ? 2.0 : 0.0;
129 }
130 const double tail = positiveErfcTail(magnitude, 0x4006db6dU);
131 return negative ? 2.0 - tail : tail;
132}

◆ exceedsThreshold()

bool gpr::numerics::exceedsThreshold ( double value,
double threshold )
inlinenoexcept

True only when value clears threshold by more than the band. A last-bit overshoot does not open a drift break, a refit, or an acquisition. Non-finite inputs do not.

Definition at line 32 of file DecisionBand.h.

33{
34 if (!std::isfinite(value) || !std::isfinite(threshold)) return false;
35 return value > threshold + posteriorIndeterminateBand(threshold);
36}

◆ exp()

double gpr::numerics::exp ( double x)
noexcept

Definition at line 20 of file PortableMath.cpp.

21{
22 return gpr_core_exp(x);
23}
double gpr_core_exp(double)

◆ lgamma()

double gpr::numerics::lgamma ( double x)
noexcept

Definition at line 28 of file PortableMath.cpp.

29{
30 return gpr_core_lgamma(x);
31}
double gpr_core_lgamma(double)

◆ log()

double gpr::numerics::log ( double x)
noexcept

Definition at line 24 of file PortableMath.cpp.

25{
26 return gpr_core_log(x);
27}
double gpr_core_log(double)

◆ perturbPosterior()

void gpr::numerics::perturbPosterior ( double & energy,
Eigen::VectorXd & gradient )
inline

Multiply a posterior energy and gradient by 1 ± amplitude, then snap both onto the band so the sign draw is invisible to the search.

GPR_POSTERIOR_RELATIVE_NOISE is the amplitude (the harness uses 1e-12). GPR_POSTERIOR_NOISE_SEED selects the sign sequence. Unset amplitude leaves the posterior unchanged. The sequence is deterministic for a seed and resets with the process.

Definition at line 87 of file DecisionBand.h.

88{
89 const char* amplitude_text = std::getenv("GPR_POSTERIOR_RELATIVE_NOISE");
90 if (amplitude_text == nullptr || amplitude_text[0] == '\0') return;
91 const double amplitude = std::atof(amplitude_text);
92 if (!(amplitude > 0.0) || !std::isfinite(amplitude)) return;
93 const char* seed_text = std::getenv("GPR_POSTERIOR_NOISE_SEED");
94 const unsigned long long seed =
95 seed_text == nullptr ? 0ULL : std::strtoull(seed_text, nullptr, 10);
96 static unsigned long long draw = 0;
97 auto sign = [&]() {
98 unsigned long long mixed =
99 (seed << 32) ^ (++draw * 0x9E3779B97F4A7C15ULL);
100 mixed = (mixed ^ (mixed >> 30)) * 0xBF58476D1CE4E5B9ULL;
101 mixed = (mixed ^ (mixed >> 27)) * 0x94D049BB133111EBULL;
102 mixed ^= mixed >> 31;
103 return (mixed & 1ULL) != 0ULL ? 1.0 : -1.0;
104 };
105 if (std::isfinite(energy)) energy *= 1.0 + amplitude * sign();
106 for (Eigen::Index i = 0; i < gradient.size(); ++i) {
107 if (std::isfinite(gradient(i))) gradient(i) *= 1.0 + amplitude * sign();
108 }
109 energy = quantizeToPosteriorBand(energy);
110 for (Eigen::Index i = 0; i < gradient.size(); ++i)
111 gradient(i) = quantizeToPosteriorBand(gradient(i));
112}
double quantizeToPosteriorBand(double value) noexcept

◆ posteriorIndeterminateBand()

double gpr::numerics::posteriorIndeterminateBand ( double scale)
inlinenoexcept

Half-width of the indeterminate band on a posterior-derived comparison.

sqrt(eps) is the finite-difference step already used to probe the hyperparameter objective. A gap smaller than that fraction of the scale is the rounding of the probe, not a new discrete action. The scale is the threshold (or the larger of the two scores); it is not a tolerance fitted to a reaction.

Definition at line 22 of file DecisionBand.h.

23{
24 const double magnitude = std::abs(scale);
25 if (!std::isfinite(magnitude)) return 0.0;
26 return std::sqrt(std::numeric_limits<double>::epsilon()) * magnitude;
27}

◆ quantizeToPosteriorBand()

double gpr::numerics::quantizeToPosteriorBand ( double value)
inlinenoexcept

Snap a posterior component onto the indeterminate band.

A relative perturbation smaller than sqrt(eps) — the harness uses 1e-12 — cannot leave the bin except exactly on its edge. The search then sees one posterior, not a last-bit family of them.

Definition at line 65 of file DecisionBand.h.

66{
67 if (!std::isfinite(value) || value == 0.0) return value;
68 // Bin at the coordinate resolution positionsCoincide already treats
69 // as one geometry (a squared norm of 1e-12). The step is that
70 // fraction of the value's binary magnitude, so a 1e-12 relative
71 // perturbation stays in the bin. A step proportional to the value
72 // itself would map every number onto itself.
73 int exponent = 0;
74 std::frexp(value, &exponent);
75 const double step = 1e-6 * std::ldexp(1.0, exponent);
76 if (!(step > 0.0) || !std::isfinite(step)) return value;
77 return std::nearbyint(value / step) * step;
78}

◆ withinThreshold()

bool gpr::numerics::withinThreshold ( double value,
double threshold )
inlinenoexcept

Complement of exceedsThreshold for finite inputs: inside the threshold, including the band on its far side.

Definition at line 40 of file DecisionBand.h.

41{
42 if (!std::isfinite(value) || !std::isfinite(threshold)) return false;
43 return !exceedsThreshold(value, threshold);
44}
bool exceedsThreshold(double value, double threshold) noexcept