assume NTL has been compiled in ISO mode
[barvinok.git] / reducer.h
blob0ddf19d54023a6d26b4217091af963153c6720d6
1 #ifndef REDUCER_H
2 #define REDUCER_H
4 #include <assert.h>
5 #include <NTL/mat_ZZ.h>
6 #include <barvinok/NTL_QQ.h>
7 #include <barvinok/options.h>
8 #include "decomposer.h"
9 #include "dpoly.h"
11 using namespace NTL;
13 struct gen_fun;
15 extern struct OrthogonalException {} Orthogonal;
17 /* base for non-parametric counting */
18 struct np_base : public signed_cone_consumer {
19 unsigned dim;
20 ZZ one;
22 np_base(unsigned dim) {
23 assert(dim > 0);
24 this->dim = dim;
25 one = 1;
28 virtual void handle(const mat_ZZ& rays, Value *vertex, const QQ& c,
29 unsigned long det,
30 barvinok_options *options) = 0;
31 virtual void handle(const signed_cone& sc, barvinok_options *options);
32 virtual void start(Polyhedron *P, barvinok_options *options);
33 void do_vertex_cone(const QQ& factor, Polyhedron *Cone,
34 Value *vertex, barvinok_options *options) {
35 current_vertex = vertex;
36 this->factor = factor;
37 barvinok_decompose(Cone, *this, options);
39 virtual void init(Polyhedron *P, int n_try) {
41 virtual void reset() {
42 assert(0);
44 virtual void get_count(Value *result) {
45 assert(0);
47 virtual ~np_base() {
50 private:
51 QQ factor;
52 Value *current_vertex;
55 struct reducer : public np_base {
56 mat_ZZ vertex;
57 //vec_ZZ den;
58 ZZ num;
59 mpq_t tcount;
60 mpz_t tn;
61 mpz_t td;
62 int lower; // call base when only this many variables is left
63 Value tz;
65 reducer(unsigned dim) : np_base(dim) {
66 vertex.SetDims(1, dim);
67 //den.SetLength(dim);
68 mpq_init(tcount);
69 mpz_init(tn);
70 mpz_init(td);
71 value_init(tz);
74 ~reducer() {
75 value_clear(tz);
76 mpq_clear(tcount);
77 mpz_clear(tn);
78 mpz_clear(td);
81 virtual void handle(const mat_ZZ& rays, Value *vertex, const QQ& c,
82 unsigned long det, barvinok_options *options);
83 void reduce(const vec_QQ& c, const mat_ZZ& num, const mat_ZZ& den_f);
84 void reduce_canonical(const vec_QQ& c, const mat_ZZ& num,
85 const mat_ZZ& den_f);
86 virtual void base(const QQ& c, const vec_ZZ& num, const mat_ZZ& den_f) = 0;
87 virtual void base(const vec_QQ& c, const mat_ZZ& num, const mat_ZZ& den_f);
88 virtual void split(const mat_ZZ& num, vec_ZZ& num_s, mat_ZZ& num_p,
89 const mat_ZZ& den_f, vec_ZZ& den_s, mat_ZZ& den_r) = 0;
90 virtual gen_fun *get_gf() {
91 assert(0);
92 return NULL;
96 void split_one(const mat_ZZ& num, vec_ZZ& num_s, mat_ZZ& num_p,
97 const mat_ZZ& den_f, vec_ZZ& den_s, mat_ZZ& den_r);
99 struct ireducer : public reducer {
100 ireducer(unsigned dim) : reducer(dim) {}
102 virtual void split(const mat_ZZ& num, vec_ZZ& num_s, mat_ZZ& num_p,
103 const mat_ZZ& den_f, vec_ZZ& den_s, mat_ZZ& den_r) {
104 split_one(num, num_s, num_p, den_f, den_s, den_r);
108 void normalize(ZZ& sign, vec_ZZ& num_s, mat_ZZ& num_p, vec_ZZ& den_s, vec_ZZ& den_p,
109 mat_ZZ& f);
111 // incremental counter
112 struct icounter : public ireducer {
113 mpq_t count;
115 icounter(unsigned dim) : ireducer(dim) {
116 mpq_init(count);
117 lower = 1;
119 ~icounter() {
120 mpq_clear(count);
122 virtual void base(const QQ& c, const vec_ZZ& num, const mat_ZZ& den_f);
123 virtual void get_count(Value *result) {
124 assert(value_one_p(&count[0]._mp_den));
125 value_assign(*result, &count[0]._mp_num);
129 #endif