Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

Math.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <numeric>
35 
36 #include <ogdf/basic/basic.h>
37 #include <ogdf/basic/Array.h>
38 
39 
40 namespace ogdf {
41 namespace Math {
42 namespace internal {
43 
44 template<typename T, int size>
46 nextPower2(T x) {
47  return x;
48 }
49 
52 template<typename T, int size>
54 nextPower2(T x) {
55  x = nextPower2<T, size/2>(x);
56  return x | (x >> size/2);
57 }
58 
59 }
60 
62 constexpr double pi = 3.14159265358979323846;
63 
65 constexpr double pi_2 = 1.57079632679489661923;
66 
68 constexpr double pi_180 = 0.01745329251994329576;
69 
71 constexpr double one_rad = 57.29577951308232087679;
72 
74 const double log_of_4 = log(4.0);
75 
77 constexpr double gamma = 0.57721566490153286061;
78 
80 template<typename T>
81 inline T nextPower2(T x) {
82  return internal::nextPower2<T, sizeof(T)*8>(x - 1) + 1;
83 }
84 
86 template<typename T, typename... Args>
87 inline static T nextPower2(T arg1, T arg2, Args... args) {
88  return nextPower2(std::max(arg1, arg2, args...));
89 }
90 
92 template<typename T>
93 inline void updateMax(T& max, const T& newValue) {
94  if(max < newValue) {
95  max = newValue;
96  }
97 }
98 
100 template<typename T>
101 inline void updateMin(T& min, const T& newValue) {
102  if(min > newValue) {
103  min = newValue;
104  }
105 }
106 
108 template<typename T>
109 OGDF_DEPRECATED("Use std::log2(x).")
110 inline T log2(T x) {
111  OGDF_ASSERT(x > 0);
112  return std::log2(x);
113 }
114 
116 inline double log4(double x) {
117  OGDF_ASSERT(x > 0);
118  return log(x) / log_of_4;
119 }
120 
122 template <typename T>
123 inline int sgn(T val) {
124  return (T(0) < val) - (val < T(0));
125 }
126 
128 inline double degreesToRadians(const double &angleInDegrees) {
129  return angleInDegrees * Math::pi_180;
130 }
131 
133 inline double radiansToDegrees(const double &angleInRadians) {
134  return angleInRadians * Math::one_rad;
135 }
136 
138 OGDF_EXPORT int binomial(int n, int k);
139 
141 OGDF_EXPORT double binomial_d(int n, int k);
142 
144 OGDF_DEPRECATED("Use std::tgamma(n+1).")
145 inline int factorial(int n) {
146  return (int) std::tgamma(n+1);
147 }
148 
150 OGDF_DEPRECATED("Use std::tgamma(n+1).")
151 inline double factorial_d(int n) {
152  return std::tgamma(n+1);
153 }
154 
156 OGDF_EXPORT double harmonic(unsigned n);
157 
164 OGDF_DEPRECATED("Use std::ilogb(v).")
165 inline int floorLog2(int v) {
166  if (v <= 0) {
167  return -1;
168  } else {
169  return std::ilogb(v);
170  }
171 }
172 
174 template<typename T>
175 inline 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 
186 template<class T, class INDEX = int>
187 inline 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 
196 template<typename T>
197 inline T lcm(T a, T b) {
198  T g = gcd(a,b);
199  OGDF_ASSERT(g != 0);
200  return (a / g) * b;
201 }
202 
204 inline void getFraction(double d, int &num, int &denom, const double epsilon = 5e-10, const int count = 10) {
205  ArrayBuffer<int> continuedFrac;
206 
207  // build continued fraction
208  int z((int)d);
209  continuedFrac.push(z);
210  d = d - z;
211  int i = 0;
212  while (d > epsilon && i++ < count) {
213  d = 1 / d;
214  z = (int)d;
215  continuedFrac.push(z);
216  d = d - z;
217  }
218 
219  // simplify continued fraction to simple fraction
220  num = 1;
221  denom = 0;
222  while (!continuedFrac.empty()) {
223  int last = continuedFrac.popRet();
224  std::swap(num, denom);
225  num += last * denom;
226  }
227 }
228 
230 template<class Container>
231 inline typename Container::value_type minValue(const Container &values) {
232  OGDF_ASSERT(!values.empty());
233  return *std::min_element(values.begin(), values.end());
234 }
235 
237 template<class Container>
238 inline typename Container::value_type maxValue(const Container &values) {
239  OGDF_ASSERT(!values.empty());
240  return *std::max_element(values.begin(), values.end());
241 }
242 
244 template<class Container>
245 inline typename Container::value_type sum(const Container &values) {
246  return std::accumulate(values.begin(), values.end(), static_cast<typename Container::value_type>(0));
247 }
248 
250 template<class Container>
251 inline double mean(const Container &values) {
252  OGDF_ASSERT(!values.empty());
253  return sum(values) / static_cast<double>(values.size());
254 }
255 
258 template<class Container>
259 inline double standardDeviation(const Container &values, double mean) {
260  OGDF_ASSERT(!values.empty());
261  double sum = 0;
262  for (auto value : values) {
263  double d = value - mean;
264  sum += d*d;
265  }
266  return sqrt(sum / values.size());
267 }
268 
270 template<class Container>
271 inline double standardDeviation(const Container &values) {
272  return standardDeviation(values, mean(values));
273 }
274 
275 }}
double mean(const Container &values)
Returns the mean of an iterable container of given values.
Definition: Math.h:251
int sgn(T val)
Returns +1 for val > 0, 0 for val = 0, and -1 for val < 0.
Definition: Math.h:123
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:200
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition: config.h:99
INDEX high() const
Returns the maximal array index.
Definition: Array.h:279
void push(E e)
Puts a new element in the buffer.
Definition: ArrayBuffer.h:102
T gcd(T a, T b)
Returns the greatest common divisor of two numbers.
Definition: Math.h:175
constexpr double pi_180
The constant .
Definition: Math.h:68
double radiansToDegrees(const double &angleInRadians)
Converts an angle from radians to degrees.
Definition: Math.h:133
double degreesToRadians(const double &angleInDegrees)
Converts an angle from degrees to radians.
Definition: Math.h:128
Declaration and implementation of Array class and Array algorithms.
double factorial_d(int n)
Returns n!.
Definition: Math.h:151
constexpr double pi
The constant .
Definition: Math.h:62
T lcm(T a, T b)
Returns the least common multipler of two numbers.
Definition: Math.h:197
constexpr double one_rad
The constant .
Definition: Math.h:71
void updateMin(T &min, const T &newValue)
Stores the minimum of min and newValue in min.
Definition: Math.h:101
std::enable_if< size==1, T >::type nextPower2(T x)
Definition: Math.h:46
bool empty() const
Returns true if the buffer is empty, false otherwise.
Definition: ArrayBuffer.h:116
T log2(T x)
Returns the logarithm of x to the base 2.
Definition: Math.h:110
const double log_of_4
The constant log(4.0).
Definition: Math.h:74
double harmonic(unsigned n)
Returns the n-th harmonic number or 1.0 if n < 1.
int factorial(int n)
Returns n!.
Definition: Math.h:145
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
constexpr double gamma
The Euler-Mascheroni constant gamma.
Definition: Math.h:77
Container::value_type minValue(const Container &values)
Returns the minimum of an iterable container of given values.
Definition: Math.h:231
int floorLog2(int v)
A method to obtain the rounded down binary logarithm of v.
Definition: Math.h:165
Basic declarations, included by all source files.
E popRet()
Removes the newest element from the buffer and returns it.
Definition: ArrayBuffer.h:113
int binomial(int n, int k)
Returns .
Container::value_type maxValue(const Container &values)
Returns the maximum of an iterable container of given values.
Definition: Math.h:238
double log4(double x)
Returns the logarithm of x to the base 4.
Definition: Math.h:116
#define OGDF_DEPRECATED(reason)
Mark a class / member / function as deprecated.
Definition: config.h:118
constexpr double pi_2
The constant .
Definition: Math.h:65
The namespace for all OGDF objects.
Container::value_type sum(const Container &values)
Returns the sum of an iterable container of given values.
Definition: Math.h:245
void updateMax(T &max, const T &newValue)
Stores the maximum of max and newValue in max.
Definition: Math.h:93
INDEX low() const
Returns the minimal array index.
Definition: Array.h:276
double binomial_d(int n, int k)
Returns .
double standardDeviation(const Container &values, double mean)
Returns the standard deviation of an iterable container of given values.
Definition: Math.h:259
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41