Macaulay2 Engine
Loading...
Searching...
No Matches
aring-RRR.hpp
Go to the documentation of this file.
1// Copyright 2012 Michael E. Stillman
2
3#ifndef _aring_RRR_hpp_
4#define _aring_RRR_hpp_
5
43
44#include "interface/gmp-util.h" // for moveTo_gmpRR
45#include "interface/random.h" // for randomMpfr
46
47#include "aring.hpp"
48#include "buffer.hpp"
49#include "ringelem.hpp"
50#include "ringmap.hpp"
51
52class RRR;
53class RingMap;
54
55namespace M2 {
69class ARingRRR : public SimpleARing<ARingRRR>
70{
71 // Higher precision real numbers
72
73 public:
74 static const RingID ringID = ring_RRR;
75
76 typedef __mpfr_struct elem;
78
79 ARingRRR(unsigned long precision) : mPrecision(precision) {}
80 // ring informational
81 size_t characteristic() const { return 0; }
82 unsigned long get_precision() const { return mPrecision; }
83 void text_out(buffer &o) const;
84
85 unsigned int computeHashValue(const elem &a) const
86 {
87 double d = mpfr_get_d(&a, MPFR_RNDN);
88 return static_cast<unsigned int>(d);
89 }
90
92 // ElementType informational ////
94
95 bool is_unit(const ElementType &f) const { return !is_zero(f); }
96 bool is_zero(const ElementType &f) const { return mpfr_cmp_si(&f, 0) == 0; }
97 bool is_equal(const ElementType &f, const ElementType &g) const
98 {
99 return mpfr_cmp(&f, &g) == 0;
100 }
101
102 int compare_elems(const ElementType &f, const ElementType &g) const
103 {
104 int cmp = mpfr_cmp(&f, &g);
105 if (cmp < 0) return -1;
106 if (cmp > 0) return 1;
107 return 0;
108 }
109
111 // to/from ringelem ////////
113 // These simply repackage the element as either a ringelem or an
114 // 'ElementType'.
115 // No reinitialization is done.
116 // Do not take the same element and store it as two different ring_elem's!!
118 {
119 mpfr_ptr res = getmemstructtype(mpfr_ptr);
120 mpfr_init2(res, mPrecision);
121 mpfr_set(res, &a, MPFR_RNDN);
123 }
124
126 {
127 mpfr_set(&result, a.get_mpfr(), MPFR_RNDN);
128 }
129
131 {
132 return *a.get_mpfr();
133 }
134
135 // 'init', 'init_set' functions
136
137 void init(ElementType &result) const { mpfr_init2(&result, mPrecision); }
138 void init_set(ElementType &result, const ElementType &a) const
139 {
140 init(result);
141 mpfr_set(&result, &a, MPFR_RNDN);
142 }
143
144 void set(ElementType &result, const ElementType &a) const
145 {
146 mpfr_set(&result, &a, MPFR_RNDN);
147 }
148
150 {
151 mpfr_set_si(&result, 0, MPFR_RNDN);
152 }
153
154 static void clear(ElementType &result) { mpfr_clear(&result); }
155 void copy(ElementType &result, const ElementType &a) const
156 {
157 mpfr_set(&result, &a, MPFR_RNDN);
158 }
159
160 void set_from_long(ElementType &result, long a) const
161 {
162 mpfr_set_si(&result, a, MPFR_RNDN);
163 }
164
165 void set_var(ElementType &result, int v) const
166 {
167 (void) v;
168 mpfr_set_si(&result, 1, MPFR_RNDN);
169 }
170
171 void set_from_mpz(ElementType &result, mpz_srcptr a) const
172 {
173 mpfr_set_z(&result, a, MPFR_RNDN);
174 }
175
176 bool set_from_mpq(ElementType &result, mpq_srcptr a) const
177 {
178 mpfr_set_q(&result, a, MPFR_RNDN);
179 return true;
180 }
181
182 bool set_from_double(ElementType &result, double a) const
183 {
184 mpfr_set_d(&result, a, MPFR_RNDN);
185 return true;
186 }
188 {
189 mpfr_set(&result, a, MPFR_RNDN);
190 return true;
191 }
192
193 // arithmetic
194 void negate(ElementType &result, const ElementType &a) const
195 {
196 mpfr_neg(&result, &a, MPFR_RNDN);
197 }
198
199 void invert(ElementType &result, const ElementType &a) const
200 // we silently assume that a != 0. If it is, result is set to a^0, i.e. 1
201 {
202 mpfr_si_div(&result, 1, &a, MPFR_RNDN);
203 }
204
206 const ElementType &a,
207 const ElementType &b) const
208 {
209 mpfr_add(&result, &a, &b, MPFR_RNDN);
210 }
211
213 const ElementType &a,
214 const ElementType &b) const
215 {
216 mpfr_fma(&result, &a, &b, &result, MPFR_RNDN);
217 }
218
220 const ElementType &a,
221 const ElementType &b) const
222 {
223 mpfr_sub(&result, &a, &b, MPFR_RNDN);
224 }
225
227 const ElementType &a,
228 const ElementType &b) const
229 {
230 // result -= a*b
231 ElementType ab;
232 init(ab);
233 mult(ab, a, b);
234 subtract(result, result, ab);
235 clear(ab);
236 }
237
239 const ElementType &a,
240 const ElementType &b) const
241 {
242 mpfr_mul(&result, &a, &b, MPFR_RNDN);
243 }
244
246 const ElementType &a,
247 const ElementType &b) const
248 {
249 mpfr_div(&result, &a, &b, MPFR_RNDN);
250 }
251
252 void power(ElementType &result, const ElementType &a, int n) const
253 {
254 mpfr_pow_si(&result, &a, n, MPFR_RNDN);
255 }
256
257 void power_mpz(ElementType &result, const ElementType &a, mpz_srcptr n) const
258 {
259 mpfr_pow_z(&result, &a, n, MPFR_RNDN);
260 }
261
262 void swap(ElementType &a, ElementType &b) const { mpfr_swap(&a, &b); }
263 void elem_text_out(buffer &o,
264 const ElementType &a,
265 bool p_one,
266 bool p_plus,
267 bool p_parens) const;
268
269 void syzygy(const ElementType &a,
270 const ElementType &b,
271 ElementType &x,
272 ElementType &y) const // remove?
273 // returns x,y s.y. x*a + y*b == 0.
274 // if possible, x is set to 1.
275 // no need to consider the case a==0 or b==0.
276 {
277 set_var(x, 0); // set x=1
278 if (!is_zero(b))
279 {
280 set(y, a);
281 negate(y, y);
282 divide(y, y, b);
283 }
284 }
285
286 void random(ElementType &result) const // redo?
287 {
289 }
290
291 void eval(const RingMap *map,
292 ElementType &f,
293 int first_var,
294 ring_elem &result) const
295 {
296 (void) first_var;
297 if (!map->get_ring()->from_BigReal(&f, result))
298 {
299 result = map->get_ring()->from_long(0);
300 ERROR("cannot coerce RRR value to ring type");
301 }
302 }
303
304 void zeroize_tiny(gmp_RR epsilon, ElementType &a) const
305 {
306 if (mpfr_cmpabs(&a, epsilon) < 0) set_zero(a);
307 }
308 void increase_norm(gmp_RRmutable norm, const ElementType &a) const
309 {
310 if (mpfr_cmpabs(&a, norm) > 0)
311 {
312 set(*norm, a);
313 abs(*norm, *norm);
314 }
315 }
316
318 {
319 mult(result, a, a);
320 }
321
322 void abs(ElementType &result, const ElementType &a) const
323 {
324 if (mpfr_cmp_si(&a, 0) < 0)
325 negate(result, a);
326 else
327 set(result, a);
328 }
329
330 double coerceToDouble(const ElementType &a) const
331 {
332 return mpfr_get_d(&a, MPFR_RNDN);
333 }
334
335 private:
336 unsigned long mPrecision;
337};
338
339}; // end namespace M2
340
341#endif
342
343// Local Variables:
344// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
345// indent-tabs-mode: nil
346// End:
Shared base of the aring framework (namespace M2) that unifies the engine's coefficient rings.
Append-only GC-backed byte buffer used throughout the engine for text output.
size_t characteristic() const
Definition aring-RRR.hpp:81
void increase_norm(gmp_RRmutable norm, const ElementType &a) const
bool set_from_BigReal(ElementType &result, gmp_RR a) const
void power_mpz(ElementType &result, const ElementType &a, mpz_srcptr n) const
void random(ElementType &result) const
void negate(ElementType &result, const ElementType &a) const
void invert(ElementType &result, const ElementType &a) const
bool is_equal(const ElementType &f, const ElementType &g) const
Definition aring-RRR.hpp:97
unsigned long mPrecision
void subtract_multiple(ElementType &result, const ElementType &a, const ElementType &b) const
bool is_zero(const ElementType &f) const
Definition aring-RRR.hpp:96
void set_from_long(ElementType &result, long a) const
void addMultipleTo(ElementType &result, const ElementType &a, const ElementType &b) const
void eval(const RingMap *map, ElementType &f, int first_var, ring_elem &result) const
void set(ElementType &result, const ElementType &a) const
void abs(ElementType &result, const ElementType &a) const
ARingRRR(unsigned long precision)
Definition aring-RRR.hpp:79
void from_ring_elem(ElementType &result, const ring_elem &a) const
void divide(ElementType &result, const ElementType &a, const ElementType &b) const
double coerceToDouble(const ElementType &a) const
int compare_elems(const ElementType &f, const ElementType &g) const
void to_ring_elem(ring_elem &result, const ElementType &a) const
void subtract(ElementType &result, const ElementType &a, const ElementType &b) const
void copy(ElementType &result, const ElementType &a) const
bool is_unit(const ElementType &f) const
Definition aring-RRR.hpp:95
void mult(ElementType &result, const ElementType &a, const ElementType &b) const
static void clear(ElementType &result)
void power(ElementType &result, const ElementType &a, int n) const
void syzygy(const ElementType &a, const ElementType &b, ElementType &x, ElementType &y) const
void init_set(ElementType &result, const ElementType &a) const
__mpfr_struct elem
Definition aring-RRR.hpp:76
const ElementType & from_ring_elem_const(const ring_elem &a) const
bool set_from_double(ElementType &result, double a) const
void init(ElementType &result) const
void set_from_mpz(ElementType &result, mpz_srcptr a) const
void text_out(buffer &o) const
Definition aring-RRR.cpp:5
void set_zero(ElementType &result) const
void abs_squared(ElementType &result, const ElementType &a) const
bool set_from_mpq(ElementType &result, mpq_srcptr a) const
void add(ElementType &result, const ElementType &a, const ElementType &b) const
void zeroize_tiny(gmp_RR epsilon, ElementType &a) const
void swap(ElementType &a, ElementType &b) const
unsigned int computeHashValue(const elem &a) const
Definition aring-RRR.hpp:85
unsigned long get_precision() const
Definition aring-RRR.hpp:82
void elem_text_out(buffer &o, const ElementType &a, bool p_one, bool p_plus, bool p_parens) const
Definition aring-RRR.cpp:6
static const RingID ringID
Definition aring-RRR.hpp:74
void set_var(ElementType &result, int v) const
A base class for simple ARings.
Definition aring.hpp:147
virtual bool from_BigReal(gmp_RR a, ring_elem &result) const
Definition ring.cpp:250
virtual ring_elem from_long(long n) const =0
const Ring * get_ring() const
Definition ringmap.hpp:111
Engine-side ring homomorphism: stores, for each source-ring variable, the target-ring element it maps...
Definition ringmap.hpp:60
mpfr_srcptr moveTo_gmpRR(mpfr_ptr _z)
Definition gmp-util.h:153
Inline helpers that move GMP / MPFR / MPFI limbs from malloc-managed storage into the bdwgc heap.
RingID
Definition aring.hpp:75
@ ring_RRR
Definition aring.hpp:89
const int ERROR
Definition m2-mem.cpp:55
VALGRIND_MAKE_MEM_DEFINED & result(result)
#define getmemstructtype(S)
Definition m2-mem.h:143
mpfr_srcptr gmp_RR
Definition m2-types.h:148
mpfr_ptr gmp_RRmutable
Definition m2-types.h:150
Definition aring-CC.cpp:3
volatile int x
#define abs(x)
Definition polyroots.cpp:51
void randomMpfr(mpfr_t result)
Definition random.cpp:240
Engine-boundary C API for the engine's PRNG and rational / real / complex random draws.
ring_elem — the universal value type carried by every Ring* in the engine.
RingMap — engine representation of a ring homomorphism.
mpfr_srcptr get_mpfr() const
Definition ringelem.hpp:130