Macaulay2 Engine
Loading...
Searching...
No Matches
aring-zz-gmp.cpp
Go to the documentation of this file.
1// Copyright 2013 Michael E. Stillman
2
3#include "aring-zz-gmp.hpp"
4#include "ringmap.hpp"
5
6namespace M2 {
7
9// This function will likely not ever get called.
11void ARingZZGMP::eval(const RingMap* map,
12 const ElementType& f,
13 int first_var,
14 ring_elem& result) const
15{
16 (void) first_var;
17 mpz_ptr f1 = static_cast<mpz_ptr>(const_cast<ElementType*>(&f));
18 result = map->get_ring()->from_int(f1);
19}
20
22 const ElementType& a,
23 bool p_one,
24 bool p_plus,
25 bool p_parens) const
26{
27 char* str;
28
29 (void) p_parens;
30 bool is_neg = (mpz_cmp_si(&a, 0) == -1);
31 bool is_one = (mpz_cmp_si(&a, 1) == 0 || mpz_cmp_si(&a, -1) == 0);
32
33 if (!is_neg && p_plus) o << '+';
34 if (is_one)
35 {
36 if (is_neg) o << '-';
37 if (p_one) o << '1';
38 }
39 else
40 {
41 str = mpz_get_str(static_cast<char*>(nullptr), 10, &a);
42 o << str;
43 delete str;
44 }
45}
46
48 const ElementType& b,
50 ElementType& y) const
51{
52 assert(!is_zero(b));
53 // First check the special cases a = 0, b = 1, -1. Other cases: use gcd.
54 if (is_zero(a))
55 {
56 set_from_long(x, 1);
57 set_zero(y);
58 return;
59 }
60 if (mpz_cmp_ui(&b, 1) == 0)
61 {
62 set_from_long(x, 1);
63 negate(y, a);
64 return;
65 }
66 if (mpz_cmp_si(&b, -1) == 0)
67 {
68 set_from_long(x, 1);
69 set(y, a);
70 return;
71 }
72 elem g;
73 init(g);
74 mpz_gcd(&g, &a, &b);
75 divide(y, a, g);
76 divide(x, b, g);
77 if (mpz_sgn(&x) > 0)
78 negate(y, y);
79 else
80 negate(x, x);
81 clear(g);
82}
83};
84
85// Local Variables:
86// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
87// indent-tabs-mode: nil
88// End:
M2::ARingZZGMP — aring integer ring backed straight by GMP mpz_t.
void init(ElementType &result) const
void eval(const RingMap *map, const ElementType &f, int first_var, ring_elem &result) const
ElementType elem
void syzygy(const ElementType &a, const ElementType &b, ElementType &x, ElementType &y) const
static void clear(ElementType &result)
void set(ElementType &result, const ElementType &a) const
void set_zero(ElementType &result) const
__mpz_struct ElementType
void elem_text_out(buffer &o, const ElementType &a, bool p_one=true, bool p_plus=false, bool p_parens=false) const
void negate(ElementType &result, const ElementType &a) const
void divide(ElementType &result, const ElementType &a, const ElementType &b) const
exact division of integers.
void set_from_long(ElementType &result, long a) const
bool is_zero(const ElementType &f) const
virtual ring_elem from_int(mpz_srcptr 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
VALGRIND_MAKE_MEM_DEFINED & result(result)
Definition aring-CC.cpp:3
volatile int x
RingMap — engine representation of a ring homomorphism.