Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/c_lib/include/pb_wrap.h
4040 views
1
2
#include <polybori/BoolePolynomial.h>
3
#include <polybori/BoolePolyRing.h>
4
#include <polybori/factories/VariableBlock.h>
5
#include <polybori/factories/VariableFactory.h>
6
#include <polybori/groebner/add_up.h>
7
#include <polybori/groebner/contained_variables.h>
8
#include <polybori/groebner/FGLMStrategy.h>
9
#include <polybori/groebner/groebner_alg.h>
10
#include <polybori/groebner/interpolate.h>
11
#include <polybori/groebner/linear_algebra_step.h>
12
#include <polybori/groebner/LiteralFactorization.h>
13
#include <polybori/groebner/ll_red_nf.h>
14
#include <polybori/groebner/nf.h>
15
#include <polybori/groebner/red_tail.h>
16
#include <polybori/groebner/minimal_elements.h>
17
#include <polybori/groebner/randomset.h>
18
#include <polybori.h>
19
#include <polybori/iterators/COrderedIter.h>
20
#include <polybori/orderings/pbori_order.h>
21
#include <polybori/pbori_defs.h>
22
#include "ccobject.h"
23
24
// M4RI
25
#ifdef __cplusplus
26
extern "C" {
27
#endif //__cplusplus
28
#define PACKED 1
29
#include "m4ri/m4ri.h"
30
#ifdef __cplusplus
31
}
32
#endif //__cplusplus
33
34
#include <sstream>
35
#include <vector>
36
37
USING_NAMESPACE_PBORI
38
USING_NAMESPACE_PBORIGB
39
40
//#define PBORI_PREALLOCATED_DEBUG
41
42
/* Allocate and Construct */
43
template <class T, class Arg>
44
T* New_p(const Arg& arg){
45
return new T(arg);
46
}
47
48
BoolePolynomial get_ith_gen(const GroebnerStrategy& strat, int i){
49
return strat.generators[i].p;
50
}
51
52
static int pairs_top_sugar(const GroebnerStrategy& strat){
53
if (strat.pairs.pairSetEmpty())
54
return -1;
55
else
56
return (strat.pairs.queue.top().sugar);
57
}
58
59
static std::vector<BoolePolynomial> someNextDegreeSpolys(GroebnerStrategy& strat, int n){
60
std::vector<BoolePolynomial> res;
61
assert(!(strat.pairs.pairSetEmpty()));
62
strat.pairs.cleanTopByChainCriterion();
63
deg_type deg=strat.pairs.queue.top().sugar;
64
65
while((!(strat.pairs.pairSetEmpty())) && \
66
(strat.pairs.queue.top().sugar<=deg) && (res.size()<n)){
67
assert(strat.pairs.queue.top().sugar==deg);
68
res.push_back(strat.nextSpoly());
69
strat.pairs.cleanTopByChainCriterion();
70
}
71
return res;
72
}
73
74
static std::vector<Polynomial> nextDegreeSpolys(GroebnerStrategy& strat){
75
std::vector<Polynomial> res;
76
assert(!(strat.pairs.pairSetEmpty()));
77
strat.pairs.cleanTopByChainCriterion();
78
deg_type deg=strat.pairs.queue.top().sugar;
79
80
while((!(strat.pairs.pairSetEmpty())) &&
81
(strat.pairs.queue.top().sugar<=deg)){
82
83
assert(strat.pairs.queue.top().sugar==deg);
84
res.push_back(strat.nextSpoly());
85
strat.pairs.cleanTopByChainCriterion();
86
}
87
return res;
88
}
89
90
static std::vector<Polynomial> small_next_degree_spolys(GroebnerStrategy& strat,
91
double f, int n){
92
std::vector<Polynomial> res;
93
assert(!(strat.pairs.pairSetEmpty()));
94
strat.pairs.cleanTopByChainCriterion();
95
deg_type deg=strat.pairs.queue.top().sugar;
96
wlen_type wlen=strat.pairs.queue.top().wlen;
97
while((!(strat.pairs.pairSetEmpty())) &&
98
(strat.pairs.queue.top().sugar<=deg) &&
99
(strat.pairs.queue.top().wlen<=wlen*f+2)&& (res.size()<n)){
100
101
assert(strat.pairs.queue.top().sugar==deg);
102
res.push_back(strat.nextSpoly());
103
strat.pairs.cleanTopByChainCriterion();
104
}
105
return res;
106
}
107
108
static void implications(GroebnerStrategy& strat, int i){
109
strat.addNonTrivialImplicationsDelayed(strat.generators[i]);
110
}
111
112
inline BooleSet::const_iterator*
113
construct_bset_begin(void* mem, const BooleSet& bset) {
114
return new(mem) BooleSet::const_iterator(bset.begin());
115
}
116
117
inline BooleSet::const_iterator*
118
construct_bset_end(void* mem, const BooleSet& bset) {
119
return new(mem) BooleSet::const_iterator(bset.end());
120
}
121
122
#define PBPolyVector_set(v,i,p) v[i] = p
123
124
125
class ring_singleton{
126
public:
127
static BoolePolyRing instance() {
128
static BoolePolyRing ring(1);
129
return ring;
130
}
131
};
132
133
134
135
template <class ValueType>
136
class DefaultRinged:
137
public ValueType {
138
typedef DefaultRinged self;
139
140
public:
141
typedef ValueType value_type;
142
typedef value_type base;
143
144
// Default constructor allocated memory
145
DefaultRinged();
146
147
DefaultRinged(const value_type& rhs): base(rhs) {}
148
149
DefaultRinged(const self& rhs): base(rhs) {}
150
151
~DefaultRinged() { }
152
153
self& operator=(const self& rhs) {
154
return operator=(static_cast<const value_type&>(rhs));
155
}
156
157
self& operator=(const value_type& rhs) {
158
base::operator=(rhs);
159
return *this;
160
}
161
};
162
163
template <class ValueType>
164
DefaultRinged<ValueType>::DefaultRinged():
165
base(ring_singleton::instance()) { }
166
167
template <>
168
DefaultRinged<FGLMStrategy>::DefaultRinged():
169
base(ring_singleton::instance(), ring_singleton::instance(),
170
PolynomialVector()) { }
171
172
173
template <class T>
174
PyObject* preallocated_to_PyString(const DefaultRinged<T> *wrapped) {
175
return _to_PyString<T>(wrapped) ;
176
}
177
178
179
template <class Type>
180
class WrappedPtr:
181
public boost::shared_ptr<Type> {
182
typedef WrappedPtr self;
183
typedef boost::shared_ptr<Type> base;
184
185
public:
186
WrappedPtr(): base() {}
187
WrappedPtr(const self& rhs): base(rhs) {}
188
189
template <class T1>
190
WrappedPtr(const T1& arg): base(new Type(arg)) {}
191
192
template <class T1, class T2>
193
WrappedPtr(const T1& arg1, const T2& arg2): base(new Type(arg1, arg2)) {}
194
195
template <class T1, class T2, class T3>
196
WrappedPtr(const T1& arg1, const T2& arg2, const T3& arg3):
197
base(new Type(arg1, arg2, arg3)) {}
198
199
operator Type&() { return base::operator*();}
200
operator const Type&() const { return base::operator*();}
201
};
202
203
204
class PBRefCounter {
205
public:
206
207
PBRefCounter(): p_count(new long(0)) {}
208
PBRefCounter(const PBRefCounter& rhs):
209
p_count(rhs.p_count) { ++(*p_count); }
210
211
~PBRefCounter() { decrease(); }
212
213
PBRefCounter&
214
operator=(const PBRefCounter& rhs) {
215
decrease();
216
p_count = rhs.p_count;
217
++(*p_count);
218
return *this;
219
}
220
221
bool released() {
222
assert(p_count);
223
if(!*p_count) {
224
kill();
225
return true;
226
}
227
return false;
228
}
229
230
private:
231
void decrease() { if(p_count) --(*p_count); }
232
void kill() {
233
delete p_count;
234
p_count = NULL;
235
}
236
long* p_count;
237
};
238
239