Loading...
Searching...
No Matches
CountedReal.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#pragma once
6
7#include <Eigen/Core>
8#include <cmath>
9#include <cstdint>
10
12
16 std::uint64_t add = 0;
17 std::uint64_t mul = 0;
18 std::uint64_t div = 0;
19 std::uint64_t exp = 0;
20 std::uint64_t sqrt = 0;
21 std::uint64_t pow = 0;
22
23 [[nodiscard]] std::uint64_t arithmetic() const noexcept
24 {
25 return add + mul + div;
26 }
27 [[nodiscard]] std::uint64_t total() const noexcept
28 {
29 return arithmetic() + exp + sqrt + pow;
30 }
31};
32
34{
35 thread_local OperationCounts counts;
36 return counts;
37}
38
44public:
45 CountedReal() = default;
46 CountedReal(double value) : value_(value) { } // NOLINT: implicit by design
47
48 [[nodiscard]] double value() const noexcept
49 {
50 return value_;
51 }
52
54 {
56 value_ += other.value_;
57 return *this;
58 }
60 {
62 value_ -= other.value_;
63 return *this;
64 }
66 {
68 value_ *= other.value_;
69 return *this;
70 }
72 {
74 value_ /= other.value_;
75 return *this;
76 }
78 {
79 return a += b;
80 }
82 {
83 return a -= b;
84 }
86 {
87 return a *= b;
88 }
90 {
91 return a /= b;
92 }
94 {
96 return CountedReal(-a.value_);
97 }
99 {
100 return a;
101 }
102 friend bool operator<(const CountedReal& a, const CountedReal& b)
103 {
104 return a.value_ < b.value_;
105 }
106 friend bool operator>(const CountedReal& a, const CountedReal& b)
107 {
108 return a.value_ > b.value_;
109 }
110 friend bool operator<=(const CountedReal& a, const CountedReal& b)
111 {
112 return a.value_ <= b.value_;
113 }
114 friend bool operator>=(const CountedReal& a, const CountedReal& b)
115 {
116 return a.value_ >= b.value_;
117 }
118 friend bool operator==(const CountedReal& a, const CountedReal& b)
119 {
120 return a.value_ == b.value_;
121 }
122 friend bool operator!=(const CountedReal& a, const CountedReal& b)
123 {
124 return a.value_ != b.value_;
125 }
126 friend CountedReal exp(const CountedReal& a)
127 {
129 return CountedReal(std::exp(a.value_));
130 }
131 friend CountedReal sqrt(const CountedReal& a)
132 {
134 return CountedReal(std::sqrt(a.value_));
135 }
136 friend CountedReal pow(const CountedReal& a, const CountedReal& b)
137 {
139 return CountedReal(std::pow(a.value_, b.value_));
140 }
141 friend CountedReal abs(const CountedReal& a)
142 {
143 return CountedReal(std::abs(a.value_));
144 }
145 friend bool isfinite(const CountedReal& a)
146 {
147 return std::isfinite(a.value_);
148 }
149
150private:
151 double value_ = 0.0;
152};
153
154inline double plainValue(double x) noexcept
155{
156 return x;
157}
158
159inline double plainValue(const CountedReal& x) noexcept
160{
161 return x.value();
162}
163
164} // namespace gpr::derivatives
165
166namespace Eigen {
167
168template <>
169struct NumTraits<gpr::derivatives::CountedReal> : NumTraits<double> {
174 enum {
182 };
183};
184
185} // namespace Eigen
@ x
Definition Enums.h:28
friend bool operator!=(const CountedReal &a, const CountedReal &b)
CountedReal & operator+=(const CountedReal &other)
Definition CountedReal.h:53
CountedReal & operator/=(const CountedReal &other)
Definition CountedReal.h:71
friend CountedReal sqrt(const CountedReal &a)
friend CountedReal operator-(const CountedReal &a)
Definition CountedReal.h:93
friend bool operator>(const CountedReal &a, const CountedReal &b)
friend CountedReal operator-(CountedReal a, const CountedReal &b)
Definition CountedReal.h:81
CountedReal & operator-=(const CountedReal &other)
Definition CountedReal.h:59
CountedReal & operator*=(const CountedReal &other)
Definition CountedReal.h:65
friend CountedReal operator*(CountedReal a, const CountedReal &b)
Definition CountedReal.h:85
friend bool isfinite(const CountedReal &a)
friend CountedReal operator+(const CountedReal &a)
Definition CountedReal.h:98
friend CountedReal operator/(CountedReal a, const CountedReal &b)
Definition CountedReal.h:89
friend CountedReal abs(const CountedReal &a)
friend bool operator<(const CountedReal &a, const CountedReal &b)
friend CountedReal pow(const CountedReal &a, const CountedReal &b)
friend bool operator==(const CountedReal &a, const CountedReal &b)
friend CountedReal exp(const CountedReal &a)
friend CountedReal operator+(CountedReal a, const CountedReal &b)
Definition CountedReal.h:77
friend bool operator<=(const CountedReal &a, const CountedReal &b)
double value() const noexcept
Definition CountedReal.h:48
friend bool operator>=(const CountedReal &a, const CountedReal &b)
double plainValue(double x) noexcept
OperationCounts & operationCounts() noexcept
Definition CountedReal.h:33
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
std::uint64_t total() const noexcept
Definition CountedReal.h:27
std::uint64_t arithmetic() const noexcept
Definition CountedReal.h:23
std::uint64_t add
additions, subtractions and negations
Definition CountedReal.h:16