Bertini 2 - C++ Core
Software for Numerical Algebraic Geometry
Loading...
Searching...
No Matches
mpfr_complex.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.
2//
3//mpfr_complex.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//mpfr_complex.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 mpfr_complex.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
32#ifndef BERTINI_MPFR_COMPLEX_HPP
33#define BERTINI_MPFR_COMPLEX_HPP
34
35
36
37
38
39#include <boost/archive/text_oarchive.hpp>
40#include <boost/archive/text_iarchive.hpp>
41#include <boost/serialization/split_member.hpp>
42
43#include <string>
44#include <assert.h>
45
46
47
49
50
51
52
53#include <boost/multiprecision/mpc.hpp>
54
55namespace bertini{
56
57namespace bmp = boost::multiprecision;
58using bmp::backends::mpc_complex_backend;
59
60#ifdef BMP_EXPRESSION_TEMPLATES
61 using mpfr_complex = bmp::number<mpc_complex_backend<0>, bmp::et_on >;
62#else
63 using mpfr_complex = bmp::number<mpc_complex_backend<0>, bmp::et_off >;
64#endif
65
66 inline auto DefaultPrecisionPolicy(){
67 return bmp::variable_precision_options::preserve_source_precision;
68 }
69
70
71
72 // shamelessly adapted from the documentation for variable precision in Boost.Multiprecision.
73 // see https://www.boost.org/doc/libs/1_82_0/libs/multiprecision/doc/html/boost_multiprecision/tut/variable.html
74 struct scoped_mpfr_precision_options_this_thread
75 {
76 boost::multiprecision::variable_precision_options saved_options;
77
78 scoped_mpfr_precision_options_this_thread(boost::multiprecision::variable_precision_options opts) : saved_options(mpfr_float::thread_default_variable_precision_options())
79 {
80 mpfr_float::thread_default_variable_precision_options(opts);
81 }
82
83 ~scoped_mpfr_precision_options_this_thread()
84 {
85 mpfr_float::thread_default_variable_precision_options(saved_options);
86 }
87
88 void reset(boost::multiprecision::variable_precision_options opts)
89 {
90 mpfr_float::thread_default_variable_precision_options(opts);
91 }
92
93 };
94
95
96
97 struct scoped_mpfr_precision_options_all_threads
98 {
99 boost::multiprecision::variable_precision_options saved_options_all_threads;
100 boost::multiprecision::variable_precision_options saved_options_this_thread;
101
102 scoped_mpfr_precision_options_all_threads(boost::multiprecision::variable_precision_options opts) :
103 saved_options_all_threads(mpfr_float::default_variable_precision_options()),
104 saved_options_this_thread(mpfr_float::default_variable_precision_options())
105 {
106 mpfr_float::default_variable_precision_options(opts);
107 mpfr_float::thread_default_variable_precision_options(opts);
108 }
109
110 ~scoped_mpfr_precision_options_all_threads()
111 {
112 mpfr_float::default_variable_precision_options(saved_options_all_threads);
113 mpfr_float::thread_default_variable_precision_options(saved_options_this_thread);
114 }
115
116 void reset(boost::multiprecision::variable_precision_options opts)
117 {
118 mpfr_float::default_variable_precision_options(opts);
119 mpfr_float::thread_default_variable_precision_options(opts);
120 }
121
122 };
123
124
125
126
127
128
129 inline auto DefaultPrecision()
130 {
131 auto p = mpfr_float::default_precision();
132 assert(p==mpfr_complex::default_precision() && "precision of real and complex multiprecision numbers have drifted...");
133 return p;
134 }
135
136 inline void DefaultPrecision(unsigned prec)
137 {
138 mpfr_float::default_precision(prec);
139 mpfr_complex::default_precision(prec);
140 }
141
142
143}
144
145
146namespace boost { namespace serialization {
150 template <typename Archive>
151 void save(Archive& ar, ::boost::multiprecision::backends::mpc_complex_backend<0> const& r, unsigned /*version*/)
152 {
153 unsigned num_digits(r.precision());
154 ar & num_digits;
155 std::string tmp = r.str(0,std::ios::scientific);
156 ar & tmp;
157 }
158
162 template <typename Archive>
163 void load(Archive& ar, ::boost::multiprecision::backends::mpc_complex_backend<0>& r, unsigned /*version*/)
164 {
165 unsigned num_digits;
166 ar & num_digits;
167 r.precision(num_digits);
168 std::string tmp;
169 ar & tmp;
170 r = tmp.c_str();
171 }
172
173}} // re: namespace boost::serialization
174
175
176BOOST_SERIALIZATION_SPLIT_FREE(::boost::multiprecision::backends::mpc_complex_backend<0>);
177
178
179
180
181
182
183namespace bertini{
184
190 inline
191 auto Precision(mpfr_complex const& num)
192 {
193 return num.precision();
194 }
195
196
202 inline void Precision(mpfr_complex & num, unsigned prec)
203 {
204 num.precision(prec);
205 }
206
207 inline
208 bool isnan(mpfr_complex const& num){return isnan(num.real()) || isnan(num.imag());};
209
210
211
212 using std::polar;
213} // re: namespace bertini
214
215
216
217
218
219
220
221
222
223
224
225#endif
226
227
228
229
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
void load(Archive &ar, ::boost::multiprecision::backends::mpc_complex_backend< 0 > &r, unsigned)
Definition mpfr_complex.hpp:163
void save(Archive &ar, ::boost::multiprecision::backends::mpc_complex_backend< 0 > const &r, unsigned)
Definition mpfr_complex.hpp:151
Extensions to the Boost.Multiprecision library.