Macaulay2 Engine
Loading...
Searching...
No Matches
aring-zz-flint.hpp
Go to the documentation of this file.
1// Copyright 2013 Michael E. Stillman
2
3#ifndef _aring_zz_flint_hpp_
4#define _aring_zz_flint_hpp_
5
39
40#include "interface/gmp-util.h" // for mpz_reallocate_limbs
41
42#include "aring.hpp"
43#include "buffer.hpp"
44#include "ringelem.hpp"
45#include "exceptions.hpp"
46#include "ZZ.hpp"
47
48// The following needs to be included before any flint files are included.
49#include <M2/gc-include.h>
50
51#pragma GCC diagnostic push
52#pragma GCC diagnostic ignored "-Wconversion"
53#include <flint/flint.h> // for flint_rand_t, fmpz, fmpz_t
54#include <flint/fmpz.h> // for fmpz_set_si, fmpz_pow_ui, fmpz_set_mpz
55#pragma GCC diagnostic pop
56
57namespace M2 {
63
64class ARingZZ : public SimpleARing<ARingZZ>
65{
66 public:
67 static const RingID ringID = ring_ZZFlint;
68
69 typedef fmpz ElementType;
71
72 ARingZZ();
73 ~ARingZZ();
74
75 public:
76 // ring informational
77 size_t characteristic() const { return 0; }
78 size_t cardinality() const { return static_cast<size_t>(-1); }
79 unsigned int computeHashValue(const ElementType& a) const
80 {
81 return static_cast<unsigned int>(fmpz_get_ui(&a));
82 }
83
87
88 bool is_unit(const ElementType& f) const
89 {
90 return fmpz_is_one(&f) or fmpz_cmp_si(&f, -1) == 0;
91 }
92 bool is_zero(const ElementType& f) const { return fmpz_is_zero(&f); }
94
97
98 bool is_equal(const ElementType& f, const ElementType& g) const
99 {
100 return fmpz_equal(&f, &g);
101 }
102 int compare_elems(const ElementType& f, const ElementType& g) const
103 {
104 int cmp = fmpz_cmp(&f, &g);
105 if (cmp > 0) return 1;
106 if (cmp < 0) return -1;
107 return 0;
108 }
109
110
113
114 void init_set(ElementType& result, const ElementType& a) const
115 {
116 fmpz_init_set(&result, &a);
117 }
118
119 void init(ElementType& result) const { fmpz_init(&result); }
120 static void clear(ElementType& result) { fmpz_clear(&result); }
121 void set(ElementType& result, const ElementType& a) const
122 {
123 fmpz_set(&result, &a);
124 }
125
126 void set_zero(ElementType& result) const { fmpz_set_si(&result, 0); }
127 void set_from_long(ElementType& result, long a) const
128 {
129 fmpz_set_si(&result, a);
130 }
131
132void set_from_mpz(ElementType& result, mpz_srcptr a) const
133 {
134 // printf("ARingZZ::calling set_from_mpz\n");
135 fmpz_set_mpz(&result, a);
136 }
137
138 bool set_from_mpq(ElementType& result, mpq_srcptr a) const
139 {
140 if (mpz_cmp_si(mpq_denref(a), 1) == 0)
141 {
142 set_from_mpz(result, mpq_numref(a));
143 return true;
144 }
145 return false;
146 }
147
149 {
150 (void) result;
151 (void) a;
152 return false;
153 }
154
155 void set_var(ElementType& result, int v) const
156 {
157 (void) v;
158 fmpz_set_si(&result, 1);
159 }
160
162
165 void negate(ElementType& result, const ElementType& a) const
166 {
167 fmpz_neg(&result, &a);
168 }
169
170 void invert(ElementType& result, const ElementType& a) const
171 {
172 if (is_unit(a))
173 set(result, a);
174 else
176 }
177
179 const ElementType& a,
180 const ElementType& b) const
181 {
182 fmpz_add(&result, &a, &b);
183 }
184
186 const ElementType& a,
187 const ElementType& b) const
188 {
189 fmpz_sub(&result, &a, &b);
190 }
191
193 const ElementType& a,
194 const ElementType& b) const
195 {
196 fmpz_submul(&result, &a, &b);
197 }
198
200 const ElementType& a,
201 const ElementType& b) const
202 {
203 fmpz_mul(&result, &a, &b);
204 }
205
208 const ElementType& a,
209 const ElementType& b) const
210 {
211 if (fmpz_divisible(&a, &b))
212 {
213 fmpz_divexact(&result, &a, &b);
214 return true;
215 }
216 else
217 return false;
218 }
219
221 const ElementType& a,
222 const unsigned long n) const
223 {
224 return fmpz_pow_ui(&result, &a, n);
225 }
226
228 const ElementType& a,
229 mpz_srcptr n) const
230 {
231 if (mpz_sgn(n) < 0) throw exc::engine_error("can only raise to a nonnegative power");
232 std::pair<bool, int> n1 = RingZZ::get_si(n);
233 if (n1.first)
234 fmpz_pow_ui(&result, &a, n1.second);
235 else
236 throw exc::engine_error("exponent too large");
237 }
238
239 void syzygy(const ElementType& a,
240 const ElementType& b,
241 ElementType& x,
242 ElementType& y) const;
244
247 void swap(ElementType& a, ElementType& b) const { fmpz_swap(&a, &b); }
249 {
250 fmpz_randm(&result, mRandomState, mMaxHeight);
251 }
252
253
257 void text_out(buffer& o) const { o << "ZZFlint"; }
258 void elem_text_out(buffer& o,
259 const ElementType& a,
260 bool p_one = true,
261 bool p_plus = false,
262 bool p_parens = false) const;
264
267
269 {
270 mpz_ptr b = getmemstructtype(mpz_ptr);
271 mpz_init(b);
272 fmpz_get_mpz(b, &a);
274 result = ring_elem(b);
275 }
276
278 {
279 fmpz_set_mpz(&result, a.get_mpz());
280 }
281
283 {
284 return PTR_TO_COEFF(a.get_mpz());
285 }
286
288
289 bool promote(const Ring* Rf, const ring_elem f, ElementType& result) const
290 {
291 (void) Rf;
292 (void) f;
293 (void) result;
294 return false;
295 }
296
297 bool lift(const Ring* Rg, const ElementType& f, ring_elem& result) const
298 {
299 (void) Rg;
300 (void) f;
301 (void) result;
302 return false;
303 }
304
305 // map : this --> target(map)
306 // primelem --> map->elem(first_var)
307 // evaluate map(f)
308 void eval(const RingMap* map,
309 const ElementType& f,
310 int first_var,
311 ring_elem& result) const;
312
313 bool coerceToLongInteger(long& result, const ElementType& n) const
314 {
315 result = fmpz_get_si(&n);
316 return fmpz_fits_si(&n);
317 }
318
319 private:
320 mutable flint_rand_t mRandomState;
322};
323};
324
325#endif
326
327// Local Variables:
328// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
329// indent-tabs-mode: nil
330// End:
Legacy RingZZ — a Ring-derived integer ring backed by GMP mpz_t.
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 subtract_multiple(ElementType &result, const ElementType &a, const ElementType &b) const
void mult(ElementType &result, const ElementType &a, const ElementType &b) const
void swap(ElementType &a, ElementType &b) const
void from_ring_elem(ElementType &result, const ring_elem &a) const
void subtract(ElementType &result, const ElementType &a, const ElementType &b) const
ElementType elem
void random(ElementType &result) const
bool is_zero(const ElementType &f) const
void add(ElementType &result, const ElementType &a, const ElementType &b) const
void text_out(buffer &o) const
bool promote(const Ring *Rf, const ring_elem f, ElementType &result) const
size_t cardinality() const
void set(ElementType &result, const ElementType &a) const
void invert(ElementType &result, const ElementType &a) const
bool is_unit(const ElementType &f) const
size_t characteristic() const
void init_set(ElementType &result, const ElementType &a) const
bool set_from_BigReal(ElementType &result, gmp_RR a) const
bool lift(const Ring *Rg, const ElementType &f, ring_elem &result) const
bool is_equal(const ElementType &f, const ElementType &g) const
unsigned int computeHashValue(const ElementType &a) const
void power_mpz(ElementType &result, const ElementType &a, mpz_srcptr n) const
bool coerceToLongInteger(long &result, const ElementType &n) const
void negate(ElementType &result, const ElementType &a) const
void set_zero(ElementType &result) const
static const RingID ringID
bool divide(ElementType &result, const ElementType &a, const ElementType &b) const
test doc
void syzygy(const ElementType &a, const ElementType &b, ElementType &x, ElementType &y) const
flint_rand_t mRandomState
void init(ElementType &result) const
void elem_text_out(buffer &o, const ElementType &a, bool p_one=true, bool p_plus=false, bool p_parens=false) const
int compare_elems(const ElementType &f, const ElementType &g) const
void set_var(ElementType &result, int v) const
void to_ring_elem(ring_elem &result, const ElementType &a) const
void eval(const RingMap *map, const ElementType &f, int first_var, ring_elem &result) const
bool set_from_mpq(ElementType &result, mpq_srcptr a) const
void power(ElementType &result, const ElementType &a, const unsigned long n) const
void set_from_long(ElementType &result, long a) const
static void clear(ElementType &result)
ElementType from_ring_elem_const(const ring_elem &a) const
void set_from_mpz(ElementType &result, mpz_srcptr a) const
A base class for simple ARings.
Definition aring.hpp:147
xxx xxx xxx
Definition ring.hpp:102
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.
void mpz_reallocate_limbs(mpz_ptr _z)
Definition gmp-util.h:46
Inline helpers that move GMP / MPFR / MPFI limbs from malloc-managed storage into the bdwgc heap.
RingID
Definition aring.hpp:75
@ ring_ZZFlint
Definition aring.hpp:78
VALGRIND_MAKE_MEM_DEFINED & result(result)
#define getmemstructtype(S)
Definition m2-mem.h:143
mpfr_srcptr gmp_RR
Definition m2-types.h:148
Definition aring-CC.cpp:3
volatile int x
ring_elem — the universal value type carried by every Ring* in the engine.
mpz_srcptr get_mpz() const
Definition ringelem.hpp:127