-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathpolynomial.cpp
More file actions
8302 lines (7515 loc) · 317 KB
/
Copy pathpolynomial.cpp
File metadata and controls
8302 lines (7515 loc) · 317 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*++
Copyright (c) 2011 Microsoft Corporation
Module Name:
polynomial.cpp
Abstract:
Goodies for creating and handling polynomials.
Author:
Leonardo (leonardo) 2011-11-15
Notes:
--*/
#include "math/polynomial/polynomial.h"
#include "util/vector.h"
#include "util/chashtable.h"
#include "util/small_object_allocator.h"
#include "util/region.h"
#include "util/id_gen.h"
#include "util/buffer.h"
#include "util/scoped_ptr_vector.h"
#include "math/polynomial/upolynomial_factorization.h"
#include "math/polynomial/polynomial_primes.h"
#include "util/permutation.h"
#include "math/polynomial/algebraic_numbers.h"
#include "util/mpzzp.h"
#include "util/timeit.h"
#include "math/polynomial/linear_eq_solver.h"
#include "util/scoped_numeral_buffer.h"
#include "util/ref_buffer.h"
#include "util/common_msgs.h"
#include <memory>
#include <numeric>
namespace polynomial {
factor_params::factor_params():
m_max_p(UINT_MAX),
m_p_trials(1),
m_max_search_size(UINT_MAX) {
}
factor_params::factor_params(unsigned max_p, unsigned p_trials, unsigned max_search_size):
m_max_p(max_p),
m_p_trials(p_trials),
m_max_search_size(max_search_size) {
}
void factor_params::updt_params(params_ref const & p) {
m_max_p = p.get_uint("max_prime", UINT_MAX);
m_p_trials = p.get_uint("num_primes", 1);
m_max_search_size = p.get_uint("max_search_size", UINT_MAX);
}
void factor_params::get_param_descrs(param_descrs & r) {
r.insert("max_search_size", CPK_UINT, "(default: infty) Z3 polynomial factorization is composed of three steps: factorization in GF(p), lifting and search. This parameter can be used to limit the search space.");
r.insert("max_prime", CPK_UINT, "(default: infty) Z3 polynomial factorization is composed of three steps: factorization in GF(p), lifting and search. This parameter limits the maximum prime number p to be used in the first step.");
r.insert("num_primes", CPK_UINT, "(default: 1) Z3 polynomial factorization is composed of three steps: factorization in GF(p), lifting and search. The search space may be reduced by factoring the polynomial in different GF(p)'s. This parameter specify the maximum number of finite factorizations to be considered, before lifiting and searching.");
}
typedef ptr_vector<monomial> monomial_vector;
void var2degree::display(std::ostream & out) const {
bool first = true;
out << "[";
for (unsigned i = 0; i < m_var2degree.size(); ++ i) {
if (!m_var2degree.empty()) {
if (!first) {
out << ",";
}
out << "x" << i << "^" << m_var2degree[i];
if (first) {
first = false;
}
}
}
out << "]";
}
// -----------------------------------
//
// Monomials
//
// -----------------------------------
/**
\brief power: var + exponent
*/
class power : public std::pair<var, unsigned> {
public:
power() = default;
power(var v, unsigned d):std::pair<var, unsigned>(v, d) {}
var get_var() const { return first; }
unsigned degree() const { return second; }
unsigned & degree() { return second; }
void set_var(var x) { first = x; }
struct lt_var {
bool operator()(power const & p1, power const & p2) {
// CMW: The assertion below does not hold on macOS, because
// their implementation of std::sort will try to compare
// two items at the same index instead of comparing
// the indices directly. I suspect that the purpose of
// this assertion was to make sure that there are
// no duplicates, so I replaced it with a new assertion at
// the end of var_degrees(...).
// SASSERT(p1.get_var() != p2.get_var());
return p1.get_var() < p2.get_var();
}
};
struct lt_degree {
bool operator()(power const & p1, power const & p2) {
return p1.degree() < p2.degree();
}
};
};
std::ostream & operator<<(std::ostream & out, power const & p) {
out << "x" << p.get_var();
if (p.degree() != 1)
out << "^" << p.degree();
return out;
}
/**
\brief Return true if the variables in pws are sorted in increasing order and are distinct.
*/
bool is_valid_power_product(unsigned sz, power const * pws) {
for (unsigned i = 1; i < sz; ++i) {
if (pws[i-1].get_var() >= pws[i].get_var())
return false;
}
return true;
}
/**
\brief Return total degree of the given power product.
*/
unsigned power_product_total_degree(unsigned sz, power const * pws) {
unsigned r = 0;
for (unsigned i = 0; i < sz; ++i)
r += pws[i].degree();
return r;
}
/**
\brief Monomials (power products)
*/
class monomial {
unsigned m_ref_count;
unsigned m_id; //!< unique id
unsigned m_total_degree; //!< total degree of the monomial
unsigned m_size; //!< number of powers
unsigned m_hash;
power m_powers[0];
friend class tmp_monomial;
void sort() {
std::sort(m_powers, m_powers + m_size, power::lt_var());
}
public:
static unsigned hash_core(unsigned sz, power const * pws) {
return string_hash(std::string_view(reinterpret_cast<char*>(const_cast<power*>(pws)), sz*sizeof(power)), 11);
}
struct hash_proc {
unsigned operator()(monomial const * m) const {
return m->m_hash;
}
};
struct eq_proc {
bool operator()(monomial const * m1, monomial const * m2) const {
if (m1->size() != m2->size() || m1->hash() != m2->hash())
return false;
// m_total_degree must not be used as a filter, because it is not updated in temporary monomials.
for (unsigned i = 0; i < m1->m_size; ++i) {
if (m1->get_power(i) != m2->get_power(i))
return false;
}
return true;
}
};
bool operator==(monomial const& other) const {
return eq_proc()(this, &other);
}
static unsigned get_obj_size(unsigned sz) { return sizeof(monomial) + sz * sizeof(power); }
monomial(unsigned id, unsigned sz, power const * pws, unsigned h):
m_ref_count(0),
m_id(id),
m_total_degree(0),
m_size(sz),
m_hash(h) {
for (unsigned i = 0; i < sz; i ++) {
power const & pw = pws[i];
m_powers[i] = pw;
SASSERT(i == 0 || get_var(i) > get_var(i-1));
SASSERT(degree(i) > 0);
m_total_degree += degree(i);
}
}
unsigned hash() const { return m_hash; }
unsigned ref_count() const { return m_ref_count; }
void inc_ref() { m_ref_count++; }
void dec_ref() { SASSERT(m_ref_count > 0); m_ref_count--; }
bool is_valid() const {
return is_valid_power_product(m_size, m_powers) && power_product_total_degree(m_size, m_powers) == m_total_degree;
}
unsigned id() const { return m_id; }
unsigned size() const { return m_size; }
unsigned total_degree() const { return m_total_degree; }
power const & get_power(unsigned idx) const { SASSERT(idx < size()); return m_powers[idx]; }
power const * get_powers() const { return m_powers; }
var get_var(unsigned idx) const { return get_power(idx).get_var(); }
unsigned degree(unsigned idx) const { return get_power(idx).degree(); }
var max_var() const {
if (m_size == 0)
return null_var;
return get_var(m_size - 1);
}
unsigned max_var_degree() const {
if (m_size == 0)
return 0;
return degree(m_size - 1);
}
#define SMALL_MONOMIAL 8
unsigned index_of(var x) const {
if (m_size == 0)
return UINT_MAX;
unsigned last = m_size - 1;
if (get_var(last) == x)
return last;
if (m_size < SMALL_MONOMIAL) {
// use linear search for small monomials
// search backwards since we usually ask for the degree of "big" variables
for (unsigned i = last; i-- > 0; ) {
if (get_var(i) == x)
return i;
}
return UINT_MAX;
}
else {
// use binary search for big monomials
int low = 0;
int high = last;
while (true) {
int mid = low + ((high - low)/2);
var x_mid = get_var(mid);
if (x > x_mid) {
low = mid + 1;
}
else if (x < x_mid) {
high = mid - 1;
}
else {
return mid;
}
if (low > high)
return UINT_MAX;
}
}
}
unsigned degree_of(var x) const {
unsigned pos = index_of(x);
if (pos == UINT_MAX)
return 0;
return degree(pos);
}
// Given the subset S of variables that are smaller than x,
// then return the maximal one.
var max_smaller_than_core(var x) const {
if (m_size == 0)
return null_var;
if (m_size < SMALL_MONOMIAL) {
// use linear search for small monomials
// search backwards since we usually ask for the degree of "big" variables
unsigned i = m_size;
while (i > 0) {
--i;
if (get_var(i) < x)
return get_var(i);
}
return null_var;
}
else {
// use binary search for big monomials
int low = 0;
int high = m_size-1;
if (x <= get_var(low)) {
return null_var;
}
if (x > get_var(high)) {
return get_var(high);
}
if (x == get_var(high)) {
SASSERT(high > 0);
return get_var(high-1);
}
while (true) {
SASSERT(0 <= low && high < static_cast<int>(m_size));
SASSERT(get_var(low) < x);
SASSERT(x < get_var(high));
SASSERT(low < high);
if (high == low + 1) {
SASSERT(get_var(low) < x);
SASSERT(x < get_var(low+1));
return get_var(low);
}
SASSERT(high > low + 1);
int mid = low + ((high - low)/2);
SASSERT(low < mid && mid < high);
var x_mid = get_var(mid);
if (x_mid == x) {
SASSERT(low < mid && mid < high && high < static_cast<int>(m_size));
SASSERT(get_var(mid-1) < x && x == get_var(mid));
return get_var(mid-1);
}
if (x < x_mid) {
high = mid;
}
else {
SASSERT(x_mid < x);
low = mid;
}
}
}
}
var max_smaller_than(var x) const {
SASSERT(x != null_var);
var y = max_smaller_than_core(x);
DEBUG_CODE({
bool found = false;
for (unsigned i = 0; i < m_size; ++i) {
if (get_var(i) < x) {
CTRACE(poly_bug, !(y != null_var && get_var(i) <= y),
tout << "m: "; display(tout); tout << "\n";
tout << "x: " << x << "\n";
tout << "y: " << y << "\n";
tout << "i: " << i << "\n";
tout << "get_var(i): " << get_var(i) << "\n";);
SASSERT(y != null_var && get_var(i) <= y);
}
if (get_var(i) == y)
found = true;
}
SASSERT(y == null_var || (y < x && found));
});
return y;
}
std::ostream& display(std::ostream & out, display_var_proc const & proc = display_var_proc(), bool use_star = false) const {
if (m_size == 0) {
out << "1";
return out;
}
for (unsigned i = 0; i < m_size; ++i) {
if (i > 0) {
if (use_star)
out << "*";
else
out << " ";
}
proc(out, get_var(i));
if (degree(i) > 1)
out << "^" << degree(i);
}
return out;
}
void display_smt2(std::ostream & out, display_var_proc const & proc = display_var_proc()) const {
if (m_size == 0) {
out << "1";
}
else if (m_size == 1 && degree(0) == 1) {
proc(out, get_var(0));
}
else {
out << "(*";
for (unsigned i = 0; i < m_size; ++i) {
var x = get_var(i);
unsigned k = degree(i);
SASSERT(k > 0);
for (unsigned j = 0; j < k; ++j) {
out << " ";
proc(out, x);
}
}
out << ")";
}
}
bool is_unit() const { return m_size == 0; }
/**
\brief Return true if the degree of every variable is even.
*/
bool is_power_of_two() const {
for (unsigned i = 0; i < m_size; ++i) {
if (degree(i) % 2 == 1)
return false;
}
return true;
}
bool is_square() const {
for (unsigned i = 0; i < m_size; ++i) {
if (degree(i) % 2 != 0)
return false;
}
return true;
}
void rename(unsigned sz, var const * xs) {
for (unsigned i = 0; i < m_size; ++i) {
power & pw = m_powers[i];
pw.set_var(xs[pw.get_var()]);
}
sort();
m_hash = hash_core(m_size, m_powers);
}
};
inline void swap(monomial * & m1, monomial * & m2) noexcept { std::swap(m1, m2); }
typedef chashtable<monomial*, monomial::hash_proc, monomial::eq_proc> monomial_table;
/**
\brief Mapping from monomials to positions.
*/
class monomial2pos {
unsigned_vector m_m2pos;
public:
unsigned get(monomial const * m) {
unsigned id = m->id();
m_m2pos.reserve(id+1, UINT_MAX);
return m_m2pos[id];
}
void reset(monomial const * m) {
unsigned id = m->id();
if (id < m_m2pos.size())
m_m2pos[id] = UINT_MAX;
}
void set(monomial const * m, unsigned pos) {
unsigned id = m->id();
m_m2pos.reserve(id+1, UINT_MAX);
SASSERT(m_m2pos[id] == UINT_MAX);
m_m2pos[id] = pos;
}
/**
\brief Save the position of the monomials in p.
*/
template<typename Poly>
void set(Poly const * p) {
unsigned sz = p->size();
for (unsigned i = 0; i < sz; ++i) {
set(p->m(i), i);
}
}
/**
\brief Undo the effects of save_pos.
*/
template<typename Poly>
void reset(Poly const * p) {
unsigned sz = p->size();
for (unsigned i = 0; i < sz; ++i) {
reset(p->m(i));
}
}
};
#define TMP_INITIAL_CAPACITY 128
/**
\brief Wrapper for temporary monomials.
*/
class tmp_monomial {
monomial * m_ptr;
unsigned m_capacity; //!< maximum number of arguments supported by m_ptr;
monomial * allocate(unsigned capacity) {
void * mem = memory::allocate(monomial::get_obj_size(capacity));
return new (mem) monomial(UINT_MAX, 0, nullptr, 0);
}
void deallocate(monomial * ptr, unsigned capacity) {
memory::deallocate(ptr);
}
void increase_capacity(unsigned new_capacity) {
SASSERT(new_capacity > m_capacity);
deallocate(m_ptr, m_capacity);
m_ptr = allocate(new_capacity);
m_capacity = new_capacity;
}
void expand_capacity(unsigned new_capacity) {
SASSERT(new_capacity > m_capacity);
monomial * new_ptr = allocate(new_capacity);
new_ptr->m_size = m_ptr->m_size;
std::uninitialized_copy(m_ptr->m_powers, m_ptr->m_powers + m_ptr->m_size, new_ptr->m_powers);
deallocate(m_ptr, m_capacity);
m_ptr = new_ptr;
m_capacity = new_capacity;
}
public:
tmp_monomial():
m_ptr(allocate(TMP_INITIAL_CAPACITY)),
m_capacity(TMP_INITIAL_CAPACITY) {
}
~tmp_monomial() {
deallocate(m_ptr, m_capacity);
}
void init(unsigned sz, power const * pws) {
if (sz > m_capacity)
increase_capacity(sz * 2);
SASSERT(sz < m_capacity);
m_ptr->m_size = sz;
std::uninitialized_copy(pws, pws + sz, m_ptr->m_powers);
}
void reset() {
m_ptr->m_size = 0;
}
unsigned size() const {
return m_ptr->m_size;
}
void push_back(power const & pw) {
if (m_ptr->m_size >= m_capacity)
expand_capacity(m_ptr->m_size * 2);
m_ptr->m_powers[m_ptr->m_size] = pw;
m_ptr->m_size++;
}
monomial * get_ptr() {
unsigned sz = m_ptr->m_size;
m_ptr->m_hash = monomial::hash_core(sz, m_ptr->m_powers);
return m_ptr;
}
void reserve(unsigned capacity) {
if (capacity > m_capacity)
increase_capacity(capacity * 2);
}
void set_size(unsigned sz) {
SASSERT(sz <= m_capacity);
m_ptr->m_size = sz;
}
void set_power(unsigned idx, power const & pw) {
SASSERT(idx < m_capacity);
m_ptr->m_powers[idx] = pw;
}
power const & get_power(unsigned idx) const { return m_ptr->m_powers[idx]; }
power const& operator[](unsigned idx) const { return get_power(idx); }
power const * get_powers() const { return m_ptr->m_powers; }
bool operator==(tmp_monomial const& other) const {
if (size() != other.size())
return false;
for (unsigned i = 0; i < size(); ++i)
if (get_power(i) != other.get_power(i))
return false;
return true;
}
};
/**
\brief Compare m1 and m2 using a lexicographical order
Return
- -1 if m1 <_lex m2,
- 0 if m1 = m2,
- 1 if m1 >_lex m2
The biggest variable dominates
x3^3 > x3^2 x1^2 > x3 x2^2 x_1 > x1^3
Remark: in out representation the biggest variable is in the last position.
*/
int lex_compare(monomial const * m1, monomial const * m2) {
if (m1 == m2)
return 0;
int sz1 = m1->size();
int sz2 = m2->size();
int idx1 = sz1 - 1;
int idx2 = sz2 - 1;
while (idx1 >= 0 && idx2 >= 0) {
power const & pw1 = m1->get_power(idx1);
power const & pw2 = m2->get_power(idx2);
if (pw1.get_var() == pw2.get_var()) {
if (pw1.degree() == pw2.degree()) {
idx1--;
idx2--;
continue;
}
return pw1.degree() < pw2.degree() ? -1 : 1;
}
return pw1.get_var() > pw2.get_var() ? 1 : -1;
}
SASSERT(idx1 >= 0 || idx2 >= 0);
SASSERT(idx1 < 0 || idx2 < 0);
return idx1 < 0 ? -1 : 1;
}
/**
Similar to lex_compare, but min_var is assumed to be the minimal variable.
*/
int lex_compare2(monomial const * m1, monomial const * m2, var min_var) {
if (m1 == m2)
return 0;
int sz1 = m1->size();
int sz2 = m2->size();
int idx1 = sz1 - 1;
int idx2 = sz2 - 1;
unsigned min_var_degree1 = 0;
unsigned min_var_degree2 = 0;
while (idx1 >= 0 && idx2 >= 0) {
power const & pw1 = m1->get_power(idx1);
power const & pw2 = m2->get_power(idx2);
if (pw1.get_var() == min_var) {
min_var_degree1 = pw1.degree();
idx1--;
if (pw2.get_var() == min_var) {
min_var_degree2 = pw2.degree();
idx2--;
}
continue;
}
if (pw2.get_var() == min_var) {
min_var_degree2 = pw2.degree();
idx2--;
continue;
}
if (pw1.get_var() == pw2.get_var()) {
if (pw1.degree() == pw2.degree()) {
idx1--;
idx2--;
continue;
}
return pw1.degree() < pw2.degree() ? -1 : 1;
}
return pw1.get_var() > pw2.get_var() ? 1 : -1;
}
if (idx1 == idx2) {
SASSERT(min_var_degree1 != min_var_degree2);
return min_var_degree1 < min_var_degree2 ? -1 : 1;
}
return idx1 < 0 ? -1 : 1;
}
struct lex_lt2 {
var m_min;
lex_lt2(var m):m_min(m) {}
bool operator()(monomial * m1, monomial * m2) const {
TRACE(lex_bug, tout << "min: x" << m_min << "\n"; m1->display(tout); tout << "\n"; m2->display(tout); tout << "\n";);
return lex_compare2(m1, m2, m_min) < 0;
}
};
/**
\brief Compare m1 and m2 using a graded lexicographical order
\see lex_compare
*/
int graded_lex_compare(monomial const * m1, monomial const * m2) {
unsigned t1 = m1->total_degree();
unsigned t2 = m2->total_degree();
if (t1 == t2)
return lex_compare(m1, m2);
else
return t1 < t2 ? -1 : 1;
}
/**
\brief Compare submonomials m1[start1, end1) and m2[start2, end2) using reverse lexicographical order
*/
int rev_lex_compare(monomial const * m1, unsigned start1, unsigned end1, monomial const * m2, unsigned start2, unsigned end2) {
SASSERT(end1 >= start1);
SASSERT(end2 >= start2);
unsigned idx1 = end1;
unsigned idx2 = end2;
while(idx1 > start1 && idx2 > start2) {
--idx1;
--idx2;
power const & pw1 = m1->get_power(idx1);
power const & pw2 = m2->get_power(idx2);
if (pw1.get_var() == pw2.get_var()) {
if (pw1.degree() == pw2.degree()) {
// Remark: the submonomials have the same total degree, but they are not equal. So, idx1 > 0 and idx2 > 0.
SASSERT(idx1 > start1 && idx2 > start2);
continue;
}
return pw1.degree() > pw2.degree() ? -1 : 1;
}
return pw1.get_var() > pw2.get_var() ? -1 : 1;
}
SASSERT(idx1 == start1 || idx2 == start2);
if (idx1 == start1)
return idx2 == start2 ? 0 : -1;
SASSERT(idx2 == start2 && idx1 != start1);
return 1;
}
/**
\brief Compare m1 and m2 using reverse lexicographical order.
\see lex_compare
*/
int rev_lex_compare(monomial const * m1, monomial const * m2) {
if (m1 == m2)
return 0;
return rev_lex_compare(m1, 0, m1->size(), m2, 0, m2->size());
}
/**
\brief Compare m1 and m2 using graded reverse lexicographical order.
\see lex_compare
*/
int graded_rev_lex_compare(monomial const * m1, monomial const * m2) {
unsigned t1 = m1->total_degree();
unsigned t2 = m2->total_degree();
if (t1 == t2)
return rev_lex_compare(m1, m2);
else
return t1 < t2 ? -1 : 1;
}
struct graded_lex_gt {
bool operator()(monomial const * m1, monomial const * m2) { return graded_lex_compare(m1, m2) < 0; }
};
/**
\brief
*/
class monomial_manager {
unsigned m_ref_count;
small_object_allocator * m_allocator;
bool m_own_allocator;
monomial_table m_monomials;
id_gen m_mid_gen; // id generator for monomials
unsigned m_next_var;
monomial * m_unit;
tmp_monomial m_mk_tmp;
tmp_monomial m_tmp1;
tmp_monomial m_tmp2;
tmp_monomial m_tmp3;
svector<power> m_powers_tmp;
public:
monomial_manager(small_object_allocator * a = nullptr) {
m_ref_count = 0;
m_next_var = 0;
if (a == nullptr) {
m_allocator = alloc(small_object_allocator, "polynomial");
m_own_allocator = true;
}
else {
m_allocator = a;
m_own_allocator = false;
}
m_unit = mk_monomial(0, static_cast<power const *>(nullptr));
inc_ref(m_unit);
}
~monomial_manager() {
dec_ref(m_unit);
CTRACE(polynomial, !m_monomials.empty(),
tout << "monomials leaked (can happen during cancelation)\n";
for (auto * m : m_monomials) {
m->display(tout << m->id() << " " << m->ref_count() << " ") << "\n";
});
for (monomial* m : m_monomials) {
unsigned obj_sz = monomial::get_obj_size(m->size());
m_allocator->deallocate(obj_sz, m);
}
m_monomials.reset();
if (m_own_allocator)
dealloc(m_allocator);
}
void inc_ref() {
m_ref_count++;
}
void dec_ref() {
SASSERT(m_ref_count > 0);
m_ref_count--;
if (m_ref_count == 0)
dealloc(this);
}
small_object_allocator & allocator() { return *m_allocator; }
var mk_var() {
var r = m_next_var;
m_next_var++;
return r;
}
unsigned num_vars() const {
return m_next_var;
}
bool is_valid(var x) const {
return x < m_next_var;
}
void del(monomial * m) {
unsigned obj_sz = monomial::get_obj_size(m->size());
m_monomials.erase(m);
m_mid_gen.recycle(m->id());
m_allocator->deallocate(obj_sz, m);
}
void inc_ref(monomial * m) {
m->inc_ref();
}
void dec_ref(monomial * m) {
m->dec_ref();
if (m->ref_count() == 0)
del(m);
}
monomial * mk_unit() { return m_unit; }
monomial * mk_monomial(tmp_monomial & tmp) {
monomial * tmp_ptr = tmp.get_ptr();
monomial * & m = m_monomials.insert_if_not_there(tmp_ptr);
if (m != tmp_ptr)
return m;
void * mem = m_allocator->allocate(monomial::get_obj_size(tmp_ptr->size()));
unsigned id = m_mid_gen.mk();
monomial * r = new (mem) monomial(id, tmp_ptr->size(), tmp_ptr->get_powers(), tmp_ptr->hash());
m = r;
SASSERT(m_monomials.contains(r));
SASSERT(*(m_monomials.find_core(r)) == r);
return r;
}
monomial * mk_monomial(unsigned sz, power const * pws) {
SASSERT(is_valid_power_product(sz, pws));
m_mk_tmp.init(sz, pws);
return mk_monomial(m_mk_tmp);
}
monomial * convert(monomial const * src) {
unsigned sz = src->size();
for (unsigned i = 0; i < sz; ++i) {
var x = src->get_var(i);
while (x >= num_vars()) {
mk_var();
}
SASSERT(x < num_vars());
}
return mk_monomial(src->size(), src->get_powers());
}
monomial * mk_monomial(var x) {
SASSERT(is_valid(x));
power pw(x, 1);
return mk_monomial(1, &pw);
}
monomial * mk_monomial(var x, unsigned k) {
if (k == 0)
return m_unit;
SASSERT(is_valid(x));
power pw(x, k);
return mk_monomial(1, &pw);
}
monomial * mk_monomial(unsigned sz, var * xs) {
if (sz == 0)
return m_unit;
if (sz == 1)
return mk_monomial(xs[0]);
m_powers_tmp.reset();
std::sort(xs, xs+sz);
SASSERT(is_valid(xs[0]));
m_powers_tmp.push_back(power(xs[0], 1));
for (unsigned i = 1; i < sz; ++i) {
var x = xs[i];
SASSERT(is_valid(x));
power & last = m_powers_tmp.back();
if (x == last.get_var())
last.degree()++;
else
m_powers_tmp.push_back(power(x, 1));
}
return mk_monomial(m_powers_tmp.size(), m_powers_tmp.data());
}
void mul(unsigned sz1, power const* pws1, unsigned sz2, power const* pws2, tmp_monomial& product_tmp) {
product_tmp.reserve(sz1 + sz2); // product has at most sz1 + sz2 powers
unsigned i1 = 0, i2 = 0;
unsigned j = 0;
while (true) {
if (i1 == sz1) {
// copy 2
for (; i2 < sz2; ++i2, ++j)
product_tmp.set_power(j, pws2[i2]);
break;
}
if (i2 == sz2) {
// copy 1
for (; i1 < sz1; ++i1, ++j)
product_tmp.set_power(j, pws1[i1]);
break;
}
power const& pw1 = pws1[i1];
power const& pw2 = pws2[i2];
unsigned v1 = pw1.get_var();
unsigned v2 = pw2.get_var();
if (v1 == v2) {
product_tmp.set_power(j, power(v1, pw1.degree() + pw2.degree()));
i1++;
i2++;
}
else if (v1 < v2) {
product_tmp.set_power(j, pw1);
i1++;
}
else {
SASSERT(v1 > v2);
product_tmp.set_power(j, pw2);
i2++;
}
j++;
}
product_tmp.set_size(j);
}
monomial * mul(unsigned sz1, power const * pws1, unsigned sz2, power const * pws2) {
SASSERT(is_valid_power_product(sz1, pws1));
SASSERT(is_valid_power_product(sz2, pws2));
tmp_monomial & product_tmp = m_tmp1;
mul(sz1, pws1, sz2, pws2, product_tmp);
TRACE(monomial_mul_bug,
tout << "before mk_monomial\n";
tout << "pws1: "; for (unsigned i = 0; i < sz1; ++i) tout << pws1[i] << " "; tout << "\n";
tout << "pws2: "; for (unsigned i = 0; i < sz2; ++i) tout << pws2[i] << " "; tout << "\n";
tout << "product_tmp: "; for (unsigned i = 0; i < product_tmp.size(); ++i) tout << product_tmp.get_power(i) << " ";
tout << "\n";);
monomial * r = mk_monomial(product_tmp);
TRACE(monomial_mul_bug,
tout << "r: "; r->display(tout); tout << "\n";
tout << "pws1: "; for (unsigned i = 0; i < sz1; ++i) tout << pws1[i] << " "; tout << "\n";
tout << "pws2: "; for (unsigned i = 0; i < sz2; ++i) tout << pws2[i] << " "; tout << "\n";
tout << "product_tmp: "; for (unsigned i = 0; i < product_tmp.size(); ++i) tout << product_tmp.get_power(i) << " ";
tout << "\n";);
SASSERT(r->is_valid());
SASSERT(r->total_degree() == power_product_total_degree(sz1, pws1) + power_product_total_degree(sz2, pws2));
return r;