Macaulay2 Engine
Loading...
Searching...
No Matches
aring-qq-gmp.cpp
Go to the documentation of this file.
1// Copyright 2013 Michael E. Stillman
2
3#include "aring-qq-gmp.hpp"
4
5#include "interface/random.h"
6#include "ringmap.hpp"
7
8namespace M2 {
9
11{
12 gmp_randinit_default(mRandomState);
13 mMaxHeight = 50;
14}
15
16// This function will likely not ever get called.
18void ARingQQGMP::eval(const RingMap* map,
19 const ElementType& f,
20 int first_var,
21 ring_elem& result) const
22{
23 (void) first_var;
24 bool ok = map->get_ring()->from_rational(&f, result);
25 if (!ok)
26 {
27 // if there is already an error message don't add in another
28 throw exc::engine_error("cannot map rational to this ring");
29 }
30}
31
33 const ElementType& a,
34 bool p_one,
35 bool p_plus,
36 bool p_parens) const
37{
38 char s[1000];
39 char* str;
40
41 (void) p_parens;
42 bool is_neg = (mpq_sgn(&a) == -1);
43 bool one = is_pm_one(a);
44
45 size_t size = mpz_sizeinbase(mpq_numref(&a), 10) +
46 mpz_sizeinbase(mpq_denref(&a), 10) + 3;
47
48 char* allocstr = (size > 1000 ? newarray_atomic(char, size) : s);
49
50 if (!is_neg && p_plus) o << '+';
51 if (one)
52 {
53 if (is_neg) o << '-';
54 if (p_one) o << '1';
55 }
56 else
57 {
58 str = mpq_get_str(allocstr, 10, &a);
59 o << str;
60 }
61 if (size > 1000) freemem(allocstr);
62}
63
65 const ElementType& b,
67 ElementType& y) const
68{
69 assert(!is_zero(b));
70 set_from_long(x, 1);
71 divide(y, a, b);
72 negate(y, y);
73}
74};
75
76// Local Variables:
77// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
78// indent-tabs-mode: nil
79// End:
M2::ARingQQGMP — arbitrary-precision rationals backed straight by GMP mpq_t.
void negate(ElementType &result, const ElementType &a) const
void syzygy(const ElementType &a, const ElementType &b, ElementType &x, ElementType &y) const
void set_from_long(ElementType &result, long a) const
void divide(ElementType &result, const ElementType &a, const ElementType &b) const
test doc
bool is_pm_one(const ElementType &f) const
gmp_randstate_t mRandomState
void elem_text_out(buffer &o, const ElementType &a, bool p_one=true, bool p_plus=false, bool p_parens=false) const
void eval(const RingMap *map, const ElementType &f, int first_var, ring_elem &result) const
bool is_zero(const ElementType &f) const
__mpq_struct ElementType
virtual bool from_rational(const mpq_srcptr q, ring_elem &result) 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
void freemem(void *s)
Definition m2-mem.cpp:103
void size_t s
Definition m2-mem.cpp:271
VALGRIND_MAKE_MEM_DEFINED & result(result)
Definition aring-CC.cpp:3
#define newarray_atomic(T, len)
Definition newdelete.hpp:91
volatile int x
Engine-boundary C API for the engine's PRNG and rational / real / complex random draws.
RingMap — engine representation of a ring homomorphism.