AutoPas  3.0.0
Loading...
Searching...
No Matches
Math.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <Eigen/Core>
10#include <cmath>
11#include <vector>
12
13namespace autopas::utils::Math {
14
18const double normalScale = 1. / std::sqrt(2 * M_PI);
19
30template <class Int_t, std::enable_if_t<std::is_integral_v<Int_t>, bool> = true>
31Int_t safeAdd(const Int_t &a, const Int_t &b, const Int_t &valUnderflow = std::numeric_limits<Int_t>::min(),
32 const Int_t &valOverflow = std::numeric_limits<Int_t>::max()) {
33 Int_t result;
34 bool overflow = __builtin_add_overflow(a, b, &result);
35 if (overflow) {
36 // if both args are negative this is an underflow.
37 if (a < 0 and b < 0) {
38 result = valUnderflow;
39 } else {
40 result = valOverflow;
41 }
42 }
43 return result;
44}
45
59template <class Float_t, std::enable_if_t<std::is_floating_point_v<Float_t>, bool> = true>
60Float_t safeAdd(const Float_t &a, const Float_t &b, const Float_t &valUnderflow = -std::numeric_limits<Float_t>::max(),
61 const Float_t &valOverflow = std::numeric_limits<Float_t>::max()) {
62 Float_t result = a + b;
63 if (std::isinf(result)) {
64 if (result > 0) {
65 result = valOverflow;
66 } else {
67 result = valUnderflow;
68 }
69 }
70 return result;
71}
72
83template <class Int_t, std::enable_if_t<std::is_integral_v<Int_t>, bool> = true>
84Int_t safeSub(const Int_t &a, const Int_t &b, const Int_t &valUnderflow = std::numeric_limits<Int_t>::min(),
85 const Int_t &valOverflow = std::numeric_limits<Int_t>::max()) {
86 Int_t result;
87 bool overflow = __builtin_sub_overflow(a, b, &result);
88 if (overflow) {
89 // underflow is only possible if we subtract a positive number. Everything else is an overflow.
90 if (b > 0) {
91 result = valUnderflow;
92 } else {
93 result = valOverflow;
94 }
95 }
96 return result;
97}
98
112template <class Float_t, std::enable_if_t<std::is_floating_point_v<Float_t>, bool> = true>
113Float_t safeSub(const Float_t &a, const Float_t &b, const Float_t &valUnderflow = -std::numeric_limits<Float_t>::max(),
114 const Float_t &valOverflow = std::numeric_limits<Float_t>::max()) {
115 Float_t result = a - b;
116 if (std::isinf(result)) {
117 if (result > 0) {
118 result = valOverflow;
119 } else {
120 result = valUnderflow;
121 }
122 }
123 return result;
124}
125
136template <class Int_t, std::enable_if_t<std::is_integral_v<Int_t>, bool> = true>
137Int_t safeMul(const Int_t &a, const Int_t &b, const Int_t &valUnderflow = std::numeric_limits<Int_t>::min(),
138 const Int_t &valOverflow = std::numeric_limits<Int_t>::max()) {
139 Int_t result;
140 bool overflow = __builtin_mul_overflow(a, b, &result);
141 if (overflow) {
142 // if exactly one arg is negative this is an underflow.
143 if ((a < 0) xor (b < 0)) {
144 result = valUnderflow;
145 } else {
146 result = valOverflow;
147 }
148 }
149 return result;
150}
151
165template <class Float_t, std::enable_if_t<std::is_floating_point_v<Float_t>, bool> = true>
166Float_t safeMul(const Float_t &a, const Float_t &b, const Float_t &valUnderflow = -std::numeric_limits<Float_t>::max(),
167 const Float_t &valOverflow = std::numeric_limits<Float_t>::max()) {
168 Float_t result = a * b;
169 if (std::isinf(result)) {
170 if (result > 0) {
171 result = valOverflow;
172 } else {
173 result = valUnderflow;
174 }
175 }
176 return result;
177}
178
186template <size_t exponent, class T>
187T pow(const T &base) {
188 if (exponent == 0) {
189 return 1;
190 }
191
192 T res = base;
193 // the compiler should unroll this loop
194 for (size_t i = 0; i < exponent - 1; ++i) {
195 res *= base;
196 }
197 return res;
198}
199
205double normalPDF(double x);
206
212double normalCDF(double x);
213
219double sigmoid(double x);
220
228bool isNearRel(double a, double b, double maxRelativeDifference = 1e-9);
229
237bool isNearAbs(double a, double b, double maxAbsoluteDifference);
238
245double roundFixed(double d, int fixedPrecision);
246
253double roundFloating(double d, int floatingPrecision);
254
260Eigen::VectorXd makeVectorXd(const std::vector<double> &elements);
261
267Eigen::VectorXi makeVectorXi(const std::vector<int> &elements);
268
269} // namespace autopas::utils::Math
Int_t safeAdd(const Int_t &a, const Int_t &b, const Int_t &valUnderflow=std::numeric_limits< Int_t >::min(), const Int_t &valOverflow=std::numeric_limits< Int_t >::max())
Addition function for integer types that is safe against over and underflow.
Definition: Math.h:31
Int_t safeSub(const Int_t &a, const Int_t &b, const Int_t &valUnderflow=std::numeric_limits< Int_t >::min(), const Int_t &valOverflow=std::numeric_limits< Int_t >::max())
Subtraction function for integer types that is safe against over and underflow.
Definition: Math.h:84
Int_t safeMul(const Int_t &a, const Int_t &b, const Int_t &valUnderflow=std::numeric_limits< Int_t >::min(), const Int_t &valOverflow=std::numeric_limits< Int_t >::max())
Multiplication function for integer types that is safe against over and underflow.
Definition: Math.h:137
T pow(const T &base)
No-overhead power function with exponent known at compile time.
Definition: Math.h:187
const double normalScale
Factor of PDF of standard normal distribution.
Definition: Math.h:18