laurent_old.cc: laurent_summator_old: member initializer list: base classes first
[barvinok.git] / barvinok.cc
blobd8886d57bbdd9fa3b2c73ab824008c1301763eda
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/mat_ZZ.h>
9 #include <NTL/LLL.h>
10 #include <isl/map.h>
11 #include <isl_set_polylib.h>
12 #include <barvinok/util.h>
13 #include <barvinok/evalue.h>
14 #include "config.h"
15 #include <barvinok/barvinok.h>
16 #include <barvinok/genfun.h>
17 #include <barvinok/options.h>
18 #include <barvinok/sample.h>
19 #include "bfcounter.h"
20 #include "conversion.h"
21 #include "counter.h"
22 #include "decomposer.h"
23 #include "euler.h"
24 #include "lattice_point.h"
25 #include "laurent.h"
26 #include "reduce_domain.h"
27 #include "remove_equalities.h"
28 #include "scale.h"
29 #include "volume.h"
30 #include "bernoulli.h"
31 #include "param_util.h"
32 #include "summate.h"
34 using namespace NTL;
35 using std::cerr;
36 using std::cout;
37 using std::endl;
38 using std::vector;
39 using std::deque;
40 using std::string;
41 using std::ostringstream;
43 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
45 class dpoly_n {
46 public:
47 Matrix *coeff;
48 ~dpoly_n() {
49 Matrix_Free(coeff);
51 dpoly_n(int d) {
52 Value d0, one;
53 value_init(d0);
54 value_init(one);
55 value_set_si(one, 1);
56 coeff = Matrix_Alloc(d+1, d+1+1);
57 value_set_si(coeff->p[0][0], 1);
58 value_set_si(coeff->p[0][d+1], 1);
59 for (int i = 1; i <= d; ++i) {
60 value_multiply(coeff->p[i][0], coeff->p[i-1][0], d0);
61 Vector_Combine(coeff->p[i-1], coeff->p[i-1]+1, coeff->p[i]+1,
62 one, d0, i);
63 value_set_si(coeff->p[i][d+1], i);
64 value_multiply(coeff->p[i][d+1], coeff->p[i][d+1], coeff->p[i-1][d+1]);
65 value_decrement(d0, d0);
67 value_clear(d0);
68 value_clear(one);
70 void div(dpoly& d, Vector *count, int sign) {
71 int len = coeff->NbRows;
72 Matrix * c = Matrix_Alloc(coeff->NbRows, coeff->NbColumns);
73 Value tmp;
74 value_init(tmp);
75 for (int i = 0; i < len; ++i) {
76 Vector_Copy(coeff->p[i], c->p[i], len+1);
77 for (int j = 1; j <= i; ++j) {
78 value_multiply(tmp, d.coeff->p[j], c->p[i][len]);
79 value_oppose(tmp, tmp);
80 Vector_Combine(c->p[i], c->p[i-j], c->p[i],
81 c->p[i-j][len], tmp, len);
82 value_multiply(c->p[i][len], c->p[i][len], c->p[i-j][len]);
84 value_multiply(c->p[i][len], c->p[i][len], d.coeff->p[0]);
86 if (sign == -1) {
87 value_set_si(tmp, -1);
88 Vector_Scale(c->p[len-1], count->p, tmp, len);
89 value_assign(count->p[len], c->p[len-1][len]);
90 } else
91 Vector_Copy(c->p[len-1], count->p, len+1);
92 Vector_Normalize(count->p, len+1);
93 value_clear(tmp);
94 Matrix_Free(c);
98 struct bfe_term : public bfc_term_base {
99 vector<evalue *> factors;
101 bfe_term(int len) : bfc_term_base(len) {
104 ~bfe_term() {
105 for (int i = 0; i < factors.size(); ++i) {
106 if (!factors[i])
107 continue;
108 free_evalue_refs(factors[i]);
109 delete factors[i];
114 static void print_int_vector(int *v, int len, const char *name)
116 cerr << name << endl;
117 for (int j = 0; j < len; ++j) {
118 cerr << v[j] << " ";
120 cerr << endl;
123 static void print_bfc_terms(mat_ZZ& factors, bfc_vec& v)
125 cerr << endl;
126 cerr << "factors" << endl;
127 cerr << factors << endl;
128 for (int i = 0; i < v.size(); ++i) {
129 cerr << "term: " << i << endl;
130 print_int_vector(v[i]->powers, factors.NumRows(), "powers");
131 cerr << "terms" << endl;
132 cerr << v[i]->terms << endl;
133 bfc_term* bfct = static_cast<bfc_term *>(v[i]);
134 cerr << bfct->c << endl;
138 static void print_bfe_terms(mat_ZZ& factors, bfc_vec& v)
140 cerr << endl;
141 cerr << "factors" << endl;
142 cerr << factors << endl;
143 for (int i = 0; i < v.size(); ++i) {
144 cerr << "term: " << i << endl;
145 print_int_vector(v[i]->powers, factors.NumRows(), "powers");
146 cerr << "terms" << endl;
147 cerr << v[i]->terms << endl;
148 bfe_term* bfet = static_cast<bfe_term *>(v[i]);
149 for (int j = 0; j < v[i]->terms.NumRows(); ++j) {
150 const char * test[] = {"a", "b"};
151 print_evalue(stderr, bfet->factors[j], test);
152 fprintf(stderr, "\n");
157 struct bfcounter : public bfcounter_base {
158 mpq_t count;
159 Value tz;
161 bfcounter(unsigned dim) : bfcounter_base(dim) {
162 mpq_init(count);
163 lower = 1;
164 value_init(tz);
166 ~bfcounter() {
167 mpq_clear(count);
168 value_clear(tz);
170 virtual void base(mat_ZZ& factors, bfc_vec& v);
171 virtual void get_count(Value *result) {
172 assert(value_one_p(&count[0]._mp_den));
173 value_assign(*result, &count[0]._mp_num);
177 void bfcounter::base(mat_ZZ& factors, bfc_vec& v)
179 unsigned nf = factors.NumRows();
181 for (int i = 0; i < v.size(); ++i) {
182 bfc_term* bfct = static_cast<bfc_term *>(v[i]);
183 int total_power = 0;
184 // factor is always positive, so we always
185 // change signs
186 for (int k = 0; k < nf; ++k)
187 total_power += v[i]->powers[k];
189 int j;
190 for (j = 0; j < nf; ++j)
191 if (v[i]->powers[j] > 0)
192 break;
194 zz2value(factors[j][0], tz);
195 dpoly D(total_power, tz, 1);
196 for (int k = 1; k < v[i]->powers[j]; ++k) {
197 zz2value(factors[j][0], tz);
198 dpoly fact(total_power, tz, 1);
199 D *= fact;
201 for ( ; ++j < nf; )
202 for (int k = 0; k < v[i]->powers[j]; ++k) {
203 zz2value(factors[j][0], tz);
204 dpoly fact(total_power, tz, 1);
205 D *= fact;
208 for (int k = 0; k < v[i]->terms.NumRows(); ++k) {
209 zz2value(v[i]->terms[k][0], tz);
210 dpoly n(total_power, tz);
211 mpq_set_si(tcount, 0, 1);
212 n.div(D, tcount, 1);
213 if (total_power % 2)
214 bfct->c[k].n = -bfct->c[k].n;
215 zz2value(bfct->c[k].n, tn);
216 zz2value(bfct->c[k].d, td);
218 mpz_mul(mpq_numref(tcount), mpq_numref(tcount), tn);
219 mpz_mul(mpq_denref(tcount), mpq_denref(tcount), td);
220 mpq_canonicalize(tcount);
221 mpq_add(count, count, tcount);
223 delete v[i];
228 /* Check whether the polyhedron is unbounded and if so,
229 * check whether it has any (and therefore an infinite number of)
230 * integer points.
231 * If one of the vertices is integer, then we are done.
232 * Otherwise, transform the polyhedron such that one of the rays
233 * is the first unit vector and cut it off at a height that ensures
234 * that if the whole polyhedron has any points, then the remaining part
235 * has integer points. In particular we add the largest coefficient
236 * of a ray to the highest vertex (rounded up).
238 static bool Polyhedron_is_infinite(Polyhedron *P, Value* result,
239 barvinok_options *options)
241 int r = 0;
242 Matrix *M, *M2;
243 Value c, tmp;
244 Value g;
245 bool first;
246 Vector *v;
247 Value offset, size;
248 Polyhedron *R;
250 if (P->NbBid == 0)
251 for (; r < P->NbRays; ++r)
252 if (value_zero_p(P->Ray[r][P->Dimension+1]))
253 break;
254 if (P->NbBid == 0 && r == P->NbRays)
255 return false;
257 if (options->count_sample_infinite) {
258 Vector *sample;
260 sample = Polyhedron_Sample(P, options);
261 if (!sample)
262 value_set_si(*result, 0);
263 else {
264 value_set_si(*result, -1);
265 Vector_Free(sample);
267 return true;
270 for (int i = 0; i < P->NbRays; ++i)
271 if (value_one_p(P->Ray[i][1+P->Dimension])) {
272 value_set_si(*result, -1);
273 return true;
276 value_init(g);
277 M = Matrix_Alloc(P->Dimension+1, P->Dimension+1);
278 Vector_Gcd(P->Ray[r]+1, P->Dimension, &g);
279 Vector_AntiScale(P->Ray[r]+1, M->p[0], g, P->Dimension+1);
280 int ok = unimodular_complete(M, 1);
281 assert(ok);
282 value_set_si(M->p[P->Dimension][P->Dimension], 1);
283 M2 = Transpose(M);
284 Matrix_Free(M);
285 P = Polyhedron_Preimage(P, M2, 0);
286 Matrix_Free(M2);
287 value_clear(g);
289 first = true;
290 value_init(offset);
291 value_init(size);
292 value_init(tmp);
293 value_set_si(size, 0);
295 for (int i = 0; i < P->NbBid; ++i) {
296 value_absolute(tmp, P->Ray[i][1]);
297 if (value_gt(tmp, size))
298 value_assign(size, tmp);
300 for (int i = P->NbBid; i < P->NbRays; ++i) {
301 if (value_zero_p(P->Ray[i][P->Dimension+1])) {
302 if (value_gt(P->Ray[i][1], size))
303 value_assign(size, P->Ray[i][1]);
304 continue;
306 mpz_cdiv_q(tmp, P->Ray[i][1], P->Ray[i][P->Dimension+1]);
307 if (first || value_gt(tmp, offset)) {
308 value_assign(offset, tmp);
309 first = false;
312 value_addto(offset, offset, size);
313 value_clear(size);
314 value_clear(tmp);
316 v = Vector_Alloc(P->Dimension+2);
317 value_set_si(v->p[0], 1);
318 value_set_si(v->p[1], -1);
319 value_assign(v->p[1+P->Dimension], offset);
320 R = AddConstraints(v->p, 1, P, options->MaxRays);
321 Polyhedron_Free(P);
322 P = R;
324 value_clear(offset);
325 Vector_Free(v);
327 value_init(c);
328 barvinok_count_with_options(P, &c, options);
329 Polyhedron_Free(P);
330 if (value_zero_p(c))
331 value_set_si(*result, 0);
332 else
333 value_set_si(*result, -1);
334 value_clear(c);
336 return true;
339 static void evalue2value(evalue *e, Value *v)
341 if (EVALUE_IS_ZERO(*e)) {
342 value_set_si(*v, 0);
343 return;
346 if (value_notzero_p(e->d)) {
347 assert(value_one_p(e->d));
348 value_assign(*v, e->x.n);
349 return;
352 assert(e->x.p->type == partition);
353 assert(e->x.p->size == 2);
354 assert(EVALUE_DOMAIN(e->x.p->arr[0])->Dimension == 0);
355 evalue2value(&e->x.p->arr[1], v);
358 static void barvinok_count_f(Polyhedron *P, Value* result,
359 barvinok_options *options);
361 void barvinok_count_with_options(Polyhedron *P, Value* result,
362 struct barvinok_options *options)
364 int allocated = 0;
365 Polyhedron *Q;
366 bool infinite = false;
368 if (P->next)
369 fprintf(stderr,
370 "barvinok_count: input is a union; only first polyhedron is counted\n");
372 if (emptyQ2(P)) {
373 value_set_si(*result, 0);
374 return;
376 if (P->NbEq != 0) {
377 Q = NULL;
378 do {
379 P = remove_equalities(P, options->MaxRays);
380 if (P)
381 P = DomainConstraintSimplify(P, options->MaxRays);
382 if (Q)
383 Polyhedron_Free(Q);
384 Q = P;
385 } while (P && !emptyQ(P) && P->NbEq != 0);
386 if (!P || emptyQ(P)) {
387 Polyhedron_Free(P);
388 value_set_si(*result, 0);
389 return;
391 allocated = 1;
393 if (Polyhedron_is_infinite(P, result, options)) {
394 if (allocated)
395 Polyhedron_Free(P);
396 return;
398 if (P->Dimension == 0) {
399 /* Test whether the constraints are satisfied */
400 POL_ENSURE_VERTICES(P);
401 value_set_si(*result, !emptyQ(P));
402 if (allocated)
403 Polyhedron_Free(P);
404 return;
406 if (options->summation == BV_SUM_BERNOULLI) {
407 Polyhedron *C = Universe_Polyhedron(0);
408 evalue *sum = barvinok_summate_unweighted(P, C, options);
409 Polyhedron_Free(C);
410 evalue2value(sum, result);
411 evalue_free(sum);
412 return;
414 Q = Polyhedron_Factor(P, 0, NULL, options->MaxRays);
415 if (Q) {
416 if (allocated)
417 Polyhedron_Free(P);
418 P = Q;
419 allocated = 1;
422 barvinok_count_f(P, result, options);
423 if (value_neg_p(*result))
424 infinite = true;
425 if (Q && P->next && value_notzero_p(*result)) {
426 Value factor;
427 value_init(factor);
429 for (Q = P->next; Q; Q = Q->next) {
430 barvinok_count_f(Q, &factor, options);
431 if (value_neg_p(factor)) {
432 infinite = true;
433 continue;
434 } else if (Q->next && value_zero_p(factor)) {
435 value_set_si(*result, 0);
436 break;
438 value_multiply(*result, *result, factor);
441 value_clear(factor);
444 if (allocated)
445 Domain_Free(P);
446 if (infinite)
447 value_set_si(*result, -1);
450 void barvinok_count(Polyhedron *P, Value* result, unsigned NbMaxCons)
452 barvinok_options *options = barvinok_options_new_with_defaults();
453 options->MaxRays = NbMaxCons;
454 barvinok_count_with_options(P, result, options);
455 barvinok_options_free(options);
458 static void barvinok_count_f(Polyhedron *P, Value* result,
459 barvinok_options *options)
461 if (emptyQ2(P)) {
462 value_set_si(*result, 0);
463 return;
466 if (P->Dimension == 1)
467 return Line_Length(P, result);
469 int c = P->NbConstraints;
470 POL_ENSURE_FACETS(P);
471 if (c != P->NbConstraints || P->NbEq != 0) {
472 Polyhedron *next = P->next;
473 P->next = NULL;
474 barvinok_count_with_options(P, result, options);
475 P->next = next;
476 return;
479 POL_ENSURE_VERTICES(P);
481 if (Polyhedron_is_infinite(P, result, options))
482 return;
484 np_base *cnt;
485 if (options->incremental_specialization == BV_SPECIALIZATION_BF)
486 cnt = new bfcounter(P->Dimension);
487 else if (options->incremental_specialization == BV_SPECIALIZATION_DF)
488 cnt = new icounter(P->Dimension);
489 else if (options->incremental_specialization == BV_SPECIALIZATION_TODD)
490 cnt = new tcounter(P->Dimension, options->max_index);
491 else
492 cnt = new counter(P->Dimension, options->max_index);
493 cnt->start(P, options);
495 cnt->get_count(result);
496 delete cnt;
499 typedef evalue * evalue_p;
501 struct enumerator_base {
502 unsigned dim;
503 evalue ** vE;
504 evalue mone;
505 vertex_decomposer *vpd;
507 enumerator_base(unsigned dim, vertex_decomposer *vpd)
509 this->dim = dim;
510 this->vpd = vpd;
512 vE = new evalue_p[vpd->PP->nbV];
513 for (int j = 0; j < vpd->PP->nbV; ++j)
514 vE[j] = 0;
516 value_init(mone.d);
517 evalue_set_si(&mone, -1, 1);
520 void decompose_at(Param_Vertices *V, int _i, barvinok_options *options) {
521 //this->pVD = pVD;
523 vE[_i] = new evalue;
524 value_init(vE[_i]->d);
525 evalue_set_si(vE[_i], 0, 1);
527 vpd->decompose_at_vertex(V, _i, options);
530 virtual ~enumerator_base() {
531 for (int j = 0; j < vpd->PP->nbV; ++j)
532 if (vE[j]) {
533 free_evalue_refs(vE[j]);
534 delete vE[j];
536 delete [] vE;
538 free_evalue_refs(&mone);
541 static enumerator_base *create(Polyhedron *P, unsigned dim,
542 Param_Polyhedron *PP,
543 barvinok_options *options);
546 struct enumerator : public signed_cone_consumer, public vertex_decomposer,
547 public enumerator_base {
548 vec_ZZ lambda;
549 vec_ZZ den;
550 term_info num;
551 Vector *c;
552 mpq_t count;
553 Value tz;
555 enumerator(Polyhedron *P, unsigned dim, Param_Polyhedron *PP) :
556 vertex_decomposer(PP, *this), enumerator_base(dim, this) {
557 randomvector(P, lambda, dim, 0);
558 den.SetLength(dim);
559 c = Vector_Alloc(dim+2);
561 mpq_init(count);
562 value_init(tz);
565 ~enumerator() {
566 mpq_clear(count);
567 Vector_Free(c);
568 value_clear(tz);
571 virtual void handle(const signed_cone& sc, barvinok_options *options);
574 void enumerator::handle(const signed_cone& sc, barvinok_options *options)
576 int sign = sc.sign;
577 assert(sc.rays.NumRows() == dim);
578 for (int k = 0; k < dim; ++k) {
579 if (lambda * sc.rays[k] == 0)
580 throw Orthogonal;
583 lattice_point(V, sc.rays, lambda, &num, sc.det, options);
584 den = sc.rays * lambda;
586 if (dim % 2)
587 sign = -sign;
589 zz2value(den[0], tz);
590 dpoly n(dim, tz, 1);
591 for (int k = 1; k < dim; ++k) {
592 zz2value(den[k], tz);
593 dpoly fact(dim, tz, 1);
594 n *= fact;
596 if (num.E != NULL) {
597 dpoly_n d(dim);
598 d.div(n, c, sign);
599 for (unsigned long i = 0; i < sc.det; ++i) {
600 evalue *EV = evalue_polynomial(c, num.E[i]);
601 eadd(EV, vE[vert]);
602 evalue_free(EV);
603 evalue_free(num.E[i]);
605 delete [] num.E;
606 } else {
607 mpq_set_si(count, 0, 1);
608 if (num.constant.length() == 1) {
609 zz2value(num.constant[0], tz);
610 dpoly d(dim, tz);
611 d.div(n, count, sign);
612 } else {
613 dpoly_n d(dim);
614 d.div(n, c, sign);
615 Value x, acc;
616 value_init(x);
617 value_init(acc);
618 for (unsigned long i = 0; i < sc.det; ++i) {
619 value_assign(acc, c->p[dim]);
620 zz2value(num.constant[i], x);
621 for (int j = dim-1; j >= 0; --j) {
622 value_multiply(acc, acc, x);
623 value_addto(acc, acc, c->p[j]);
625 value_addto(mpq_numref(count), mpq_numref(count), acc);
627 mpz_set(mpq_denref(count), c->p[dim+1]);
628 value_clear(acc);
629 value_clear(x);
631 evalue EV;
632 value_init(EV.d);
633 evalue_set(&EV, &count[0]._mp_num, &count[0]._mp_den);
634 eadd(&EV, vE[vert]);
635 free_evalue_refs(&EV);
639 struct ienumerator_base : enumerator_base {
640 evalue ** E_vertex;
642 ienumerator_base(unsigned dim, vertex_decomposer *vpd) :
643 enumerator_base(dim,vpd) {
644 E_vertex = new evalue_p[dim];
647 virtual ~ienumerator_base() {
648 delete [] E_vertex;
651 evalue *E_num(int i, int d) {
652 return E_vertex[i + (dim-d)];
656 struct cumulator {
657 evalue *factor;
658 evalue *v;
659 dpoly_r *r;
661 cumulator(evalue *factor, evalue *v, dpoly_r *r) :
662 factor(factor), v(v), r(r) {}
664 void cumulate(barvinok_options *options);
666 virtual void add_term(const vector<int>& powers, evalue *f2) = 0;
667 virtual ~cumulator() {}
670 void cumulator::cumulate(barvinok_options *options)
672 evalue cum; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
673 evalue f;
674 evalue t; // E_num[0] - (m-1)
675 evalue *cst;
676 evalue mone;
678 if (options->lookup_table) {
679 value_init(mone.d);
680 evalue_set_si(&mone, -1, 1);
683 value_init(cum.d);
684 evalue_copy(&cum, factor);
685 value_init(f.d);
686 value_init(f.x.n);
687 value_set_si(f.d, 1);
688 value_set_si(f.x.n, 1);
689 value_init(t.d);
690 evalue_copy(&t, v);
692 if (!options->lookup_table) {
693 for (cst = &t; value_zero_p(cst->d); ) {
694 if (cst->x.p->type == fractional)
695 cst = &cst->x.p->arr[1];
696 else
697 cst = &cst->x.p->arr[0];
701 for (int m = 0; m < r->len; ++m) {
702 if (m > 0) {
703 if (m > 1) {
704 value_set_si(f.d, m);
705 emul(&f, &cum);
706 if (!options->lookup_table)
707 value_subtract(cst->x.n, cst->x.n, cst->d);
708 else
709 eadd(&mone, &t);
711 emul(&t, &cum);
713 dpoly_r_term_list& current = r->c[r->len-1-m];
714 dpoly_r_term_list::iterator j;
715 for (j = current.begin(); j != current.end(); ++j) {
716 if ((*j)->coeff == 0)
717 continue;
718 evalue *f2 = new evalue;
719 value_init(f2->d);
720 value_init(f2->x.n);
721 zz2value((*j)->coeff, f2->x.n);
722 zz2value(r->denom, f2->d);
723 emul(&cum, f2);
725 add_term((*j)->powers, f2);
728 free_evalue_refs(&f);
729 free_evalue_refs(&t);
730 free_evalue_refs(&cum);
731 if (options->lookup_table)
732 free_evalue_refs(&mone);
735 struct E_poly_term {
736 vector<int> powers;
737 evalue *E;
740 struct ie_cum : public cumulator {
741 vector<E_poly_term *> terms;
743 ie_cum(evalue *factor, evalue *v, dpoly_r *r) : cumulator(factor, v, r) {}
745 virtual void add_term(const vector<int>& powers, evalue *f2);
748 void ie_cum::add_term(const vector<int>& powers, evalue *f2)
750 int k;
751 for (k = 0; k < terms.size(); ++k) {
752 if (terms[k]->powers == powers) {
753 eadd(f2, terms[k]->E);
754 free_evalue_refs(f2);
755 delete f2;
756 break;
759 if (k >= terms.size()) {
760 E_poly_term *ET = new E_poly_term;
761 ET->powers = powers;
762 ET->E = f2;
763 terms.push_back(ET);
767 struct ienumerator : public signed_cone_consumer, public vertex_decomposer,
768 public ienumerator_base {
769 mat_ZZ den;
770 mat_ZZ vertex;
771 mpq_t tcount;
772 Value tz;
774 ienumerator(unsigned dim, Param_Polyhedron *PP) :
775 vertex_decomposer(PP, *this), ienumerator_base(dim, this) {
776 vertex.SetDims(1, dim);
778 den.SetDims(dim, dim);
779 mpq_init(tcount);
780 value_init(tz);
783 ~ienumerator() {
784 mpq_clear(tcount);
785 value_clear(tz);
788 virtual void handle(const signed_cone& sc, barvinok_options *options);
789 void reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
790 barvinok_options *options);
793 void ienumerator::reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
794 barvinok_options *options)
796 unsigned len = den_f.NumRows(); // number of factors in den
797 unsigned dim = num.NumCols();
798 assert(num.NumRows() == 1);
800 if (dim == 0) {
801 eadd(factor, vE[vert]);
802 return;
805 vec_ZZ den_s;
806 mat_ZZ den_r;
807 vec_ZZ num_s;
808 mat_ZZ num_p;
810 split_one(num, num_s, num_p, den_f, den_s, den_r);
812 vec_ZZ den_p;
813 den_p.SetLength(len);
815 ZZ one;
816 one = 1;
817 normalize(one, num_s, num_p, den_s, den_p, den_r);
818 if (one != 1)
819 emul(&mone, factor);
821 int only_param = 0;
822 int no_param = 0;
823 for (int k = 0; k < len; ++k) {
824 if (den_p[k] == 0)
825 ++no_param;
826 else if (den_s[k] == 0)
827 ++only_param;
829 if (no_param == 0) {
830 reduce(factor, num_p, den_r, options);
831 } else {
832 int k, l;
833 mat_ZZ pden;
834 pden.SetDims(only_param, dim-1);
836 for (k = 0, l = 0; k < len; ++k)
837 if (den_s[k] == 0)
838 pden[l++] = den_r[k];
840 for (k = 0; k < len; ++k)
841 if (den_p[k] == 0)
842 break;
844 zz2value(num_s[0], tz);
845 dpoly n(no_param, tz);
846 zz2value(den_s[k], tz);
847 dpoly D(no_param, tz, 1);
848 for ( ; ++k < len; )
849 if (den_p[k] == 0) {
850 zz2value(den_s[k], tz);
851 dpoly fact(no_param, tz, 1);
852 D *= fact;
855 dpoly_r * r = 0;
856 // if no_param + only_param == len then all powers
857 // below will be all zero
858 if (no_param + only_param == len) {
859 if (E_num(0, dim) != 0)
860 r = new dpoly_r(n, len);
861 else {
862 mpq_set_si(tcount, 0, 1);
863 one = 1;
864 n.div(D, tcount, 1);
866 if (value_notzero_p(mpq_numref(tcount))) {
867 evalue f;
868 value_init(f.d);
869 value_init(f.x.n);
870 value_assign(f.x.n, mpq_numref(tcount));
871 value_assign(f.d, mpq_denref(tcount));
872 emul(&f, factor);
873 reduce(factor, num_p, pden, options);
874 free_evalue_refs(&f);
876 return;
878 } else {
879 for (k = 0; k < len; ++k) {
880 if (den_s[k] == 0 || den_p[k] == 0)
881 continue;
883 zz2value(den_s[k], tz);
884 dpoly pd(no_param-1, tz, 1);
886 int l;
887 for (l = 0; l < k; ++l)
888 if (den_r[l] == den_r[k])
889 break;
891 if (r == 0)
892 r = new dpoly_r(n, pd, l, len);
893 else {
894 dpoly_r *nr = new dpoly_r(r, pd, l, len);
895 delete r;
896 r = nr;
900 dpoly_r *rc = r->div(D);
901 delete r;
902 r = rc;
903 if (E_num(0, dim) == 0) {
904 int common = pden.NumRows();
905 dpoly_r_term_list& final = r->c[r->len-1];
906 int rows;
907 evalue t;
908 evalue f;
909 value_init(f.d);
910 value_init(f.x.n);
911 zz2value(r->denom, f.d);
912 dpoly_r_term_list::iterator j;
913 for (j = final.begin(); j != final.end(); ++j) {
914 if ((*j)->coeff == 0)
915 continue;
916 rows = common;
917 for (int k = 0; k < r->dim; ++k) {
918 int n = (*j)->powers[k];
919 if (n == 0)
920 continue;
921 pden.SetDims(rows+n, pden.NumCols());
922 for (int l = 0; l < n; ++l)
923 pden[rows+l] = den_r[k];
924 rows += n;
926 value_init(t.d);
927 evalue_copy(&t, factor);
928 zz2value((*j)->coeff, f.x.n);
929 emul(&f, &t);
930 reduce(&t, num_p, pden, options);
931 free_evalue_refs(&t);
933 free_evalue_refs(&f);
934 } else {
935 ie_cum cum(factor, E_num(0, dim), r);
936 cum.cumulate(options);
938 int common = pden.NumRows();
939 int rows;
940 for (int j = 0; j < cum.terms.size(); ++j) {
941 rows = common;
942 pden.SetDims(rows, pden.NumCols());
943 for (int k = 0; k < r->dim; ++k) {
944 int n = cum.terms[j]->powers[k];
945 if (n == 0)
946 continue;
947 pden.SetDims(rows+n, pden.NumCols());
948 for (int l = 0; l < n; ++l)
949 pden[rows+l] = den_r[k];
950 rows += n;
952 reduce(cum.terms[j]->E, num_p, pden, options);
953 free_evalue_refs(cum.terms[j]->E);
954 delete cum.terms[j]->E;
955 delete cum.terms[j];
958 delete r;
962 void ienumerator::handle(const signed_cone& sc, barvinok_options *options)
964 assert(sc.det == 1);
965 assert(sc.rays.NumRows() == dim);
967 lattice_point(V, sc.rays, vertex[0], E_vertex, options);
969 den = sc.rays;
971 evalue one;
972 value_init(one.d);
973 evalue_set_si(&one, sc.sign, 1);
974 reduce(&one, vertex, den, options);
975 free_evalue_refs(&one);
977 for (int i = 0; i < dim; ++i)
978 if (E_vertex[i])
979 evalue_free(E_vertex[i]);
982 struct bfenumerator : public vertex_decomposer, public bf_base,
983 public ienumerator_base {
984 evalue *factor;
986 bfenumerator(unsigned dim, Param_Polyhedron *PP) :
987 vertex_decomposer(PP, *this),
988 bf_base(dim), ienumerator_base(dim, this) {
989 lower = 0;
990 factor = NULL;
993 ~bfenumerator() {
996 virtual void handle(const signed_cone& sc, barvinok_options *options);
997 virtual void base(mat_ZZ& factors, bfc_vec& v);
999 bfc_term_base* new_bf_term(int len) {
1000 bfe_term* t = new bfe_term(len);
1001 return t;
1004 virtual void set_factor(bfc_term_base *t, int k, int change) {
1005 bfe_term* bfet = static_cast<bfe_term *>(t);
1006 factor = bfet->factors[k];
1007 assert(factor != NULL);
1008 bfet->factors[k] = NULL;
1009 if (change)
1010 emul(&mone, factor);
1013 virtual void set_factor(bfc_term_base *t, int k, mpq_t &q, int change) {
1014 bfe_term* bfet = static_cast<bfe_term *>(t);
1015 factor = bfet->factors[k];
1016 assert(factor != NULL);
1017 bfet->factors[k] = NULL;
1019 evalue f;
1020 value_init(f.d);
1021 value_init(f.x.n);
1022 if (change)
1023 value_oppose(f.x.n, mpq_numref(q));
1024 else
1025 value_assign(f.x.n, mpq_numref(q));
1026 value_assign(f.d, mpq_denref(q));
1027 emul(&f, factor);
1028 free_evalue_refs(&f);
1031 virtual void set_factor(bfc_term_base *t, int k, const QQ& c, int change) {
1032 bfe_term* bfet = static_cast<bfe_term *>(t);
1034 factor = new evalue;
1036 evalue f;
1037 value_init(f.d);
1038 value_init(f.x.n);
1039 zz2value(c.n, f.x.n);
1040 if (change)
1041 value_oppose(f.x.n, f.x.n);
1042 zz2value(c.d, f.d);
1044 value_init(factor->d);
1045 evalue_copy(factor, bfet->factors[k]);
1046 emul(&f, factor);
1047 free_evalue_refs(&f);
1050 void set_factor(evalue *f, int change) {
1051 if (change)
1052 emul(&mone, f);
1053 factor = f;
1056 virtual void insert_term(bfc_term_base *t, int i) {
1057 bfe_term* bfet = static_cast<bfe_term *>(t);
1058 int len = t->terms.NumRows()-1; // already increased by one
1060 bfet->factors.resize(len+1);
1061 for (int j = len; j > i; --j) {
1062 bfet->factors[j] = bfet->factors[j-1];
1063 t->terms[j] = t->terms[j-1];
1065 bfet->factors[i] = factor;
1066 factor = NULL;
1069 virtual void update_term(bfc_term_base *t, int i) {
1070 bfe_term* bfet = static_cast<bfe_term *>(t);
1072 eadd(factor, bfet->factors[i]);
1073 free_evalue_refs(factor);
1074 delete factor;
1077 virtual bool constant_vertex(int dim) { return E_num(0, dim) == 0; }
1079 virtual void cum(bf_reducer *bfr, bfc_term_base *t, int k, dpoly_r *r,
1080 barvinok_options *options);
1083 enumerator_base *enumerator_base::create(Polyhedron *P, unsigned dim,
1084 Param_Polyhedron *PP,
1085 barvinok_options *options)
1087 enumerator_base *eb;
1089 if (options->incremental_specialization == BV_SPECIALIZATION_BF)
1090 eb = new bfenumerator(dim, PP);
1091 else if (options->incremental_specialization == BV_SPECIALIZATION_DF)
1092 eb = new ienumerator(dim, PP);
1093 else
1094 eb = new enumerator(P, dim, PP);
1096 return eb;
1099 struct bfe_cum : public cumulator {
1100 bfenumerator *bfe;
1101 bfc_term_base *told;
1102 int k;
1103 bf_reducer *bfr;
1105 bfe_cum(evalue *factor, evalue *v, dpoly_r *r, bf_reducer *bfr,
1106 bfc_term_base *t, int k, bfenumerator *e) :
1107 cumulator(factor, v, r), bfe(e), told(t), k(k),
1108 bfr(bfr) {
1111 virtual void add_term(const vector<int>& powers, evalue *f2);
1114 void bfe_cum::add_term(const vector<int>& powers, evalue *f2)
1116 bfr->update_powers(powers);
1118 bfc_term_base * t = bfe->find_bfc_term(bfr->vn, bfr->npowers, bfr->nnf);
1119 bfe->set_factor(f2, bfr->l_changes % 2);
1120 bfe->add_term(t, told->terms[k], bfr->l_extra_num);
1123 void bfenumerator::cum(bf_reducer *bfr, bfc_term_base *t, int k,
1124 dpoly_r *r, barvinok_options *options)
1126 bfe_term* bfet = static_cast<bfe_term *>(t);
1127 bfe_cum cum(bfet->factors[k], E_num(0, bfr->d), r, bfr, t, k, this);
1128 cum.cumulate(options);
1131 void bfenumerator::base(mat_ZZ& factors, bfc_vec& v)
1133 for (int i = 0; i < v.size(); ++i) {
1134 assert(v[i]->terms.NumRows() == 1);
1135 evalue *factor = static_cast<bfe_term *>(v[i])->factors[0];
1136 eadd(factor, vE[vert]);
1137 delete v[i];
1141 void bfenumerator::handle(const signed_cone& sc, barvinok_options *options)
1143 assert(sc.det == 1);
1144 assert(sc.rays.NumRows() == enumerator_base::dim);
1146 bfe_term* t = new bfe_term(enumerator_base::dim);
1147 vector< bfc_term_base * > v;
1148 v.push_back(t);
1150 t->factors.resize(1);
1152 t->terms.SetDims(1, enumerator_base::dim);
1153 lattice_point(V, sc.rays, t->terms[0], E_vertex, options);
1155 // the elements of factors are always lexpositive
1156 mat_ZZ factors;
1157 int s = setup_factors(sc.rays, factors, t, sc.sign);
1159 t->factors[0] = new evalue;
1160 value_init(t->factors[0]->d);
1161 evalue_set_si(t->factors[0], s, 1);
1162 reduce(factors, v, options);
1164 for (int i = 0; i < enumerator_base::dim; ++i)
1165 if (E_vertex[i])
1166 evalue_free(E_vertex[i]);
1169 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1170 barvinok_options *options);
1172 /* Destroys C */
1173 static evalue* barvinok_enumerate_cst(Polyhedron *P, Polyhedron* C,
1174 struct barvinok_options *options)
1176 evalue *eres;
1178 if (emptyQ2(C)) {
1179 Polyhedron_Free(C);
1180 return evalue_zero();
1183 ALLOC(evalue, eres);
1184 value_init(eres->d);
1185 value_set_si(eres->d, 0);
1186 eres->x.p = new_enode(partition, 2, C->Dimension);
1187 EVALUE_SET_DOMAIN(eres->x.p->arr[0],
1188 DomainConstraintSimplify(C, options->MaxRays));
1189 value_set_si(eres->x.p->arr[1].d, 1);
1190 value_init(eres->x.p->arr[1].x.n);
1191 if (emptyQ2(P))
1192 value_set_si(eres->x.p->arr[1].x.n, 0);
1193 else
1194 barvinok_count_with_options(P, &eres->x.p->arr[1].x.n, options);
1195 if (value_mone_p(eres->x.p->arr[1].x.n)) {
1196 value_clear(eres->x.p->arr[1].x.n);
1197 value_set_si(eres->x.p->arr[1].d, -2); /* NaN */
1200 return eres;
1203 static evalue* enumerate(Polyhedron *P, Polyhedron* C,
1204 struct barvinok_options *options)
1206 Polyhedron *next;
1207 Polyhedron *Porig = P;
1208 Polyhedron *Corig = C;
1209 Polyhedron *CEq = NULL;
1210 unsigned nparam = C->Dimension;
1211 evalue *eres;
1212 Matrix *CP = NULL;
1214 evalue factor;
1215 value_init(factor.d);
1216 evalue_set_si(&factor, 1, 1);
1218 /* for now */
1219 POL_ENSURE_FACETS(P);
1220 POL_ENSURE_VERTICES(P);
1221 POL_ENSURE_FACETS(C);
1222 POL_ENSURE_VERTICES(C);
1224 if (C->Dimension == 0 || emptyQ(P) || emptyQ(C)) {
1225 constant:
1226 if (CEq == Porig)
1227 CEq = Polyhedron_Copy(CEq);
1228 eres = barvinok_enumerate_cst(P, CEq ? CEq : Polyhedron_Copy(C), options);
1229 out:
1230 if (CP) {
1231 evalue_backsubstitute(eres, CP, options->MaxRays);
1232 Matrix_Free(CP);
1235 emul(&factor, eres);
1236 if (options->approx->method == BV_APPROX_DROP) {
1237 if (options->approx->approximation == BV_APPROX_SIGN_UPPER)
1238 evalue_frac2polynomial(eres, 1, options->MaxRays);
1239 if (options->approx->approximation == BV_APPROX_SIGN_LOWER)
1240 evalue_frac2polynomial(eres, -1, options->MaxRays);
1241 if (options->approx->approximation == BV_APPROX_SIGN_APPROX)
1242 evalue_frac2polynomial(eres, 0, options->MaxRays);
1244 reduce_evalue(eres);
1245 free_evalue_refs(&factor);
1246 if (P != Porig)
1247 Domain_Free(P);
1248 if (C != Corig)
1249 Polyhedron_Free(C);
1251 return eres;
1253 if (Polyhedron_is_unbounded(P, nparam, options->MaxRays))
1254 goto constant;
1256 if (P->Dimension == nparam) {
1257 CEq = DomainIntersection(P, C, options->MaxRays);
1258 P = Universe_Polyhedron(0);
1259 goto constant;
1261 if (P->NbEq != 0 || C->NbEq != 0) {
1262 Polyhedron *Q = P;
1263 Polyhedron *D = C;
1264 remove_all_equalities(&P, &C, &CP, NULL, nparam, options->MaxRays);
1265 if (C != D && D != Corig)
1266 Polyhedron_Free(D);
1267 if (P != Q && Q != Porig)
1268 Domain_Free(Q);
1269 eres = enumerate(P, C, options);
1270 goto out;
1273 Polyhedron *T = Polyhedron_Factor(P, nparam, NULL, options->MaxRays);
1274 if (T || (P->Dimension == nparam+1)) {
1275 Polyhedron *C2 = C;
1276 Polyhedron *FC = Factor_Context(T ? T : P, nparam, options->MaxRays);
1277 C = DomainIntersection(C, FC, options->MaxRays);
1278 if (C2 != Corig)
1279 Polyhedron_Free(C2);
1280 Polyhedron_Free(FC);
1282 if (T) {
1283 if (P != Porig)
1284 Polyhedron_Free(P);
1285 P = T;
1286 if (T->Dimension == C->Dimension) {
1287 P = T->next;
1288 T->next = NULL;
1289 Polyhedron_Free(T);
1293 next = P->next;
1294 P->next = NULL;
1295 eres = barvinok_enumerate_ev_f(P, C, options);
1296 P->next = next;
1298 if (P->next) {
1299 Polyhedron *Q;
1300 evalue *f;
1302 for (Q = P->next; Q; Q = Q->next) {
1303 Polyhedron *next = Q->next;
1304 Q->next = NULL;
1306 f = barvinok_enumerate_ev_f(Q, C, options);
1307 emul(f, eres);
1308 evalue_free(f);
1310 Q->next = next;
1314 goto out;
1317 evalue* barvinok_enumerate_with_options(Polyhedron *P, Polyhedron* C,
1318 struct barvinok_options *options)
1320 Polyhedron *next, *Cnext, *C1;
1321 Polyhedron *Corig = C;
1322 evalue *eres;
1324 if (P->next)
1325 fprintf(stderr,
1326 "barvinok_enumerate: input is a union; only first polyhedron is enumerated\n");
1328 if (C->next)
1329 fprintf(stderr,
1330 "barvinok_enumerate: context is a union; only first polyhedron is considered\n");
1332 Cnext = C->next;
1333 C->next = NULL;
1334 C1 = Polyhedron_Project(P, C->Dimension);
1335 C = DomainIntersection(C, C1, options->MaxRays);
1336 Polyhedron_Free(C1);
1337 next = P->next;
1338 P->next = NULL;
1340 if (options->approx->method == BV_APPROX_BERNOULLI ||
1341 options->summation == BV_SUM_BERNOULLI) {
1342 int summation = options->summation;
1343 options->summation = BV_SUM_BERNOULLI;
1344 eres = barvinok_summate_unweighted(P, C, options);
1345 options->summation = summation;
1346 } else
1347 eres = enumerate(P, C, options);
1348 Domain_Free(C);
1350 P->next= next;
1351 Corig->next = Cnext;
1353 return eres;
1356 evalue* barvinok_enumerate_ev(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1358 evalue *E;
1359 barvinok_options *options = barvinok_options_new_with_defaults();
1360 options->MaxRays = MaxRays;
1361 E = barvinok_enumerate_with_options(P, C, options);
1362 barvinok_options_free(options);
1363 return E;
1366 evalue *Param_Polyhedron_Enumerate(Param_Polyhedron *PP, Polyhedron *P,
1367 Polyhedron *C,
1368 struct barvinok_options *options)
1370 evalue *eres;
1371 Param_Domain *D;
1372 unsigned nparam = C->Dimension;
1373 unsigned dim = P->Dimension - nparam;
1375 int nd;
1376 for (nd = 0, D=PP->D; D; ++nd, D=D->next);
1377 evalue_section *s = new evalue_section[nd];
1378 Polyhedron *TC = true_context(P, C, options->MaxRays);
1380 enumerator_base *et = NULL;
1381 try_again:
1382 if (et)
1383 delete et;
1385 et = enumerator_base::create(P, dim, PP, options);
1387 FORALL_REDUCED_DOMAIN(PP, TC, nd, options, i, D, rVD)
1388 Param_Vertices *V;
1390 s[i].E = evalue_zero();
1391 s[i].D = rVD;
1393 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1394 if (!et->vE[_i])
1395 try {
1396 et->decompose_at(V, _i, options);
1397 } catch (OrthogonalException &e) {
1398 FORALL_REDUCED_DOMAIN_RESET;
1399 for (; i >= 0; --i) {
1400 evalue_free(s[i].E);
1401 Domain_Free(s[i].D);
1403 goto try_again;
1405 eadd(et->vE[_i] , s[i].E);
1406 END_FORALL_PVertex_in_ParamPolyhedron;
1407 evalue_range_reduction_in_domain(s[i].E, rVD);
1408 END_FORALL_REDUCED_DOMAIN
1409 Polyhedron_Free(TC);
1411 delete et;
1412 eres = evalue_from_section_array(s, nd);
1413 delete [] s;
1415 return eres;
1418 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1419 barvinok_options *options)
1421 unsigned nparam = C->Dimension;
1422 bool do_scale = options->approx->method == BV_APPROX_SCALE;
1424 if (options->summation == BV_SUM_EULER)
1425 return barvinok_summate_unweighted(P, C, options);
1427 if (options->approx->method == BV_APPROX_VOLUME)
1428 return Param_Polyhedron_Volume(P, C, options);
1430 if (P->Dimension - nparam == 1 && !do_scale)
1431 return ParamLine_Length(P, C, options);
1433 Param_Polyhedron *PP = NULL;
1434 evalue *eres;
1436 if (do_scale) {
1437 eres = scale_bound(P, C, options);
1438 if (eres)
1439 return eres;
1442 PP = Polyhedron2Param_Polyhedron(P, C, options);
1444 if (do_scale)
1445 eres = scale(PP, P, C, options);
1446 else
1447 eres = Param_Polyhedron_Enumerate(PP, P, C, options);
1449 if (PP)
1450 Param_Polyhedron_Free(PP);
1452 return eres;
1455 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1457 evalue *EP = barvinok_enumerate_ev(P, C, MaxRays);
1459 return partition2enumeration(EP);
1462 evalue* barvinok_enumerate_union(Polyhedron *D, Polyhedron* C, unsigned MaxRays)
1464 evalue *EP;
1465 gen_fun *gf = barvinok_enumerate_union_series(D, C, MaxRays);
1466 EP = *gf;
1467 delete gf;
1468 return EP;
1471 static __isl_give isl_pw_qpolynomial *basic_set_card(
1472 __isl_take isl_basic_set *bset)
1474 isl_ctx *ctx;
1475 isl_space *dim;
1476 isl_pw_qpolynomial *pwqp;
1477 unsigned nparam = isl_basic_set_dim(bset, isl_dim_param);
1478 Polyhedron *U = Universe_Polyhedron(nparam);
1479 Polyhedron *P;
1480 evalue *E;
1481 barvinok_options *options;
1482 int options_allocated = 0;
1484 ctx = isl_basic_set_get_ctx(bset);
1485 options = isl_ctx_peek_barvinok_options(ctx);
1486 if (!options) {
1487 options = barvinok_options_new_with_defaults();
1488 options_allocated = 1;
1491 dim = isl_basic_set_get_space(bset);
1492 dim = isl_space_domain(dim);
1494 P = isl_basic_set_to_polylib(bset);
1495 E = enumerate(P, U, options);
1497 pwqp = isl_pw_qpolynomial_from_evalue(dim, E);
1498 isl_basic_set_free(bset);
1500 evalue_free(E);
1501 Polyhedron_Free(P);
1502 Polyhedron_Free(U);
1503 if (options_allocated)
1504 barvinok_options_free(options);
1506 return pwqp;
1509 static isl_stat basic_map_card(__isl_take isl_basic_map *bmap, void *user)
1511 isl_pw_qpolynomial **sum = (isl_pw_qpolynomial **)user;
1512 isl_pw_qpolynomial *pwqp;
1513 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
1514 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
1515 isl_space *target_dim;
1516 isl_basic_set *bset;
1518 target_dim = isl_basic_map_get_space(bmap);
1519 target_dim = isl_space_domain(target_dim);
1521 bmap = isl_basic_map_move_dims(bmap, isl_dim_param, nparam,
1522 isl_dim_in, 0, n_in);
1524 bset = isl_basic_map_range(bmap);
1525 bset = isl_basic_set_lift(bset);
1526 pwqp = isl_basic_set_multiplicative_call(bset, &basic_set_card);
1528 pwqp = isl_pw_qpolynomial_move_dims(pwqp, isl_dim_in, 0,
1529 isl_dim_param, nparam, n_in);
1530 pwqp = isl_pw_qpolynomial_reset_domain_space(pwqp, target_dim);
1531 *sum = isl_pw_qpolynomial_add(*sum, pwqp);
1533 return isl_stat_ok;
1536 static __isl_give isl_pw_qpolynomial *card_as_sum(__isl_take isl_map *map,
1537 barvinok_options *options)
1539 isl_ctx *ctx;
1540 isl_val *one;
1541 isl_space *dim;
1542 isl_set *set;
1543 isl_qpolynomial *qp;
1544 isl_pw_qpolynomial *pwqp;
1545 int summation = options->summation;
1547 if (!map)
1548 return NULL;
1550 options->summation = BV_SUM_BERNOULLI;
1552 set = isl_map_wrap(map);
1553 dim = isl_set_get_space(set);
1554 ctx = isl_map_get_ctx(map);
1555 one = isl_val_one(ctx);
1556 qp = isl_qpolynomial_val_on_domain(dim, one);
1558 pwqp = isl_pw_qpolynomial_alloc(set, qp);
1559 pwqp = isl_pw_qpolynomial_sum(pwqp);
1561 options->summation = summation;
1563 return pwqp;
1566 __isl_give isl_pw_qpolynomial *isl_map_card(__isl_take isl_map *map)
1568 isl_ctx *ctx;
1569 isl_space *dim;
1570 isl_pw_qpolynomial *sum;
1571 barvinok_options *options;
1573 ctx = isl_map_get_ctx(map);
1574 options = isl_ctx_peek_barvinok_options(ctx);
1575 if (options &&
1576 (options->approx->method == BV_APPROX_BERNOULLI ||
1577 options->summation == BV_SUM_BERNOULLI))
1578 return card_as_sum(map, options);
1580 dim = isl_map_get_space(map);
1581 dim = isl_space_domain(dim);
1582 dim = isl_space_from_domain(dim);
1583 dim = isl_space_add_dims(dim, isl_dim_out, 1);
1584 sum = isl_pw_qpolynomial_zero(dim);
1586 map = isl_map_make_disjoint(map);
1587 map = isl_map_compute_divs(map);
1589 if (isl_map_foreach_basic_map(map, &basic_map_card, &sum) < 0)
1590 goto error;
1592 isl_map_free(map);
1594 return sum;
1595 error:
1596 isl_map_free(map);
1597 isl_pw_qpolynomial_free(sum);
1598 return NULL;
1601 __isl_give isl_pw_qpolynomial *isl_set_card(__isl_take isl_set *set)
1603 isl_pw_qpolynomial *pwqp;
1604 pwqp = isl_map_card(isl_map_from_range(set));
1605 pwqp = isl_pw_qpolynomial_project_domain_on_params(pwqp);
1606 return pwqp;
1609 __isl_give isl_pw_qpolynomial *isl_basic_map_card(__isl_take isl_basic_map *bmap)
1611 return isl_map_card(isl_map_from_basic_map(bmap));
1614 __isl_give isl_pw_qpolynomial *isl_basic_set_card(__isl_take isl_basic_set *bset)
1616 isl_pw_qpolynomial *pwqp;
1617 pwqp = isl_basic_map_card(isl_basic_map_from_range(bset));
1618 pwqp = isl_pw_qpolynomial_project_domain_on_params(pwqp);
1619 return pwqp;
1622 static isl_stat set_card(__isl_take isl_set *set, void *user)
1624 isl_union_pw_qpolynomial **res = (isl_union_pw_qpolynomial **)user;
1625 isl_pw_qpolynomial *pwqp;
1626 isl_union_pw_qpolynomial *upwqp;
1628 pwqp = isl_set_card(set);
1629 upwqp = isl_union_pw_qpolynomial_from_pw_qpolynomial(pwqp);
1630 *res = isl_union_pw_qpolynomial_add(*res, upwqp);
1632 return isl_stat_ok;
1635 __isl_give isl_union_pw_qpolynomial *isl_union_set_card(
1636 __isl_take isl_union_set *uset)
1638 isl_space *dim;
1639 isl_union_pw_qpolynomial *res;
1641 dim = isl_union_set_get_space(uset);
1642 res = isl_union_pw_qpolynomial_zero(dim);
1643 if (isl_union_set_foreach_set(uset, &set_card, &res) < 0)
1644 goto error;
1645 isl_union_set_free(uset);
1647 return res;
1648 error:
1649 isl_union_set_free(uset);
1650 isl_union_pw_qpolynomial_free(res);
1651 return NULL;
1654 static isl_stat map_card(__isl_take isl_map *map, void *user)
1656 isl_union_pw_qpolynomial **res = (isl_union_pw_qpolynomial **)user;
1657 isl_pw_qpolynomial *pwqp;
1658 isl_union_pw_qpolynomial *upwqp;
1660 pwqp = isl_map_card(map);
1661 upwqp = isl_union_pw_qpolynomial_from_pw_qpolynomial(pwqp);
1662 *res = isl_union_pw_qpolynomial_add(*res, upwqp);
1664 return isl_stat_ok;
1667 __isl_give isl_union_pw_qpolynomial *isl_union_map_card(
1668 __isl_take isl_union_map *umap)
1670 isl_space *dim;
1671 isl_union_pw_qpolynomial *res;
1673 dim = isl_union_map_get_space(umap);
1674 res = isl_union_pw_qpolynomial_zero(dim);
1675 if (isl_union_map_foreach_map(umap, &map_card, &res) < 0)
1676 goto error;
1677 isl_union_map_free(umap);
1679 return res;
1680 error:
1681 isl_union_map_free(umap);
1682 isl_union_pw_qpolynomial_free(res);
1683 return NULL;