iscc.c: directly include required headers
[barvinok.git] / barvinok.cc
blob0236d4762124724587720fd389db53a77a948335
1 #include <assert.h>
2 #include <iostream>
3 #include <vector>
4 #include <deque>
5 #include <string>
6 #include <sstream>
7 #include <gmp.h>
8 #include <NTL/ZZ.h>
9 #include <NTL/vec_ZZ.h>
10 #include <NTL/mat_ZZ.h>
11 #include <NTL/LLL.h>
12 #include <isl/val.h>
13 #include <isl/space.h>
14 #include <isl/set.h>
15 #include <isl/map.h>
16 #include <isl/union_set.h>
17 #include <isl/union_map.h>
18 #include <isl/polynomial.h>
19 #include <isl_set_polylib.h>
20 #include <barvinok/polylib.h>
21 #include <barvinok/util.h>
22 #include <barvinok/evalue.h>
23 #include "config.h"
24 #include <barvinok/barvinok.h>
25 #include <barvinok/genfun.h>
26 #include <barvinok/options.h>
27 #include <barvinok/sample.h>
28 #include "bfcounter.h"
29 #include "conversion.h"
30 #include "counter.h"
31 #include "decomposer.h"
32 #include "euler.h"
33 #include "lattice_point.h"
34 #include "laurent.h"
35 #include "reduce_domain.h"
36 #include "remove_equalities.h"
37 #include "scale.h"
38 #include "volume.h"
39 #include "bernoulli.h"
40 #include "param_util.h"
41 #include "summate.h"
43 using namespace NTL;
44 using std::cerr;
45 using std::cout;
46 using std::endl;
47 using std::vector;
48 using std::deque;
49 using std::string;
50 using std::ostringstream;
52 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
54 class dpoly_n {
55 public:
56 Matrix *coeff;
57 ~dpoly_n() {
58 Matrix_Free(coeff);
60 dpoly_n(int d) {
61 Value d0, one;
62 value_init(d0);
63 value_init(one);
64 value_set_si(one, 1);
65 coeff = Matrix_Alloc(d+1, d+1+1);
66 value_set_si(coeff->p[0][0], 1);
67 value_set_si(coeff->p[0][d+1], 1);
68 for (int i = 1; i <= d; ++i) {
69 value_multiply(coeff->p[i][0], coeff->p[i-1][0], d0);
70 Vector_Combine(coeff->p[i-1], coeff->p[i-1]+1, coeff->p[i]+1,
71 one, d0, i);
72 value_set_si(coeff->p[i][d+1], i);
73 value_multiply(coeff->p[i][d+1], coeff->p[i][d+1], coeff->p[i-1][d+1]);
74 value_decrement(d0, d0);
76 value_clear(d0);
77 value_clear(one);
79 void div(dpoly& d, Vector *count, int sign) {
80 int len = coeff->NbRows;
81 Matrix * c = Matrix_Alloc(coeff->NbRows, coeff->NbColumns);
82 Value tmp;
83 value_init(tmp);
84 for (int i = 0; i < len; ++i) {
85 Vector_Copy(coeff->p[i], c->p[i], len+1);
86 for (int j = 1; j <= i; ++j) {
87 value_multiply(tmp, d.coeff->p[j], c->p[i][len]);
88 value_oppose(tmp, tmp);
89 Vector_Combine(c->p[i], c->p[i-j], c->p[i],
90 c->p[i-j][len], tmp, len);
91 value_multiply(c->p[i][len], c->p[i][len], c->p[i-j][len]);
93 value_multiply(c->p[i][len], c->p[i][len], d.coeff->p[0]);
95 if (sign == -1) {
96 value_set_si(tmp, -1);
97 Vector_Scale(c->p[len-1], count->p, tmp, len);
98 value_assign(count->p[len], c->p[len-1][len]);
99 } else
100 Vector_Copy(c->p[len-1], count->p, len+1);
101 Vector_Normalize(count->p, len+1);
102 value_clear(tmp);
103 Matrix_Free(c);
107 struct bfe_term : public bfc_term_base {
108 vector<evalue *> factors;
110 bfe_term(int len) : bfc_term_base(len) {
113 ~bfe_term() {
114 for (int i = 0; i < factors.size(); ++i) {
115 if (!factors[i])
116 continue;
117 free_evalue_refs(factors[i]);
118 delete factors[i];
123 static void print_int_vector(int *v, int len, const char *name)
125 cerr << name << endl;
126 for (int j = 0; j < len; ++j) {
127 cerr << v[j] << " ";
129 cerr << endl;
132 static void print_bfc_terms(mat_ZZ& factors, bfc_vec& v)
133 __attribute__((unused));
134 static void print_bfc_terms(mat_ZZ& factors, bfc_vec& v)
136 cerr << endl;
137 cerr << "factors" << endl;
138 cerr << factors << endl;
139 for (int i = 0; i < v.size(); ++i) {
140 cerr << "term: " << i << endl;
141 print_int_vector(v[i]->powers, factors.NumRows(), "powers");
142 cerr << "terms" << endl;
143 cerr << v[i]->terms << endl;
144 bfc_term* bfct = static_cast<bfc_term *>(v[i]);
145 cerr << bfct->c << endl;
149 static void print_bfe_terms(mat_ZZ& factors, bfc_vec& v)
150 __attribute__((unused));
151 static void print_bfe_terms(mat_ZZ& factors, bfc_vec& v)
153 cerr << endl;
154 cerr << "factors" << endl;
155 cerr << factors << endl;
156 for (int i = 0; i < v.size(); ++i) {
157 cerr << "term: " << i << endl;
158 print_int_vector(v[i]->powers, factors.NumRows(), "powers");
159 cerr << "terms" << endl;
160 cerr << v[i]->terms << endl;
161 bfe_term* bfet = static_cast<bfe_term *>(v[i]);
162 for (int j = 0; j < v[i]->terms.NumRows(); ++j) {
163 const char * test[] = {"a", "b"};
164 print_evalue(stderr, bfet->factors[j], test);
165 fprintf(stderr, "\n");
170 struct bfcounter : public bfcounter_base {
171 mpq_t count;
172 Value tz;
174 bfcounter(unsigned dim) : bfcounter_base(dim) {
175 mpq_init(count);
176 lower = 1;
177 value_init(tz);
179 ~bfcounter() {
180 mpq_clear(count);
181 value_clear(tz);
183 virtual void base(mat_ZZ& factors, bfc_vec& v);
184 virtual void get_count(Value *result) {
185 assert(value_one_p(&count[0]._mp_den));
186 value_assign(*result, &count[0]._mp_num);
190 void bfcounter::base(mat_ZZ& factors, bfc_vec& v)
192 unsigned nf = factors.NumRows();
194 for (int i = 0; i < v.size(); ++i) {
195 bfc_term* bfct = static_cast<bfc_term *>(v[i]);
196 int total_power = 0;
197 // factor is always positive, so we always
198 // change signs
199 for (int k = 0; k < nf; ++k)
200 total_power += v[i]->powers[k];
202 int j;
203 for (j = 0; j < nf; ++j)
204 if (v[i]->powers[j] > 0)
205 break;
207 zz2value(factors[j][0], tz);
208 dpoly D(total_power, tz, 1);
209 for (int k = 1; k < v[i]->powers[j]; ++k) {
210 zz2value(factors[j][0], tz);
211 dpoly fact(total_power, tz, 1);
212 D *= fact;
214 for ( ; ++j < nf; )
215 for (int k = 0; k < v[i]->powers[j]; ++k) {
216 zz2value(factors[j][0], tz);
217 dpoly fact(total_power, tz, 1);
218 D *= fact;
221 for (int k = 0; k < v[i]->terms.NumRows(); ++k) {
222 zz2value(v[i]->terms[k][0], tz);
223 dpoly n(total_power, tz);
224 mpq_set_si(tcount, 0, 1);
225 n.div(D, tcount, 1);
226 if (total_power % 2)
227 bfct->c[k].n = -bfct->c[k].n;
228 zz2value(bfct->c[k].n, tn);
229 zz2value(bfct->c[k].d, td);
231 mpz_mul(mpq_numref(tcount), mpq_numref(tcount), tn);
232 mpz_mul(mpq_denref(tcount), mpq_denref(tcount), td);
233 mpq_canonicalize(tcount);
234 mpq_add(count, count, tcount);
236 delete v[i];
241 /* Check whether the polyhedron is unbounded and if so,
242 * check whether it has any (and therefore an infinite number of)
243 * integer points.
244 * If one of the vertices is integer, then we are done.
245 * Otherwise, transform the polyhedron such that one of the rays
246 * is the first unit vector and cut it off at a height that ensures
247 * that if the whole polyhedron has any points, then the remaining part
248 * has integer points. In particular we add the largest coefficient
249 * of a ray to the highest vertex (rounded up).
251 static bool Polyhedron_is_infinite(Polyhedron *P, Value* result,
252 barvinok_options *options)
254 int r = 0;
255 Matrix *M, *M2;
256 Value c, tmp;
257 Value g;
258 bool first;
259 Vector *v;
260 Value offset, size;
261 Polyhedron *R;
263 if (P->NbBid == 0)
264 for (; r < P->NbRays; ++r)
265 if (value_zero_p(P->Ray[r][P->Dimension+1]))
266 break;
267 if (P->NbBid == 0 && r == P->NbRays)
268 return false;
270 if (options->count_sample_infinite) {
271 Vector *sample;
273 sample = Polyhedron_Sample(P, options);
274 if (!sample)
275 value_set_si(*result, 0);
276 else {
277 value_set_si(*result, -1);
278 Vector_Free(sample);
280 return true;
283 for (int i = 0; i < P->NbRays; ++i)
284 if (value_one_p(P->Ray[i][1+P->Dimension])) {
285 value_set_si(*result, -1);
286 return true;
289 value_init(g);
290 M = Matrix_Alloc(P->Dimension+1, P->Dimension+1);
291 Vector_Gcd(P->Ray[r]+1, P->Dimension, &g);
292 Vector_AntiScale(P->Ray[r]+1, M->p[0], g, P->Dimension+1);
293 int ok = unimodular_complete(M, 1);
294 assert(ok);
295 value_set_si(M->p[P->Dimension][P->Dimension], 1);
296 M2 = Transpose(M);
297 Matrix_Free(M);
298 P = Polyhedron_Preimage(P, M2, 0);
299 Matrix_Free(M2);
300 value_clear(g);
302 first = true;
303 value_init(offset);
304 value_init(size);
305 value_init(tmp);
306 value_set_si(size, 0);
308 for (int i = 0; i < P->NbBid; ++i) {
309 value_absolute(tmp, P->Ray[i][1]);
310 if (value_gt(tmp, size))
311 value_assign(size, tmp);
313 for (int i = P->NbBid; i < P->NbRays; ++i) {
314 if (value_zero_p(P->Ray[i][P->Dimension+1])) {
315 if (value_gt(P->Ray[i][1], size))
316 value_assign(size, P->Ray[i][1]);
317 continue;
319 mpz_cdiv_q(tmp, P->Ray[i][1], P->Ray[i][P->Dimension+1]);
320 if (first || value_gt(tmp, offset)) {
321 value_assign(offset, tmp);
322 first = false;
325 value_addto(offset, offset, size);
326 value_clear(size);
327 value_clear(tmp);
329 v = Vector_Alloc(P->Dimension+2);
330 value_set_si(v->p[0], 1);
331 value_set_si(v->p[1], -1);
332 value_assign(v->p[1+P->Dimension], offset);
333 R = AddConstraints(v->p, 1, P, options->MaxRays);
334 Polyhedron_Free(P);
335 P = R;
337 value_clear(offset);
338 Vector_Free(v);
340 value_init(c);
341 barvinok_count_with_options(P, &c, options);
342 Polyhedron_Free(P);
343 if (value_zero_p(c))
344 value_set_si(*result, 0);
345 else
346 value_set_si(*result, -1);
347 value_clear(c);
349 return true;
352 static void evalue2value(evalue *e, Value *v)
354 if (EVALUE_IS_ZERO(*e)) {
355 value_set_si(*v, 0);
356 return;
359 if (value_notzero_p(e->d)) {
360 assert(value_one_p(e->d));
361 value_assign(*v, e->x.n);
362 return;
365 assert(e->x.p->type == partition);
366 assert(e->x.p->size == 2);
367 assert(EVALUE_DOMAIN(e->x.p->arr[0])->Dimension == 0);
368 evalue2value(&e->x.p->arr[1], v);
371 static void barvinok_count_f(Polyhedron *P, Value* result,
372 barvinok_options *options);
374 void barvinok_count_with_options(Polyhedron *P, Value* result,
375 struct barvinok_options *options)
377 int allocated = 0;
378 Polyhedron *Q;
379 bool infinite = false;
381 if (P->next)
382 fprintf(stderr,
383 "barvinok_count: input is a union; only first polyhedron is counted\n");
385 if (emptyQ2(P)) {
386 value_set_si(*result, 0);
387 return;
389 if (P->NbEq != 0) {
390 Q = NULL;
391 do {
392 P = remove_equalities(P, options->MaxRays);
393 if (P)
394 P = DomainConstraintSimplify(P, options->MaxRays);
395 if (Q)
396 Polyhedron_Free(Q);
397 Q = P;
398 } while (P && !emptyQ(P) && P->NbEq != 0);
399 if (!P || emptyQ(P)) {
400 Polyhedron_Free(P);
401 value_set_si(*result, 0);
402 return;
404 allocated = 1;
406 if (Polyhedron_is_infinite(P, result, options)) {
407 if (allocated)
408 Polyhedron_Free(P);
409 return;
411 if (P->Dimension == 0) {
412 /* Test whether the constraints are satisfied */
413 POL_ENSURE_VERTICES(P);
414 value_set_si(*result, !emptyQ(P));
415 if (allocated)
416 Polyhedron_Free(P);
417 return;
419 if (options->summation == BV_SUM_BERNOULLI) {
420 Polyhedron *C = Universe_Polyhedron(0);
421 evalue *sum = barvinok_summate_unweighted(P, C, options);
422 Polyhedron_Free(C);
423 evalue2value(sum, result);
424 evalue_free(sum);
425 return;
427 Q = Polyhedron_Factor(P, 0, NULL, options->MaxRays);
428 if (Q) {
429 if (allocated)
430 Polyhedron_Free(P);
431 P = Q;
432 allocated = 1;
435 barvinok_count_f(P, result, options);
436 if (value_neg_p(*result))
437 infinite = true;
438 if (Q && P->next && value_notzero_p(*result)) {
439 Value factor;
440 value_init(factor);
442 for (Q = P->next; Q; Q = Q->next) {
443 barvinok_count_f(Q, &factor, options);
444 if (value_neg_p(factor)) {
445 infinite = true;
446 continue;
447 } else if (Q->next && value_zero_p(factor)) {
448 value_set_si(*result, 0);
449 break;
451 value_multiply(*result, *result, factor);
454 value_clear(factor);
457 if (allocated)
458 Domain_Free(P);
459 if (infinite)
460 value_set_si(*result, -1);
463 void barvinok_count(Polyhedron *P, Value* result, unsigned NbMaxCons)
465 barvinok_options *options = barvinok_options_new_with_defaults();
466 options->MaxRays = NbMaxCons;
467 barvinok_count_with_options(P, result, options);
468 barvinok_options_free(options);
471 static void barvinok_count_f(Polyhedron *P, Value* result,
472 barvinok_options *options)
474 if (emptyQ2(P)) {
475 value_set_si(*result, 0);
476 return;
479 if (P->Dimension == 1)
480 return Line_Length(P, result);
482 int c = P->NbConstraints;
483 POL_ENSURE_FACETS(P);
484 if (c != P->NbConstraints || P->NbEq != 0) {
485 Polyhedron *next = P->next;
486 P->next = NULL;
487 barvinok_count_with_options(P, result, options);
488 P->next = next;
489 return;
492 POL_ENSURE_VERTICES(P);
494 if (Polyhedron_is_infinite(P, result, options))
495 return;
497 np_base *cnt;
498 if (options->incremental_specialization == BV_SPECIALIZATION_BF)
499 cnt = new bfcounter(P->Dimension);
500 else if (options->incremental_specialization == BV_SPECIALIZATION_DF)
501 cnt = new icounter(P->Dimension);
502 else if (options->incremental_specialization == BV_SPECIALIZATION_TODD)
503 cnt = new tcounter(P->Dimension, options->max_index);
504 else
505 cnt = new counter(P->Dimension, options->max_index);
506 cnt->start(P, options);
508 cnt->get_count(result);
509 delete cnt;
512 typedef evalue * evalue_p;
514 struct enumerator_base {
515 unsigned dim;
516 evalue ** vE;
517 evalue mone;
518 vertex_decomposer *vpd;
520 enumerator_base(unsigned dim, vertex_decomposer *vpd)
522 this->dim = dim;
523 this->vpd = vpd;
525 vE = new evalue_p[vpd->PP->nbV];
526 for (int j = 0; j < vpd->PP->nbV; ++j)
527 vE[j] = 0;
529 value_init(mone.d);
530 evalue_set_si(&mone, -1, 1);
533 void decompose_at(Param_Vertices *V, int _i, barvinok_options *options) {
534 //this->pVD = pVD;
536 vE[_i] = new evalue;
537 value_init(vE[_i]->d);
538 evalue_set_si(vE[_i], 0, 1);
540 vpd->decompose_at_vertex(V, _i, options);
543 virtual ~enumerator_base() {
544 for (int j = 0; j < vpd->PP->nbV; ++j)
545 if (vE[j]) {
546 free_evalue_refs(vE[j]);
547 delete vE[j];
549 delete [] vE;
551 free_evalue_refs(&mone);
554 static enumerator_base *create(Polyhedron *P, unsigned dim,
555 Param_Polyhedron *PP,
556 barvinok_options *options);
559 struct enumerator : public signed_cone_consumer, public vertex_decomposer,
560 public enumerator_base {
561 vec_ZZ lambda;
562 vec_ZZ den;
563 term_info num;
564 Vector *c;
565 mpq_t count;
566 Value tz;
568 enumerator(Polyhedron *P, unsigned dim, Param_Polyhedron *PP) :
569 vertex_decomposer(PP, *this), enumerator_base(dim, this) {
570 randomvector(P, lambda, dim, 0);
571 den.SetLength(dim);
572 c = Vector_Alloc(dim+2);
574 mpq_init(count);
575 value_init(tz);
578 ~enumerator() {
579 mpq_clear(count);
580 Vector_Free(c);
581 value_clear(tz);
584 virtual void handle(const signed_cone& sc, barvinok_options *options);
587 void enumerator::handle(const signed_cone& sc, barvinok_options *options)
589 int sign = sc.sign;
590 assert(sc.rays.NumRows() == dim);
591 for (int k = 0; k < dim; ++k) {
592 if (lambda * sc.rays[k] == 0)
593 throw Orthogonal;
596 lattice_point(V, sc.rays, lambda, &num, sc.det, options);
597 den = sc.rays * lambda;
599 if (dim % 2)
600 sign = -sign;
602 zz2value(den[0], tz);
603 dpoly n(dim, tz, 1);
604 for (int k = 1; k < dim; ++k) {
605 zz2value(den[k], tz);
606 dpoly fact(dim, tz, 1);
607 n *= fact;
609 if (num.E != NULL) {
610 dpoly_n d(dim);
611 d.div(n, c, sign);
612 for (unsigned long i = 0; i < sc.det; ++i) {
613 evalue *EV = evalue_polynomial(c, num.E[i]);
614 eadd(EV, vE[vert]);
615 evalue_free(EV);
616 evalue_free(num.E[i]);
618 delete [] num.E;
619 } else {
620 mpq_set_si(count, 0, 1);
621 if (num.constant.length() == 1) {
622 zz2value(num.constant[0], tz);
623 dpoly d(dim, tz);
624 d.div(n, count, sign);
625 } else {
626 dpoly_n d(dim);
627 d.div(n, c, sign);
628 Value x, acc;
629 value_init(x);
630 value_init(acc);
631 for (unsigned long i = 0; i < sc.det; ++i) {
632 value_assign(acc, c->p[dim]);
633 zz2value(num.constant[i], x);
634 for (int j = dim-1; j >= 0; --j) {
635 value_multiply(acc, acc, x);
636 value_addto(acc, acc, c->p[j]);
638 value_addto(mpq_numref(count), mpq_numref(count), acc);
640 mpz_set(mpq_denref(count), c->p[dim+1]);
641 value_clear(acc);
642 value_clear(x);
644 evalue EV;
645 value_init(EV.d);
646 evalue_set(&EV, &count[0]._mp_num, &count[0]._mp_den);
647 eadd(&EV, vE[vert]);
648 free_evalue_refs(&EV);
652 struct ienumerator_base : enumerator_base {
653 evalue ** E_vertex;
655 ienumerator_base(unsigned dim, vertex_decomposer *vpd) :
656 enumerator_base(dim,vpd) {
657 E_vertex = new evalue_p[dim];
660 virtual ~ienumerator_base() {
661 delete [] E_vertex;
664 evalue *E_num(int i, int d) {
665 return E_vertex[i + (dim-d)];
669 struct cumulator {
670 evalue *factor;
671 evalue *v;
672 dpoly_r *r;
674 cumulator(evalue *factor, evalue *v, dpoly_r *r) :
675 factor(factor), v(v), r(r) {}
677 void cumulate(barvinok_options *options);
679 virtual void add_term(const vector<int>& powers, evalue *f2) = 0;
680 virtual ~cumulator() {}
683 void cumulator::cumulate(barvinok_options *options)
685 evalue cum; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
686 evalue f;
687 evalue t; // E_num[0] - (m-1)
688 evalue *cst;
689 evalue mone;
691 if (options->lookup_table) {
692 value_init(mone.d);
693 evalue_set_si(&mone, -1, 1);
696 value_init(cum.d);
697 evalue_copy(&cum, factor);
698 value_init(f.d);
699 value_init(f.x.n);
700 value_set_si(f.d, 1);
701 value_set_si(f.x.n, 1);
702 value_init(t.d);
703 evalue_copy(&t, v);
705 if (!options->lookup_table) {
706 for (cst = &t; value_zero_p(cst->d); ) {
707 if (cst->x.p->type == fractional)
708 cst = &cst->x.p->arr[1];
709 else
710 cst = &cst->x.p->arr[0];
714 for (int m = 0; m < r->len; ++m) {
715 if (m > 0) {
716 if (m > 1) {
717 value_set_si(f.d, m);
718 emul(&f, &cum);
719 if (!options->lookup_table)
720 value_subtract(cst->x.n, cst->x.n, cst->d);
721 else
722 eadd(&mone, &t);
724 emul(&t, &cum);
726 dpoly_r_term_list& current = r->c[r->len-1-m];
727 dpoly_r_term_list::iterator j;
728 for (j = current.begin(); j != current.end(); ++j) {
729 if ((*j)->coeff == 0)
730 continue;
731 evalue *f2 = new evalue;
732 value_init(f2->d);
733 value_init(f2->x.n);
734 zz2value((*j)->coeff, f2->x.n);
735 zz2value(r->denom, f2->d);
736 emul(&cum, f2);
738 add_term((*j)->powers, f2);
741 free_evalue_refs(&f);
742 free_evalue_refs(&t);
743 free_evalue_refs(&cum);
744 if (options->lookup_table)
745 free_evalue_refs(&mone);
748 struct E_poly_term {
749 vector<int> powers;
750 evalue *E;
753 struct ie_cum : public cumulator {
754 vector<E_poly_term *> terms;
756 ie_cum(evalue *factor, evalue *v, dpoly_r *r) : cumulator(factor, v, r) {}
758 virtual void add_term(const vector<int>& powers, evalue *f2);
761 void ie_cum::add_term(const vector<int>& powers, evalue *f2)
763 int k;
764 for (k = 0; k < terms.size(); ++k) {
765 if (terms[k]->powers == powers) {
766 eadd(f2, terms[k]->E);
767 free_evalue_refs(f2);
768 delete f2;
769 break;
772 if (k >= terms.size()) {
773 E_poly_term *ET = new E_poly_term;
774 ET->powers = powers;
775 ET->E = f2;
776 terms.push_back(ET);
780 struct ienumerator : public signed_cone_consumer, public vertex_decomposer,
781 public ienumerator_base {
782 mat_ZZ den;
783 mat_ZZ vertex;
784 mpq_t tcount;
785 Value tz;
787 ienumerator(unsigned dim, Param_Polyhedron *PP) :
788 vertex_decomposer(PP, *this), ienumerator_base(dim, this) {
789 vertex.SetDims(1, dim);
791 den.SetDims(dim, dim);
792 mpq_init(tcount);
793 value_init(tz);
796 ~ienumerator() {
797 mpq_clear(tcount);
798 value_clear(tz);
801 virtual void handle(const signed_cone& sc, barvinok_options *options);
802 void reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
803 barvinok_options *options);
806 void ienumerator::reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
807 barvinok_options *options)
809 unsigned len = den_f.NumRows(); // number of factors in den
810 unsigned dim = num.NumCols();
811 assert(num.NumRows() == 1);
813 if (dim == 0) {
814 eadd(factor, vE[vert]);
815 return;
818 vec_ZZ den_s;
819 mat_ZZ den_r;
820 vec_ZZ num_s;
821 mat_ZZ num_p;
823 split_one(num, num_s, num_p, den_f, den_s, den_r);
825 vec_ZZ den_p;
826 den_p.SetLength(len);
828 ZZ one;
829 one = 1;
830 normalize(one, num_s, num_p, den_s, den_p, den_r);
831 if (one != 1)
832 emul(&mone, factor);
834 int only_param = 0;
835 int no_param = 0;
836 for (int k = 0; k < len; ++k) {
837 if (den_p[k] == 0)
838 ++no_param;
839 else if (den_s[k] == 0)
840 ++only_param;
842 if (no_param == 0) {
843 reduce(factor, num_p, den_r, options);
844 } else {
845 int k, l;
846 mat_ZZ pden;
847 pden.SetDims(only_param, dim-1);
849 for (k = 0, l = 0; k < len; ++k)
850 if (den_s[k] == 0)
851 pden[l++] = den_r[k];
853 for (k = 0; k < len; ++k)
854 if (den_p[k] == 0)
855 break;
857 zz2value(num_s[0], tz);
858 dpoly n(no_param, tz);
859 zz2value(den_s[k], tz);
860 dpoly D(no_param, tz, 1);
861 for ( ; ++k < len; )
862 if (den_p[k] == 0) {
863 zz2value(den_s[k], tz);
864 dpoly fact(no_param, tz, 1);
865 D *= fact;
868 dpoly_r * r = 0;
869 // if no_param + only_param == len then all powers
870 // below will be all zero
871 if (no_param + only_param == len) {
872 if (E_num(0, dim) != 0)
873 r = new dpoly_r(n, len);
874 else {
875 mpq_set_si(tcount, 0, 1);
876 one = 1;
877 n.div(D, tcount, 1);
879 if (value_notzero_p(mpq_numref(tcount))) {
880 evalue f;
881 value_init(f.d);
882 value_init(f.x.n);
883 value_assign(f.x.n, mpq_numref(tcount));
884 value_assign(f.d, mpq_denref(tcount));
885 emul(&f, factor);
886 reduce(factor, num_p, pden, options);
887 free_evalue_refs(&f);
889 return;
891 } else {
892 for (k = 0; k < len; ++k) {
893 if (den_s[k] == 0 || den_p[k] == 0)
894 continue;
896 zz2value(den_s[k], tz);
897 dpoly pd(no_param-1, tz, 1);
899 int l;
900 for (l = 0; l < k; ++l)
901 if (den_r[l] == den_r[k])
902 break;
904 if (r == 0)
905 r = new dpoly_r(n, pd, l, len);
906 else {
907 dpoly_r *nr = new dpoly_r(r, pd, l, len);
908 delete r;
909 r = nr;
913 dpoly_r *rc = r->div(D);
914 delete r;
915 r = rc;
916 if (E_num(0, dim) == 0) {
917 int common = pden.NumRows();
918 dpoly_r_term_list& final = r->c[r->len-1];
919 int rows;
920 evalue t;
921 evalue f;
922 value_init(f.d);
923 value_init(f.x.n);
924 zz2value(r->denom, f.d);
925 dpoly_r_term_list::iterator j;
926 for (j = final.begin(); j != final.end(); ++j) {
927 if ((*j)->coeff == 0)
928 continue;
929 rows = common;
930 for (int k = 0; k < r->dim; ++k) {
931 int n = (*j)->powers[k];
932 if (n == 0)
933 continue;
934 pden.SetDims(rows+n, pden.NumCols());
935 for (int l = 0; l < n; ++l)
936 pden[rows+l] = den_r[k];
937 rows += n;
939 value_init(t.d);
940 evalue_copy(&t, factor);
941 zz2value((*j)->coeff, f.x.n);
942 emul(&f, &t);
943 reduce(&t, num_p, pden, options);
944 free_evalue_refs(&t);
946 free_evalue_refs(&f);
947 } else {
948 ie_cum cum(factor, E_num(0, dim), r);
949 cum.cumulate(options);
951 int common = pden.NumRows();
952 int rows;
953 for (int j = 0; j < cum.terms.size(); ++j) {
954 rows = common;
955 pden.SetDims(rows, pden.NumCols());
956 for (int k = 0; k < r->dim; ++k) {
957 int n = cum.terms[j]->powers[k];
958 if (n == 0)
959 continue;
960 pden.SetDims(rows+n, pden.NumCols());
961 for (int l = 0; l < n; ++l)
962 pden[rows+l] = den_r[k];
963 rows += n;
965 reduce(cum.terms[j]->E, num_p, pden, options);
966 free_evalue_refs(cum.terms[j]->E);
967 delete cum.terms[j]->E;
968 delete cum.terms[j];
971 delete r;
975 void ienumerator::handle(const signed_cone& sc, barvinok_options *options)
977 assert(sc.det == 1);
978 assert(sc.rays.NumRows() == dim);
980 lattice_point(V, sc.rays, vertex[0], E_vertex, options);
982 den = sc.rays;
984 evalue one;
985 value_init(one.d);
986 evalue_set_si(&one, sc.sign, 1);
987 reduce(&one, vertex, den, options);
988 free_evalue_refs(&one);
990 for (int i = 0; i < dim; ++i)
991 if (E_vertex[i])
992 evalue_free(E_vertex[i]);
995 struct bfenumerator : public vertex_decomposer, public bf_base,
996 public ienumerator_base {
997 evalue *factor;
999 bfenumerator(unsigned dim, Param_Polyhedron *PP) :
1000 vertex_decomposer(PP, *this),
1001 bf_base(dim), ienumerator_base(dim, this) {
1002 lower = 0;
1003 factor = NULL;
1006 ~bfenumerator() {
1009 virtual void handle(const signed_cone& sc, barvinok_options *options);
1010 virtual void base(mat_ZZ& factors, bfc_vec& v);
1012 bfc_term_base* new_bf_term(int len) {
1013 bfe_term* t = new bfe_term(len);
1014 return t;
1017 virtual void set_factor(bfc_term_base *t, int k, int change) {
1018 bfe_term* bfet = static_cast<bfe_term *>(t);
1019 factor = bfet->factors[k];
1020 assert(factor != NULL);
1021 bfet->factors[k] = NULL;
1022 if (change)
1023 emul(&mone, factor);
1026 virtual void set_factor(bfc_term_base *t, int k, mpq_t &q, int change) {
1027 bfe_term* bfet = static_cast<bfe_term *>(t);
1028 factor = bfet->factors[k];
1029 assert(factor != NULL);
1030 bfet->factors[k] = NULL;
1032 evalue f;
1033 value_init(f.d);
1034 value_init(f.x.n);
1035 if (change)
1036 value_oppose(f.x.n, mpq_numref(q));
1037 else
1038 value_assign(f.x.n, mpq_numref(q));
1039 value_assign(f.d, mpq_denref(q));
1040 emul(&f, factor);
1041 free_evalue_refs(&f);
1044 virtual void set_factor(bfc_term_base *t, int k, const QQ& c, int change) {
1045 bfe_term* bfet = static_cast<bfe_term *>(t);
1047 factor = new evalue;
1049 evalue f;
1050 value_init(f.d);
1051 value_init(f.x.n);
1052 zz2value(c.n, f.x.n);
1053 if (change)
1054 value_oppose(f.x.n, f.x.n);
1055 zz2value(c.d, f.d);
1057 value_init(factor->d);
1058 evalue_copy(factor, bfet->factors[k]);
1059 emul(&f, factor);
1060 free_evalue_refs(&f);
1063 void set_factor(evalue *f, int change) {
1064 if (change)
1065 emul(&mone, f);
1066 factor = f;
1069 virtual void insert_term(bfc_term_base *t, int i) {
1070 bfe_term* bfet = static_cast<bfe_term *>(t);
1071 int len = t->terms.NumRows()-1; // already increased by one
1073 bfet->factors.resize(len+1);
1074 for (int j = len; j > i; --j) {
1075 bfet->factors[j] = bfet->factors[j-1];
1076 t->terms[j] = t->terms[j-1];
1078 bfet->factors[i] = factor;
1079 factor = NULL;
1082 virtual void update_term(bfc_term_base *t, int i) {
1083 bfe_term* bfet = static_cast<bfe_term *>(t);
1085 eadd(factor, bfet->factors[i]);
1086 free_evalue_refs(factor);
1087 delete factor;
1090 virtual bool constant_vertex(int dim) { return E_num(0, dim) == 0; }
1092 virtual void cum(bf_reducer *bfr, bfc_term_base *t, int k, dpoly_r *r,
1093 barvinok_options *options);
1096 enumerator_base *enumerator_base::create(Polyhedron *P, unsigned dim,
1097 Param_Polyhedron *PP,
1098 barvinok_options *options)
1100 enumerator_base *eb;
1102 if (options->incremental_specialization == BV_SPECIALIZATION_BF)
1103 eb = new bfenumerator(dim, PP);
1104 else if (options->incremental_specialization == BV_SPECIALIZATION_DF)
1105 eb = new ienumerator(dim, PP);
1106 else
1107 eb = new enumerator(P, dim, PP);
1109 return eb;
1112 struct bfe_cum : public cumulator {
1113 bfenumerator *bfe;
1114 bfc_term_base *told;
1115 int k;
1116 bf_reducer *bfr;
1118 bfe_cum(evalue *factor, evalue *v, dpoly_r *r, bf_reducer *bfr,
1119 bfc_term_base *t, int k, bfenumerator *e) :
1120 cumulator(factor, v, r), bfe(e), told(t), k(k),
1121 bfr(bfr) {
1124 virtual void add_term(const vector<int>& powers, evalue *f2);
1127 void bfe_cum::add_term(const vector<int>& powers, evalue *f2)
1129 bfr->update_powers(powers);
1131 bfc_term_base * t = bfe->find_bfc_term(bfr->vn, bfr->npowers, bfr->nnf);
1132 bfe->set_factor(f2, bfr->l_changes % 2);
1133 bfe->add_term(t, told->terms[k], bfr->l_extra_num);
1136 void bfenumerator::cum(bf_reducer *bfr, bfc_term_base *t, int k,
1137 dpoly_r *r, barvinok_options *options)
1139 bfe_term* bfet = static_cast<bfe_term *>(t);
1140 bfe_cum cum(bfet->factors[k], E_num(0, bfr->d), r, bfr, t, k, this);
1141 cum.cumulate(options);
1144 void bfenumerator::base(mat_ZZ& factors, bfc_vec& v)
1146 for (int i = 0; i < v.size(); ++i) {
1147 assert(v[i]->terms.NumRows() == 1);
1148 evalue *factor = static_cast<bfe_term *>(v[i])->factors[0];
1149 eadd(factor, vE[vert]);
1150 delete v[i];
1154 void bfenumerator::handle(const signed_cone& sc, barvinok_options *options)
1156 assert(sc.det == 1);
1157 assert(sc.rays.NumRows() == enumerator_base::dim);
1159 bfe_term* t = new bfe_term(enumerator_base::dim);
1160 vector< bfc_term_base * > v;
1161 v.push_back(t);
1163 t->factors.resize(1);
1165 t->terms.SetDims(1, enumerator_base::dim);
1166 lattice_point(V, sc.rays, t->terms[0], E_vertex, options);
1168 // the elements of factors are always lexpositive
1169 mat_ZZ factors;
1170 int s = setup_factors(sc.rays, factors, t, sc.sign);
1172 t->factors[0] = new evalue;
1173 value_init(t->factors[0]->d);
1174 evalue_set_si(t->factors[0], s, 1);
1175 reduce(factors, v, options);
1177 for (int i = 0; i < enumerator_base::dim; ++i)
1178 if (E_vertex[i])
1179 evalue_free(E_vertex[i]);
1182 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1183 barvinok_options *options);
1185 /* Destroys C */
1186 static evalue* barvinok_enumerate_cst(Polyhedron *P, Polyhedron* C,
1187 struct barvinok_options *options)
1189 evalue *eres;
1191 if (emptyQ2(C)) {
1192 Polyhedron_Free(C);
1193 return evalue_zero();
1196 ALLOC(evalue, eres);
1197 value_init(eres->d);
1198 value_set_si(eres->d, 0);
1199 eres->x.p = new_enode(partition, 2, C->Dimension);
1200 EVALUE_SET_DOMAIN(eres->x.p->arr[0],
1201 DomainConstraintSimplify(C, options->MaxRays));
1202 value_set_si(eres->x.p->arr[1].d, 1);
1203 value_init(eres->x.p->arr[1].x.n);
1204 if (emptyQ2(P))
1205 value_set_si(eres->x.p->arr[1].x.n, 0);
1206 else
1207 barvinok_count_with_options(P, &eres->x.p->arr[1].x.n, options);
1208 if (value_mone_p(eres->x.p->arr[1].x.n)) {
1209 value_clear(eres->x.p->arr[1].x.n);
1210 value_set_si(eres->x.p->arr[1].d, -2); /* NaN */
1213 return eres;
1216 static evalue* enumerate(Polyhedron *P, Polyhedron* C,
1217 struct barvinok_options *options)
1219 Polyhedron *next;
1220 Polyhedron *Porig = P;
1221 Polyhedron *Corig = C;
1222 Polyhedron *CEq = NULL;
1223 unsigned nparam = C->Dimension;
1224 evalue *eres;
1225 Matrix *CP = NULL;
1227 evalue factor;
1228 value_init(factor.d);
1229 evalue_set_si(&factor, 1, 1);
1231 /* for now */
1232 POL_ENSURE_FACETS(P);
1233 POL_ENSURE_VERTICES(P);
1234 POL_ENSURE_FACETS(C);
1235 POL_ENSURE_VERTICES(C);
1237 if (C->Dimension == 0 || emptyQ(P) || emptyQ(C)) {
1238 constant:
1239 if (CEq == Porig)
1240 CEq = Polyhedron_Copy(CEq);
1241 eres = barvinok_enumerate_cst(P, CEq ? CEq : Polyhedron_Copy(C), options);
1242 out:
1243 if (CP) {
1244 evalue_backsubstitute(eres, CP, options->MaxRays);
1245 Matrix_Free(CP);
1248 emul(&factor, eres);
1249 if (options->approx->method == BV_APPROX_DROP) {
1250 if (options->approx->approximation == BV_APPROX_SIGN_UPPER)
1251 evalue_frac2polynomial(eres, 1, options->MaxRays);
1252 if (options->approx->approximation == BV_APPROX_SIGN_LOWER)
1253 evalue_frac2polynomial(eres, -1, options->MaxRays);
1254 if (options->approx->approximation == BV_APPROX_SIGN_APPROX)
1255 evalue_frac2polynomial(eres, 0, options->MaxRays);
1257 reduce_evalue(eres);
1258 free_evalue_refs(&factor);
1259 if (P != Porig)
1260 Domain_Free(P);
1261 if (C != Corig)
1262 Polyhedron_Free(C);
1264 return eres;
1266 if (Polyhedron_is_unbounded(P, nparam, options->MaxRays))
1267 goto constant;
1269 if (P->Dimension == nparam) {
1270 CEq = DomainIntersection(P, C, options->MaxRays);
1271 P = Universe_Polyhedron(0);
1272 goto constant;
1274 if (P->NbEq != 0 || C->NbEq != 0) {
1275 Polyhedron *Q = P;
1276 Polyhedron *D = C;
1277 remove_all_equalities(&P, &C, &CP, NULL, nparam, options->MaxRays);
1278 if (C != D && D != Corig)
1279 Polyhedron_Free(D);
1280 if (P != Q && Q != Porig)
1281 Domain_Free(Q);
1282 eres = enumerate(P, C, options);
1283 goto out;
1286 Polyhedron *T = Polyhedron_Factor(P, nparam, NULL, options->MaxRays);
1287 if (T || (P->Dimension == nparam+1)) {
1288 Polyhedron *C2 = C;
1289 Polyhedron *FC = Factor_Context(T ? T : P, nparam, options->MaxRays);
1290 C = DomainIntersection(C, FC, options->MaxRays);
1291 if (C2 != Corig)
1292 Polyhedron_Free(C2);
1293 Polyhedron_Free(FC);
1295 if (T) {
1296 if (P != Porig)
1297 Polyhedron_Free(P);
1298 P = T;
1299 if (T->Dimension == C->Dimension) {
1300 P = T->next;
1301 T->next = NULL;
1302 Polyhedron_Free(T);
1306 next = P->next;
1307 P->next = NULL;
1308 eres = barvinok_enumerate_ev_f(P, C, options);
1309 P->next = next;
1311 if (P->next) {
1312 Polyhedron *Q;
1313 evalue *f;
1315 for (Q = P->next; Q; Q = Q->next) {
1316 Polyhedron *next = Q->next;
1317 Q->next = NULL;
1319 f = barvinok_enumerate_ev_f(Q, C, options);
1320 emul(f, eres);
1321 evalue_free(f);
1323 Q->next = next;
1327 goto out;
1330 evalue* barvinok_enumerate_with_options(Polyhedron *P, Polyhedron* C,
1331 struct barvinok_options *options)
1333 Polyhedron *next, *Cnext, *C1;
1334 Polyhedron *Corig = C;
1335 evalue *eres;
1337 if (P->next)
1338 fprintf(stderr,
1339 "barvinok_enumerate: input is a union; only first polyhedron is enumerated\n");
1341 if (C->next)
1342 fprintf(stderr,
1343 "barvinok_enumerate: context is a union; only first polyhedron is considered\n");
1345 Cnext = C->next;
1346 C->next = NULL;
1347 C1 = Polyhedron_Project(P, C->Dimension);
1348 C = DomainIntersection(C, C1, options->MaxRays);
1349 Polyhedron_Free(C1);
1350 next = P->next;
1351 P->next = NULL;
1353 if (options->approx->method == BV_APPROX_BERNOULLI ||
1354 options->summation == BV_SUM_BERNOULLI) {
1355 int summation = options->summation;
1356 options->summation = BV_SUM_BERNOULLI;
1357 eres = barvinok_summate_unweighted(P, C, options);
1358 options->summation = summation;
1359 } else
1360 eres = enumerate(P, C, options);
1361 Domain_Free(C);
1363 P->next= next;
1364 Corig->next = Cnext;
1366 return eres;
1369 evalue* barvinok_enumerate_ev(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1371 evalue *E;
1372 barvinok_options *options = barvinok_options_new_with_defaults();
1373 options->MaxRays = MaxRays;
1374 E = barvinok_enumerate_with_options(P, C, options);
1375 barvinok_options_free(options);
1376 return E;
1379 evalue *Param_Polyhedron_Enumerate(Param_Polyhedron *PP, Polyhedron *P,
1380 Polyhedron *C,
1381 struct barvinok_options *options)
1383 evalue *eres;
1384 Param_Domain *D;
1385 unsigned nparam = C->Dimension;
1386 unsigned dim = P->Dimension - nparam;
1388 int nd;
1389 for (nd = 0, D=PP->D; D; ++nd, D=D->next);
1390 evalue_section *s = new evalue_section[nd];
1391 Polyhedron *TC = true_context(P, C, options->MaxRays);
1393 enumerator_base *et = NULL;
1394 try_again:
1395 if (et)
1396 delete et;
1398 et = enumerator_base::create(P, dim, PP, options);
1400 FORALL_REDUCED_DOMAIN(PP, TC, nd, options, i, D, rVD)
1401 Param_Vertices *V;
1403 s[i].E = evalue_zero();
1404 s[i].D = rVD;
1406 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1407 if (!et->vE[_i])
1408 try {
1409 et->decompose_at(V, _i, options);
1410 } catch (OrthogonalException &e) {
1411 FORALL_REDUCED_DOMAIN_RESET;
1412 for (; i >= 0; --i) {
1413 evalue_free(s[i].E);
1414 Domain_Free(s[i].D);
1416 goto try_again;
1418 eadd(et->vE[_i] , s[i].E);
1419 END_FORALL_PVertex_in_ParamPolyhedron;
1420 evalue_range_reduction_in_domain(s[i].E, rVD);
1421 END_FORALL_REDUCED_DOMAIN
1422 Polyhedron_Free(TC);
1424 delete et;
1425 eres = evalue_from_section_array(s, nd);
1426 delete [] s;
1428 return eres;
1431 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1432 barvinok_options *options)
1434 unsigned nparam = C->Dimension;
1435 bool do_scale = options->approx->method == BV_APPROX_SCALE;
1437 if (options->summation == BV_SUM_EULER)
1438 return barvinok_summate_unweighted(P, C, options);
1440 if (options->approx->method == BV_APPROX_VOLUME)
1441 return Param_Polyhedron_Volume(P, C, options);
1443 if (P->Dimension - nparam == 1 && !do_scale)
1444 return ParamLine_Length(P, C, options);
1446 Param_Polyhedron *PP = NULL;
1447 evalue *eres;
1449 if (do_scale) {
1450 eres = scale_bound(P, C, options);
1451 if (eres)
1452 return eres;
1455 PP = Polyhedron2Param_Polyhedron(P, C, options);
1457 if (do_scale)
1458 eres = scale(PP, P, C, options);
1459 else
1460 eres = Param_Polyhedron_Enumerate(PP, P, C, options);
1462 if (PP)
1463 Param_Polyhedron_Free(PP);
1465 return eres;
1468 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1470 evalue *EP = barvinok_enumerate_ev(P, C, MaxRays);
1472 return partition2enumeration(EP);
1475 evalue* barvinok_enumerate_union(Polyhedron *D, Polyhedron* C, unsigned MaxRays)
1477 evalue *EP;
1478 gen_fun *gf = barvinok_enumerate_union_series(D, C, MaxRays);
1479 EP = *gf;
1480 delete gf;
1481 return EP;
1484 static __isl_give isl_pw_qpolynomial *basic_set_card(
1485 __isl_take isl_basic_set *bset)
1487 isl_ctx *ctx;
1488 isl_space *dim;
1489 isl_pw_qpolynomial *pwqp;
1490 unsigned nparam = isl_basic_set_dim(bset, isl_dim_param);
1491 Polyhedron *U = Universe_Polyhedron(nparam);
1492 Polyhedron *P;
1493 evalue *E;
1494 barvinok_options *options;
1495 int options_allocated = 0;
1497 ctx = isl_basic_set_get_ctx(bset);
1498 options = isl_ctx_peek_barvinok_options(ctx);
1499 if (!options) {
1500 options = barvinok_options_new_with_defaults();
1501 options_allocated = 1;
1504 dim = isl_basic_set_get_space(bset);
1505 dim = isl_space_domain(dim);
1507 P = isl_basic_set_to_polylib(bset);
1508 E = enumerate(P, U, options);
1510 pwqp = isl_pw_qpolynomial_from_evalue(dim, E);
1511 isl_basic_set_free(bset);
1513 evalue_free(E);
1514 Polyhedron_Free(P);
1515 Polyhedron_Free(U);
1516 if (options_allocated)
1517 barvinok_options_free(options);
1519 return pwqp;
1522 static isl_stat basic_map_card(__isl_take isl_basic_map *bmap, void *user)
1524 isl_pw_qpolynomial **sum = (isl_pw_qpolynomial **)user;
1525 isl_pw_qpolynomial *pwqp;
1526 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
1527 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
1528 isl_space *target_dim;
1529 isl_basic_set *bset;
1531 target_dim = isl_basic_map_get_space(bmap);
1532 target_dim = isl_space_domain(target_dim);
1534 bmap = isl_basic_map_move_dims(bmap, isl_dim_param, nparam,
1535 isl_dim_in, 0, n_in);
1537 bset = isl_basic_map_range(bmap);
1538 bset = isl_basic_set_lift(bset);
1539 pwqp = isl_basic_set_multiplicative_call(bset, &basic_set_card);
1541 pwqp = isl_pw_qpolynomial_move_dims(pwqp, isl_dim_in, 0,
1542 isl_dim_param, nparam, n_in);
1543 pwqp = isl_pw_qpolynomial_reset_domain_space(pwqp, target_dim);
1544 *sum = isl_pw_qpolynomial_add(*sum, pwqp);
1546 return isl_stat_ok;
1549 static __isl_give isl_pw_qpolynomial *card_as_sum(__isl_take isl_map *map,
1550 barvinok_options *options)
1552 isl_ctx *ctx;
1553 isl_val *one;
1554 isl_space *dim;
1555 isl_set *set;
1556 isl_qpolynomial *qp;
1557 isl_pw_qpolynomial *pwqp;
1558 int summation = options->summation;
1560 if (!map)
1561 return NULL;
1563 options->summation = BV_SUM_BERNOULLI;
1565 set = isl_map_wrap(map);
1566 dim = isl_set_get_space(set);
1567 ctx = isl_map_get_ctx(map);
1568 one = isl_val_one(ctx);
1569 qp = isl_qpolynomial_val_on_domain(dim, one);
1571 pwqp = isl_pw_qpolynomial_alloc(set, qp);
1572 pwqp = isl_pw_qpolynomial_sum(pwqp);
1574 options->summation = summation;
1576 return pwqp;
1579 __isl_give isl_pw_qpolynomial *isl_map_card(__isl_take isl_map *map)
1581 isl_ctx *ctx;
1582 isl_space *dim;
1583 isl_pw_qpolynomial *sum;
1584 barvinok_options *options;
1586 ctx = isl_map_get_ctx(map);
1587 options = isl_ctx_peek_barvinok_options(ctx);
1588 if (options &&
1589 (options->approx->method == BV_APPROX_BERNOULLI ||
1590 options->summation == BV_SUM_BERNOULLI))
1591 return card_as_sum(map, options);
1593 dim = isl_map_get_space(map);
1594 dim = isl_space_domain(dim);
1595 dim = isl_space_from_domain(dim);
1596 dim = isl_space_add_dims(dim, isl_dim_out, 1);
1597 sum = isl_pw_qpolynomial_zero(dim);
1599 map = isl_map_make_disjoint(map);
1600 map = isl_map_compute_divs(map);
1602 if (isl_map_foreach_basic_map(map, &basic_map_card, &sum) < 0)
1603 goto error;
1605 isl_map_free(map);
1607 return sum;
1608 error:
1609 isl_map_free(map);
1610 isl_pw_qpolynomial_free(sum);
1611 return NULL;
1614 __isl_give isl_pw_qpolynomial *isl_set_card(__isl_take isl_set *set)
1616 isl_pw_qpolynomial *pwqp;
1617 pwqp = isl_map_card(isl_map_from_range(set));
1618 pwqp = isl_pw_qpolynomial_project_domain_on_params(pwqp);
1619 return pwqp;
1622 __isl_give isl_pw_qpolynomial *isl_basic_map_card(__isl_take isl_basic_map *bmap)
1624 return isl_map_card(isl_map_from_basic_map(bmap));
1627 __isl_give isl_pw_qpolynomial *isl_basic_set_card(__isl_take isl_basic_set *bset)
1629 isl_pw_qpolynomial *pwqp;
1630 pwqp = isl_basic_map_card(isl_basic_map_from_range(bset));
1631 pwqp = isl_pw_qpolynomial_project_domain_on_params(pwqp);
1632 return pwqp;
1635 static isl_stat set_card(__isl_take isl_set *set, void *user)
1637 isl_union_pw_qpolynomial **res = (isl_union_pw_qpolynomial **)user;
1638 isl_pw_qpolynomial *pwqp;
1639 isl_union_pw_qpolynomial *upwqp;
1641 pwqp = isl_set_card(set);
1642 upwqp = isl_union_pw_qpolynomial_from_pw_qpolynomial(pwqp);
1643 *res = isl_union_pw_qpolynomial_add(*res, upwqp);
1645 return isl_stat_ok;
1648 __isl_give isl_union_pw_qpolynomial *isl_union_set_card(
1649 __isl_take isl_union_set *uset)
1651 isl_space *dim;
1652 isl_union_pw_qpolynomial *res;
1654 dim = isl_union_set_get_space(uset);
1655 res = isl_union_pw_qpolynomial_zero(dim);
1656 if (isl_union_set_foreach_set(uset, &set_card, &res) < 0)
1657 goto error;
1658 isl_union_set_free(uset);
1660 return res;
1661 error:
1662 isl_union_set_free(uset);
1663 isl_union_pw_qpolynomial_free(res);
1664 return NULL;
1667 static isl_stat map_card(__isl_take isl_map *map, void *user)
1669 isl_union_pw_qpolynomial **res = (isl_union_pw_qpolynomial **)user;
1670 isl_pw_qpolynomial *pwqp;
1671 isl_union_pw_qpolynomial *upwqp;
1673 pwqp = isl_map_card(map);
1674 upwqp = isl_union_pw_qpolynomial_from_pw_qpolynomial(pwqp);
1675 *res = isl_union_pw_qpolynomial_add(*res, upwqp);
1677 return isl_stat_ok;
1680 __isl_give isl_union_pw_qpolynomial *isl_union_map_card(
1681 __isl_take isl_union_map *umap)
1683 isl_space *dim;
1684 isl_union_pw_qpolynomial *res;
1686 dim = isl_union_map_get_space(umap);
1687 res = isl_union_pw_qpolynomial_zero(dim);
1688 if (isl_union_map_foreach_map(umap, &map_card, &res) < 0)
1689 goto error;
1690 isl_union_map_free(umap);
1692 return res;
1693 error:
1694 isl_union_map_free(umap);
1695 isl_union_pw_qpolynomial_free(res);
1696 return NULL;