Macaulay2 Engine
Loading...
Searching...
No Matches
res-monomial-sorter.hpp
Go to the documentation of this file.
1/* Copyright 2018, Michael E. Stillman */
2
3#ifndef _res_monomial_sorter_hpp_
4#define _res_monomial_sorter_hpp_
5
40
41#include "ExponentVector.hpp" // for ntuple
42#include "monoid.hpp" // for Monoid
43#include "schreyer-resolution/res-moninfo.hpp" // for ResMonoid
44#include "schreyer-resolution/res-schreyer-order.hpp" // for ResSchreyerOrder
45#include "schreyer-resolution/res-monomial-types.hpp" // for res_packed_mon...
46#include "style.hpp" // for GT, LT
47
48#include <memtailor/Arena.h> // for Arena
49#include <algorithm> // for stable_sort
50#include <utility> // for pair
51#include <vector> // for vector
52
68{
69private:
71 const std::vector<int*> mMonoms;
72 static long mNumComparisons;
73public:
74 MonomialSorterObject(const Monoid& M, const std::vector<int*> monoms) : mMonoid(M), mMonoms(monoms) {}
75
76
77 bool operator()(int a, int b)
78 {
79 // implements < function. In fact, a and b should not refer to objects that are == under this order.
80 // should we flag that?
81
83 bool result = false;
84 const int* m = mMonoms[a];
85 const int* n = mMonoms[b];
86 // TODO: make sure this is the order we want!!
87 int cmp = mMonoid.compare(m+2, m[1], n+2, n[1]);
88 if (cmp == LT) result = false;
89 else if (cmp == GT) result = true;
90 else
91 {
92 // compare using tie breaker
93 auto cmptie = m[0] - n[0];
94 result = (cmptie > 0);
95 }
96#if 0
97 buffer o;
98 o << "comparing: ";
99 mMonoid.elem_text_out(o, m+2);
100 o << " and ";
101 mMonoid.elem_text_out(o, n+2);
102 o << " result: " << (result ? "true" : "false");
103 emit_line(o.str());
104#endif
105 return result;
106 }
107
109 long numComparisons() const { return mNumComparisons; }
110};
111
127{
128private:
132 const std::vector<res_packed_monomial>& mColumns;
133
135 memt::Arena mArena;
136 std::vector<int*> mMonoms; // each monom: [tiebreaker, basecomp, followed by totalmon]
137 std::vector<int> mPositions;
138public:
140 const ResMonoid& resMonoid,
141 const ResSchreyerOrder& S, // order at level-1 in free res
142 const std::vector<res_packed_monomial>& columns // at level.
143 ) :
144 mMonoid(M),
145 mResMonoid(resMonoid),
147 mColumns(columns),
149 {
150 }
151
153 {
154 for (int i=0; i<mColumns.size(); i++)
155 {
156 std::pair<int*, int*> mon = mArena.allocArrayNoCon<int>(mMonoid.monomial_size() + 2);
157 toMonomial(mColumns[i], mon);
158 mMonoms.push_back(mon.first);
159 }
160 }
161
162 bool ordered()
163 {
164 setMonoms();
166 for (int i=1; i<mMonoms.size(); i++)
167 {
168 if (not C(i-1,i)) return false;
169 }
170 return true;
171 }
172
173 std::vector<int> sort()
174 {
175 setMonoms();
176
177 std::vector<int> result;
178
179 for (int i=0; i<mColumns.size(); i++)
180 result.push_back(i);
181
184
185 std::stable_sort(result.begin(), result.end(), C);
186
188 return result;
189 }
190
191 long numComparisons() const { return mNumComparisons; }
192private:
193 void toMonomial(res_packed_monomial mon, std::pair<int*,int*> resultAlreadyAllocateds)
194 {
195 int comp, comp2;
196 int nvars = mMonoid.n_vars();
197 std::pair<int*, int*> exp = mArena.allocArrayNoCon<int>(nvars);
198 std::pair<int*, int*> exp2 = mArena.allocArrayNoCon<int>(nvars);
199 mResMonoid.to_expvector(mon, exp.first, comp);
200 mResMonoid.to_expvector(mSchreyerOrder.mTotalMonom[comp], exp2.first, comp2);
201 exponents::mult(nvars, exp.first, exp2.first, exp2.first);
202 auto p = resultAlreadyAllocateds.first;
203 *p++ = mSchreyerOrder.mTieBreaker[comp];
204 *p++ = comp2;
205 mMonoid.from_expvector(exp2.first, p);
206 mArena.freeTop(exp2.first); // note: can only pop one at a time from an mt::Arena!
207 mArena.freeTop(exp.first);
208 }
209};
210
211#if 0
212class ResMonomialTransformer
213{
214 ResMonomialTransformer(const Monoid& M,
215 const ResMonoid& resM,
216 const ResSchreyerOrder& schreyerOrder,
217 memt::Arena& arena
218 ) :
219 mArena(arena),
220 mMonoid(M),
221 mResMonoid(resM),
222 mSchreyerOrder(schreyerOrder)
223 {
224 // nothing further to do here
225 }
226
227public:
228 // input: res_packed_monomial range
229 // output: (monomial,comp) range:
230 std::pair<int*, int*> transform(std::pair<int*, int*>& input)
231 {
232 int comp, comp2;
233 int nvars = mMonoid.n_vars();
234 std::pair<int*, int*> result = mArena.allocArrayNoCon<int>(mMonoid.monomial_size() + 2);
235 std::pair<int*, int*> exp = mArena.allocArrayNoCon<int>(nvars);
236 std::pair<int*, int*> exp2 = mArena.allocArrayNoCon<int>(nvars);
237 mResMonoid.to_expvector(mon.first, exp.first, comp);
238 mResMonoid.to_expvector(mSchreyerOrder.mTotalMonom[comp], exp2.first, comp2);
239 exponents::mult(nvars, exp.first, exp2.first, exp2.first);
240 auto p = resultAlreadyAllocateds.first;
241 *p++ = mSchreyerOrder.mTieBreaker[comp];
242 *p++ = comp2;
243 mMonoid.from_expvector(exp2.first, p);
244 mArena.freeTop(exp2.first); // note: can only pop one at a time from an mt::Arena!
245 mArena.freeTop(exp.first);
246 }
247private:
248 memt::Arena& mArena;
249 const Monoid& mMonoid;
250 const ResMonoid& mResMonoid;
251 const ResSchreyerOrder& mSchreyerOrder;
252};
253#endif
254
255#endif
256// Local Variables:
257// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
258// indent-tabs-mode: nil
259// End:
Dense exponent-vector template [e_0, ..., e_{nvars-1}] for monomial operations.
static void mult(int nvars, ConstExponents a, ConstExponents b, Exponents result)
Engine-side commutative monomial monoid: variable names, ordering, multidegree machinery,...
Definition monoid.hpp:89
const std::vector< int * > mMonoms
MonomialSorterObject(const Monoid &M, const std::vector< int * > monoms)
bool operator()(int a, int b)
Strict-weak comparator on integer indices into a std::vector<int*> of monomials, used by the resoluti...
std::vector< int > sort()
const ResSchreyerOrder & mSchreyerOrder
std::vector< int > mPositions
std::vector< int * > mMonoms
const std::vector< res_packed_monomial > & mColumns
const ResMonoid & mResMonoid
ResMonomialSorter(const Monoid &M, const ResMonoid &resMonoid, const ResSchreyerOrder &S, const std::vector< res_packed_monomial > &columns)
void toMonomial(res_packed_monomial mon, std::pair< int *, int * > resultAlreadyAllocateds)
char * str()
Definition buffer.hpp:72
int p
VALGRIND_MAKE_MEM_DEFINED & result(result)
Monoid — variable count, naming, grading, and monomial order of a polynomial ring.
const mpreal exp2(const mpreal &x, mp_rnd_t r=mpreal::get_default_rnd())
Definition mpreal.h:2299
const mpreal exp(const mpreal &x, mp_rnd_t r=mpreal::get_default_rnd())
Definition mpreal.h:2298
ResMonoidDense ResMonoid
ResMonoid dispatcher — single typedef switch between ResMonoidDense and ResMonoidSparse.
res_monomial_word * res_packed_monomial
Typed-monomial vocabulary shared by ResMonoid, ResPolyRing, SchreyerFrame, and F4Res.
ResSchreyerOrder — per-free-module-summand data implementing the Schreyer order on the next level.
Per-level Schreyer-order data attached to a SchreyerFrame::Level.
const int GT
Definition style.hpp:41
const int LT
Definition style.hpp:39
Engine-wide stylistic constants: LT / EQ / GT codes, INTSIZE, GEOHEAP_SIZE.
void emit_line(const char *s)
Definition text-io.cpp:47