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
, options
);
20 void np_base::start(Polyhedron
*P
, barvinok_options
*options
)
27 for (int i
= 0; i
< P
->NbRays
; ++i
) {
28 if (!value_pos_p(P
->Ray
[i
][dim
+1]))
31 Polyhedron
*C
= supporting_cone(P
, i
);
32 do_vertex_cone(factor
, C
, P
->Ray
[i
]+1, options
);
35 } catch (OrthogonalException
&e
) {
43 * f: the powers in the denominator for the remaining vars
44 * each row refers to a factor
45 * den_s: for each factor, the power of (s+1)
47 * num_s: powers in the numerator corresponding to the summed vars
48 * num_p: powers in the numerator corresponding to the remaining vars
49 * number of rays in cone: "dim" = "k"
50 * length of each ray: "dim" = "d"
51 * for now, it is assumed: k == d
53 * den_p: for each factor
54 * 0: independent of remaining vars
55 * 1: power corresponds to corresponding row in f
57 * all inputs are subject to change
59 void normalize(ZZ
& sign
, vec_ZZ
& num_s
, mat_ZZ
& num_p
, vec_ZZ
& den_s
, vec_ZZ
& den_p
,
62 unsigned dim
= f
.NumRows();
63 unsigned nparam
= num_p
.NumCols();
64 unsigned nvar
= dim
- nparam
;
68 for (int j
= 0; j
< den_s
.length(); ++j
) {
74 for (k
= 0; k
< nparam
; ++k
)
81 for (int i
= 0; i
< num_p
.NumRows(); ++i
)
89 den_s
[j
] = abs(den_s
[j
]);
90 for (int i
= 0; i
< num_p
.NumRows(); ++i
)
99 void reducer::base(const vec_QQ
& c
, const mat_ZZ
& num
, const mat_ZZ
& den_f
)
101 for (int i
= 0; i
< num
.NumRows(); ++i
)
102 base(c
[i
], num
[i
], den_f
);
105 struct dpoly_r_scanner
{
107 const dpoly
* const *num
;
110 dpoly_r_term_list::iterator
*iter
;
114 dpoly_r_scanner(const dpoly
* const *num
, int n
, const dpoly_r
*rc
, int dim
)
115 : num(num
), rc(rc
), n(n
), dim(dim
), powers(dim
, 0) {
117 iter
= new dpoly_r_term_list::iterator
[rc
->len
];
118 for (int i
= 0; i
< rc
->len
; ++i
) {
120 for (k
= 0; k
< n
; ++k
)
121 if (value_notzero_p(num
[k
]->coeff
->p
[rc
->len
-1-i
]))
124 iter
[i
] = rc
->c
[i
].begin();
126 iter
[i
] = rc
->c
[i
].end();
133 for (int i
= 0; i
< rc
->len
; ++i
) {
134 if (iter
[i
] == rc
->c
[i
].end())
137 pos
= new int[rc
->len
];
140 if ((*iter
[i
])->powers
< (*iter
[pos
[0]])->powers
) {
143 } else if ((*iter
[i
])->powers
== (*iter
[pos
[0]])->powers
)
151 powers
= (*iter
[pos
[0]])->powers
;
152 for (int k
= 0; k
< n
; ++k
) {
153 value2zz(num
[k
]->coeff
->p
[rc
->len
-1-pos
[0]], tmp
);
154 mul(coeff
[k
], (*iter
[pos
[0]])->coeff
, tmp
);
157 for (int i
= 1; i
< len
; ++i
) {
158 for (int k
= 0; k
< n
; ++k
) {
159 value2zz(num
[k
]->coeff
->p
[rc
->len
-1-pos
[i
]], tmp
);
160 mul(tmp
, (*iter
[pos
[i
]])->coeff
, tmp
);
161 add(coeff
[k
], coeff
[k
], tmp
);
176 void reducer::reduce_canonical(const vec_QQ
& c
, const mat_ZZ
& num
,
182 for (int i
= 0; i
< c2
.length(); ++i
) {
183 c2
[i
].canonicalize();
187 if (i
< c2
.length()-1) {
188 num2
[i
] = num2
[c2
.length()-1];
189 c2
[i
] = c2
[c2
.length()-1];
191 num2
.SetDims(num2
.NumRows()-1, num2
.NumCols());
192 c2
.SetLength(c2
.length()-1);
195 reduce(c2
, num2
, den_f
);
198 void reducer::reduce(const vec_QQ
& c
, const mat_ZZ
& num
, const mat_ZZ
& den_f
)
200 assert(c
.length() == num
.NumRows());
201 unsigned len
= den_f
.NumRows(); // number of factors in den
204 if (num
.NumCols() == lower
) {
208 assert(num
.NumCols() > 1);
209 assert(num
.NumRows() > 0);
216 split(num
, num_s
, num_p
, den_f
, den_s
, den_r
);
219 den_p
.SetLength(len
);
221 ZZ
sign(INIT_VAL
, 1);
222 normalize(sign
, num_s
, num_p
, den_s
, den_p
, den_r
);
225 int only_param
= 0; // k-r-s from text
226 int no_param
= 0; // r from text
227 for (int k
= 0; k
< len
; ++k
) {
230 else if (den_s
[k
] == 0)
234 reduce(c2
, num_p
, den_r
);
238 pden
.SetDims(only_param
, den_r
.NumCols());
240 for (k
= 0, l
= 0; k
< len
; ++k
)
242 pden
[l
++] = den_r
[k
];
244 for (k
= 0; k
< len
; ++k
)
248 dpoly
**n
= new dpoly
*[num_s
.length()];
249 for (int i
= 0; i
< num_s
.length(); ++i
) {
250 zz2value(num_s
[i
], tz
);
251 n
[i
] = new dpoly(no_param
, tz
);
252 /* Search for other numerator (j) with same num_p.
253 * If found, replace a[j]/b[j] * n[j] and a[i]/b[i] * n[i]
254 * by 1/(b[j]*b[i]/g) * (a[j]*b[i]/g * n[j] + a[i]*b[j]/g * n[i])
255 * where g = gcd(b[i], b[j].
257 for (int j
= 0; j
< i
; ++j
) {
258 if (num_p
[i
] != num_p
[j
])
260 ZZ g
= GCD(c2
[i
].d
, c2
[j
].d
);
261 zz2value(c2
[j
].n
* c2
[i
].d
/g
, tz
);
263 zz2value(c2
[i
].n
* c2
[j
].d
/g
, tz
);
267 c2
[j
].d
*= c2
[i
].d
/g
;
269 if (i
< num_s
.length()-1) {
270 num_s
[i
] = num_s
[num_s
.length()-1];
271 num_p
[i
] = num_p
[num_s
.length()-1];
272 c2
[i
] = c2
[num_s
.length()-1];
274 num_s
.SetLength(num_s
.length()-1);
275 c2
.SetLength(c2
.length()-1);
276 num_p
.SetDims(num_p
.NumRows()-1, num_p
.NumCols());
281 zz2value(den_s
[k
], tz
);
282 dpoly
D(no_param
, tz
, 1);
285 zz2value(den_s
[k
], tz
);
286 dpoly
fact(no_param
, tz
, 1);
290 if (no_param
+ only_param
== len
) {
292 q
.SetLength(num_s
.length());
293 for (int i
= 0; i
< num_s
.length(); ++i
) {
294 mpq_set_si(tcount
, 0, 1);
295 n
[i
]->div(D
, tcount
, 1);
297 value2zz(mpq_numref(tcount
), q
[i
].n
);
298 value2zz(mpq_denref(tcount
), q
[i
].d
);
301 for (int i
= q
.length()-1; i
>= 0; --i
) {
303 q
[i
] = q
[q
.length()-1];
304 num_p
[i
] = num_p
[q
.length()-1];
305 q
.SetLength(q
.length()-1);
306 num_p
.SetDims(num_p
.NumRows()-1, num_p
.NumCols());
311 reduce(q
, num_p
, pden
);
314 dpoly
one(no_param
, tz
);
317 for (k
= 0; k
< len
; ++k
) {
318 if (den_s
[k
] == 0 || den_p
[k
] == 0)
321 zz2value(den_s
[k
], tz
);
322 dpoly
pd(no_param
-1, tz
, 1);
325 for (l
= 0; l
< k
; ++l
)
326 if (den_r
[l
] == den_r
[k
])
330 r
= new dpoly_r(one
, pd
, l
, len
);
332 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
339 factor
.SetLength(c2
.length());
340 int common
= pden
.NumRows();
341 dpoly_r
*rc
= r
->div(D
);
342 for (int i
= 0; i
< num_s
.length(); ++i
) {
343 factor
[i
].d
= c2
[i
].d
;
344 factor
[i
].d
*= rc
->denom
;
347 dpoly_r_scanner
scanner(n
, num_s
.length(), rc
, len
);
349 while (scanner
.next()) {
351 for (i
= 0; i
< num_s
.length(); ++i
)
352 if (scanner
.coeff
[i
] != 0)
354 if (i
== num_s
.length())
357 pden
.SetDims(rows
, pden
.NumCols());
358 for (int k
= 0; k
< rc
->dim
; ++k
) {
359 int n
= scanner
.powers
[k
];
362 pden
.SetDims(rows
+n
, pden
.NumCols());
363 for (int l
= 0; l
< n
; ++l
)
364 pden
[rows
+l
] = den_r
[k
];
367 /* The denominators in factor are kept constant
368 * over all iterations of the enclosing while loop.
369 * The rational numbers in factor may therefore not be
370 * canonicalized. Some may even be zero.
372 for (int i
= 0; i
< num_s
.length(); ++i
) {
373 factor
[i
].n
= c2
[i
].n
;
374 factor
[i
].n
*= scanner
.coeff
[i
];
376 reduce_canonical(factor
, num_p
, pden
);
382 for (int i
= 0; i
< num_s
.length(); ++i
)
388 void reducer::handle(const mat_ZZ
& den
, Value
*V
, const QQ
& c
,
389 unsigned long det
, barvinok_options
*options
)
393 Matrix
*points
= Matrix_Alloc(det
, dim
);
394 Matrix
* Rays
= zz2matrix(den
);
395 lattice_points_fixed(V
, V
, Rays
, Rays
, points
, det
);
397 matrix2zz(points
, vertex
, points
->NbRows
, points
->NbColumns
);
400 vc
.SetLength(vertex
.NumRows());
401 for (int i
= 0; i
< vc
.length(); ++i
)
404 reduce(vc
, vertex
, den
);
407 void split_one(const mat_ZZ
& num
, vec_ZZ
& num_s
, mat_ZZ
& num_p
,
408 const mat_ZZ
& den_f
, vec_ZZ
& den_s
, mat_ZZ
& den_r
)
410 unsigned len
= den_f
.NumRows(); // number of factors in den
411 unsigned d
= num
.NumCols() - 1;
413 den_s
.SetLength(len
);
414 den_r
.SetDims(len
, d
);
416 for (int r
= 0; r
< len
; ++r
) {
417 den_s
[r
] = den_f
[r
][0];
418 for (int k
= 1; k
<= d
; ++k
)
419 den_r
[r
][k
-1] = den_f
[r
][k
];
422 num_s
.SetLength(num
.NumRows());
423 num_p
.SetDims(num
.NumRows(), d
);
424 for (int i
= 0; i
< num
.NumRows(); ++i
) {
425 num_s
[i
] = num
[i
][0];
426 for (int k
= 1 ; k
<= d
; ++k
)
427 num_p
[i
][k
-1] = num
[i
][k
];
431 void icounter::base(const QQ
& c
, const vec_ZZ
& num
, const mat_ZZ
& den_f
)
436 unsigned len
= den_f
.NumRows(); // number of factors in den
441 den_s
.SetLength(len
);
442 assert(num
.length() == 1);
444 for (r
= 0; r
< len
; ++r
)
445 den_s
[r
] = den_f
[r
][0];
446 int sign
= (len
% 2) ? -1 : 1;
450 zz2value(den_s
[0], tz
);
452 for (int k
= 1; k
< len
; ++k
) {
453 zz2value(den_s
[k
], tz
);
454 dpoly
fact(len
, tz
, 1);
457 mpq_set_si(tcount
, 0, 1);
460 value_oppose(tn
, tn
);
462 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
463 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
464 mpq_canonicalize(tcount
);
466 value_assign(mpq_numref(tcount
), tn
);
467 value_assign(mpq_denref(tcount
), td
);
469 mpq_add(count
, count
, tcount
);