Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Math.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2022 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#ifndef TOOLBOX_UTIL_MATH_HPP
18#define TOOLBOX_UTIL_MATH_HPP
19
20#include <toolbox/Config.h>
21
22#include <bit>
23#include <cmath>
24#include <array>
25#include <cassert>
26#include <cstdint>
27
28namespace toolbox {
29inline namespace util {
30
32constexpr bool is_pow2(std::size_t n) noexcept
33{
34 return n > 0 && (n & (n - 1)) == 0;
35}
36
38inline unsigned next_pow2(unsigned n) noexcept
39{
40 return n <= 1 ? 1 : 1 << (sizeof(n) * 8 - std::countl_zero(n - 1));
41}
42
44inline unsigned long next_pow2(unsigned long n) noexcept
45{
46 return n <= 1 ? 1 : 1 << (sizeof(n) * 8 - std::countl_zero(n - 1));
47}
48
49template <int BitsN>
50constexpr std::size_t ceil_pow2(std::size_t size) noexcept
51{
52 enum { Max = (1 << BitsN) - 1 };
53 return ((size + Max) >> BitsN) << BitsN;
54}
55
57 public:
58 constexpr VarAccum() noexcept = default;
59 ~VarAccum() = default;
60
61 // Copy.
62 constexpr VarAccum(const VarAccum&) noexcept = default;
63 VarAccum& operator=(const VarAccum&) noexcept = default;
64
65 // Move.
66 constexpr VarAccum(VarAccum&&) noexcept = default;
67 VarAccum& operator=(VarAccum&&) noexcept = default;
68
69 bool empty() const noexcept { return size_ == 0; }
70 std::size_t size() const noexcept { return size_; }
71 double mean() const noexcept { return mean_; }
72 double sum2() const noexcept { return sum2_; }
73 double min() const noexcept { return min_; }
74 double max() const noexcept { return max_; }
75
76 void clear() noexcept
77 {
78 size_ = 0;
79 mean_ = 0.0;
80 sum2_ = 0.0;
81 min_ = std::numeric_limits<double>::max();
82 max_ = std::numeric_limits<double>::min();
83 }
84 void append(double val) noexcept
85 {
86 ++size_;
87 double delta{val - mean_};
88 mean_ += delta / size_;
89 sum2_ += delta * (val - mean_);
90 min_ = std::min(min_, val);
91 max_ = std::max(max_, val);
92 }
93 template <typename... ArgsT>
94 void append(double first, ArgsT... args) noexcept
95 {
96 append(first);
97 // Recursively apply to tail.
98 append(args...);
99 }
100
101 private:
102 std::size_t size_{0};
103 double mean_{0.0};
104 double sum2_{0.0};
105 double min_{std::numeric_limits<double>::max()};
106 double max_{std::numeric_limits<double>::min()};
107};
108
109inline double var(const VarAccum& v) noexcept
110{
111 return v.size() > 1 ? v.sum2() / (v.size() - 1) : std::numeric_limits<double>::quiet_NaN();
112}
113
114inline double varp(const VarAccum& v) noexcept
115{
116 return !v.empty() ? v.sum2() / v.size() : std::numeric_limits<double>::quiet_NaN();
117}
118
119inline double stdev(const VarAccum& v) noexcept
120{
121 return std::sqrt(var(v));
122}
123
124inline double stdevp(const VarAccum& v) noexcept
125{
126 return std::sqrt(varp(v));
127}
128
129inline double zscore(double mean, double sd, double val) noexcept
130{
131 return (val - mean) / sd;
132}
133
134inline double pctile95(double mean, double sd) noexcept
135{
136 // NORMSINV(0.95) = 1.6448536
137 return mean + 1.6448536 * sd;
138}
139
140inline double pctile99(double mean, double sd) noexcept
141{
142 // NORMSINV(0.99) = 2.3263479
143 return mean + 2.3263479 * sd;
144}
145
146inline double pctile999(double mean, double sd) noexcept
147{
148 // NORMSINV(0.999) = 3.0902323
149 return mean + 3.0902323 * sd;
150}
151
153constexpr std::size_t ceil(std::size_t dividend, std::size_t divisor) noexcept
154{
155 return (dividend - 1) / divisor + 1;
156}
157
161constexpr std::uint64_t pow10(int n) noexcept {
162 constexpr std::array<std::uint64_t, 20> DecimalPowers = {
163 1ULL,
164 10ULL,
165 100ULL,
166 1000ULL,
167 10000ULL,
168 100000ULL,
169 1000000ULL,
170 10000000ULL,
171 100000000ULL,
172 1000000000ULL,
173 10000000000ULL,
174 100000000000ULL,
175 1000000000000ULL,
176 10000000000000ULL,
177 100000000000000ULL,
178 1000000000000000ULL,
179 10000000000000000ULL,
180 100000000000000000ULL,
181 1000000000000000000ULL,
182 10000000000000000000ULL
183 };
184
185 assert(n >= 0 && static_cast<std::size_t>(n) < DecimalPowers.size());
186 return DecimalPowers[n];
187};
188
189} // namespace util
190} // namespace toolbox
191
192#endif // TOOLBOX_UTIL_MATH_HPP
#define TOOLBOX_API
Definition Config.h:39
v append(1345, 1301, 1368, 1322, 1310, 1370, 1318, 1350, 1303, 1299)
void clear() noexcept
Definition Math.hpp:76
constexpr VarAccum() noexcept=default
std::size_t size() const noexcept
Definition Math.hpp:70
double max() const noexcept
Definition Math.hpp:74
double sum2() const noexcept
Definition Math.hpp:72
double mean() const noexcept
Definition Math.hpp:71
void append(double val) noexcept
Definition Math.hpp:84
double min() const noexcept
Definition Math.hpp:73
void append(double first, ArgsT... args) noexcept
Definition Math.hpp:94
double mean(const Histogram &h) noexcept
Definition Utility.cpp:62
double var(const VarAccum &v) noexcept
Definition Math.hpp:109
double stdevp(const VarAccum &v) noexcept
Definition Math.hpp:124
constexpr std::size_t size(const detail::Struct< detail::Member< TagsT, ValuesT >... > &s)
Definition Struct.hpp:98
unsigned next_pow2(unsigned n) noexcept
Definition Math.hpp:38
double pctile99(double mean, double sd) noexcept
Definition Math.hpp:140
double pctile999(double mean, double sd) noexcept
Definition Math.hpp:146
double varp(const VarAccum &v) noexcept
Definition Math.hpp:114
constexpr std::uint64_t pow10(int n) noexcept
Definition Math.hpp:161
constexpr std::size_t ceil(std::size_t dividend, std::size_t divisor) noexcept
Definition Math.hpp:153
double zscore(double mean, double sd, double val) noexcept
Definition Math.hpp:129
double pctile95(double mean, double sd) noexcept
Definition Math.hpp:134
double stdev(const VarAccum &v) noexcept
Definition Math.hpp:119
constexpr bool is_pow2(std::size_t n) noexcept
Definition Math.hpp:32
constexpr std::size_t ceil_pow2(std::size_t size) noexcept
Definition Math.hpp:50
constexpr auto bind() noexcept
Definition Slot.hpp:92