doc: update primal Barvinok reference
[barvinok.git] / 4coins.cc
blob6180ef1b9dcb56245c3ab8ce0bdb479580eb2d5c
1 #include <iostream>
2 #include <barvinok/genfun.h>
3 #include <barvinok/util.h>
4 #include <barvinok/barvinok.h>
5 #include <barvinok/basis_reduction.h>
6 #include "config.h"
8 using std::cout;
9 using std::cerr;
10 using std::endl;
12 static Polyhedron *uncone(Polyhedron *C, unsigned MaxRays)
14 Matrix *Constraints;
15 Polyhedron *P;
16 int i;
18 Constraints = Matrix_Alloc(C->NbConstraints, 1+(C->Dimension-1)+1);
19 for (i = 0; i < C->NbConstraints; ++i) {
20 /* positivity constraints */
21 if (First_Non_Zero(C->Constraint[i]+1, C->Dimension) == -1) {
22 assert(i == C->NbConstraints-1);
23 Constraints->NbRows = i;
24 break;
26 assert(value_zero_p(C->Constraint[i][1+C->Dimension]));
27 Vector_Copy(C->Constraint[i], Constraints->p[i], 1+C->Dimension);
29 P = Constraints2Polyhedron(Constraints, MaxRays);
30 Matrix_Free(Constraints);
32 return P;
35 void static scan(gen_fun *S)
37 Value c;
38 Vector *params;
39 value_init(c);
40 params = Vector_Alloc(2);
41 for (int j = 19; j <= 39; ++j) {
42 value_set_si(params->p[1], j);
43 for (int i = 8; i <= 17; ++i) {
44 value_set_si(params->p[0], i);
45 S->coefficient(params->p, &c);
46 fprintf(stderr, "%d %d: ", i, j);
47 value_print(stderr, VALUE_FMT, c);
48 fprintf(stderr, "\n");
51 value_clear(c);
52 Vector_Free(params);
55 bool is_indicator(gen_fun *gf, barvinok_options *options)
57 gen_fun *test;
58 QQ mone(-1, 1);
60 test = gf->Hadamard_product(gf, options);
61 test->add(mone, gf);
63 test->print(std::cerr, 0, NULL);
64 cerr << endl;
67 int main(int argc, char **argv)
69 Matrix *M;
70 Polyhedron *P, *C, *B, *Pold;
71 Vector *small;
72 Matrix *T, *basis;
73 gen_fun *S, *S_shift, *hp, *S_divide, *frob;
74 vec_ZZ up;
75 Value c;
76 bool ok;
77 QQ mone(-1, 1);
78 Vector *coins;
79 barvinok_options *options = barvinok_options_new_with_defaults();
81 coins = Vector_Read();
82 assert(coins);
83 assert(coins->Size == 4);
85 M = Matrix_Alloc(5, 7);
86 Vector_Copy(coins->p, M->p[0]+1, 4);
87 Vector_Free(coins);
88 value_set_si(M->p[0][1+4], -1);
89 for (int i = 0; i < 4; ++i) {
90 value_set_si(M->p[1+i][0], 1);
91 value_set_si(M->p[1+i][1+i], 1);
93 C = Constraints2Polyhedron(M, options->MaxRays);
94 Matrix_Free(M);
95 C = remove_equalities_p(C, 4, NULL);
96 assert(C->NbEq == 0);
97 Polyhedron_Print(stderr, P_VALUE_FMT, C);
99 B = uncone(C, options->MaxRays);
101 basis = Polyhedron_Reduced_Basis(B, options);
102 small = Vector_Alloc(B->Dimension + 2);
103 Vector_Copy(basis->p[0], small->p, B->Dimension);
104 Matrix_Free(basis);
105 Polyhedron_Free(B);
107 T = unimodular_complete(small);
108 Vector_Free(small);
109 Vector_Exchange(T->p[0], T->p[2], T->NbColumns);
110 Matrix_Print(stderr, P_VALUE_FMT, T);
112 Polyhedron_Print(stderr, P_VALUE_FMT, C);
113 P = Polyhedron_Image(C, T, options->MaxRays);
114 Matrix_Free(T);
115 Polyhedron_Free(C);
117 Polyhedron_Print(stderr, P_VALUE_FMT, P);
119 up.SetLength(2);
120 up[0] = 1;
121 up[1] = 0;
123 S = barvinok_enumerate_scarf_series(P, 2, 2, options);
124 S->print(std::cerr, 0, NULL);
125 cerr << endl;
127 S_shift = new gen_fun(S);
128 S_divide = new gen_fun(S);
129 S_divide->divide(up);
130 S_divide->shift(up);
132 value_init(c);
134 do {
135 S_shift->shift(up);
136 hp = S->Hadamard_product(S_shift, options);
137 S->add(mone, hp);
138 delete hp;
140 S_divide->shift(up);
141 hp = S->Hadamard_product(S_divide, options);
142 ok = hp->summate(&c);
143 if (ok)
144 assert(value_zero_p(c));
145 delete hp;
146 } while(!ok);
148 value_clear(c);
150 frob = S->summate(1, options);
151 frob->print(std::cout, 0, NULL);
152 cout << endl;
153 delete frob;
155 delete S_shift;
156 delete S_divide;
157 delete S;
159 Polyhedron_Free(P);
160 barvinok_options_free(options);