Macaulay2 Engine
Loading...
Searching...
No Matches
reducedgb-ZZ.cpp
Go to the documentation of this file.
1// Copyright 2005, Michael E. Stillman
2
3#include "reducedgb-ZZ.hpp"
4#include "monideal.hpp"
5#include <functional>
6#include <algorithm>
7#include "text-io.hpp"
9{
10 delete T;
11 ringtableZZ = nullptr;
12}
13
15 const PolynomialRing *originalR0,
16 const FreeModule *F0,
17 const FreeModule *Fsyz0)
18 : ReducedGB(R0, originalR0, F0, Fsyz0),
19 T(nullptr),
20 ringtableZZ(nullptr)
21{
23 if (originalR->is_quotient_ring())
24 ringtableZZ = originalR->get_quotient_MonomialTableZZ();
25}
26
27void ReducedGB_ZZ::set_gb(VECTOR(POLY) & polys0) { (void) polys0; }
43{
45 const FreeModule *F;
46 const VECTOR(POLY) & gb;
48 const FreeModule *F0,
49 const VECTOR(POLY) & gb0)
50 : R(R0), F(F0), gb(gb0)
51 {
52 }
53 bool operator()(int xx, int yy)
54 {
55 gbvector *x = gb[xx].f;
56 gbvector *y = gb[yy].f;
57 int cmp = R->gbvector_compare(F, x, y);
58 if (cmp == LT) return true;
59 if (cmp == GT) return false;
60 // Now order them in ascending order on the coeff (which should always be
61 // POSITIVE).
62 return (mpz_cmp(x->coeff.get_mpz(), y->coeff.get_mpz()) < 0);
63 }
64};
65
66void ReducedGB_ZZ::minimalize(const VECTOR(POLY) & polys0, bool auto_reduced)
67// I have to decide: does this ADD to the existing set?
68{
69 // First sort these elements via increasing lex order (or monomial order?)
70 // Next insert minimal elements into T, and polys
71
72 VECTOR(int) positions;
73 positions.reserve(polys0.size());
74
75 for (int i = 0; i < polys0.size(); i++) positions.push_back(i);
76
77 std::stable_sort(
78 positions.begin(), positions.end(), ReducedGB_ZZ_sorter(R, F, polys0));
79
80 // Now loop through each element, and see if the lead monomial is in T.
81 // If not, add it in , and place element into 'polys'.
82
83 for (VECTOR(int)::iterator i = positions.begin(); i != positions.end(); i++)
84 {
85 gbvector *f = polys0[*i].f;
86 exponents_t e = R->exponents_make();
87 R->gbvector_get_lead_exponents(F, f, e);
88 if ((!ringtableZZ ||
89 !ringtableZZ->find_term_divisors(1, f->coeff.get_mpz(), e, 1)) &&
90 T->find_term_divisors(1, f->coeff.get_mpz(), e, f->comp) == 0)
91 {
92 // Keep this element
93
94 POLY h;
95 ring_elem junk;
96
97 h.f = R->gbvector_copy(f);
98 h.fsyz = R->gbvector_copy(polys0[*i].fsyz);
99
100 if (auto_reduced) remainder(h, false, junk); // This auto-reduces h.
101
102 if (h.f != nullptr && mpz_sgn(h.f->coeff.get_mpz()) < 0)
103 {
104 R->gbvector_mult_by_coeff_to(h.f, globalZZ->minus_one());
105 R->gbvector_mult_by_coeff_to(h.fsyz, globalZZ->minus_one());
106 }
107
108 T->insert(h.f->coeff.get_mpz(), e, h.f->comp, INTSIZE(polys));
109 polys.push_back(h);
110 }
111 else
112 R->exponents_delete(e);
113 }
114}
115
117 int comp,
118 int &result_loc)
119{
120 int w = T->find_smallest_coeff_divisor(exp, comp); // gives smallest coeff
121 int r = -1;
122 if (ringtableZZ) r = ringtableZZ->find_smallest_coeff_divisor(exp, 1);
123
124 if (r < 0)
125 {
126 if (w < 0) return DIVISOR_NONE;
127 result_loc = w;
128 return DIVISOR_MODULE;
129 }
130 // r >= 0
131 if (w < 0)
132 {
133 result_loc = r;
134 return DIVISOR_RING;
135 }
136 // r >= 0, w >= 0
137 mpz_srcptr rc = originalR->quotient_gbvector(r)->coeff.get_mpz();
138 mpz_srcptr wc = polys[w].f->coeff.get_mpz();
139 if (mpz_cmpabs(rc, wc) > 0)
140 {
141 result_loc = w;
142 return DIVISOR_MODULE;
143 }
144 result_loc = r;
145 return DIVISOR_RING;
146}
147
148void ReducedGB_ZZ::remainder(POLY &f, bool use_denom, ring_elem &denom)
149{
150 gbvector *zero = nullptr;
151 gbvector head;
152 gbvector *frem = &head;
153 (void) use_denom;
154 (void) denom;
155 frem->next = nullptr;
156 POLY h = f;
157 exponents_t EXP = ALLOCATE_EXPONENTS(R->exponent_byte_size());
158 gbvector *r;
159 POLY g;
160 while (!R->gbvector_is_zero(h.f))
161 {
162 int w;
163 R->gbvector_get_lead_exponents(F, h.f, EXP);
164 int x = h.f->comp;
165 enum divisor_type typ = find_divisor(EXP, x, w);
166 switch (typ)
167 {
168 case DIVISOR_RING:
169 r = const_cast<gbvector *>(originalR->quotient_gbvector(w));
170 if (R->gbvector_reduce_lead_term_ZZ(F, Fsyz, h.f, zero, r, zero))
171 continue;
172 break;
173 case DIVISOR_MODULE:
174 g = polys[w];
175 if (R->gbvector_reduce_lead_term_ZZ(
176 F, Fsyz, h.f, h.fsyz, g.f, g.fsyz))
177 continue;
178 break;
179 case DIVISOR_NONE:
180 break;
181 }
182 frem->next = h.f;
183 frem = frem->next;
184 h.f = h.f->next;
185 frem->next = nullptr;
186 }
187 h.f = head.next;
188 f.f = h.f;
189 originalR->get_quotient_info()->gbvector_normal_form(Fsyz, h.fsyz);
190 f.fsyz = h.fsyz;
191}
192
193void ReducedGB_ZZ::remainder(gbvector *&f, bool use_denom, ring_elem &denom)
194{
195 gbvector *zero = nullptr;
196 gbvector head;
197 gbvector *frem = &head;
198 (void) use_denom;
199 (void) denom;
200 frem->next = nullptr;
201 gbvector *h = f;
202 exponents_t EXP = ALLOCATE_EXPONENTS(R->exponent_byte_size());
203
204 gbvector *r;
205 POLY g;
206 while (!R->gbvector_is_zero(h))
207 {
208 int w;
209 R->gbvector_get_lead_exponents(F, h, EXP);
210 int x = h->comp;
211 enum divisor_type typ = find_divisor(EXP, x, w);
212 switch (typ)
213 {
214 case DIVISOR_RING:
215 r = const_cast<gbvector *>(originalR->quotient_gbvector(w));
216 if (R->gbvector_reduce_lead_term_ZZ(F, Fsyz, h, zero, r, zero))
217 continue;
218 break;
219 case DIVISOR_MODULE:
220 g = polys[w];
221 if (M2_gbTrace >= 4)
222 {
223 buffer o;
224 R->gbvector_text_out(o, F, h);
225 o << newline << " divisor " << w << " is ";
226 R->gbvector_text_out(o, F, g.f);
227 o << newline;
228 emit(o.str());
229 }
230 if (R->gbvector_reduce_lead_term_ZZ(F, Fsyz, h, zero, g.f, zero))
231 continue;
232 break;
233 case DIVISOR_NONE:
234 break;
235 }
236 frem->next = h;
237 frem = frem->next;
238 h = h->next;
239 frem->next = nullptr;
240 }
241 h = head.next;
242 f = h;
243}
244
245// Local Variables:
246// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
247// indent-tabs-mode: nil
248// End:
exponents::Exponents exponents_t
Engine-side free module R^n over a Ring.
Definition freemod.hpp:66
int n_vars() const
Definition gbring.hpp:232
Polynomial-ring view tuned for the inner loop of classical Buchberger Groebner-basis computations.
Definition gbring.hpp:120
static MonomialTableZZ * make(int nvars)
Abstract base for the engine's polynomial-ring hierarchy.
Definition polyring.hpp:96
ReducedGB_ZZ(GBRing *R0, const PolynomialRing *originalR0, const FreeModule *F0, const FreeModule *Fsyz0)
const MonomialTableZZ * ringtableZZ
virtual void set_gb(VECTOR(POLY) &polys0)
MonomialTableZZ * T
virtual void minimalize(const VECTOR(POLY) &polys0, bool auto_reduced)
virtual void remainder(POLY &f, bool use_denom, ring_elem &denom)
virtual ~ReducedGB_ZZ()
enum divisor_type find_divisor(exponents_t exp, int comp, int &result_loc)
const FreeModule * Fsyz
Definition reducedgb.hpp:67
GBRing * R
Definition reducedgb.hpp:64
const PolynomialRing * originalR
Definition reducedgb.hpp:65
const FreeModule * F
Definition reducedgb.hpp:66
ReducedGB(GBRing *R0, const PolynomialRing *originalR0, const FreeModule *F0, const FreeModule *Fsyz0)
Definition reducedgb.cpp:35
char * str()
Definition buffer.hpp:72
void gb(IntermediateBasis &F, int n)
RingZZ * globalZZ
Definition relem.cpp:13
int zero
char newline[]
Definition m2-types.cpp:49
int M2_gbTrace
Definition m2-types.cpp:52
MonomialIdeal — exponent-vector-only representation of an ideal generated by monomials.
#define ALLOCATE_EXPONENTS(byte_len)
Definition monoid.hpp:62
#define VECTOR(T)
Definition newdelete.hpp:78
volatile int x
#define POLY(q)
Definition poly.cpp:23
gbvector * fsyz
Definition gbring.hpp:99
gbvector * f
Definition gbring.hpp:98
const FreeModule * F
ReducedGB_ZZ_sorter(GBRing *R0, const FreeModule *F0, const VECTOR(POLY) &gb0)
bool operator()(int xx, int yy)
const VECTOR(POLY) &gb
Index comparator used to permute ReducedGB_ZZ's gb array into canonical reduced-GB order over ZZ.
ring_elem coeff
Definition gbring.hpp:81
gbvector * next
Definition gbring.hpp:80
int comp
Definition gbring.hpp:82
const int GT
Definition style.hpp:41
const int LT
Definition style.hpp:39
#define INTSIZE(a)
Definition style.hpp:37
void emit(const char *s)
Definition text-io.cpp:41
Text-formatting helpers layered on buffer: bignum print, line wrapping, M2_gbTrace-gated emit.
mpz_srcptr get_mpz() const
Definition ringelem.hpp:127