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>
36#include <ogdf/basic/basic.h>
37
38#include <cmath>
39#include <numeric>
40#include <type_traits>
41#include <utility>
42
43namespace ogdf {
44namespace Math {
45namespace internal {
46
47template<typename T, int size>
48inline typename std::enable_if<size == 1, T>::type nextPower2(T x) {
49 return x;
50}
51
54template<typename T, int size>
55inline typename std::enable_if<size != 1, T>::type nextPower2(T x) {
56 x = nextPower2<T, size / 2>(x);
57 return x | (x >> size / 2);
58}
59
60}
61
63constexpr double pi = 3.14159265358979323846;
64
66constexpr double pi_2 = 1.57079632679489661923;
67
69constexpr double pi_180 = 0.01745329251994329576;
70
72constexpr double one_rad = 57.29577951308232087679;
73
75const double log_of_4 = log(4.0);
76
78constexpr double gamma = 0.57721566490153286061;
79
81template<typename T>
82inline T nextPower2(T x) {
83 return internal::nextPower2<T, sizeof(T) * 8>(x - 1) + 1;
84}
85
87template<typename T, typename... Args>
88inline static T nextPower2(T arg1, T arg2, Args... args) {
89 return nextPower2(std::max(arg1, arg2, args...));
90}
91
93template<typename T>
94inline void updateMax(T& max, const T& newValue) {
95 if (max < newValue) {
96 max = newValue;
97 }
98}
99
101template<typename T>
102inline void updateMin(T& min, const T& newValue) {
103 if (min > newValue) {
104 min = newValue;
105 }
106}
107
109template<typename T>
110OGDF_DEPRECATED("Use std::log2(x).")
111inline T log2(T x) {
112 OGDF_ASSERT(x > 0);
113 return std::log2(x);
114}
115
117inline double log4(double x) {
118 OGDF_ASSERT(x > 0);
119 return log(x) / log_of_4;
120}
121
123template<typename T>
124inline int sgn(T val) {
125 return (T(0) < val) - (val < T(0));
126}
127
129inline double degreesToRadians(const double& angleInDegrees) {
130 return angleInDegrees * Math::pi_180;
131}
132
134inline double radiansToDegrees(const double& angleInRadians) {
135 return angleInRadians * Math::one_rad;
136}
137
139OGDF_EXPORT int binomial(int n, int k);
140
142OGDF_EXPORT double binomial_d(int n, int k);
143
145OGDF_DEPRECATED("Use std::tgamma(n+1).")
146
147inline int factorial(int n) { return (int)std::tgamma(n + 1); }
148
150OGDF_DEPRECATED("Use std::tgamma(n+1).")
151
152inline double factorial_d(int n) { return std::tgamma(n + 1); }
153
155OGDF_EXPORT double harmonic(unsigned n);
156
163OGDF_DEPRECATED("Use std::ilogb(v).")
164
165inline int floorLog2(int v) {
166 if (v <= 0) {
167 return -1;
168 } else {
169 return std::ilogb(v);
170 }
171}
172
174template<typename T>
175inline T gcd(T a, T b) {
176 // If b > a, they will be swapped in the first iteration.
177 do {
178 T c = a % b;
179 a = b;
180 b = c;
181 } while (b > 0);
182 return a;
183}
184
186template<class T, class INDEX = int>
187inline T gcd(const Array<T, INDEX>& numbers) {
188 T current_gcd = numbers[numbers.low()];
189 for (INDEX i = numbers.low() + 1; i <= numbers.high(); i++) {
190 current_gcd = gcd(current_gcd, numbers[i]);
191 }
192 return current_gcd;
193}
194
196template<typename T>
197inline T lcm(T a, T b) {
198 T g = gcd(a, b);
199 OGDF_ASSERT(g != 0);
200 return (a / g) * b;
201}
202
204inline void getFraction(double d, int& num, int& denom, const double epsilon = 5e-10,
205 const int count = 10) {
206 ArrayBuffer<int> continuedFrac;
207
208 // build continued fraction
209 int z((int)d);
210 continuedFrac.push(z);
211 d = d - z;
212 int i = 0;
213 while (d > epsilon && i++ < count) {
214 d = 1 / d;
215 z = (int)d;
216 continuedFrac.push(z);
217 d = d - z;
218 }
219
220 // simplify continued fraction to simple fraction
221 num = 1;
222 denom = 0;
223 while (!continuedFrac.empty()) {
224 int last = continuedFrac.popRet();
225 std::swap(num, denom);
226 num += last * denom;
227 }
228}
229
231template<class Container>
232inline typename Container::value_type minValue(const Container& values) {
233 OGDF_ASSERT(!values.empty());
234 return *std::min_element(values.begin(), values.end());
235}
236
238template<class Container>
239inline typename Container::value_type maxValue(const Container& values) {
240 OGDF_ASSERT(!values.empty());
241 return *std::max_element(values.begin(), values.end());
242}
243
245template<class Container>
246inline typename Container::value_type sum(const Container& values) {
247 return std::accumulate(values.begin(), values.end(),
248 static_cast<typename Container::value_type>(0));
249}
250
252template<class Container>
253inline double mean(const Container& values) {
254 OGDF_ASSERT(!values.empty());
255 return sum(values) / static_cast<double>(values.size());
256}
257
260template<class Container>
261inline double standardDeviation(const Container& values, double mean) {
262 OGDF_ASSERT(!values.empty());
263 double sum = 0;
264 for (auto value : values) {
265 double d = value - mean;
266 sum += d * d;
267 }
268 return sqrt(sum / values.size());
269}
270
272template<class Container>
273inline double standardDeviation(const Container& values) {
274 return standardDeviation(values, mean(values));
275}
276
277}
278}
Declaration and implementation of Array class and Array algorithms.
Declaration and implementation of ArrayBuffer class.
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:64
E popRet()
Removes the newest element from the buffer and returns it.
void push(E e)
Puts a new element in the buffer.
bool empty() const
Returns true if the buffer is empty, false otherwise.
The parameterized class Array implements dynamic arrays of type E.
Definition Array.h:219
INDEX high() const
Returns the maximal array index.
Definition Array.h:299
INDEX low() const
Returns the minimal array index.
Definition Array.h:296
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF dynamic library (shared object / DLL),...
Definition config.h:117
#define OGDF_DEPRECATED(reason)
Mark a class / member / function as deprecated.
Definition config.h:206
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:52
std::enable_if< size==1, T >::type nextPower2(T x)
Definition Math.h:48
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:102
const double log_of_4
The constant log(4.0).
Definition Math.h:75
double standardDeviation(const Container &values, double mean)
Returns the standard deviation of an iterable container of given values.
Definition Math.h:261
constexpr double gamma
The Euler-Mascheroni constant gamma.
Definition Math.h:78
int floorLog2(int v)
A method to obtain the rounded down binary logarithm of v.
Definition Math.h:165
constexpr double pi
The constant .
Definition Math.h:63
constexpr double pi_180
The constant .
Definition Math.h:69
T lcm(T a, T b)
Returns the least common multipler of two numbers.
Definition Math.h:197
int sgn(T val)
Returns +1 for val > 0, 0 for val = 0, and -1 for val < 0.
Definition Math.h:124
double log4(double x)
Returns the logarithm of x to the base 4.
Definition Math.h:117
int factorial(int n)
Returns n!.
Definition Math.h:147
T gcd(T a, T b)
Returns the greatest common divisor of two numbers.
Definition Math.h:175
double degreesToRadians(const double &angleInDegrees)
Converts an angle from degrees to radians.
Definition Math.h:129
void updateMax(T &max, const T &newValue)
Stores the maximum of max and newValue in max.
Definition Math.h:94
Container::value_type minValue(const Container &values)
Returns the minimum of an iterable container of given values.
Definition Math.h:232
T log2(T x)
Returns the logarithm of x to the base 2.
Definition Math.h:111
constexpr double one_rad
The constant .
Definition Math.h:72
double binomial_d(int n, int k)
Returns .
constexpr double pi_2
The constant .
Definition Math.h:66
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:204
int binomial(int n, int k)
Returns .
double factorial_d(int n)
Returns n!.
Definition Math.h:152
double radiansToDegrees(const double &angleInRadians)
Converts an angle from radians to degrees.
Definition Math.h:134
Container::value_type maxValue(const Container &values)
Returns the maximum of an iterable container of given values.
Definition Math.h:239
T nextPower2(T x)
Returns the smallest power of 2 that is no less than the given (integral) argument.
Definition Math.h:82
double mean(const Container &values)
Returns the mean of an iterable container of given values.
Definition Math.h:253
The namespace for all OGDF objects.