Bertini 2 - C++ Core
Software for Numerical Algebraic Geometry
Loading...
Searching...
No Matches
random.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.
2//
3// bertini2/random.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// bertini2/random.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 bertini2/random.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
30
31#ifndef BERTINI_RANDOM_HPP
32#define BERTINI_RANDOM_HPP
33
34
35
36
38#include <boost/random.hpp>
39
40
41namespace bertini
42{
46 template <unsigned long digits = 50>
47 inline
48 mpz_int RandomInt()
49 {
50 using namespace boost::random;
51 static mt19937 mt;
52 static uniform_int_distribution<mpz_int> ui(-(mpz_int(1) << digits*1000L/301L), mpz_int(1) << digits*1000L/301L);
53 return ui(mt);
54 }
55
56
60 template <unsigned long digits = 50>
61 mpq_rational RandomRat()
62 {
63 using namespace boost::random;
64 static mt19937 mt;
65 static uniform_int_distribution<mpz_int> ui(-(mpz_int(1) << digits*1000L/301L), mpz_int(1) << digits*1000L/301L);
66 return mpq_rational(ui(mt),ui(mt));
67 }
68
69
75 template <unsigned int length_in_digits>
76 mpfr_float RandomMp()
77 {
78
79 using namespace boost::multiprecision;
80 using namespace boost::random;
81
82 static uniform_real_distribution<number<mpfr_float_backend<length_in_digits>, et_on> > distribution(0,1);
83 static independent_bits_engine<mt19937, length_in_digits*1000L/301L, mpz_int> bit_generator;
84
85 mpfr_float a{distribution(bit_generator)};
86 return a;
87 }
88
95 template <unsigned int length_in_digits>
96 void RandomMpAssign(mpfr_float & a)
97 {
99 }
100
109 template <unsigned int length_in_digits>
110 mpfr_float RandomMp(const mpfr_float & left, const mpfr_float & right)
111 {
112 return (right-left)*RandomMp<length_in_digits>()+left;
113 }
114
115
116
120 mpfr_float RandomMp();
121
127 mpfr_float RandomMp(unsigned num_digits);
128
132 mpfr_float RandomMp(const mpfr_float & a, const mpfr_float & b);
133
137 mpfr_float RandomMp(const mpfr_float & a, const mpfr_float & b, unsigned num_digits);
138
144 void RandomMpAssign(mpfr_float & a, unsigned num_digits);
145
146
147
148
149} // re: namespace bertini
150
151
152
153
154namespace bertini{
155
156namespace multiprecision{
157
158
159using complex = bertini::mpfr_complex;
161
162
163
167 inline
168 void RandomRealAssign(complex & a, unsigned num_digits)
169 {
170 auto cached = DefaultPrecision();
171 DefaultPrecision(num_digits);
172 complex temp(RandomMp(mpfr_float(-1),mpfr_float(1),num_digits)); // ,0
173 a.swap(temp);
174 DefaultPrecision(cached);
175 }
176
180 inline complex RandomReal()
181 {
182 return complex(RandomMp(mpfr_float(-1),mpfr_float(1))); // ,0
183 }
184
188 inline complex RandomReal(unsigned num_digits)
189 {
190 auto cached = DefaultPrecision();
191 DefaultPrecision(num_digits);
192 auto result = complex(RandomMp(mpfr_float(-1),mpfr_float(1),num_digits));// ,0
193 DefaultPrecision(cached);
194 return result;
195 }
196
197
198
199
200
201
202
206 inline complex rand()
207 {
208 return complex( RandomMp(mpfr_float(-1),mpfr_float(1)), RandomMp(mpfr_float(-1),mpfr_float(1)) );
209 }
210
211
215 inline complex rand_unit()
216 {
217 complex returnme( RandomMp(mpfr_float(-1),mpfr_float(1)), RandomMp(mpfr_float(-1),mpfr_float(1)) );
218 return returnme / sqrt( abs(returnme));
219 }
220
221 inline complex RandomUnit()
222 {
223 return rand_unit();
224 }
225
226 inline
227 void rand_assign(complex & a, unsigned num_digits)
228 {
229 auto cached = DefaultPrecision();
230 DefaultPrecision(num_digits);
231
232 mpfr_complex temp( RandomMp(num_digits), RandomMp(num_digits) );
233 a = std::move(temp);
234 DefaultPrecision(cached);
235 }
236
237 inline
238 void RandomComplexAssign(complex & a, unsigned num_digits)
239 {
240 rand_assign(a,num_digits);
241 }
242
243 inline
244 complex RandomComplex(unsigned num_digits)
245 {
246 complex z;
247 RandomComplexAssign(z, num_digits);
248 return z;
249 }
250
251 inline
252 void RandomUnitAssign(complex & a, unsigned num_digits)
253 {
254 auto cached = DefaultPrecision();
255 DefaultPrecision(num_digits);
256 a.precision(num_digits);
257
258 complex temp(RandomMp(num_digits),RandomMp(num_digits));
259 a = std::move(temp/sqrt(abs(temp)));
260 DefaultPrecision(cached);
261 }
262
263 inline
264 complex RandomUnit(unsigned num_digits)
265 {
266 complex a;
267 RandomUnitAssign(a,num_digits);
268 return a;
269 }
270
271} // namespace multiprecision
272
273
274} // namespaces
275// }
276
277
278
279#endif
280
281
282
283
The main multiprecision complex number type. This is essentially boost::multiprecision' complex.
mpz_int RandomInt()
Definition random.hpp:48
complex rand_unit()
Definition random.hpp:215
mpfr_float RandomMp()
Definition random.hpp:76
complex RandomReal()
Definition random.hpp:180
void RandomRealAssign(complex &a, unsigned num_digits)
Definition random.hpp:168
complex rand()
Definition random.hpp:206
mpq_rational RandomRat()
Definition random.hpp:61
void RandomMpAssign(mpfr_float &a)
Definition random.hpp:96