Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
Math.h
Go to the documentation of this file.
1
32#pragma once
33
34#include <ogdf/basic/Array.h>
35#include <ogdf/basic/basic.h>
36
37#include <numeric>
38
39namespace ogdf {
40namespace Math {
41namespace internal {
42
43template<typename T, int size>
44inline typename std::enable_if<size == 1, T>::type nextPower2(T x) {
45 return x;
46}
47
50template<typename T, int size>
51inline typename std::enable_if<size != 1, T>::type nextPower2(T x) {
52 x = nextPower2<T, size / 2>(x);
53 return x | (x >> size / 2);
54}
55
56}
57
59constexpr double pi = 3.14159265358979323846;
60
62constexpr double pi_2 = 1.57079632679489661923;
63
65constexpr double pi_180 = 0.01745329251994329576;
66
68constexpr double one_rad = 57.29577951308232087679;
69
71const double log_of_4 = log(4.0);
72
74constexpr double gamma = 0.57721566490153286061;
75
77template<typename T>
78inline T nextPower2(T x) {
79 return internal::nextPower2<T, sizeof(T) * 8>(x - 1) + 1;
80}
81
83template<typename T, typename... Args>
84inline static T nextPower2(T arg1, T arg2, Args... args) {
85 return nextPower2(std::max(arg1, arg2, args...));
86}
87
89template<typename T>
90inline void updateMax(T& max, const T& newValue) {
91 if (max < newValue) {
92 max = newValue;
93 }
94}
95
97template<typename T>
98inline void updateMin(T& min, const T& newValue) {
99 if (min > newValue) {
100 min = newValue;
101 }
102}
103
105template<typename T>
106OGDF_DEPRECATED("Use std::log2(x).")
107inline T log2(T x) {
108 OGDF_ASSERT(x > 0);
109 return std::log2(x);
110}
111
113inline double log4(double x) {
114 OGDF_ASSERT(x > 0);
115 return log(x) / log_of_4;
116}
117
119template<typename T>
120inline int sgn(T val) {
121 return (T(0) < val) - (val < T(0));
122}
123
125inline double degreesToRadians(const double& angleInDegrees) {
127}
128
130inline double radiansToDegrees(const double& angleInRadians) {
132}
133
135OGDF_EXPORT int binomial(int n, int k);
136
138OGDF_EXPORT double binomial_d(int n, int k);
139
141OGDF_DEPRECATED("Use std::tgamma(n+1).")
142
143inline int factorial(int n) { return (int)std::tgamma(n + 1); }
144
146OGDF_DEPRECATED("Use std::tgamma(n+1).")
147
148inline double factorial_d(int n) { return std::tgamma(n + 1); }
149
151OGDF_EXPORT double harmonic(unsigned n);
152
159OGDF_DEPRECATED("Use std::ilogb(v).")
160
161inline int floorLog2(int v) {
162 if (v <= 0) {
163 return -1;
164 } else {
165 return std::ilogb(v);
166 }
167}
168
170template<typename T>
171inline T gcd(T a, T b) {
172 // If b > a, they will be swapped in the first iteration.
173 do {
174 T c = a % b;
175 a = b;
176 b = c;
177 } while (b > 0);
178 return a;
179}
180
182template<class T, class INDEX = int>
183inline T gcd(const Array<T, INDEX>& numbers) {
184 T current_gcd = numbers[numbers.low()];
185 for (INDEX i = numbers.low() + 1; i <= numbers.high(); i++) {
187 }
188 return current_gcd;
189}
190
192template<typename T>
193inline T lcm(T a, T b) {
194 T g = gcd(a, b);
195 OGDF_ASSERT(g != 0);
196 return (a / g) * b;
197}
198
200inline void getFraction(double d, int& num, int& denom, const double epsilon = 5e-10,
201 const int count = 10) {
203
204 // build continued fraction
205 int z((int)d);
206 continuedFrac.push(z);
207 d = d - z;
208 int i = 0;
209 while (d > epsilon && i++ < count) {
210 d = 1 / d;
211 z = (int)d;
212 continuedFrac.push(z);
213 d = d - z;
214 }
215
216 // simplify continued fraction to simple fraction
217 num = 1;
218 denom = 0;
219 while (!continuedFrac.empty()) {
220 int last = continuedFrac.popRet();
221 std::swap(num, denom);
222 num += last * denom;
223 }
224}
225
227template<class Container>
228inline typename Container::value_type minValue(const Container& values) {
229 OGDF_ASSERT(!values.empty());
230 return *std::min_element(values.begin(), values.end());
231}
232
234template<class Container>
235inline typename Container::value_type maxValue(const Container& values) {
236 OGDF_ASSERT(!values.empty());
237 return *std::max_element(values.begin(), values.end());
238}
239
241template<class Container>
242inline typename Container::value_type sum(const Container& values) {
243 return std::accumulate(values.begin(), values.end(),
244 static_cast<typename Container::value_type>(0));
245}
246
248template<class Container>
249inline double mean(const Container& values) {
250 OGDF_ASSERT(!values.empty());
251 return sum(values) / static_cast<double>(values.size());
252}
253
256template<class Container>
257inline double standardDeviation(const Container& values, double mean) {
258 OGDF_ASSERT(!values.empty());
259 double sum = 0;
260 for (auto value : values) {
261 double d = value - mean;
262 sum += d * d;
263 }
264 return sqrt(sum / values.size());
265}
266
268template<class Container>
269inline double standardDeviation(const Container& values) {
271}
272
273}
274}
Declaration and implementation of Array class and Array algorithms.
Basic declarations, included by all source files.
An array that keeps track of the number of inserted elements; also usable as an efficient stack.
Definition ArrayBuffer.h:56
The parameterized class Array implements dynamic arrays of type E.
Definition Array.h:214
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition config.h:101
#define OGDF_DEPRECATED(reason)
Mark a class / member / function as deprecated.
Definition config.h:120
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:41
static MultilevelBuilder * getDoubleFactoredZeroAdjustedMerger()
std::enable_if< size==1, T >::type nextPower2(T x)
Definition Math.h:44
double harmonic(unsigned n)
Returns the n-th harmonic number or 1.0 if n < 1.
void updateMin(T &min, const T &newValue)
Stores the minimum of min and newValue in min.
Definition Math.h:98
const double log_of_4
The constant log(4.0).
Definition Math.h:71
double standardDeviation(const Container &values, double mean)
Returns the standard deviation of an iterable container of given values.
Definition Math.h:257
constexpr double gamma
The Euler-Mascheroni constant gamma.
Definition Math.h:74
int floorLog2(int v)
A method to obtain the rounded down binary logarithm of v.
Definition Math.h:161
constexpr double pi
The constant .
Definition Math.h:59
constexpr double pi_180
The constant .
Definition Math.h:65
T lcm(T a, T b)
Returns the least common multipler of two numbers.
Definition Math.h:193
int sgn(T val)
Returns +1 for val > 0, 0 for val = 0, and -1 for val < 0.
Definition Math.h:120
double log4(double x)
Returns the logarithm of x to the base 4.
Definition Math.h:113
int factorial(int n)
Returns n!.
Definition Math.h:143
T gcd(T a, T b)
Returns the greatest common divisor of two numbers.
Definition Math.h:171
double degreesToRadians(const double &angleInDegrees)
Converts an angle from degrees to radians.
Definition Math.h:125
void updateMax(T &max, const T &newValue)
Stores the maximum of max and newValue in max.
Definition Math.h:90
Container::value_type minValue(const Container &values)
Returns the minimum of an iterable container of given values.
Definition Math.h:228
T log2(T x)
Returns the logarithm of x to the base 2.
Definition Math.h:107
constexpr double one_rad
The constant .
Definition Math.h:68
double binomial_d(int n, int k)
Returns .
constexpr double pi_2
The constant .
Definition Math.h:62
void getFraction(double d, int &num, int &denom, const double epsilon=5e-10, const int count=10)
Converts a double to a fraction.
Definition Math.h:200
int binomial(int n, int k)
Returns .
double factorial_d(int n)
Returns n!.
Definition Math.h:148
double radiansToDegrees(const double &angleInRadians)
Converts an angle from radians to degrees.
Definition Math.h:130
Container::value_type maxValue(const Container &values)
Returns the maximum of an iterable container of given values.
Definition Math.h:235
T nextPower2(T x)
Returns the smallest power of 2 that is no less than the given (integral) argument.
Definition Math.h:78
double mean(const Container &values)
Returns the mean of an iterable container of given values.
Definition Math.h:249
The namespace for all OGDF objects.