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 nparam
= num_p
.NumCols();
65 for (int j
= 0; j
< den_s
.length(); ++j
) {
71 for (k
= 0; k
< nparam
; ++k
)
78 for (int i
= 0; i
< num_p
.NumRows(); ++i
)
86 den_s
[j
] = abs(den_s
[j
]);
87 for (int i
= 0; i
< num_p
.NumRows(); ++i
)
96 void reducer::base(const vec_QQ
& c
, const mat_ZZ
& num
, const mat_ZZ
& den_f
)
98 for (int i
= 0; i
< num
.NumRows(); ++i
)
99 base(c
[i
], num
[i
], den_f
);
102 struct dpoly_r_scanner
{
104 const dpoly
* const *num
;
107 dpoly_r_term_list::iterator
*iter
;
111 dpoly_r_scanner(const dpoly
* const *num
, int n
, const dpoly_r
*rc
, int dim
)
112 : rc(rc
), num(num
), n(n
), dim(dim
), powers(dim
, 0) {
114 iter
= new dpoly_r_term_list::iterator
[rc
->len
];
115 for (int i
= 0; i
< rc
->len
; ++i
) {
117 for (k
= 0; k
< n
; ++k
)
118 if (value_notzero_p(num
[k
]->coeff
->p
[rc
->len
-1-i
]))
121 iter
[i
] = rc
->c
[i
].begin();
123 iter
[i
] = rc
->c
[i
].end();
130 for (int i
= 0; i
< rc
->len
; ++i
) {
131 if (iter
[i
] == rc
->c
[i
].end())
134 pos
= new int[rc
->len
];
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
);
173 void reducer::reduce_canonical(const vec_QQ
& c
, const mat_ZZ
& num
,
179 for (int i
= 0; i
< c2
.length(); ++i
) {
180 c2
[i
].canonicalize();
184 if (i
< c2
.length()-1) {
185 num2
[i
] = num2
[c2
.length()-1];
186 c2
[i
] = c2
[c2
.length()-1];
188 num2
.SetDims(num2
.NumRows()-1, num2
.NumCols());
189 c2
.SetLength(c2
.length()-1);
192 reduce(c2
, num2
, den_f
);
195 void reducer::reduce(const vec_QQ
& c
, const mat_ZZ
& num
, const mat_ZZ
& den_f
)
197 assert(c
.length() == num
.NumRows());
198 unsigned len
= den_f
.NumRows(); // number of factors in den
201 if (num
.NumCols() == lower
) {
205 assert(num
.NumCols() > 1);
206 assert(num
.NumRows() > 0);
213 split(num
, num_s
, num_p
, den_f
, den_s
, den_r
);
216 den_p
.SetLength(len
);
218 ZZ
sign(INIT_VAL
, 1);
219 normalize(sign
, num_s
, num_p
, den_s
, den_p
, den_r
);
222 int only_param
= 0; // k-r-s from text
223 int no_param
= 0; // r from text
224 for (int k
= 0; k
< len
; ++k
) {
227 else if (den_s
[k
] == 0)
231 reduce(c2
, num_p
, den_r
);
235 pden
.SetDims(only_param
, den_r
.NumCols());
237 for (k
= 0, l
= 0; k
< len
; ++k
)
239 pden
[l
++] = den_r
[k
];
241 for (k
= 0; k
< len
; ++k
)
245 dpoly
**n
= new dpoly
*[num_s
.length()];
246 for (int i
= 0; i
< num_s
.length(); ++i
) {
247 zz2value(num_s
[i
], tz
);
248 n
[i
] = new dpoly(no_param
, tz
);
249 /* Search for other numerator (j) with same num_p.
250 * If found, replace a[j]/b[j] * n[j] and a[i]/b[i] * n[i]
251 * by 1/(b[j]*b[i]/g) * (a[j]*b[i]/g * n[j] + a[i]*b[j]/g * n[i])
252 * where g = gcd(b[i], b[j].
254 for (int j
= 0; j
< i
; ++j
) {
255 if (num_p
[i
] != num_p
[j
])
257 ZZ g
= GCD(c2
[i
].d
, c2
[j
].d
);
258 zz2value(c2
[j
].n
* c2
[i
].d
/g
, tz
);
260 zz2value(c2
[i
].n
* c2
[j
].d
/g
, tz
);
264 c2
[j
].d
*= c2
[i
].d
/g
;
266 if (i
< num_s
.length()-1) {
267 num_s
[i
] = num_s
[num_s
.length()-1];
268 num_p
[i
] = num_p
[num_s
.length()-1];
269 c2
[i
] = c2
[num_s
.length()-1];
271 num_s
.SetLength(num_s
.length()-1);
272 c2
.SetLength(c2
.length()-1);
273 num_p
.SetDims(num_p
.NumRows()-1, num_p
.NumCols());
278 zz2value(den_s
[k
], tz
);
279 dpoly
D(no_param
, tz
, 1);
282 zz2value(den_s
[k
], tz
);
283 dpoly
fact(no_param
, tz
, 1);
287 if (no_param
+ only_param
== len
) {
289 q
.SetLength(num_s
.length());
290 for (int i
= 0; i
< num_s
.length(); ++i
) {
291 mpq_set_si(tcount
, 0, 1);
292 n
[i
]->div(D
, tcount
, 1);
294 value2zz(mpq_numref(tcount
), q
[i
].n
);
295 value2zz(mpq_denref(tcount
), q
[i
].d
);
298 for (int i
= q
.length()-1; i
>= 0; --i
) {
300 q
[i
] = q
[q
.length()-1];
301 num_p
[i
] = num_p
[q
.length()-1];
302 q
.SetLength(q
.length()-1);
303 num_p
.SetDims(num_p
.NumRows()-1, num_p
.NumCols());
308 reduce(q
, num_p
, pden
);
311 dpoly
one(no_param
, tz
);
314 for (k
= 0; k
< len
; ++k
) {
315 if (den_s
[k
] == 0 || den_p
[k
] == 0)
318 zz2value(den_s
[k
], tz
);
319 dpoly
pd(no_param
-1, tz
, 1);
322 for (l
= 0; l
< k
; ++l
)
323 if (den_r
[l
] == den_r
[k
])
327 r
= new dpoly_r(one
, pd
, l
, len
);
329 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
336 factor
.SetLength(c2
.length());
337 int common
= pden
.NumRows();
338 dpoly_r
*rc
= r
->div(D
);
339 for (int i
= 0; i
< num_s
.length(); ++i
) {
340 factor
[i
].d
= c2
[i
].d
;
341 factor
[i
].d
*= rc
->denom
;
344 dpoly_r_scanner
scanner(n
, num_s
.length(), rc
, len
);
346 while (scanner
.next()) {
348 for (i
= 0; i
< num_s
.length(); ++i
)
349 if (scanner
.coeff
[i
] != 0)
351 if (i
== num_s
.length())
354 pden
.SetDims(rows
, pden
.NumCols());
355 for (int k
= 0; k
< rc
->dim
; ++k
) {
356 int n
= scanner
.powers
[k
];
359 pden
.SetDims(rows
+n
, pden
.NumCols());
360 for (int l
= 0; l
< n
; ++l
)
361 pden
[rows
+l
] = den_r
[k
];
364 /* The denominators in factor are kept constant
365 * over all iterations of the enclosing while loop.
366 * The rational numbers in factor may therefore not be
367 * canonicalized. Some may even be zero.
369 for (int i
= 0; i
< num_s
.length(); ++i
) {
370 factor
[i
].n
= c2
[i
].n
;
371 factor
[i
].n
*= scanner
.coeff
[i
];
373 reduce_canonical(factor
, num_p
, pden
);
379 for (int i
= 0; i
< num_s
.length(); ++i
)
385 void reducer::handle(const mat_ZZ
& den
, Value
*V
, const QQ
& c
,
386 unsigned long det
, barvinok_options
*options
)
390 Matrix
*points
= Matrix_Alloc(det
, dim
);
391 Matrix
* Rays
= zz2matrix(den
);
392 lattice_points_fixed(V
, V
, Rays
, Rays
, points
, det
);
394 matrix2zz(points
, vertex
, points
->NbRows
, points
->NbColumns
);
397 vc
.SetLength(vertex
.NumRows());
398 for (int i
= 0; i
< vc
.length(); ++i
)
401 reduce(vc
, vertex
, den
);
404 void split_one(const mat_ZZ
& num
, vec_ZZ
& num_s
, mat_ZZ
& num_p
,
405 const mat_ZZ
& den_f
, vec_ZZ
& den_s
, mat_ZZ
& den_r
)
407 unsigned len
= den_f
.NumRows(); // number of factors in den
408 unsigned d
= num
.NumCols() - 1;
410 den_s
.SetLength(len
);
411 den_r
.SetDims(len
, d
);
413 for (int r
= 0; r
< len
; ++r
) {
414 den_s
[r
] = den_f
[r
][0];
415 for (int k
= 1; k
<= d
; ++k
)
416 den_r
[r
][k
-1] = den_f
[r
][k
];
419 num_s
.SetLength(num
.NumRows());
420 num_p
.SetDims(num
.NumRows(), d
);
421 for (int i
= 0; i
< num
.NumRows(); ++i
) {
422 num_s
[i
] = num
[i
][0];
423 for (int k
= 1 ; k
<= d
; ++k
)
424 num_p
[i
][k
-1] = num
[i
][k
];
428 void icounter::base(const QQ
& c
, const vec_ZZ
& num
, const mat_ZZ
& den_f
)
433 unsigned len
= den_f
.NumRows(); // number of factors in den
438 den_s
.SetLength(len
);
439 assert(num
.length() == 1);
441 for (r
= 0; r
< len
; ++r
)
442 den_s
[r
] = den_f
[r
][0];
443 int sign
= (len
% 2) ? -1 : 1;
447 zz2value(den_s
[0], tz
);
449 for (int k
= 1; k
< len
; ++k
) {
450 zz2value(den_s
[k
], tz
);
451 dpoly
fact(len
, tz
, 1);
454 mpq_set_si(tcount
, 0, 1);
457 value_oppose(tn
, tn
);
459 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
460 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
461 mpq_canonicalize(tcount
);
463 value_assign(mpq_numref(tcount
), tn
);
464 value_assign(mpq_denref(tcount
), td
);
466 mpq_add(count
, count
, tcount
);