Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

Math.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/basic.h>
35 #include <ogdf/basic/Stack.h>
36 #include <ogdf/basic/Array.h>
37 
38 
39 namespace ogdf {
40 
41 
43 
44 public:
46  static const double pi;
47 
49  static const double pi_2;
50 
52  static const double log_of_4;
53 
55  static const double gamma;
56 
58  template<typename T>
59  OGDF_DEPRECATED("Use std::log2(x).")
60  static inline T log2(T x) {
61  OGDF_ASSERT(x > 0);
62  return std::log2(x);
63  }
64 
66  static inline double log4(double x) {
67  OGDF_ASSERT(x > 0);
68  return log(x) / log_of_4;
69  }
70 
72  template <typename T>
73  static inline int sgn(T val) {
74  return (T(0) < val) - (val < T(0));
75  }
76 
78  static int binomial(int n, int k);
79 
81  static double binomial_d(int n, int k);
82 
84  OGDF_DEPRECATED("Use std::tgamma(n+1).")
85  static inline int factorial(int n)
86  {
87  return (int) std::tgamma(n+1);
88  }
89 
91  OGDF_DEPRECATED("Use std::tgamma(n+1).")
92  static inline double factorial_d(int n)
93  {
94  return std::tgamma(n+1);
95  }
96 
98  static double harmonic(unsigned n);
99 
106  OGDF_DEPRECATED("Use std::ilogb(v).")
107  static inline int floorLog2(int v) {
108  if (v <= 0) {
109  return -1;
110  } else {
111  return std::ilogb(v);
112  }
113  }
114 
116  template<typename T>
117  static T gcd(T a, T b)
118  {
119  // If b > a, they will be swapped in the first iteration.
120  do {
121  T c = a % b;
122  a = b;
123  b = c;
124  } while (b > 0);
125  return a;
126  }
127 
129  template<class T, class INDEX = int>
130  static T gcd(const Array<T,INDEX> &numbers) {
131  T current_gcd = numbers[numbers.low()];
132  for (INDEX i = numbers.low()+1; i<=numbers.high(); i++) {
133  current_gcd = gcd(current_gcd, numbers[i]);
134  }
135  return current_gcd;
136  }
137 
139  template<typename T>
140  static T lcm(T a, T b) {
141  T g = gcd(a,b);
142  OGDF_ASSERT(g != 0);
143  return (a / g) * b;
144  }
145 
147  static void getFraction(double d, int &num, int &denom, const double epsilon = 5e-10, const int count = 10)
148  {
149  Stack<int> continuedFrac;
150 
151  // build continued fraction
152  int z((int)d);
153  continuedFrac.push(z);
154  d = d - z;
155  int i = 0;
156  while (d > epsilon && i++ < count) {
157  d = 1 / d;
158  z = (int)d;
159  continuedFrac.push(z);
160  d = d - z;
161  }
162 
163  // simplify continued fraction to simple fraction
164  num = 1;
165  denom = 0;
166  while (!continuedFrac.empty()) {
167  int last = continuedFrac.pop();
168  swap(num, denom);
169  num += last * denom;
170  }
171  }
172 };
173 
174 
175 }
static const double pi
The constant .
Definition: Math.h:46
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:56
#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:131
bool empty() const
Returns true iff the stack is empty.
Definition: Stack.h:222
Declaration and implementation of Array class and Array algorithms.
static 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:147
static const double log_of_4
The constant log(4.0).
Definition: Math.h:52
E pop()
Removes the top-most element from the stack and returns it.
Definition: Stack.h:272
void push(const E &x)
Adds element x as top-most element to the stack.
Definition: Stack.h:256
static T gcd(T a, T b)
Returns the greatest common divisor of two numbers.
Definition: Math.h:117
static T lcm(T a, T b)
Returns the least common multipler of two numbers.
Definition: Math.h:140
Basic declarations, included by all source files.
static T gcd(const Array< T, INDEX > &numbers)
Returns the greatest common divisor of a list of numbers.
Definition: Math.h:130
static int sgn(T val)
Returns +1 for val > 0, 0 for val = 0, and -1 for val < 0.
Definition: Math.h:73
#define OGDF_DEPRECATED(reason)
Mark a class / member / function as deprecated.
Definition: config.h:118
The namespace for all OGDF objects.
static const double pi_2
The constant .
Definition: Math.h:49
static const double gamma
The Euler-Mascheroni constant gamma.
Definition: Math.h:55
INDEX low() const
Returns the minimal array index.
Definition: Array.h:128
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
static double log4(double x)
Returns the logarithm of x to the base 4.
Definition: Math.h:66
The parameterized class Stack<E> implements list-based stacks.
Definition: Stack.h:192
Declaration and implementation of list-based stacks (StackPure<E> and Stack<E>).