Bertini 2 - C++ Core
Software for Numerical Algebraic Geometry
Loading...
Searching...
No Matches
num_traits.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.
2//
3//num_traits.hpp is free software: you can redistribute it and/or modify
4//it under the terms of the GNU General Public License as published by
5//the Free Software Foundation, either version 3 of the License, or
6//(at your option) any later version.
7//
8//num_traits.hpp is distributed in the hope that it will be useful,
9//but WITHOUT ANY WARRANTY; without even the implied warranty of
10//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11//GNU General Public License for more details.
12//
13//You should have received a copy of the GNU General Public License
14//along with num_traits.hpp. If not, see <http://www.gnu.org/licenses/>.
15//
16// Copyright(C) 2015 - 2021 by Bertini2 Development Team
17//
18// See <http://www.gnu.org/licenses/> for a copy of the license,
19// as well as COPYING. Bertini2 is provided with permitted
20// additional terms in the b2/licenses/ directory.
21
22// individual authors of this file include:
23// silviana amethyst, university of wisconsin eau claire
24
32
33#ifndef BERTINI_NUM_TRAITS_HPP
34#define BERTINI_NUM_TRAITS_HPP
35
36#include <random>
37#include <complex>
38#include <cmath>
40#include "bertini2/random.hpp"
41
42
43namespace bertini
44{
45 template<typename T>
46 T RandomUnit();
47
48 template<typename T>
49 struct NumTraits
50 {};
51
52
53
54 template <> struct NumTraits<double>
55 {
56 inline static unsigned NumDigits()
57 {
58 return 16;
59 }
60
61 inline static unsigned NumFuzzyDigits()
62 {
63 return 14;
64 }
65
66 inline
67 static unsigned TolToDigits(double tol)
68 {
69 return ceil(-log10(tol));
70 }
71
72 inline static
73 double FromString(std::string const& s)
74 {
75 return boost::lexical_cast<double>(s);
76 }
77
78 inline static
79 double FromRational(mpq_rational const& n, unsigned /* precision */)
80 {
81 return double(n);
82 }
83
84 using Real = double;
85 using Complex = dbl_complex;
86 };
87
88
89 template <> struct NumTraits<dbl_complex >
90 {
91 inline static unsigned NumDigits()
92 {
93 return 16;
94 }
95
96 inline static unsigned NumFuzzyDigits()
97 {
98 return 14;
99 }
100
101 inline static
102 dbl_complex FromString(std::string const& s)
103 {
104 return boost::lexical_cast<dbl_complex>(s);
105 }
106
107 inline static
108 dbl_complex FromString(std::string const& s, std::string const& t)
109 {
110 return dbl_complex(boost::lexical_cast<double>(s),boost::lexical_cast<double>(t));
111 }
112
113 inline static
114 dbl_complex FromRational(mpq_rational const& n, unsigned /* precision */)
115 {
116 return dbl_complex(static_cast<double>(n),0);
117 }
118
119 using Real = double;
120 using Complex = dbl_complex;
121 };
122
123
124 inline
125 unsigned PrecisionIncrement()
126 {
127 return 10;
128 }
129
130 inline
131 unsigned DoublePrecision()
132 {
133 return 16;
134 }
135
136 inline
137 unsigned LowestMultiplePrecision()
138 {
139 return 20;
140 }
141
142 inline
143 unsigned MaxPrecisionAllowed()
144 {
145 return 1000;
146 }
147
153 inline
154 unsigned Precision(double)
155 {
156 return DoublePrecision();
157 }
158
162 inline
163 void Precision(double, unsigned prec)
164 {
165 if (prec!=DoublePrecision())
166 {
167 std::stringstream err_msg;
168 err_msg << "trying to change precision of a double to " << prec;
169 throw std::runtime_error(err_msg.str());
170 }
171 }
172
178 inline
179 unsigned Precision(dbl_complex)
180 {
181 return DoublePrecision();
182 }
183
187 inline
188 void Precision(dbl_complex, unsigned prec)
189 {
190 if (prec!=DoublePrecision())
191 {
192 std::stringstream err_msg;
193 err_msg << "trying to change precision of a double to " << prec;
194 throw std::runtime_error(err_msg.str());
195 }
196 }
197
198 inline
199 dbl_complex rand_complex()
200 {
201 using std::abs;
202 using std::sqrt;
203 static std::default_random_engine generator;
204 static std::uniform_real_distribution<double> distribution(-1.0,1.0);
205 dbl_complex returnme(distribution(generator), distribution(generator));
206 return returnme / sqrt( abs(returnme));
207 }
208
209 template <> inline
210 dbl_complex RandomUnit<dbl_complex >()
211 {
212 static std::default_random_engine generator;
213 static std::uniform_real_distribution<double> distribution(-1.0,1.0);
214 dbl_complex returnme(distribution(generator), distribution(generator));
215 return returnme / abs(returnme);
216 }
217
218 template <>
219 inline
220 mpfr_complex RandomUnit<mpfr_complex>()
221 {
222 return multiprecision::RandomUnit();
223 }
224}// re: namespace bertini
225
226
227
228
229
230
231
232
233
234
235
236
237namespace bertini {
238
239
240
241 template <> struct NumTraits<mpfr_float>
242 {
243 inline static unsigned NumDigits()
244 {
245 return DefaultPrecision();
246 }
247
248 inline static unsigned NumFuzzyDigits()
249 {
250 return DefaultPrecision()-3;
251 }
252
253 inline
254 static unsigned TolToDigits(mpfr_float tol)
255 {
256 mpfr_float b = ceil(-log10(tol));
257 return b.convert_to<unsigned int>();
258 }
259
260 inline static
261 mpfr_float FromString(std::string const& s)
262 {
263 return mpfr_float(s);
264 }
265
266 inline static
267 mpfr_float FromRational(mpq_rational const& n, unsigned precision)
268 {
269 return mpfr_float(n,precision);
270 }
271
272 using Real = mpfr_float;
273 using Complex = mpfr_complex;
274 };
275
276
277
278 template <> struct NumTraits<mpfr_complex>
279 {
280 inline static unsigned NumDigits()
281 {
282 return DefaultPrecision();
283 }
284
285 inline static
286 mpfr_complex FromString(std::string const& s)
287 {
288 return mpfr_complex(s);
289 }
290
291 inline static
292 mpfr_complex FromString(std::string const& s, std::string const& t)
293 {
294 return mpfr_complex(s,t);
295 }
296
297 inline static
298 mpfr_complex FromRational(mpq_rational const& n, unsigned precision)
299 {
300 return mpfr_complex(n,0,precision);
301 }
302
303 using Real = mpfr_float;
304 using Complex = mpfr_complex;
305 };
306
307 template <> struct NumTraits<mpq_rational>
308 {
309 inline static
310 mpq_rational FromString(std::string const& s)
311 {
312 return mpq_rational(s);
313 }
314 };
315
316}
317
318#endif
319
320
unsigned Precision(Eigen::MatrixBase< Derived > const &v)
Get the precision of an Eigen object. If the object is empty, it's the precision of a default-constru...
Definition eigen_extensions.hpp:292
The main multiprecision complex number type. This is essentially boost::multiprecision' complex.
stuff for generating random numbers