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
25namespace toolbox {
26inline namespace util {
27
29constexpr bool is_pow2(std::size_t n) noexcept
30{
31 return n > 0 && (n & (n - 1)) == 0;
32}
33
35inline unsigned next_pow2(unsigned n) noexcept
36{
37 return n <= 1 ? 1 : 1 << (sizeof(n) * 8 - std::countl_zero(n - 1));
38}
39
41inline unsigned long next_pow2(unsigned long n) noexcept
42{
43 return n <= 1 ? 1 : 1 << (sizeof(n) * 8 - std::countl_zero(n - 1));
44}
45
46template <int BitsN>
47constexpr std::size_t ceil_pow2(std::size_t size) noexcept
48{
49 enum { Max = (1 << BitsN) - 1 };
50 return ((size + Max) >> BitsN) << BitsN;
51}
52
54 public:
55 constexpr VarAccum() noexcept = default;
56 ~VarAccum() = default;
57
58 // Copy.
59 constexpr VarAccum(const VarAccum&) noexcept = default;
60 VarAccum& operator=(const VarAccum&) noexcept = default;
61
62 // Move.
63 constexpr VarAccum(VarAccum&&) noexcept = default;
64 VarAccum& operator=(VarAccum&&) noexcept = default;
65
66 bool empty() const noexcept { return size_ == 0; }
67 std::size_t size() const noexcept { return size_; }
68 double mean() const noexcept { return mean_; }
69 double sum2() const noexcept { return sum2_; }
70 double min() const noexcept { return min_; }
71 double max() const noexcept { return max_; }
72
73 void clear() noexcept
74 {
75 size_ = 0;
76 mean_ = 0.0;
77 sum2_ = 0.0;
78 min_ = std::numeric_limits<double>::max();
79 max_ = std::numeric_limits<double>::min();
80 }
81 void append(double val) noexcept
82 {
83 ++size_;
84 double delta{val - mean_};
85 mean_ += delta / size_;
86 sum2_ += delta * (val - mean_);
87 min_ = std::min(min_, val);
88 max_ = std::max(max_, val);
89 }
90 template <typename... ArgsT>
91 void append(double first, ArgsT... args) noexcept
92 {
93 append(first);
94 // Recursively apply to tail.
95 append(args...);
96 }
97
98 private:
99 std::size_t size_{0};
100 double mean_{0.0};
101 double sum2_{0.0};
102 double min_{std::numeric_limits<double>::max()};
103 double max_{std::numeric_limits<double>::min()};
104};
105
106inline double var(const VarAccum& v) noexcept
107{
108 return v.size() > 1 ? v.sum2() / (v.size() - 1) : std::numeric_limits<double>::quiet_NaN();
109}
110
111inline double varp(const VarAccum& v) noexcept
112{
113 return !v.empty() ? v.sum2() / v.size() : std::numeric_limits<double>::quiet_NaN();
114}
115
116inline double stdev(const VarAccum& v) noexcept
117{
118 return std::sqrt(var(v));
119}
120
121inline double stdevp(const VarAccum& v) noexcept
122{
123 return std::sqrt(varp(v));
124}
125
126inline double zscore(double mean, double sd, double val) noexcept
127{
128 return (val - mean) / sd;
129}
130
131inline double pctile95(double mean, double sd) noexcept
132{
133 // NORMSINV(0.95) = 1.6448536
134 return mean + 1.6448536 * sd;
135}
136
137inline double pctile99(double mean, double sd) noexcept
138{
139 // NORMSINV(0.99) = 2.3263479
140 return mean + 2.3263479 * sd;
141}
142
143inline double pctile999(double mean, double sd) noexcept
144{
145 // NORMSINV(0.999) = 3.0902323
146 return mean + 3.0902323 * sd;
147}
148
150constexpr std::size_t ceil(std::size_t dividend, std::size_t divisor) noexcept
151{
152 return (dividend - 1) / divisor + 1;
153}
154
155} // namespace util
156} // namespace toolbox
157
158#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:73
constexpr VarAccum() noexcept=default
std::size_t size() const noexcept
Definition Math.hpp:67
double max() const noexcept
Definition Math.hpp:71
double sum2() const noexcept
Definition Math.hpp:69
double mean() const noexcept
Definition Math.hpp:68
void append(double val) noexcept
Definition Math.hpp:81
double min() const noexcept
Definition Math.hpp:70
void append(double first, ArgsT... args) noexcept
Definition Math.hpp:91
double mean(const Histogram &h) noexcept
Definition Utility.cpp:66
double var(const VarAccum &v) noexcept
Definition Math.hpp:106
double stdevp(const VarAccum &v) noexcept
Definition Math.hpp:121
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:35
double pctile99(double mean, double sd) noexcept
Definition Math.hpp:137
double pctile999(double mean, double sd) noexcept
Definition Math.hpp:143
double varp(const VarAccum &v) noexcept
Definition Math.hpp:111
constexpr std::size_t ceil(std::size_t dividend, std::size_t divisor) noexcept
Definition Math.hpp:150
double zscore(double mean, double sd, double val) noexcept
Definition Math.hpp:126
double pctile95(double mean, double sd) noexcept
Definition Math.hpp:131
double stdev(const VarAccum &v) noexcept
Definition Math.hpp:116
constexpr bool is_pow2(std::size_t n) noexcept
Definition Math.hpp:29
constexpr std::size_t ceil_pow2(std::size_t size) noexcept
Definition Math.hpp:47
constexpr auto bind() noexcept
Definition Slot.hpp:92