Polyhedron_Sample: allow equalities in input polyhedra
[barvinok.git] / bfcounter.h
blob4eb8b039c6f0461bdcd9e753005284e6008c93de
1 #include <vector>
2 #include <gmp.h>
3 extern "C" {
4 #include <polylib/polylibgmp.h>
6 #include <barvinok/NTL_QQ.h>
7 #include "reducer.h"
9 struct bfc_term_base {
10 // the number of times a given factor appears in the denominator
11 int *powers;
12 mat_ZZ terms;
14 bfc_term_base(int len) {
15 powers = new int[len];
18 virtual ~bfc_term_base() {
19 delete [] powers;
23 struct bfc_term : public bfc_term_base {
24 vec_QQ c;
26 bfc_term(int len) : bfc_term_base(len) {}
29 typedef std::vector< bfc_term_base * > bfc_vec;
31 struct bf_reducer;
33 struct bf_base : public np_base {
34 ZZ one;
35 mpq_t tcount;
36 mpz_t tn;
37 mpz_t td;
38 int lower; // call base when only this many variables is left
40 bf_base(unsigned dim) : np_base(dim) {
41 mpq_init(tcount);
42 mpz_init(tn);
43 mpz_init(td);
44 one = 1;
47 ~bf_base() {
48 mpq_clear(tcount);
49 mpz_clear(tn);
50 mpz_clear(td);
53 virtual void handle_polar(Polyhedron *C, Value *vertex, QQ c);
54 int setup_factors(Polyhedron *P, mat_ZZ& factors, bfc_term_base* t, int s);
56 bfc_term_base* find_bfc_term(bfc_vec& v, int *powers, int len);
57 void add_term(bfc_term_base *t, vec_ZZ& num1, vec_ZZ& num);
58 void add_term(bfc_term_base *t, vec_ZZ& num);
60 void reduce(mat_ZZ& factors, bfc_vec& v);
61 virtual void base(mat_ZZ& factors, bfc_vec& v) = 0;
63 virtual bfc_term_base* new_bf_term(int len) = 0;
64 virtual void set_factor(bfc_term_base *t, int k, int change) = 0;
65 virtual void set_factor(bfc_term_base *t, int k, mpq_t &f, int change) = 0;
66 virtual void set_factor(bfc_term_base *t, int k, const QQ& c, int change) = 0;
67 virtual void update_term(bfc_term_base *t, int i) = 0;
68 virtual void insert_term(bfc_term_base *t, int i) = 0;
69 virtual bool constant_vertex(int dim) = 0;
70 virtual void cum(bf_reducer *bfr, bfc_term_base *t, int k,
71 dpoly_r *r) = 0;
74 struct bfcounter_base : public bf_base {
75 QQ c;
77 bfcounter_base(unsigned dim) : bf_base(dim) {
80 bfc_term_base* new_bf_term(int len);
82 virtual void set_factor(bfc_term_base *t, int k, int change);
83 virtual void set_factor(bfc_term_base *t, int k, mpq_t &f, int change);
84 virtual void set_factor(bfc_term_base *t, int k, const QQ& c_factor, int change);
85 virtual void insert_term(bfc_term_base *t, int i);
86 virtual void update_term(bfc_term_base *t, int i);
88 virtual bool constant_vertex(int dim) { return true; }
89 virtual void cum(bf_reducer *bfr, bfc_term_base *t, int k, dpoly_r *r) {
90 assert(0);
94 struct bf_reducer {
95 mat_ZZ& factors;
96 bfc_vec& v;
97 bf_base *bf;
99 unsigned nf;
100 unsigned d;
102 mat_ZZ nfactors;
103 int *old2new;
104 int *sign;
105 unsigned int nnf;
106 bfc_vec vn;
108 vec_ZZ extra_num;
109 int changes;
110 int no_param; // r from text
111 int only_param; // k-r-s from text
112 int total_power; // k from text
114 // created in compute_reduced_factors
115 int *bpowers;
116 // set in update_powers
117 int *npowers;
118 vec_ZZ l_extra_num;
119 int l_changes;
121 bf_reducer(mat_ZZ& factors, bfc_vec& v, bf_base *bf)
122 : factors(factors), v(v), bf(bf) {
123 nf = factors.NumRows();
124 d = factors.NumCols();
125 old2new = new int[nf];
126 sign = new int[nf];
128 extra_num.SetLength(d-1);
130 ~bf_reducer() {
131 delete [] old2new;
132 delete [] sign;
133 delete [] npowers;
134 delete [] bpowers;
137 void compute_reduced_factors();
138 void compute_extra_num(int i);
140 void reduce();
142 void update_powers(int *powers, int len);