Makefile.am: keep better track of failed tests
[barvinok/uuh.git] / reducer.cc
blob7969b95d54abe7f1d5ccf4b0506be35346aede47
1 #include <vector>
2 #include <barvinok/util.h>
3 #include "reducer.h"
4 #include "lattice_point.h"
6 using std::vector;
7 using std::cerr;
8 using std::endl;
10 struct OrthogonalException Orthogonal;
12 void np_base::handle(const signed_cone& sc, barvinok_options *options)
14 assert(sc.rays.NumRows() == dim);
15 factor.n *= sc.sign;
16 handle(sc.rays, current_vertex, factor, sc.det, options);
17 factor.n *= sc.sign;
20 void np_base::start(Polyhedron *P, barvinok_options *options)
22 QQ factor(1, 1);
23 for (;;) {
24 try {
25 init(P);
26 for (int i = 0; i < P->NbRays; ++i) {
27 if (!value_pos_p(P->Ray[i][dim+1]))
28 continue;
30 Polyhedron *C = supporting_cone(P, i);
31 do_vertex_cone(factor, C, P->Ray[i]+1, options);
33 break;
34 } catch (OrthogonalException &e) {
35 reset();
40 /* input:
41 * f: the powers in the denominator for the remaining vars
42 * each row refers to a factor
43 * den_s: for each factor, the power of (s+1)
44 * sign
45 * num_s: powers in the numerator corresponding to the summed vars
46 * num_p: powers in the numerator corresponding to the remaining vars
47 * number of rays in cone: "dim" = "k"
48 * length of each ray: "dim" = "d"
49 * for now, it is assumed: k == d
50 * output:
51 * den_p: for each factor
52 * 0: independent of remaining vars
53 * 1: power corresponds to corresponding row in f
55 * all inputs are subject to change
57 void normalize(ZZ& sign, vec_ZZ& num_s, mat_ZZ& num_p, vec_ZZ& den_s, vec_ZZ& den_p,
58 mat_ZZ& f)
60 unsigned dim = f.NumRows();
61 unsigned nparam = num_p.NumCols();
62 unsigned nvar = dim - nparam;
64 int change = 0;
66 for (int j = 0; j < den_s.length(); ++j) {
67 if (den_s[j] == 0) {
68 den_p[j] = 1;
69 continue;
71 int k;
72 for (k = 0; k < nparam; ++k)
73 if (f[j][k] != 0)
74 break;
75 if (k < nparam) {
76 den_p[j] = 1;
77 if (den_s[j] > 0) {
78 f[j] = -f[j];
79 for (int i = 0; i < num_p.NumRows(); ++i)
80 num_p[i] += f[j];
82 } else
83 den_p[j] = 0;
84 if (den_s[j] > 0)
85 change ^= 1;
86 else {
87 den_s[j] = abs(den_s[j]);
88 for (int i = 0; i < num_p.NumRows(); ++i)
89 num_s[i] += den_s[j];
93 if (change)
94 sign = -sign;
97 void reducer::base(const vec_QQ& c, const mat_ZZ& num, const mat_ZZ& den_f)
99 for (int i = 0; i < num.NumRows(); ++i)
100 base(c[i], num[i], den_f);
103 struct dpoly_r_scanner {
104 const dpoly_r *rc;
105 const dpoly * const *num;
106 int n;
107 int dim;
108 dpoly_r_term_list::iterator *iter;
109 vector<int> powers;
110 vec_ZZ coeff;
112 dpoly_r_scanner(const dpoly * const *num, int n, const dpoly_r *rc, int dim)
113 : num(num), rc(rc), n(n), dim(dim), powers(dim, 0) {
114 coeff.SetLength(n);
115 iter = new dpoly_r_term_list::iterator[rc->len];
116 for (int i = 0; i < rc->len; ++i) {
117 int k;
118 for (k = 0; k < n; ++k)
119 if (value_notzero_p(num[k]->coeff->p[rc->len-1-i]))
120 break;
121 if (k < n)
122 iter[i] = rc->c[i].begin();
123 else
124 iter[i] = rc->c[i].end();
127 bool next() {
128 int *pos;
129 int len = 0;
131 for (int i = 0; i < rc->len; ++i) {
132 if (iter[i] == rc->c[i].end())
133 continue;
134 if (!len) {
135 pos = new int[rc->len];
136 pos[len++] = i;
137 } else {
138 if ((*iter[i])->powers < (*iter[pos[0]])->powers) {
139 pos[0] = i;
140 len = 1;
141 } else if ((*iter[i])->powers == (*iter[pos[0]])->powers)
142 pos[len++] = i;
146 if (!len)
147 return false;
149 powers = (*iter[pos[0]])->powers;
150 for (int k = 0; k < n; ++k) {
151 value2zz(num[k]->coeff->p[rc->len-1-pos[0]], tmp);
152 mul(coeff[k], (*iter[pos[0]])->coeff, tmp);
154 ++iter[pos[0]];
155 for (int i = 1; i < len; ++i) {
156 for (int k = 0; k < n; ++k) {
157 value2zz(num[k]->coeff->p[rc->len-1-pos[i]], tmp);
158 mul(tmp, (*iter[pos[i]])->coeff, tmp);
159 add(coeff[k], coeff[k], tmp);
161 ++iter[pos[i]];
164 delete [] pos;
165 return true;
167 ~dpoly_r_scanner() {
168 delete [] iter;
170 private:
171 ZZ tmp;
174 void reducer::reduce_canonical(const vec_QQ& c, const mat_ZZ& num,
175 const mat_ZZ& den_f)
177 vec_QQ c2 = c;
178 mat_ZZ num2 = num;
180 for (int i = 0; i < c2.length(); ++i) {
181 c2[i].canonicalize();
182 if (c2[i].n != 0)
183 continue;
185 if (i < c2.length()-1) {
186 num2[i] = num2[c2.length()-1];
187 c2[i] = c2[c2.length()-1];
189 num2.SetDims(num2.NumRows()-1, num2.NumCols());
190 c2.SetLength(c2.length()-1);
191 --i;
193 reduce(c2, num2, den_f);
196 void reducer::reduce(const vec_QQ& c, const mat_ZZ& num, const mat_ZZ& den_f)
198 assert(c.length() == num.NumRows());
199 unsigned len = den_f.NumRows(); // number of factors in den
200 vec_QQ c2 = c;
202 if (num.NumCols() == lower) {
203 base(c, num, den_f);
204 return;
206 assert(num.NumCols() > 1);
207 assert(num.NumRows() > 0);
209 vec_ZZ den_s;
210 mat_ZZ den_r;
211 vec_ZZ num_s;
212 mat_ZZ num_p;
214 split(num, num_s, num_p, den_f, den_s, den_r);
216 vec_ZZ den_p;
217 den_p.SetLength(len);
219 ZZ sign(INIT_VAL, 1);
220 normalize(sign, num_s, num_p, den_s, den_p, den_r);
221 c2 *= sign;
223 int only_param = 0; // k-r-s from text
224 int no_param = 0; // r from text
225 for (int k = 0; k < len; ++k) {
226 if (den_p[k] == 0)
227 ++no_param;
228 else if (den_s[k] == 0)
229 ++only_param;
231 if (no_param == 0) {
232 reduce(c2, num_p, den_r);
233 } else {
234 int k, l;
235 mat_ZZ pden;
236 pden.SetDims(only_param, den_r.NumCols());
238 for (k = 0, l = 0; k < len; ++k)
239 if (den_s[k] == 0)
240 pden[l++] = den_r[k];
242 for (k = 0; k < len; ++k)
243 if (den_p[k] == 0)
244 break;
246 dpoly **n = new dpoly *[num_s.length()];
247 for (int i = 0; i < num_s.length(); ++i) {
248 zz2value(num_s[i], tz);
249 n[i] = new dpoly(no_param, tz);
250 /* Search for other numerator (j) with same num_p.
251 * If found, replace a[j]/b[j] * n[j] and a[i]/b[i] * n[i]
252 * by 1/(b[j]*b[i]/g) * (a[j]*b[i]/g * n[j] + a[i]*b[j]/g * n[i])
253 * where g = gcd(b[i], b[j].
255 for (int j = 0; j < i; ++j) {
256 if (num_p[i] != num_p[j])
257 continue;
258 ZZ g = GCD(c2[i].d, c2[j].d);
259 zz2value(c2[j].n * c2[i].d/g, tz);
260 *n[j] *= tz;
261 zz2value(c2[i].n * c2[j].d/g, tz);
262 *n[i] *= tz;
263 *n[j] += *n[i];
264 c2[j].n = 1;
265 c2[j].d *= c2[i].d/g;
266 delete n[i];
267 if (i < num_s.length()-1) {
268 num_s[i] = num_s[num_s.length()-1];
269 num_p[i] = num_p[num_s.length()-1];
270 c2[i] = c2[num_s.length()-1];
272 num_s.SetLength(num_s.length()-1);
273 c2.SetLength(c2.length()-1);
274 num_p.SetDims(num_p.NumRows()-1, num_p.NumCols());
275 --i;
276 break;
279 zz2value(den_s[k], tz);
280 dpoly D(no_param, tz, 1);
281 for ( ; ++k < len; )
282 if (den_p[k] == 0) {
283 zz2value(den_s[k], tz);
284 dpoly fact(no_param, tz, 1);
285 D *= fact;
288 if (no_param + only_param == len) {
289 vec_QQ q;
290 q.SetLength(num_s.length());
291 for (int i = 0; i < num_s.length(); ++i) {
292 mpq_set_si(tcount, 0, 1);
293 n[i]->div(D, tcount, 1);
295 value2zz(mpq_numref(tcount), q[i].n);
296 value2zz(mpq_denref(tcount), q[i].d);
297 q[i] *= c2[i];
299 for (int i = q.length()-1; i >= 0; --i) {
300 if (q[i].n == 0) {
301 q[i] = q[q.length()-1];
302 num_p[i] = num_p[q.length()-1];
303 q.SetLength(q.length()-1);
304 num_p.SetDims(num_p.NumRows()-1, num_p.NumCols());
308 if (q.length() != 0)
309 reduce(q, num_p, pden);
310 } else {
311 value_set_si(tz, 0);
312 dpoly one(no_param, tz);
313 dpoly_r *r = NULL;
315 for (k = 0; k < len; ++k) {
316 if (den_s[k] == 0 || den_p[k] == 0)
317 continue;
319 zz2value(den_s[k], tz);
320 dpoly pd(no_param-1, tz, 1);
322 int l;
323 for (l = 0; l < k; ++l)
324 if (den_r[l] == den_r[k])
325 break;
327 if (!r)
328 r = new dpoly_r(one, pd, l, len);
329 else {
330 dpoly_r *nr = new dpoly_r(r, pd, l, len);
331 delete r;
332 r = nr;
336 vec_QQ factor;
337 factor.SetLength(c2.length());
338 int common = pden.NumRows();
339 dpoly_r *rc = r->div(D);
340 for (int i = 0; i < num_s.length(); ++i) {
341 factor[i].d = c2[i].d;
342 factor[i].d *= rc->denom;
345 dpoly_r_scanner scanner(n, num_s.length(), rc, len);
346 int rows;
347 while (scanner.next()) {
348 int i;
349 for (i = 0; i < num_s.length(); ++i)
350 if (scanner.coeff[i] != 0)
351 break;
352 if (i == num_s.length())
353 continue;
354 rows = common;
355 pden.SetDims(rows, pden.NumCols());
356 for (int k = 0; k < rc->dim; ++k) {
357 int n = scanner.powers[k];
358 if (n == 0)
359 continue;
360 pden.SetDims(rows+n, pden.NumCols());
361 for (int l = 0; l < n; ++l)
362 pden[rows+l] = den_r[k];
363 rows += n;
365 /* The denominators in factor are kept constant
366 * over all iterations of the enclosing while loop.
367 * The rational numbers in factor may therefore not be
368 * canonicalized. Some may even be zero.
370 for (int i = 0; i < num_s.length(); ++i) {
371 factor[i].n = c2[i].n;
372 factor[i].n *= scanner.coeff[i];
374 reduce_canonical(factor, num_p, pden);
377 delete rc;
378 delete r;
380 for (int i = 0; i < num_s.length(); ++i)
381 delete n[i];
382 delete [] n;
386 void reducer::handle(const mat_ZZ& den, Value *V, const QQ& c,
387 unsigned long det, barvinok_options *options)
389 vec_QQ vc;
391 Matrix *points = Matrix_Alloc(det, dim);
392 Matrix* Rays = zz2matrix(den);
393 lattice_points_fixed(V, V, Rays, Rays, points, det);
394 Matrix_Free(Rays);
395 matrix2zz(points, vertex, points->NbRows, points->NbColumns);
396 Matrix_Free(points);
398 vc.SetLength(vertex.NumRows());
399 for (int i = 0; i < vc.length(); ++i)
400 vc[i] = c;
402 reduce(vc, vertex, den);
405 void split_one(const mat_ZZ& num, vec_ZZ& num_s, mat_ZZ& num_p,
406 const mat_ZZ& den_f, vec_ZZ& den_s, mat_ZZ& den_r)
408 unsigned len = den_f.NumRows(); // number of factors in den
409 unsigned d = num.NumCols() - 1;
411 den_s.SetLength(len);
412 den_r.SetDims(len, d);
414 for (int r = 0; r < len; ++r) {
415 den_s[r] = den_f[r][0];
416 for (int k = 1; k <= d; ++k)
417 den_r[r][k-1] = den_f[r][k];
420 num_s.SetLength(num.NumRows());
421 num_p.SetDims(num.NumRows(), d);
422 for (int i = 0; i < num.NumRows(); ++i) {
423 num_s[i] = num[i][0];
424 for (int k = 1 ; k <= d; ++k)
425 num_p[i][k-1] = num[i][k];
429 void icounter::base(const QQ& c, const vec_ZZ& num, const mat_ZZ& den_f)
431 zz2value(c.n, tn);
432 zz2value(c.d, td);
434 unsigned len = den_f.NumRows(); // number of factors in den
436 if (len > 0) {
437 int r;
438 vec_ZZ den_s;
439 den_s.SetLength(len);
440 assert(num.length() == 1);
441 ZZ num_s = num[0];
442 for (r = 0; r < len; ++r)
443 den_s[r] = den_f[r][0];
444 int sign = (len % 2) ? -1 : 1;
446 zz2value(num_s, tz);
447 dpoly n(len, tz);
448 zz2value(den_s[0], tz);
449 dpoly D(len, tz, 1);
450 for (int k = 1; k < len; ++k) {
451 zz2value(den_s[k], tz);
452 dpoly fact(len, tz, 1);
453 D *= fact;
455 mpq_set_si(tcount, 0, 1);
456 n.div(D, tcount, 1);
457 if (sign == -1)
458 value_oppose(tn, tn);
460 mpz_mul(mpq_numref(tcount), mpq_numref(tcount), tn);
461 mpz_mul(mpq_denref(tcount), mpq_denref(tcount), td);
462 mpq_canonicalize(tcount);
463 } else {
464 value_assign(mpq_numref(tcount), tn);
465 value_assign(mpq_denref(tcount), td);
467 mpq_add(count, count, tcount);