2 #include <barvinok/util.h>
4 #include "lattice_point.h"
10 struct OrthogonalException Orthogonal
;
12 void np_base::handle(const signed_cone
& sc
, barvinok_options
*options
)
14 assert(sc
.rays
.NumRows() == dim
);
16 handle(sc
.rays
, current_vertex
, factor
, sc
.det
, sc
.closed
, options
);
20 void np_base::start(Polyhedron
*P
, barvinok_options
*options
)
26 for (int i
= 0; i
< P
->NbRays
; ++i
) {
27 if (!value_pos_p(P
->Ray
[i
][dim
+1]))
30 Polyhedron
*C
= supporting_cone(P
, i
);
31 do_vertex_cone(factor
, C
, P
->Ray
[i
]+1, options
);
34 } catch (OrthogonalException
&e
) {
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)
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
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
,
60 unsigned dim
= f
.NumRows();
61 unsigned nparam
= num_p
.NumCols();
62 unsigned nvar
= dim
- nparam
;
66 for (int j
= 0; j
< den_s
.length(); ++j
) {
72 for (k
= 0; k
< nparam
; ++k
)
79 for (int i
= 0; i
< num_p
.NumRows(); ++i
)
87 den_s
[j
] = abs(den_s
[j
]);
88 for (int i
= 0; i
< num_p
.NumRows(); ++i
)
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
{
105 const dpoly
* const *num
;
108 dpoly_r_term_list::iterator
*iter
;
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) {
115 iter
= new dpoly_r_term_list::iterator
[rc
->len
];
116 for (int i
= 0; i
< rc
->len
; ++i
) {
118 for (k
= 0; k
< n
; ++k
)
119 if (value_notzero_p(num
[k
]->coeff
->p
[rc
->len
-1-i
]))
122 iter
[i
] = rc
->c
[i
].begin();
124 iter
[i
] = rc
->c
[i
].end();
131 for (int i
= 0; i
< rc
->len
; ++i
) {
132 if (iter
[i
] == rc
->c
[i
].end())
137 if ((*iter
[i
])->powers
< (*iter
[pos
[0]])->powers
) {
140 } else if ((*iter
[i
])->powers
== (*iter
[pos
[0]])->powers
)
148 powers
= (*iter
[pos
[0]])->powers
;
149 for (int k
= 0; k
< n
; ++k
) {
150 value2zz(num
[k
]->coeff
->p
[rc
->len
-1-pos
[0]], tmp
);
151 mul(coeff
[k
], (*iter
[pos
[0]])->coeff
, tmp
);
154 for (int i
= 1; i
< len
; ++i
) {
155 for (int k
= 0; k
< n
; ++k
) {
156 value2zz(num
[k
]->coeff
->p
[rc
->len
-1-pos
[i
]], tmp
);
157 mul(tmp
, (*iter
[pos
[i
]])->coeff
, tmp
);
158 add(coeff
[k
], coeff
[k
], tmp
);
172 void reducer::reduce(const vec_QQ
& c
, const mat_ZZ
& num
, const mat_ZZ
& den_f
)
174 assert(c
.length() == num
.NumRows());
175 unsigned len
= den_f
.NumRows(); // number of factors in den
178 if (num
.NumCols() == lower
) {
182 assert(num
.NumCols() > 1);
183 assert(num
.NumRows() > 0);
190 split(num
, num_s
, num_p
, den_f
, den_s
, den_r
);
193 den_p
.SetLength(len
);
195 ZZ
sign(INIT_VAL
, 1);
196 normalize(sign
, num_s
, num_p
, den_s
, den_p
, den_r
);
199 int only_param
= 0; // k-r-s from text
200 int no_param
= 0; // r from text
201 for (int k
= 0; k
< len
; ++k
) {
204 else if (den_s
[k
] == 0)
208 reduce(c2
, num_p
, den_r
);
212 pden
.SetDims(only_param
, den_r
.NumCols());
214 for (k
= 0, l
= 0; k
< len
; ++k
)
216 pden
[l
++] = den_r
[k
];
218 for (k
= 0; k
< len
; ++k
)
222 dpoly
*n
[num_s
.length()];
223 for (int i
= 0; i
< num_s
.length(); ++i
) {
224 zz2value(num_s
[i
], tz
);
225 n
[i
] = new dpoly(no_param
, tz
);
226 /* Search for other numerator (j) with same num_p.
227 * If found, replace a[j]/b[j] * n[j] and a[i]/b[i] * n[i]
228 * by 1/(b[j]*b[i]/g) * (a[j]*b[i]/g * n[j] + a[i]*b[j]/g * n[i])
229 * where g = gcd(b[i], b[j].
231 for (int j
= 0; j
< i
; ++j
) {
232 if (num_p
[i
] != num_p
[j
])
234 ZZ g
= GCD(c2
[i
].d
, c2
[j
].d
);
235 zz2value(c2
[j
].n
* c2
[i
].d
/g
, tz
);
237 zz2value(c2
[i
].n
* c2
[j
].d
/g
, tz
);
241 c2
[j
].d
*= c2
[i
].d
/g
;
243 if (i
< num_s
.length()-1) {
244 num_s
[i
] = num_s
[num_s
.length()-1];
245 num_p
[i
] = num_p
[num_s
.length()-1];
246 c2
[i
] = c2
[num_s
.length()-1];
248 num_s
.SetLength(num_s
.length()-1);
249 c2
.SetLength(c2
.length()-1);
250 num_p
.SetDims(num_p
.NumRows()-1, num_p
.NumCols());
255 zz2value(den_s
[k
], tz
);
256 dpoly
D(no_param
, tz
, 1);
259 zz2value(den_s
[k
], tz
);
260 dpoly
fact(no_param
, tz
, 1);
264 if (no_param
+ only_param
== len
) {
266 q
.SetLength(num_s
.length());
267 for (int i
= 0; i
< num_s
.length(); ++i
) {
268 mpq_set_si(tcount
, 0, 1);
269 n
[i
]->div(D
, tcount
, 1);
271 value2zz(mpq_numref(tcount
), q
[i
].n
);
272 value2zz(mpq_denref(tcount
), q
[i
].d
);
275 for (int i
= q
.length()-1; i
>= 0; --i
) {
277 q
[i
] = q
[q
.length()-1];
278 num_p
[i
] = num_p
[q
.length()-1];
279 q
.SetLength(q
.length()-1);
280 num_p
.SetDims(num_p
.NumRows()-1, num_p
.NumCols());
285 reduce(q
, num_p
, pden
);
288 dpoly
one(no_param
, tz
);
291 for (k
= 0; k
< len
; ++k
) {
292 if (den_s
[k
] == 0 || den_p
[k
] == 0)
295 zz2value(den_s
[k
], tz
);
296 dpoly
pd(no_param
-1, tz
, 1);
299 for (l
= 0; l
< k
; ++l
)
300 if (den_r
[l
] == den_r
[k
])
304 r
= new dpoly_r(one
, pd
, l
, len
);
306 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
313 factor
.SetLength(c2
.length());
314 int common
= pden
.NumRows();
315 dpoly_r
*rc
= r
->div(D
);
316 for (int i
= 0; i
< num_s
.length(); ++i
) {
317 factor
[i
].d
= c2
[i
].d
;
318 factor
[i
].d
*= rc
->denom
;
321 dpoly_r_scanner
scanner(n
, num_s
.length(), rc
, len
);
323 while (scanner
.next()) {
325 for (i
= 0; i
< num_s
.length(); ++i
)
326 if (scanner
.coeff
[i
] != 0)
328 if (i
== num_s
.length())
331 pden
.SetDims(rows
, pden
.NumCols());
332 for (int k
= 0; k
< rc
->dim
; ++k
) {
333 int n
= scanner
.powers
[k
];
336 pden
.SetDims(rows
+n
, pden
.NumCols());
337 for (int l
= 0; l
< n
; ++l
)
338 pden
[rows
+l
] = den_r
[k
];
341 for (int i
= 0; i
< num_s
.length(); ++i
) {
342 factor
[i
].n
= c2
[i
].n
;
343 factor
[i
].n
*= scanner
.coeff
[i
];
345 reduce(factor
, num_p
, pden
);
351 for (int i
= 0; i
< num_s
.length(); ++i
)
356 void reducer::handle(const mat_ZZ
& den
, Value
*V
, const QQ
& c
, unsigned long det
,
357 int *closed
, barvinok_options
*options
)
361 Matrix
*points
= Matrix_Alloc(det
, dim
);
362 Matrix
* Rays
= zz2matrix(den
);
363 lattice_points_fixed(V
, V
, Rays
, Rays
, points
, det
, closed
);
365 matrix2zz(points
, vertex
, points
->NbRows
, points
->NbColumns
);
368 vc
.SetLength(vertex
.NumRows());
369 for (int i
= 0; i
< vc
.length(); ++i
)
372 reduce(vc
, vertex
, den
);
375 void split_one(const mat_ZZ
& num
, vec_ZZ
& num_s
, mat_ZZ
& num_p
,
376 const mat_ZZ
& den_f
, vec_ZZ
& den_s
, mat_ZZ
& den_r
)
378 unsigned len
= den_f
.NumRows(); // number of factors in den
379 unsigned d
= num
.NumCols() - 1;
381 den_s
.SetLength(len
);
382 den_r
.SetDims(len
, d
);
384 for (int r
= 0; r
< len
; ++r
) {
385 den_s
[r
] = den_f
[r
][0];
386 for (int k
= 1; k
<= d
; ++k
)
387 den_r
[r
][k
-1] = den_f
[r
][k
];
390 num_s
.SetLength(num
.NumRows());
391 num_p
.SetDims(num
.NumRows(), d
);
392 for (int i
= 0; i
< num
.NumRows(); ++i
) {
393 num_s
[i
] = num
[i
][0];
394 for (int k
= 1 ; k
<= d
; ++k
)
395 num_p
[i
][k
-1] = num
[i
][k
];
399 void normalize(ZZ
& sign
, ZZ
& num
, vec_ZZ
& den
)
401 unsigned dim
= den
.length();
405 for (int j
= 0; j
< den
.length(); ++j
) {
409 den
[j
] = abs(den
[j
]);
417 void icounter::base(const QQ
& c
, const vec_ZZ
& num
, const mat_ZZ
& den_f
)
420 unsigned len
= den_f
.NumRows(); // number of factors in den
422 den_s
.SetLength(len
);
423 assert(num
.length() == 1);
425 for (r
= 0; r
< len
; ++r
)
426 den_s
[r
] = den_f
[r
][0];
427 ZZ sign
= ZZ(INIT_VAL
, 1);
428 normalize(sign
, num_s
, den_s
);
432 zz2value(den_s
[0], tz
);
434 for (int k
= 1; k
< len
; ++k
) {
435 zz2value(den_s
[k
], tz
);
436 dpoly
fact(len
, tz
, 1);
439 mpq_set_si(tcount
, 0, 1);
443 value_oppose(tn
, tn
);
445 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
446 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
447 mpq_canonicalize(tcount
);
448 mpq_add(count
, count
, tcount
);
451 void infinite_icounter::base(const QQ
& c
, const vec_ZZ
& num
, const mat_ZZ
& den_f
)
454 unsigned len
= den_f
.NumRows(); // number of factors in den
456 den_s
.SetLength(len
);
457 assert(num
.length() == 1);
460 for (r
= 0; r
< len
; ++r
)
461 den_s
[r
] = den_f
[r
][0];
462 ZZ sign
= ZZ(INIT_VAL
, 1);
463 normalize(sign
, num_s
, den_s
);
467 zz2value(den_s
[0], tz
);
469 for (int k
= 1; k
< len
; ++k
) {
470 zz2value(den_s
[k
], tz
);
471 dpoly
fact(len
, tz
, 1);
481 value_oppose(tmp
, tmp
);
482 value_assign(mpq_numref(factor
), tmp
);
484 value_assign(mpq_denref(factor
), tmp
);
486 n
.div(D
, count
, factor
);