Macaulay2 Engine
Loading...
Searching...
No Matches
monideal.hpp
Go to the documentation of this file.
1// Copyright 1994-2021 Michael E. Stillman
2
3#ifndef _monideal_hh_
4#define _monideal_hh_
5
39
40#include "ExponentVector.hpp"
41#include "ExponentList.hpp"
42#include "int-bag.hpp"
43#include "ring.hpp"
44#include "polyring.hpp"
45#include "mem.hpp"
46
47#if 0
48SparseMonomial: pointer to an array of ints. First is the length. [len, v1, e1, v2, e2, ..., vr, er]
49 len == 2r+1,
50 in comm case: v1 < v2 < ...< vr;
51 SparseMonomial::Iterator: i.var, i.exponent, or *i = <i.var, i.exponent>
52 for (auto a : monom) // a is a std::pair.
53 allocation: std::vector<int>, preallocated Range, MemoryBlock.
54 AllocatedSparseMonomial(sizinginfo)
55 LCM(a, b, allocatedspace) --> returns a SparseMonomial.
56
57
58#endif
59
73class Nmi_node : public our_new_delete // monomial ideal internal node ///
74{
75 friend class MonomialIdeal;
76 friend class AssociatedPrimes;
77 friend class MinimalPrimes;
78
79private:
80 int var;
81 int exp;
85 enum { node, leaf } tag;
86 union
87 {
88 Nmi_node *down; // 'up' node, if this is a head of a list
90 } val;
91
92 // Nmi_node(int v, int e, Nmi_node *d)
93 // : var(v), exp(e), left(NULL), right(NULL), header(NULL), tag(node)
94 // { val.down = d; }
95 //
96 // Nmi_node(int v, int e, Bag *b)
97 // : var(v), exp(e), left(NULL), right(NULL), header(NULL), tag(leaf)
98 // { val.bag = b; }
99 //
100 // ~Nmi_node();
101
102 Nmi_node *&down() { return val.down; }
103 Bag *&baggage() { return val.bag; }
104 gc_vector<int> &monom() { return val.bag->monom(); } // varpower
105 const gc_vector<int> &monom() const { return val.bag->monom(); } // varpower
107 {
108 q->header = header;
109 q->left = left;
110 q->right = this;
111 left = left->right = q;
112 }
113};
114
134// these objects are immutable once they are sent to the front end (i.e.
135// once their hash code has been set...
136{
139 int count; // We hack this a bit: the low order bit (count%1==1) means that
140 // we own the stash
141 // count//2 is the actual number of nodes here.
143 friend class AssociatedPrimes;
144 friend class MinimalPrimes;
145
146 private:
147 Nmi_node *new_internal_mi_node(int v, int e, Nmi_node *d);
148 Nmi_node *new_leaf_mi_node(int v, int e, Bag *b);
150
151 Nmi_node *first_node() const;
152 Nmi_node *last_node() const;
153 Nmi_node *next(Nmi_node *p) const;
154 Nmi_node *prev(Nmi_node *p) const;
155
156 void insert1(Nmi_node *&p, Bag *b);
157 void remove1(Nmi_node *p);
158 void do_node(Nmi_node *p, int indent, int disp) const;
159 void do_tree(Nmi_node *p, int depth, int indent, int disp) const;
160 int debug_check(Nmi_node *p, const Nmi_node *up) const;
161
162 bool isWellFormed() const; // throws errors, or returns true, currently.
163 protected:
164 virtual unsigned int computeHashValue() const;
165
166 public:
167 MonomialIdeal(const PolynomialRing *RR, stash *mi_stash = nullptr);
169
171 VECTOR(Bag *) &elems, // we now own these elements
172 VECTOR(Bag *) &rejects, // except for the ones we place into here
173 stash *mi_stash0 = nullptr);
174
176 VECTOR(Bag *) &elems, // we now own these elements, and will free those not needed
177 stash *mi_stash0 = nullptr);
178
179 MonomialIdeal *copy() const;
180
181 void remove_MonomialIdeal(); // frees all of the internal things
184
185 // Informational
186 int size() const { return count / 2; }
187 int topvar() const { return (mi == nullptr ? -1 : mi->var); }
188 void text_out(buffer &o) const;
189
190 const PolynomialRing *get_ring() const { return R; }
191
192 // Insertion of new monomials.
193 void insert_minimal(Bag *b);
194 // Insert baggage 'b'. It is assumed
195 // that 'b' is not already in the monomial ideal.
196
197 int insert(Bag *b);
198
199 void insert_w_deletions(Bag *b, VECTOR(Bag *) &deletions);
200 // Insert 'm', removing any monomials divisible by 'm', and
201 // returning their baggage in a list of moninfo *'s.
202
203 int remove(Bag *&b);
204 // Deletion. Remove the lexicographically largest element, placing
205 // it into b. Return 1 if an element is removed.
206
207 int search_expvector(const_exponents m, Bag *&b) const;
208 // Search. Return whether a monomial which divides 'm' is
209 // found. If so, return the baggage. 'm' is assumed to be an
210 // exponent vector of length larger than the top variable occurring
211 // in 'this'
212 int search(const_varpower m, Bag *&b) const;
213 // Search. Return whether a monomial which divides 'm' is
214 // found. If so, return the baggage. 'm' is a varpower monomial.
215 void find_all_divisors(const_exponents exp, VECTOR(Bag *)& b) const;
216 // Search. Return a list of all elements which divide 'exp'.
217
230 class Iterator {
231 private:
234 public:
235 using iterator_category = std::forward_iterator_tag;
236 using difference_type = std::ptrdiff_t;
238 using pointer = Bag*; // or also value_type*
239 using reference = Bag&; // or also value_type&
240
241
243 : mMonomialTable(MI),
244 mPointer(MI.first_node())
245 {
246 }
247
248 // Start at the end.
249 Iterator(const MonomialIdeal& MI, int)
250 : mMonomialTable(MI),
251 mPointer(nullptr)
252 {
253 }
254
255 // Internal use: used to start at the end.
257 : mMonomialTable(MI),
258 mPointer(p)
259 {
260 }
261
262 reference operator*() const { return * mPointer->val.bag; }
263 pointer operator->() { return mPointer->val.bag; }
264
265 // Prefix increment, decrement
266 Iterator& operator++() { mPointer = mMonomialTable.next(mPointer); return *this; }
267 Iterator& operator--() { mPointer = mMonomialTable.prev(mPointer); return *this; }
268
269 // Postfix increment, decrement
270 Iterator operator++(int) { Iterator tmp = *this; ++(*this); return tmp; }
271 Iterator operator--(int) { Iterator tmp = *this; --(*this); return tmp; }
272
273 friend bool operator== (const Iterator& a, const Iterator& b) { return a.mPointer == b.mPointer; };
274 friend bool operator!= (const Iterator& a, const Iterator& b) { return a.mPointer != b.mPointer; };
275 };
276
277 Iterator begin() const { return Iterator(*this); }
278 Iterator beginAtLast() const { return Iterator(*this, last_node()); }
279 Iterator end() const { return Iterator(*this, 1); }
280 void *next(void *p) const;
281 void *prev(void *p) const;
282 int valid(void *p) const { return (p != nullptr); }
283 void debug_out(int disp = 1) const;
284 void debug_check() const;
285
286 bool is_equal(const MonomialIdeal &mi) const;
287
289 MonomialIdeal *intersect(const MonomialIdeal &J) const;
291 MonomialIdeal *quotient(const MonomialIdeal &J) const;
293 MonomialIdeal *sat(const MonomialIdeal &J) const;
294
295 M2_arrayint lcm() const;
296 // Returns the lcm of all of the generators of this, as an array of ints
297
299 // a is a vector which is entrywise >= lcm(this).
300
301 MonomialIdeal *radical() const;
302
303 MonomialIdeal *borel() const;
304 bool is_borel() const;
305
306 MonomialIdeal *operator+(const MonomialIdeal &F) const;
307 MonomialIdeal *operator-(const MonomialIdeal &F) const;
308 MonomialIdeal *operator*(const MonomialIdeal &G) const;
309
310 bool is_one() const;
311 int n_pure_powers() const;
312};
313
315{
318
320 : mi(new MonomialIdeal(R)), mi_search(new MonomialIdeal(R))
321 {
322 }
323
324 monideal_pair(const PolynomialRing *R, stash *mi_stash)
325 : mi(new MonomialIdeal(R, mi_stash)),
326 mi_search(new MonomialIdeal(R, mi_stash))
327 {
328 }
329};
330
331//-----------------------------------------------------------------
332
334{
335 insert1(mi, b);
336 count += 2;
337}
338
339inline Nmi_node *MonomialIdeal::first_node() const { return next(mi); }
340inline Nmi_node *MonomialIdeal::last_node() const { return prev(mi); }
341
342#endif
343
344// Local Variables:
345// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
346// indent-tabs-mode: nil
347// End:
varpower::ConstExponents const_varpower
Variable-length sparse (variable, exponent) encoding of monomials.
exponents::ConstExponents const_exponents
Dense exponent-vector template [e_0, ..., e_{nvars-1}] for monomial operations.
EngineObject()
Definition hash.hpp:66
Thin RAII wrapper around memtailor::Arena providing bump-pointer array allocation with optional mutex...
Iterator & operator--()
Definition monideal.hpp:267
Iterator & operator++()
Definition monideal.hpp:266
friend bool operator!=(const Iterator &a, const Iterator &b)
Definition monideal.hpp:274
std::forward_iterator_tag iterator_category
Definition monideal.hpp:235
Iterator(const MonomialIdeal &MI, Nmi_node *p)
Definition monideal.hpp:256
std::ptrdiff_t difference_type
Definition monideal.hpp:236
Iterator operator++(int)
Definition monideal.hpp:270
Iterator(const MonomialIdeal &MI, int)
Definition monideal.hpp:249
Iterator operator--(int)
Definition monideal.hpp:271
reference operator*() const
Definition monideal.hpp:262
const MonomialIdeal & mMonomialTable
Definition monideal.hpp:232
friend bool operator==(const Iterator &a, const Iterator &b)
Definition monideal.hpp:273
Iterator(const MonomialIdeal &MI)
Definition monideal.hpp:242
Bidirectional forward iterator over the Bags stored in a MonomialIdeal.
Definition monideal.hpp:230
int search(const_varpower m, Bag *&b) const
Definition monideal.cpp:278
Nmi_node * next(Nmi_node *p) const
Definition monideal.cpp:285
void do_node(Nmi_node *p, int indent, int disp) const
Definition monideal.cpp:465
int insert(Bag *b)
Definition monideal.cpp:622
Nmi_node * last_node() const
Definition monideal.hpp:340
virtual unsigned int computeHashValue() const
Definition monideal.cpp:28
MonomialIdeal * erase(const_varpower m) const
Definition monideal.cpp:853
virtual ~MonomialIdeal()
Definition monideal.hpp:168
int search_expvector(const_exponents m, Bag *&b) const
Definition monideal.cpp:214
int remove(Bag *&b)
Definition monideal.cpp:451
void insert_minimal(Bag *b)
Definition monideal.hpp:333
M2_arrayint lcm() const
Definition monideal.cpp:819
bool is_equal(const MonomialIdeal &mi) const
Definition monideal.cpp:195
MonomialIdeal * operator+(const MonomialIdeal &F) const
Definition monideal.cpp:726
void debug_out(int disp=1) const
Definition monideal.cpp:508
int n_pure_powers() const
Definition monideal.cpp:985
Nmi_node * new_leaf_mi_node(int v, int e, Bag *b)
Definition monideal.cpp:62
MonomialIdeal * quotient(const_varpower m) const
Definition monideal.cpp:759
int valid(void *p) const
Definition monideal.hpp:282
const PolynomialRing * R
Definition monideal.hpp:137
void delete_mi_node(Nmi_node *p)
Definition monideal.cpp:78
Iterator beginAtLast() const
Definition monideal.hpp:278
bool is_borel() const
Definition monideal.cpp:955
MonomialIdeal(const PolynomialRing *RR, stash *mi_stash=nullptr)
Definition monideal.cpp:91
MonomialIdeal * sat(const MonomialIdeal &J) const
Definition monideal.cpp:869
MonomialIdeal * operator*(const MonomialIdeal &G) const
Definition monideal.cpp:710
void find_all_divisors(const_exponents exp, VECTOR(Bag *)&b) const
Definition monideal.cpp:246
void text_out(buffer &o) const
Definition monideal.cpp:639
Nmi_node * first_node() const
Definition monideal.hpp:339
int size() const
Definition monideal.hpp:186
friend class AssociatedPrimes
Definition monideal.hpp:143
MonomialIdeal * copy() const
Definition monideal.cpp:187
friend class MinimalPrimes
Definition monideal.hpp:144
void remove_MonomialIdeal()
Definition monideal.cpp:43
void do_tree(Nmi_node *p, int depth, int indent, int disp) const
Definition monideal.cpp:494
Nmi_node * mi
Definition monideal.hpp:138
void insert1(Nmi_node *&p, Bag *b)
Definition monideal.cpp:321
stash * mi_stash
Definition monideal.hpp:142
MonomialIdeal * intersect(const_varpower m) const
Definition monideal.cpp:696
bool is_one() const
Definition monideal.cpp:977
bool isWellFormed() const
Definition monideal.cpp:578
int topvar() const
Definition monideal.hpp:187
void insert_w_deletions(Bag *b, VECTOR(Bag *) &deletions)
MonomialIdeal * operator-(const MonomialIdeal &F) const
Definition monideal.cpp:742
MonomialIdeal * borel() const
Definition monideal.cpp:940
Iterator end() const
Definition monideal.hpp:279
void debug_check() const
Definition monideal.cpp:567
const_varpower second_elem() const
Definition monideal.cpp:182
Nmi_node * new_internal_mi_node(int v, int e, Nmi_node *d)
Definition monideal.cpp:49
MonomialIdeal * alexander_dual(const M2_arrayint a) const
Definition monideal.cpp:834
const PolynomialRing * get_ring() const
Definition monideal.hpp:190
MonomialIdeal * radical() const
Definition monideal.cpp:899
Nmi_node * prev(Nmi_node *p) const
Definition monideal.cpp:303
void remove1(Nmi_node *p)
Definition monideal.cpp:397
Iterator begin() const
Definition monideal.hpp:277
const_varpower first_elem() const
Definition monideal.cpp:177
Engine-side monomial ideal: a decision tree of Nmi_nodes storing the (typically minimal) generators b...
Definition monideal.hpp:136
Bag *& baggage()
Definition monideal.hpp:103
enum Nmi_node::@355074146072071371146336002330246050056154227161 tag
const gc_vector< int > & monom() const
Definition monideal.hpp:105
gc_vector< int > & monom()
Definition monideal.hpp:104
Nmi_node * header
Definition monideal.hpp:84
void insert_to_left(Nmi_node *q)
Definition monideal.hpp:106
Bag * bag
Definition monideal.hpp:89
union Nmi_node::@215171170000357111350013011034226177066064152112 val
friend class AssociatedPrimes
Definition monideal.hpp:76
Nmi_node * down
Definition monideal.hpp:88
friend class MinimalPrimes
Definition monideal.hpp:77
Nmi_node *& down()
Definition monideal.hpp:102
Nmi_node * left
Definition monideal.hpp:82
Nmi_node * right
Definition monideal.hpp:83
friend class MonomialIdeal
Definition monideal.hpp:75
Internal tree node of the MonomialIdeal decision tree.
Definition monideal.hpp:74
Abstract base for the engine's polynomial-ring hierarchy.
Definition polyring.hpp:96
Definition mem.hpp:78
int p
int_bag Bag
Definition int-bag.hpp:70
int_bag / Bag — minimal (payload, varpower monomial) carrier shared by monomial-ideal code.
stash and doubling_stash — legacy size-class allocator interfaces, now stubbed to plain GC allocation...
typename std::vector< T, gc_allocator< T > > gc_vector
a version of the STL vector, which allocates its backing memory with gc.
Definition newdelete.hpp:76
#define VECTOR(T)
Definition newdelete.hpp:78
PolynomialRing — abstract polynomial-ring base, the engine's most-reused class.
tbb::flow::graph G
Ring — the legacy abstract base class for every coefficient and polynomial ring.
MonomialIdeal * mi
Definition monideal.hpp:316
monideal_pair(const PolynomialRing *R, stash *mi_stash)
Definition monideal.hpp:324
monideal_pair(const PolynomialRing *R)
Definition monideal.hpp:319
MonomialIdeal * mi_search
Definition monideal.hpp:317