Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

math.h
Go to the documentation of this file.
1 
31 #pragma once
32 
33 #ifdef OGDF_INCLUDE_CGAL
34 
35 # include <CGAL/CORE_Expr.h>
36 # include <CGAL/Gmpfr.h>
37 # include <CGAL/Gmpq.h>
38 # include <CGAL/gmpxx.h>
39 # include <mpfr.h>
40 
41 # ifndef OGDF_GEOMETRIC_MPFR_NUMBER_OF_DIGITS
42 
43 # define OGDF_GEOMETRIC_MPFR_NUMBER_OF_DIGITS 77 // equals 256 bits
44 
45 # endif
46 
47 # ifndef OGDF_GEOMETRIC_MPFR_EQUALITY_THRESHOLD
48 # define OGDF_GEOMETRIC_MPFR_EQUALITY_THRESHOLD 1e-20
49 # endif
50 
51 
52 namespace ogdf {
53 namespace internal {
54 namespace gcm {
55 namespace tools {
56 
57 template<typename T>
58 inline double cast(const T& a) {
59  //return a.to_double();
60  return CGAL::to_double(a);
61 }
62 
63 template<>
64 inline double cast(const double& a) {
65  return a;
66 }
67 
68 inline mpfr_rnd_t std_rnd_to_mpfr_rnd(const std::float_round_style& e) {
69  switch (e) {
70  case std::round_indeterminate:
71  OGDF_ASSERT(false);
72  return MPFR_RNDZ;
73  case std::round_toward_zero:
74  return MPFR_RNDZ;
75  case std::round_to_nearest:
76  return MPFR_RNDN;
77  case std::round_toward_infinity:
78  return MPFR_RNDU;
79  case std::round_toward_neg_infinity:
80  return MPFR_RNDD;
81  default:
82  OGDF_ASSERT(false);
83  return MPFR_RNDZ;
84  }
85 }
86 
87 template<typename T>
88 inline const T const_pi() {
89  return CGAL_PI;
90 }
91 
92 template<>
93 inline const CGAL::Gmpfr const_pi() {
94  CGAL::Gmpfr y;
95  mpfr_const_pi(y.fr(), std_rnd_to_mpfr_rnd(CGAL::Gmpfr::get_default_rndmode()));
96  return y;
97 }
98 
99 template<typename T>
100 inline const T approx_sqrt(const T& v) {
101  return (T)sqrt(CGAL::to_double(v));
102 }
103 
104 inline const mpz_class approx_sqrt(const mpz_class& v) { return sqrt(v); }
105 
106 inline const mpq_class approx_sqrt(const mpq_class& v) {
107  return mpq_class(approx_sqrt(v.get_num()), approx_sqrt(v.get_den()));
108 }
109 
110 inline const CGAL::Gmpfr approx_sqrt(const CGAL::Gmpfr& v) {
111  CGAL::Gmpfr y;
112  mpfr_sqrt(y.fr(), v.fr(), std_rnd_to_mpfr_rnd(CGAL::Gmpfr::get_default_rndmode()));
113  return y;
114 }
115 
116 inline const CORE::Expr approx_sqrt(const CORE::Expr& v) { return CGAL::sqrt(v); }
117 
118 template<typename T>
119 inline const T acos(const T& v) {
120  return (T)std::acos(CGAL::to_double(v));
121 }
122 
123 template<>
124 inline const CGAL::Gmpfr acos(const CGAL::Gmpfr& v) {
125  CGAL::Gmpfr y;
126  mpfr_acos(y.fr(), v.fr(), std_rnd_to_mpfr_rnd(CGAL::Gmpfr::get_default_rndmode()));
127  return y;
128 }
129 
130 template<>
131 inline const CORE::Expr acos(const CORE::Expr& v) {
132  return std::acos(CGAL::to_double(v));
133 }
134 
135 // DIGITS
136 
137 
138 template<typename t>
139 inline bool isEqual(const t& a, const t& b) {
140  return a == b;
141 }
142 
143 template<>
144 inline bool isEqual(const double& a, const double& b) {
145  return fabs(a - b) < OGDF_GEOMETRIC_MPFR_EQUALITY_THRESHOLD;
146 }
147 
148 template<>
149 inline bool isEqual(const CGAL::Gmpfr& a, const CGAL::Gmpfr& b) {
150  return abs(a - b) < OGDF_GEOMETRIC_MPFR_EQUALITY_THRESHOLD;
151 }
152 
153 template<typename T>
154 inline bool isLessEqual(const T& a, const T& b) {
155  return (a < b) || isEqual(a, b);
156 }
157 
158 template<typename T>
159 inline bool isLess(const T& a, const T& b) {
160  return (a < b) && !isEqual(a, b);
161 }
162 
163 template<typename T>
164 inline bool isGreaterEqual(const T& a, const T& b) {
165  return (a > b) || isEqual(a, b);
166 }
167 
168 template<typename T>
169 inline bool isGreater(const T& a, const T& b) {
170  return (a > b) && !isEqual(a, b);
171 }
172 
173 template<typename T>
174 inline bool isZero(const T& a) {
175  return isEqual(a, (T)0.0);
176 }
177 
178 }
179 } // namespace
180 
181 inline std::ostream& operator<<(std::ostream& os, const CGAL::Gmpq& p) {
182  os.precision(20);
183  os << CGAL::to_double(p);
184  return os;
185 }
186 
187 inline std::ostream& operator<<(std::ostream& os, const CORE::Expr& p) {
188  os.precision(20);
189  os << CGAL::to_double(p);
190  return os;
191 }
192 
193 }
194 }
195 
196 #endif
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:54
ogdf::operator<<
std::ostream & operator<<(std::ostream &os, const ogdf::Array< E, INDEX > &a)
Prints array a to output stream os.
Definition: Array.h:978