Macaulay2 Engine
Loading...
Searching...
No Matches
aring-RR.hpp
Go to the documentation of this file.
1// Copyright 2012 Michael E. Stillman
2
3#ifndef _aring_RR_hpp_
4#define _aring_RR_hpp_
5
38
39#include "interface/random.h"
40#include "exceptions.hpp"
41#include "aring.hpp"
42#include "buffer.hpp"
43#include "ringelem.hpp"
44#include "ringmap.hpp"
45
46class RingMap;
47
48namespace M2 {
61class ARingRR : public SimpleARing<ARingRR>
62{
63 // approximate real numbers, implemented as doubles.
64 public:
65 static const RingID ringID = ring_RR;
66
67 typedef double elem;
69
71 // ring informational
72 size_t characteristic() const { return 0; }
73 unsigned long get_precision() const { return 53; }
74 void text_out(buffer &o) const;
75
76 unsigned int computeHashValue(const elem &a) const
77 {
78 return static_cast<unsigned int>(a);
79 }
80
82 // ElementType informational ////
84
85 bool is_unit(const ElementType &f) const { return !is_zero(f); }
86 bool is_zero(const ElementType &f) const { return f == 0.0; }
87 bool is_equal(const ElementType &f, const ElementType &g) const
88 {
89 return f == g;
90 }
91
92 int compare_elems(const ElementType &f, const ElementType &g) const
93 {
94 double cmp = f - g;
95 if (cmp < 0) return -1;
96 if (cmp > 0) return 1;
97 return 0;
98 }
99
101 // to/from ringelem ////////
103 // These simply repackage the element as either a ringelem or an
104 // 'ElementType'.
105 // No reinitialization is done.
106 // Do not take the same element and store it as two different ring_elem's!!
108 {
109 result = ring_elem(a);
110 }
111
113 {
114 result = a.get_double();
115 }
116
118 {
119 return a.get_double();
120 }
121
122 // 'init', 'init_set' functions
123
124 void init(ElementType &result) const { result = 0.0; }
125 void init_set(ElementType &result, const ElementType &a) const { result = a; }
126 void set(ElementType &result, const ElementType &a) const { result = a; }
127 void set_zero(ElementType &result) const { result = 0.0; }
129 {
130 (void) result;
131 }
132
133 void copy(ElementType &result, const ElementType &a) const { set(result, a); }
134 void set_from_long(ElementType &result, long a) const
135 {
136 result = static_cast<double>(a);
137 }
138
139 void set_var(ElementType &result, int v) const
140 {
141 (void) v;
142 result = 1.0;
143 }
144
145 void set_from_mpz(ElementType &result, mpz_srcptr a) const
146 {
147 result = mpz_get_d(a);
148 }
149
150 bool set_from_mpq(ElementType &result, mpq_srcptr a) const
151 {
152 result = mpq_get_d(a);
153 return true;
154 }
155
157 {
158 result = mpfr_get_d(a, MPFR_RNDN);
159 return true;
160 }
161 bool set_from_double(ElementType &result, double a) const
162 {
163 result = a;
164 return true;
165 }
166
167 // arithmetic
168 void negate(ElementType &result, const ElementType &a) const { result = -a; }
169 void invert(ElementType &result, const ElementType &a) const
170 // we silently assume that a != 0. If it is, result is set to a^0, i.e. 1
171 {
172 result = 1.0 / a;
173 }
174
176 const ElementType &a,
177 const ElementType &b) const
178 {
179 result = a + b;
180 }
181
183 const ElementType &a,
184 const ElementType &b) const
185 {
186 result += a * b;
187 }
188
190 const ElementType &a,
191 const ElementType &b) const
192 {
193 result = a - b;
194 }
195
197 const ElementType &a,
198 const ElementType &b) const
199 {
200 result -= a * b;
201 }
202
204 const ElementType &a,
205 const ElementType &b) const
206 {
207 result = a * b;
208 }
209
211 const ElementType &a,
212 const ElementType &b) const
213 {
214 result = a / b;
215 }
216
218 {
219 result = a * a;
220 }
221
222 void abs(ElementType &result, const ElementType &a) const
223 {
224 result = fabs(a);
225 }
226
227 void power(ElementType &result, const ElementType &a, int n) const
228 {
229 result = pow(a, n);
230 }
231
232 void power_mpz(ElementType &result, const ElementType &a, mpz_srcptr n) const
233 {
234 std::pair<bool, int> n1 = RingZZ::get_si(n);
235 if (n1.first)
236 power(result, a, n1.second);
237 else
238 throw exc::engine_error("exponent too large");
239 }
240
241 void swap(ElementType &a, ElementType &b) const { std::swap(a, b); }
242 void elem_text_out(buffer &o,
243 const ElementType &a,
244 bool p_one = true,
245 bool p_plus = false,
246 bool p_parens = false) const;
247
248 void syzygy(const ElementType &a,
249 const ElementType &b,
250 ElementType &x,
251 ElementType &y) const // remove?
252 // returns x,y s.y. x*a + y*b == 0.
253 // if possible, x is set to 1.
254 // no need to consider the case a==0 or b==0.
255 {
256 // TODO: remove this?
257 set_var(x, 0); // set x=1
258 if (!is_zero(b))
259 {
260 set(y, a);
261 negate(y, y);
262 divide(y, y, b);
263 }
264 }
265
266 void random(ElementType &result) const // redo?
267 {
269 }
270
271 void eval(const RingMap *map,
272 ElementType &f,
273 int first_var,
274 ring_elem &result) const
275 {
276 (void) first_var;
277 if (!map->get_ring()->from_double(f, result))
278 {
279 result = map->get_ring()->from_long(0);
280 ERROR("cannot map double to ring type");
281 }
282 }
283
284 void zeroize_tiny(gmp_RR epsilon, ElementType &a) const
285 {
286 if (mpfr_cmp_d(epsilon, fabs(a)) > 0) set_zero(a);
287 }
288
289 void increase_norm(mpfr_ptr norm, const ElementType &a) const
290 {
291 double d = fabs(a);
292 if (mpfr_cmp_d(norm, d) < 0) mpfr_set_d(norm, d, MPFR_RNDN);
293 }
294
295 double coerceToDouble(const ElementType &a) const { return a; }
296};
297
298}; // end namespace M2
299
300#endif
301
302// Local Variables:
303// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
304// indent-tabs-mode: nil
305// 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.
void init(ElementType &result) const
Definition aring-RR.hpp:124
void set_zero(ElementType &result) const
Definition aring-RR.hpp:127
bool set_from_mpq(ElementType &result, mpq_srcptr a) const
Definition aring-RR.hpp:150
void to_ring_elem(ring_elem &result, const ElementType &a) const
Definition aring-RR.hpp:107
double elem
Definition aring-RR.hpp:67
int compare_elems(const ElementType &f, const ElementType &g) const
Definition aring-RR.hpp:92
ElementType from_ring_elem_const(const ring_elem &a) const
Definition aring-RR.hpp:117
void mult(ElementType &result, const ElementType &a, const ElementType &b) const
Definition aring-RR.hpp:203
void init_set(ElementType &result, const ElementType &a) const
Definition aring-RR.hpp:125
void divide(ElementType &result, const ElementType &a, const ElementType &b) const
Definition aring-RR.hpp:210
void copy(ElementType &result, const ElementType &a) const
Definition aring-RR.hpp:133
elem ElementType
Definition aring-RR.hpp:68
bool is_zero(const ElementType &f) const
Definition aring-RR.hpp:86
void text_out(buffer &o) const
Definition aring-RR.cpp:5
void swap(ElementType &a, ElementType &b) const
Definition aring-RR.hpp:241
double coerceToDouble(const ElementType &a) const
Definition aring-RR.hpp:295
void elem_text_out(buffer &o, const ElementType &a, bool p_one=true, bool p_plus=false, bool p_parens=false) const
Definition aring-RR.cpp:6
void negate(ElementType &result, const ElementType &a) const
Definition aring-RR.hpp:168
bool set_from_BigReal(ElementType &result, gmp_RR a) const
Definition aring-RR.hpp:156
bool is_unit(const ElementType &f) const
Definition aring-RR.hpp:85
void set(ElementType &result, const ElementType &a) const
Definition aring-RR.hpp:126
size_t characteristic() const
Definition aring-RR.hpp:72
void increase_norm(mpfr_ptr norm, const ElementType &a) const
Definition aring-RR.hpp:289
bool set_from_double(ElementType &result, double a) const
Definition aring-RR.hpp:161
void subtract_multiple(ElementType &result, const ElementType &a, const ElementType &b) const
Definition aring-RR.hpp:196
static void clear(ElementType &result)
Definition aring-RR.hpp:128
unsigned long get_precision() const
Definition aring-RR.hpp:73
void abs(ElementType &result, const ElementType &a) const
Definition aring-RR.hpp:222
void set_var(ElementType &result, int v) const
Definition aring-RR.hpp:139
void power(ElementType &result, const ElementType &a, int n) const
Definition aring-RR.hpp:227
void from_ring_elem(ElementType &result, const ring_elem &a) const
Definition aring-RR.hpp:112
unsigned int computeHashValue(const elem &a) const
Definition aring-RR.hpp:76
void set_from_long(ElementType &result, long a) const
Definition aring-RR.hpp:134
void abs_squared(ElementType &result, const ElementType &a) const
Definition aring-RR.hpp:217
void power_mpz(ElementType &result, const ElementType &a, mpz_srcptr n) const
Definition aring-RR.hpp:232
void invert(ElementType &result, const ElementType &a) const
Definition aring-RR.hpp:169
static const RingID ringID
Definition aring-RR.hpp:65
void add(ElementType &result, const ElementType &a, const ElementType &b) const
Definition aring-RR.hpp:175
void subtract(ElementType &result, const ElementType &a, const ElementType &b) const
Definition aring-RR.hpp:189
void zeroize_tiny(gmp_RR epsilon, ElementType &a) const
Definition aring-RR.hpp:284
void addMultipleTo(ElementType &result, const ElementType &a, const ElementType &b) const
Definition aring-RR.hpp:182
void random(ElementType &result) const
Definition aring-RR.hpp:266
void syzygy(const ElementType &a, const ElementType &b, ElementType &x, ElementType &y) const
Definition aring-RR.hpp:248
void eval(const RingMap *map, ElementType &f, int first_var, ring_elem &result) const
Definition aring-RR.hpp:271
void set_from_mpz(ElementType &result, mpz_srcptr a) const
Definition aring-RR.hpp:145
bool is_equal(const ElementType &f, const ElementType &g) const
Definition aring-RR.hpp:87
A base class for simple ARings.
Definition aring.hpp:147
virtual ring_elem from_long(long n) const =0
virtual bool from_double(double a, ring_elem &result) const
Definition ring.cpp:270
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
static std::pair< bool, int > get_si(mpz_srcptr n)
Definition ZZ.cpp:46
namespace exc — internal C++ exception types and the TRY / CATCH macro pair.
RingID
Definition aring.hpp:75
@ ring_RR
Definition aring.hpp:87
const int ERROR
Definition m2-mem.cpp:55
VALGRIND_MAKE_MEM_DEFINED & result(result)
mpfr_srcptr gmp_RR
Definition m2-types.h:148
Definition aring-CC.cpp:3
void swap(mpfr::mpreal &x, mpfr::mpreal &y)
Definition mpreal.h:3244
volatile int x
double randomDouble()
Definition random.cpp:248
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.
double get_double() const
Definition ringelem.hpp:126