barvinok 0.41.8
[barvinok.git] / doc / Internal.tex
blob8b2c239cd17aa6d7092a405903e1ae7d18d9a72f
1 \section{\protect\PolyLib/ interface of the \protect\ai[\tt]{barvinok} library
2 (obsolescent)}
4 Although \barvinok/ currently still uses \PolyLib/ internally,
5 this is likely to change in the not too distant future.
6 Consider using \isl/ based alternatives for the functions in this section
7 as the latter are likely to be removed in future releases.
9 Our \barvinok/ library is built on top of \PolyLib/
10 \shortcite{Wilde1993,Loechner1999}.
11 In particular, it reuses the implementations
12 of the algorithm of
13 \shortciteN{Loechner97parameterized}
14 for computing parametric vertices
15 and the algorithm of
16 \shortciteN{Clauss1998parametric}
17 for computing chamber decompositions.
18 Initially, our library was meant to be a replacement
19 for the algorithm of \shortciteN{Clauss1998parametric},
20 also implemented in \PolyLib/, for computing quasi-polynomials.
21 To ease the transition of application programs we
22 tried to reuse the existing data structures as much as possible.
24 \subsection{Existing Data Structures}
25 \label{a:existing}
27 Inside \PolyLib/ integer values are represented by the
28 \ai[\tt]{Value} data type.
29 Depending on a configure option, the data type may
30 either by a 32-bit integer, a 64-bit integer
31 or an arbitrary precision integer using \ai[\tt]{GMP}.
32 The \barvinok/ library requires that \PolyLib/ is compiled
33 with support for arbitrary precision integers.
35 The basic structure for representing (unions of) polyhedra is a
36 \ai[\tt]{Polyhedron}.
37 \begin{verbatim}
38 typedef struct polyhedron {
39 unsigned Dimension, NbConstraints, NbRays, NbEq, NbBid;
40 Value **Constraint;
41 Value **Ray;
42 Value *p_Init;
43 int p_Init_size;
44 struct polyhedron *next;
45 } Polyhedron;
46 \end{verbatim}
47 The attribute \ai[\tt]{Dimension} is the dimension
48 of the ambient space, i.e., the number of variables.
49 The attributes \ai[\tt]{Constraint}
50 and \ai[\tt]{Ray} point to two-dimensional arrays
51 of constraints and generators, respectively.
52 The number of rows is stored in
53 \ai[\tt]{NbConstraints} and
54 \ai[\tt]{NbRays}, respectively.
55 The number of columns in both arrays is equal
56 to \verb!1+Dimension+1!.
57 The first column of \ai[\tt]{Constraint} is either
58 $0$ or $1$ depending on whether the constraint
59 is an equality ($0$) or an inequality ($1$).
60 The number of equalities is stored in \ai[\tt]{NbEq}.
61 If the constraint is $\sp a x + c \ge 0$, then
62 the next columns contain the coefficients $a_i$
63 and the final column contains the constant $c$.
64 The first column of \ai[\tt]{Ray} is either
65 $0$ or $1$ depending on whether the generator
66 is a line ($0$) or a vertex or ray ($1$).
67 The number of lines is stored in \ai[\tt]{NbBid}.
68 Let $d$ be the \ac{lcm} of the denominators of the coordinates
69 of a vertex $\vec v$, then the next columns contain
70 $d v_i$ and the final column contains $d$.
71 For a ray, the final column contains $0$.
72 The field \ai[\tt]{next} points to the next polyhedron in
73 the union of polyhedra.
74 It is \verb+0+ if this is the last (or only) polyhedron in the union.
75 For more information on this structure, we refer to \shortciteN{Wilde1993}.
77 Quasi-polynomials are represented using the
78 \ai[\tt]{evalue} and \ai[\tt]{enode} structures.
79 \begin{verbatim}
80 typedef enum { polynomial, periodic, evector } enode_type;
82 typedef struct _evalue {
83 Value d; /* denominator */
84 union {
85 Value n; /* numerator (if denominator != 0) */
86 struct _enode *p; /* pointer (if denominator == 0) */
87 } x;
88 } evalue;
90 typedef struct _enode {
91 enode_type type; /* polynomial or periodic or evector */
92 int size; /* number of attached pointers */
93 int pos; /* parameter position */
94 evalue arr[1]; /* array of rational/pointer */
95 } enode;
96 \end{verbatim}
97 If the field \ai[\tt]{d} of an \ai[\tt]{evalue} is zero, then
98 the \ai[\tt]{evalue} is a placeholder for a pointer to
99 an \ai[\tt]{enode}, stored in \ai[\tt]{x.p}.
100 Otherwise, the \ai[\tt]{evalue} is a rational number with
101 numerator \ai[\tt]{x.n} and denominator \ai[\tt]{d}.
102 An \ai[\tt]{enode} is either a \ai[\tt]{polynomial}
103 or a \ai[\tt]{periodic}, depending on the value
104 of \ai[\tt]{type}.
105 The length of the array \ai[\tt]{arr} is stored in \ai[\tt]{size}.
106 For a \ai[\tt]{polynomial}, \ai[\tt]{arr} contains the coefficients.
107 For a \ai[\tt]{periodic}, it contains the values for the different
108 residue classes modulo the parameter indicated by \ai[\tt]{pos}.
109 For a polynomial, \ai[\tt]{pos} refers to the variable
110 of the polynomial.
111 The value of \ai[\tt]{pos} is \verb+1+ for the first parameter.
112 That is, if the value of \ai[\tt]{pos} is \verb+1+ and the first
113 parameter is $p$, and if the length of the array is $l$,
114 then in case it is a polynomial, the
115 \ai[\tt]{enode} represents
117 \verb+arr[0]+ + \verb+arr[1]+ p + \verb+arr[2]+ p^2 + \cdots +
118 \verb+arr[l-1]+ p^{l-1}
121 If it is a periodic, then it represents
123 \left[
124 \verb+arr[0]+, \verb+arr[1]+, \verb+arr[2]+, \ldots,
125 \verb+arr[l-1]+
126 \right]_p
129 Note that the elements of a \ai[\tt]{periodic} may themselves
130 be other \ai[\tt]{periodic}s or even \ai[\tt]{polynomial}s.
131 In our library, we only allow the elements of a \ai[\tt]{periodic}
132 to be other \ai[\tt]{periodic}s or rational numbers.
133 The chambers and their corresponding quasi-polynomial are
134 stored in \ai[\tt]{Enumeration} structures.
135 \begin{verbatim}
136 typedef struct _enumeration {
137 Polyhedron *ValidityDomain; /* constraints on the parameters */
138 evalue EP; /* dimension = combined space */
139 struct _enumeration *next; /* Ehrhart Polynomial,
140 corresponding to parameter
141 values inside the domain
142 ValidityDomain above */
143 } Enumeration;
144 \end{verbatim}
145 For more information on these structures, we refer to \shortciteN{Loechner1999}.
147 \begin{example}
148 Figure~\ref{f:Loechner} is a skillful reconstruction
149 of Figure~2 from \shortciteN{Loechner1999}.
150 It shows the contents of the \ai[\tt]{enode} structures
151 representing the quasi-polynomial
153 [1,2]_p p^2 + 3 p + \frac 5 2
156 \begin{figure}
157 \begin{xy}
158 \POS(0,0)*!UL{\hbox{
160 \begin{tabular}{|c|c|c|}
161 \hline
162 \multicolumn{2}{|c|}{type} & polynomial \\
163 \hline
164 \multicolumn{2}{|c|}{size} & 3 \\
165 \hline
166 \multicolumn{2}{|c|}{pos} & 1 \\
167 \hline
168 \smash{\lower 6.25pt\hbox{arr[0]}} & d & 2 \\
169 \cline{2-3}
170 & x.n & 5 \\
171 \hline
172 \smash{\lower 6.25pt\hbox{arr[1]}} & d & 1 \\
173 \cline{2-3}
174 & x.n & 3 \\
175 \hline
176 \smash{\lower 6.25pt\hbox{arr[2]}} & d & 0 \\
177 \cline{2-3}
178 & x.p & \\
179 \hline
180 \end{tabular}
182 }="box1"
183 +DR*!DR\hbox{\strut\hskip 1.5\tabcolsep\phantom{\tt polynomial}\hskip 1.5\tabcolsep}+C="a"
184 \POS(60,-15)*!UL{\hbox{
186 \begin{tabular}{|c|c|c|}
187 \hline
188 \multicolumn{2}{|c|}{type} & periodic \\
189 \hline
190 \multicolumn{2}{|c|}{size} & 2 \\
191 \hline
192 \multicolumn{2}{|c|}{pos} & 1 \\
193 \hline
194 \smash{\lower 6.25pt\hbox{arr[0]}} & d & 1 \\
195 \cline{2-3}
196 & x.n & 1 \\
197 \hline
198 \smash{\lower 6.25pt\hbox{arr[1]}} & d & 1 \\
199 \cline{2-3}
200 & x.n & 2 \\
201 \hline
202 \end{tabular}
204 }="box2"
205 +UL+<0.5\tabcolsep,0pt>*!UL\hbox{\strut}+CL="b"
206 \POS"a"\ar@(r,l) "b"
207 \POS"box1"+UC*++!D\hbox{\tt enode}
208 \POS"box2"+UC*++!D\hbox{\tt enode}
209 \end{xy}
210 \caption{The quasi-polynomial $[1,2]_p p^2 + 3 p + \frac 5 2$.}
211 \label{f:Loechner}
212 \end{figure}
213 \end{example}
215 \subsection{Options}
216 \label{a:options}
218 The \ai[\tt]{barvinok\_options} structure contains various
219 options that influence the behavior of the library.
221 \begin{verbatim}
222 struct barvinok_options {
223 struct barvinok_stats *stats;
225 /* PolyLib options */
226 unsigned MaxRays;
228 /* NTL options */
229 /* LLL reduction parameter delta=LLL_a/LLL_b */
230 long LLL_a;
231 long LLL_b;
233 /* barvinok options */
234 #define BV_SPECIALIZATION_BF 2
235 #define BV_SPECIALIZATION_DF 1
236 #define BV_SPECIALIZATION_RANDOM 0
237 #define BV_SPECIALIZATION_TODD 3
238 int incremental_specialization;
240 unsigned long max_index;
241 int primal;
242 int lookup_table;
243 int count_sample_infinite;
245 int try_Delaunay_triangulation;
247 #define BV_APPROX_SIGN_NONE 0
248 #define BV_APPROX_SIGN_APPROX 1
249 #define BV_APPROX_SIGN_LOWER 2
250 #define BV_APPROX_SIGN_UPPER 3
251 int polynomial_approximation;
252 #define BV_APPROX_NONE 0
253 #define BV_APPROX_DROP 1
254 #define BV_APPROX_SCALE 2
255 #define BV_APPROX_VOLUME 3
256 #define BV_APPROX_BERNOULLI 4
257 int approximation_method;
258 #define BV_APPROX_SCALE_FAST (1 << 0)
259 #define BV_APPROX_SCALE_NARROW (1 << 1)
260 #define BV_APPROX_SCALE_NARROW2 (1 << 2)
261 #define BV_APPROX_SCALE_CHAMBER (1 << 3)
262 int scale_flags;
263 #define BV_VOL_LIFT 0
264 #define BV_VOL_VERTEX 1
265 #define BV_VOL_BARYCENTER 2
266 int volume_triangulate;
268 /* basis reduction options */
269 #define BV_GBR_GLPK 1
270 #define BV_GBR_CDD 2
271 int gbr_lp_solver;
273 #define BV_LP_POLYLIB 0
274 #define BV_LP_GLPK 1
275 #define BV_LP_CDD 2
276 #define BV_LP_CDDF 3
277 int lp_solver;
279 #define BV_HULL_GBR 0
280 #define BV_HULL_HILBERT 1
281 int integer_hull;
284 struct barvinok_options *barvinok_options_new_with_defaults();
285 \end{verbatim}
287 The function \ai[\tt]{barvinok\_options\_new\_with\_defaults}
288 can be used to create a \ai[\tt]{barvinok\_options} structure
289 with default values.
291 \begin{itemize}
292 \item \PolyLib/ options
294 \begin{itemize}
296 \item \ai[\tt]{MaxRays}
298 The value of \ai[\tt]{MaxRays} is passed to various \PolyLib/
299 functions and defines the
300 maximum size of a table used in the \ai{double description} computation
301 in the \PolyLib/ function \ai[\tt]{Chernikova}.
302 In earlier versions of \PolyLib/,
303 this parameter had to be conservatively set
304 to a high number to ensure successful operation,
305 resulting in significant memory overhead.
306 Our change to allow this table to grow
307 dynamically is available in recent versions of \PolyLib/.
308 In these versions, the value no longer indicates the maximal
309 table size, but rather the size of the initial allocation.
310 This value may be set to \verb+0+ or left as set
311 by \ai[\tt]{barvinok\_options\_new\_with\_defaults}.
313 \end{itemize}
315 \item \ai[\tt]{NTL} options
317 \begin{itemize}
319 \item \ai[\tt]{LLL\_a}
320 \item \ai[\tt]{LLL\_b}
322 The values used for the \ai{reduction parameter}
323 in the call to \ai[\tt]{NTL}'s implementation of \indac{LLL}.
325 \end{itemize}
327 \item \ai[\tt]{barvinok} specific options
329 \begin{itemize}
331 \item \ai[\tt]{incremental\_specialization}
333 Selects the \ai{specialization} algorithm to be used.
334 If set to {\tt 0} then a direct specialization is performed
335 using a random vector.
336 Value {\tt 1} selects a depth first incremental specialization,
337 while value {\tt 2} selects a breadth first incremental specialization.
338 For more information we refer to~\citeN[Section~4.4.3]{Verdoolaege2005PhD}.
340 \end{itemize}
342 \end{itemize}
344 \subsection{Data Structures for Quasi-polynomials}
345 \label{a:data}
347 Internally, we do not represent our \ai{quasi-polynomial}s
348 as step-polynomials, but instead as polynomials of
349 fractional parts of degree-$1$ polynomials.
350 However, we also allow our quasi-polynomials to be represented
351 as polynomials with periodic numbers for coefficients,
352 similarly to \shortciteN{Loechner1999}.
353 By default, the current version of \barvinok/ uses
354 \ai[\tt]{fractional}s, but this can be changed through
355 the \ai[\tt]{--disable-fractional} configure option.
356 When this option is specified, the periodic numbers
357 are represented as
358 an explicit enumeration using the \ai[\tt]{periodic} type.
359 A quasi-polynomial based on fractional
360 parts can also be converted to an actual step-polynomial
361 using \ai[\tt]{evalue\_frac2floor}, but this is not fully
362 supported yet.
364 For reasons of compatibility,%
365 \footnote{Also known as laziness.}
366 we shoehorned our representations for piecewise quasi-polynomials
367 into the existing data structures.
368 To this effect, we introduced four new types,
369 \ai[\tt]{fractional}, \ai[\tt]{relation},
370 \ai[\tt]{partition} and \ai[\tt]{flooring}.
371 \begin{verbatim}
372 typedef enum { polynomial, periodic, evector, fractional,
373 relation, partition, flooring } enode_type;
374 \end{verbatim}
375 The field \ai[\tt]{pos} is not used in most of these
376 additional types and is therefore set to \verb+-1+.
378 The types \ai[\tt]{fractional} and \ai[\tt]{flooring}
379 represent polynomial expressions in a fractional part or a floor respectively.
380 The generator is stored in \verb+arr[0]+, while the
381 coefficients are stored in the remaining array elements.
382 That is, an \ai[\tt]{enode} of type \ai[\tt]{fractional}
383 represents
385 \verb+arr[1]+ + \verb+arr[2]+ \{\verb+arr[0]+\} +
386 \verb+arr[3]+ \{\verb+arr[0]+\}^2 + \cdots +
387 \verb+arr[l-1]+ \{\verb+arr[0]+\}^{l-2}
390 An \ai[\tt]{enode} of type \ai[\tt]{flooring}
391 represents
393 \verb+arr[1]+ + \verb+arr[2]+ \lfloor\verb+arr[0]+\rfloor +
394 \verb+arr[3]+ \lfloor\verb+arr[0]+\rfloor^2 + \cdots +
395 \verb+arr[l-1]+ \lfloor\verb+arr[0]+\rfloor^{l-2}
399 \begin{example}
400 The internal representation of the quasi-polynomial
401 $$\left(1+2 \left\{\frac p 2\right\}\right) p^2 + 3 p + \frac 5 2$$
402 is shown in Figure~\ref{f:fractional}.
404 \begin{figure}
405 \begin{xy}
406 \POS(0,0)*!UL{\hbox{
408 \begin{tabular}{|c|c|c|}
409 \hline
410 \multicolumn{2}{|c|}{type} & polynomial \\
411 \hline
412 \multicolumn{2}{|c|}{size} & 3 \\
413 \hline
414 \multicolumn{2}{|c|}{pos} & 1 \\
415 \hline
416 \smash{\lower 6.25pt\hbox{arr[0]}} & d & 2 \\
417 \cline{2-3}
418 & x.n & 5 \\
419 \hline
420 \smash{\lower 6.25pt\hbox{arr[1]}} & d & 1 \\
421 \cline{2-3}
422 & x.n & 3 \\
423 \hline
424 \smash{\lower 6.25pt\hbox{arr[2]}} & d & 0 \\
425 \cline{2-3}
426 & x.p & \\
427 \hline
428 \end{tabular}
430 }="box1"
431 +DR*!DR\hbox{\strut\hskip 1.5\tabcolsep\phantom{\tt polynomial}\hskip 1.5\tabcolsep}+C="a"
432 \POS(60,0)*!UL{\hbox{
434 \begin{tabular}{|c|c|c|}
435 \hline
436 \multicolumn{2}{|c|}{type} & fractional \\
437 \hline
438 \multicolumn{2}{|c|}{size} & 3 \\
439 \hline
440 \multicolumn{2}{|c|}{pos} & -1 \\
441 \hline
442 \smash{\lower 6.25pt\hbox{arr[0]}} & d & 0 \\
443 \cline{2-3}
444 & x.p & \\
445 \hline
446 \smash{\lower 6.25pt\hbox{arr[1]}} & d & 1 \\
447 \cline{2-3}
448 & x.n & 1 \\
449 \hline
450 \smash{\lower 6.25pt\hbox{arr[2]}} & d & 1 \\
451 \cline{2-3}
452 & x.n & 2 \\
453 \hline
454 \end{tabular}
456 }="box2"
457 +UL+<0.5\tabcolsep,0pt>*!UL\hbox{\strut}+CL="b"
458 \POS"a"\ar@(r,l) "b"
459 \POS"box2"+UR*!UR{\hbox{
461 \begin{tabular}{|c|}
462 \hline
463 fractional \\
464 \hline
465 3 \\
466 \hline
467 -1 \\
468 \hline
469 0 \\
470 \hline
471 \end{tabular}
473 }+CD*!U{\strut}+C="c"
474 \POS(60,-50)*!UL{\hbox{
476 \begin{tabular}{|c|c|c|}
477 \hline
478 \multicolumn{2}{|c|}{type} & polynomial \\
479 \hline
480 \multicolumn{2}{|c|}{size} & 2 \\
481 \hline
482 \multicolumn{2}{|c|}{pos} & 1 \\
483 \hline
484 \smash{\lower 6.25pt\hbox{arr[0]}} & d & 1 \\
485 \cline{2-3}
486 & x.n & 0 \\
487 \hline
488 \smash{\lower 6.25pt\hbox{arr[1]}} & d & 2 \\
489 \cline{2-3}
490 & x.n & 1 \\
491 \hline
492 \end{tabular}
494 }="box3"
495 +UR-<0.8\tabcolsep,0pt>*!UR\hbox{\strut}+CR="d"
496 \POS"c"\ar@(r,r) "d"
497 \POS"box1"+UC*++!D\hbox{\tt enode}
498 \POS"box2"+UC*++!D\hbox{\tt enode}
499 \POS"box3"+UC*++!D\hbox{\tt enode}
500 \end{xy}
501 \caption{The quasi-polynomial
502 $\left(1+2 \left\{\frac p 2\right\}\right) p^2 + 3 p + \frac 5 2$.}
503 \label{f:fractional}
504 \end{figure}
506 \end{example}
508 The \ai[\tt]{relation} type is used to represent \ai{stride}s.
509 In particular, if the value of \ai[\tt]{size} is 2, then
510 the value of a \ai[\tt]{relation} is (in pseudo-code):
511 \begin{verbatim}
512 (value(arr[0]) == 0) ? value(arr[1]) : 0
513 \end{verbatim}
514 If the size is 3, then the value is:
515 \begin{verbatim}
516 (value(arr[0]) == 0) ? value(arr[1]) : value(arr[2])
517 \end{verbatim}
518 The type of \verb+arr[0]+ is typically \ai[\tt]{fractional}.
520 Finally, the \ai[\tt]{partition} type is used to
521 represent piecewise quasi-polynomials.
522 We prefer to encode this information inside \ai[\tt]{evalue}s
523 themselves
524 rather than using \ai[\tt]{Enumeration}s since we want
525 to perform the same kinds of operations on both quasi-polynomials
526 and piecewise quasi-polynomials.
527 An \ai[\tt]{enode} of type \ai[\tt]{partition} may not be nested
528 inside another \ai[\tt]{enode}.
529 The size of the array is twice the number of ``chambers''.
530 Pointers to chambers are stored in the even slots,
531 whereas pointer to the associated quasi-polynomials
532 are stored in the odd slots.
533 To be able to store pointers to chambers, the
534 definition of \ai[\tt]{evalue} was changed as follows.
535 \begin{verbatim}
536 typedef struct _evalue {
537 Value d; /* denominator */
538 union {
539 Value n; /* numerator (if denominator > 0) */
540 struct _enode *p; /* pointer (if denominator == 0) */
541 Polyhedron *D; /* domain (if denominator == -1) */
542 } x;
543 } evalue;
544 \end{verbatim}
545 Note that we allow a ``chamber'' to be a union of polyhedra
546 as discussed in \citeN[Section~4.5.1]{Verdoolaege2005PhD}.
547 Chambers with extra variables, i.e., those of
548 \citeN[Section~4.6.5]{Verdoolaege2005PhD},
549 are only partially supported.
550 The field \ai[\tt]{pos} is set to the actual dimension,
551 i.e., the number of parameters.
553 \subsection{Operations on Quasi-polynomials}
554 \label{a:operations}
556 In this section we discuss some of the more important
557 operations on \ai[\tt]{evalue}s provided by the
558 \barvinok/ library.
559 Some of these operations are extensions
560 of the functions from \PolyLib/ with the same name.
562 Most of these operation are also provided by \isl/ on
563 \ai[\tt]{isl\_pw\_qpolynomial}s, which are set to replace
564 \ai[\tt]{evalue}s. Use \ai[\tt]{isl\_pw\_qpolynomial\_from\_evalue} to convert
565 from \ai[\tt]{evalue}s to \ai[\tt]{isl\_pw\_qpolynomial}s.
566 \begin{verbatim}
567 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_evalue(
568 __isl_take isl_space *dim, const evalue *e);
569 \end{verbatim}
571 \begin{verbatim}
572 void eadd(const evalue *e1,evalue *res);
573 void emul(const evalue *e1, evalue *res);
574 \end{verbatim}
575 The functions \ai[\tt]{eadd} and \ai[\tt]{emul} takes
576 two (pointers to) \ai[\tt]{evalue}s \verb+e1+ and \verb+res+
577 and computes their sum and product respectively.
578 The result is stored in \verb+res+, overwriting (and deallocating)
579 the original value of \verb+res+.
580 It is an error if exactly one of
581 the arguments of \ai[\tt]{eadd} is of type \ai[\tt]{partition}
582 (unless the other argument is \verb+0+).
583 The addition and multiplication operations are described
584 in \citeN[Section~4.5.1]{Verdoolaege2005PhD}
585 and~\citeN[Section~4.5.2]{Verdoolaege2005PhD}
586 respectively.
588 The function \ai[\tt]{eadd} is an extension of the function
589 \ai[\tt]{new\_eadd} from \shortciteN{Seghir2002}.
590 Apart from supporting the additional types from Section~\ref{a:data},
591 the new version also additionally imposes an order on the nesting of
592 different \ai[\tt]{enode}s.
593 Without such an ordering, \ai[\tt]{evalue}s could be constructed
594 representing for example
596 (0 y^ 0 + ( 0 x^0 + 1 x^1 ) y^1 ) x^0 + (0 y^0 - 1 y^1) x^1
599 which is just a funny way of saying $0$.
601 \begin{verbatim}
602 void eor(evalue *e1, evalue *res);
603 \end{verbatim}
604 The function \ai[\tt]{eor} implements the \ai{union}
605 operation from \citeN[Section~4.5.3]{Verdoolaege2005PhD}. Both arguments
606 are assumed to correspond to indicator functions.
608 \begin{verbatim}
609 evalue *esum(evalue *E, int nvar);
610 evalue *evalue_sum(evalue *E, int nvar, unsigned MaxRays);
611 \end{verbatim}
612 The function \ai[\tt]{esum} has been superseded by
613 \ai[\tt]{evalue\_sum}.
614 The function \ai[\tt]{evalue\_sum} performs the summation
615 operation from \citeN[Section~4.5.4]{Verdoolaege2005PhD}.
616 The piecewise step-polynomial represented by \verb+E+ is summated
617 over its first \verb+nvar+ variables.
618 Note that \verb+E+ must be zero or of type \ai[\tt]{partition}.
619 The function returns the result in a newly allocated
620 \ai[\tt]{evalue}.
621 Note also that \verb+E+ needs to have been converted
622 from \ai[\tt]{fractional}s to \ai[\tt]{flooring}s using
623 the function \ai[\tt]{evalue\_frac2floor}.
624 \begin{verbatim}
625 void evalue_frac2floor(evalue *e);
626 \end{verbatim}
627 This function also ensures that the arguments of the
628 \ai[\tt]{flooring}s are positive in the relevant chambers.
629 It currently assumes that the argument of each
630 \ai[\tt]{fractional} in the original \ai[\tt]{evalue}
631 has a minimum in the corresponding chamber.
633 \begin{verbatim}
634 double compute_evalue(const evalue *e, Value *list_args);
635 Value *compute_poly(Enumeration *en,Value *list_args);
636 evalue *evalue_eval(const evalue *e, Value *values);
637 \end{verbatim}
638 The functions \ai[\tt]{compute\_evalue},
639 \ai[\tt]{compute\_poly} and
640 \ai[\tt]{evalue\_eval}
641 evaluate a (piecewise) quasi-polynomial
642 at a certain point. The argument \verb+list_args+
643 points to an array of \ai[\tt]{Value}s that is assumed
644 to be long enough.
645 The \verb+double+ return value of \ai[\tt]{compute\_evalue}
646 is inherited from \PolyLib/.
648 \begin{verbatim}
649 void print_evalue(FILE *DST, const evalue *e, char **pname);
650 \end{verbatim}
651 The function \ai[\tt]{print\_evalue} dumps a human-readable
652 representation to the stream pointed to by \verb+DST+.
653 The argument \verb+pname+ points
654 to an array of character strings representing the parameter names.
655 The array is assumed to be long enough.
657 \begin{verbatim}
658 int eequal(const evalue *e1, const evalue *e2);
659 \end{verbatim}
660 The function \ai[\tt]{eequal} return true (\verb+1+) if its
661 two arguments are structurally identical.
662 I.e., it does {\em not\/} check whether the two
663 (piecewise) quasi-polynomial represent the same function.
665 \begin{verbatim}
666 void reduce_evalue (evalue *e);
667 \end{verbatim}
668 The function \ai[\tt]{reduce\_evalue} performs some
669 simplifications on \ai[\tt]{evalue}s.
670 Here, we only describe the simplifications that are directly
671 related to the internal representation.
672 Some other simplifications are explained in
673 \citeN[Section~4.7.2]{Verdoolaege2005PhD}.
674 If the highest order coefficients of a \ai[\tt]{polynomial},
675 \ai[\tt]{fractional} or \ai[\tt]{flooring} are zero (possibly
676 after some other simplifications), then the size of the array
677 is reduced. If only the constant term remains, i.e.,
678 the size is reduced to $1$ for \ai[\tt]{polynomial} or to $2$
679 for the other types, then the whole node is replaced by
680 the constant term.
681 Additionally, if the argument of a \ai[\tt]{fractional}
682 has been reduced to a constant, then the whole node
683 is replaced by its partial evaluation.
684 A \ai[\tt]{relation} is similarly reduced if its second
685 branch or both its branches are zero.
686 Chambers with zero associated quasi-polynomials are
687 discarded from a \ai[\tt]{partition}.
689 \subsection{Generating Functions}
691 The representation of \rgf/s uses
692 some basic types from the \ai[\tt]{NTL} library \shortcite{NTL}
693 for representing arbitrary precision integers
694 (\ai[\tt]{ZZ})
695 as well as vectors (\ai[\tt]{vec\_ZZ}) and matrices (\ai[\tt]{mat\_ZZ})
696 of such integers.
697 We further introduces a type \ai[\tt]{QQ} for representing a rational
698 number and use vectors (\ai[\tt]{vec\_QQ}) of such numbers.
699 \begin{verbatim}
700 struct QQ {
701 ZZ n;
702 ZZ d;
705 NTL_vector_decl(QQ,vec_QQ);
706 \end{verbatim}
708 Each term in a \rgf/ is represented by a \ai[\tt]{short\_rat}
709 structure.
710 \begin{verbatim}
711 struct short_rat {
712 struct {
713 /* rows: terms in numerator */
714 vec_QQ coeff;
715 mat_ZZ power;
716 } n;
717 struct {
718 /* rows: factors in denominator */
719 mat_ZZ power;
720 } d;
722 \end{verbatim}
723 The fields \ai[\tt]{n} and \ai[\tt]{d} represent the
724 numerator and the denominator respectively.
725 Note that in our implementation we combine terms
726 with the same denominator.
727 In the numerator, each element of \ai[\tt]{coeff} and each row of \ai[\tt]{power}
728 represents a single such term.
729 The vector \ai[\tt]{coeff} contains the rational coefficients
730 $\alpha_i$ of each term.
731 The columns of \ai[\tt]{power} correspond to the powers
732 of the variables.
733 In the denominator, each row of \ai[\tt]{power}
734 corresponds to the power $\vec b_{ij}$ of a
735 factor in the denominator.
737 \begin{example}
738 Figure~\ref{fig:rat}
739 shows the internal representation of
741 \frac{\frac 3 2 \, x_0^2 x_1^3 + 2 \, x_0^5 x_1^{-7}}
742 { (1 - x_0 x_1^{-3}) (1 - x_1^2)}
746 \begin{figure}
747 \begin{center}
748 \begin{minipage}{0cm}
749 \begin{xy}
750 *\hbox{
752 \begin{tabular}{|c|c|c|}
753 \hline
754 n.coeff & 3 & 2 \\
755 \cline{2-3}
756 & 2 & 1 \\
757 \hline
758 n.power & 2 & 3 \\
759 \cline{2-3}
760 & 5 & -7 \\
761 \hline
762 d.power & 1 & -3 \\
763 \cline{2-3}
764 & 0 & 2 \\
765 \hline
766 \end{tabular}
767 }+UC*++!D\hbox{\tt short\_rat}
768 \end{xy}
769 \end{minipage}
770 \end{center}
771 \caption{Representation of
773 \left(\frac 3 2 \, x_0^2 x_1^3 + 2 \, x_0^5 x_1^{-7}\right)
774 / \left( (1 - x_0 x_1^{-3}) (1 - x_1^2)\right)
776 \label{fig:rat}
777 \end{figure}
779 \end{example}
781 The whole \rgf/ is represented by a \ai[\tt]{gen\_fun}
782 structure.
783 \begin{verbatim}
784 typedef std::set<short_rat *,
785 short_rat_lex_smaller_denominator > short_rat_list;
787 struct gen_fun {
788 short_rat_list term;
789 Polyhedron *context;
791 void add(const QQ& c, const vec_ZZ& num, const mat_ZZ& den);
792 void add(short_rat *r);
793 void add(const QQ& c, const gen_fun *gf,
794 barvinok_options *options);
795 void substitute(Matrix *CP);
796 gen_fun *Hadamard_product(const gen_fun *gf,
797 barvinok_options *options);
798 void print(std::ostream& os,
799 unsigned int nparam, char **param_name) const;
800 operator evalue *() const;
801 ZZ coefficient(Value* params, barvinok_options *options) const;
802 void coefficient(Value* params, Value* c) const;
804 gen_fun(Polyhedron *C);
805 gen_fun(Value c);
806 gen_fun(const gen_fun *gf);
807 ~gen_fun();
809 \end{verbatim}
810 A new \ai[\tt]{gen\_fun} can be constructed either as empty \rgf/ (possibly
811 with a given context \verb+C+), as a copy of an existing \rgf/ \verb+gf+, or as
812 constant \rgf/ with value for the constant term specified by \verb+c+.
814 The first \ai[\tt]{gen\_fun::add} method adds a new term to the \rgf/,
815 described by the coefficient \verb+c+, the numerator \verb+num+ and the
816 denominator \verb+den+.
817 It makes all powers in the denominator lexico-positive,
818 orders them in lexicographical order and inserts the new
819 term in \ai[\tt]{term} according to the lexicographical
820 order of the combined powers in the denominator.
821 The second \ai[\tt]{gen\_fun::add} method adds \verb+c+ times \verb+gf+
822 to the \rgf/.
824 The method \ai[\tt]{gen\_fun::operator evalue *} performs
825 the conversion from \rgf/ to \psp/ explained in
826 \citeN[Section~4.5.5]{Verdoolaege2005PhD}.
827 The \ai[\tt]{Polyhedron} \ai[\tt]{context} is the superset
828 of all points where the enumerator is non-zero used during this conversion,
829 i.e., it is the set $Q$ from \citeN[Equation~4.31]{Verdoolaege2005PhD}.
830 If \ai[\tt]{context} is \verb+NULL+ the maximal
831 allowed context is assumed, i.e., the maximal
832 region with lexico-positive rays.
834 The method \ai[\tt]{gen\_fun::coefficient} computes the coefficient
835 of the term with power given by \verb+params+ and stores the result
836 in \verb+c+.
837 This method performs essentially the same computations as
838 \ai[\tt]{gen\_fun::operator evalue *}, except that it adds extra
839 equality constraints based on the specified values for the power.
841 The method \ai[\tt]{gen\_fun::substitute} performs the
842 \ai{monomial substitution} specified by the homogeneous matrix \verb+CP+
843 that maps a set of ``\ai{compressed parameter}s'' \shortcite{Meister2004PhD}
844 to the original set of parameters.
845 That is, if we are given a \rgf/ $G(\vec z)$ that encodes the
846 explicit function $g(\vec i')$, where $\vec i'$ are the coordinates of
847 the transformed space, and \verb+CP+ represents the map
848 $\vec i = A \vec i' + \vec a$ back to the original space with coordinates $\vec i$,
849 then this method transforms the \rgf/ to $F(\vec x)$ encoding the
850 same explicit function $f(\vec i)$, i.e.,
851 $$f(\vec i) = f(A \vec i' + \vec a) = g(\vec i ').$$
852 This means that the coefficient of the term
853 $\vec x^{\vec i} = \vec x^{A \vec i' + \vec a}$ in $F(\vec x)$ should be equal to the
854 coefficient of the term $\vec z^{\vec i'}$ in $G(\vec z)$.
855 In other words, if
857 G(\vec z) =
858 \sum_i \epsilon_i \frac{\vec z^{\vec v_i}}{\prod_j (1-\vec z^{\vec b_{ij}})}
860 then
862 F(\vec x) =
863 \sum_i \epsilon_i \frac{\vec x^{A \vec v_i + \vec a}}
864 {\prod_j (1-\vec x^{A \vec b_{ij}})}
868 The method \ai[\tt]{gen\_fun::Hadamard\_product} computes the
869 \ai{Hadamard product} of the current \rgf/ with the \rgf/ \verb+gf+,
870 as explained in \citeN[Section~4.5.2]{Verdoolaege2005PhD}.
872 \subsection{Counting Functions}
873 \label{a:counting:functions}
875 Our library provides essentially three different counting functions:
876 one for non-parametric polytopes, one for parametric polytopes
877 and one for parametric sets with existential variables.
878 The old versions of these functions have a ``\ai[\tt]{MaxRays}''
879 argument, while the new versions have a more general
880 \ai[\tt]{barvinok\_options} argument.
881 For more information on \ai[\tt]{barvinok\_options}, see Section~\ref{a:options}.
883 \begin{verbatim}
884 void barvinok_count(Polyhedron *P, Value* result,
885 unsigned NbMaxCons);
886 void barvinok_count_with_options(Polyhedron *P, Value* result,
887 struct barvinok_options *options);
888 \end{verbatim}
889 The function \ai[\tt]{barvinok\_count} or
890 \ai[\tt]{barvinok\_count\_with\_options} enumerates the non-parametric
891 polytope \verb+P+ and returns the result in the \ai[\tt]{Value}
892 pointed to by \verb+result+, which needs to have been allocated
893 and initialized.
894 If \verb+P+ is a union, then only the first set in the union will
895 be taken into account.
896 For the meaning of the argument \verb+NbMaxCons+, see
897 the discussion on \ai[\tt]{MaxRays} in Section~\ref{a:options}.
899 The function \ai[\tt]{barvinok\_enumerate} for enumerating
900 parametric polytopes was meant to be
901 a drop-in replacement of \PolyLib/'s \ai[\tt]{Polyhedron\_Enumerate}
902 function.
903 Unfortunately, the latter has been changed to
904 accept an extra argument in recent versions of \PolyLib/ as shown below.
905 \begin{verbatim}
906 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C,
907 unsigned MaxRays);
908 extern Enumeration *Polyhedron_Enumerate(Polyhedron *P,
909 Polyhedron *C, unsigned MAXRAYS, char **pname);
910 \end{verbatim}
911 The argument \verb+MaxRays+ has the same meaning as the argument
912 \verb+NbMaxCons+ above.
913 The argument \verb+P+ refers to the $(d+n)$-dimensional
914 polyhedron defining the parametric polytope.
915 The argument \verb+C+ is an $n$-dimensional polyhedron containing
916 extra constraints on the parameter space.
917 Its primary use is to indicate how many of the dimensions
918 in \verb+P+ refer to parameters as any constraint in \verb+C+
919 could equally well have been added to \verb+P+ itself.
920 Note that the dimensions referring to the parameters should
921 appear {\em last}.
922 If either \verb+P+ or \verb+C+ is a union,
923 then only the first set in the union will be taken into account.
924 The result is a newly allocated \ai[\tt]{Enumeration}.
925 As an alternative we also provide a function
926 (\ai[\tt]{barvinok\_enumerate\_ev} or
927 \ai[\tt]{barvinok\_enumerate\_with\_options}) that returns
928 an \ai[\tt]{evalue}.
929 \begin{verbatim}
930 evalue* barvinok_enumerate_ev(Polyhedron *P, Polyhedron* C,
931 unsigned MaxRays);
932 evalue* barvinok_enumerate_with_options(Polyhedron *P,
933 Polyhedron* C, struct barvinok_options *options);
934 \end{verbatim}
936 For enumerating parametric sets with existentially quantified variables,
937 we provide two functions:
938 \ai[\tt]{barvinok\_enumerate\_e},
940 \ai[\tt]{barvinok\_enumerate\_isl}.
941 \begin{verbatim}
942 evalue* barvinok_enumerate_e(Polyhedron *P,
943 unsigned exist, unsigned nparam, unsigned MaxRays);
944 evalue* barvinok_enumerate_e_with_options(Polyhedron *P,
945 unsigned exist, unsigned nparam,
946 struct barvinok_options *options);
947 evalue *barvinok_enumerate_isl(Polyhedron *P,
948 unsigned exist, unsigned nparam,
949 struct barvinok_options *options);
950 evalue *barvinok_enumerate_scarf(Polyhedron *P,
951 unsigned exist, unsigned nparam,
952 struct barvinok_options *options);
953 \end{verbatim}
954 The first function tries the simplification rules from
955 \citeN[Section~4.6.2]{Verdoolaege2005PhD} before resorting to the method
956 based on \indac{PIP} from \citeN[Section~4.6.3]{Verdoolaege2005PhD}.
957 The second function immediately applies the technique from
958 \citeN[Section~4.6.3]{Verdoolaege2005PhD}.
959 The argument \verb+exist+ refers to the number of existential variables,
960 whereas
961 the argument \verb+nparam+ refers to the number of parameters.
962 The order of the dimensions in \verb+P+ is:
963 counted variables first, then existential variables and finally
964 the parameters.
965 The function \ai[\tt]{barvinok\_enumerate\_scarf} performs the same
966 computation as the function \ai[\tt]{barvinok\_enumerate\_scarf\_series}
967 below, but produces an explicit representation instead of a generating function.
969 \begin{verbatim}
970 gen_fun * barvinok_series(Polyhedron *P, Polyhedron* C,
971 unsigned MaxRays);
972 gen_fun * barvinok_series_with_options(Polyhedron *P,
973 Polyhedron* C, barvinok_options *options);
974 gen_fun *barvinok_enumerate_e_series(Polyhedron *P,
975 unsigned exist, unsigned nparam,
976 barvinok_options *options);
977 gen_fun *barvinok_enumerate_scarf_series(Polyhedron *P,
978 unsigned exist, unsigned nparam,
979 barvinok_options *options);
980 \end{verbatim}
981 The function
982 \ai[\tt]{barvinok\_series} or
983 \ai[\tt]{barvinok\_series\_with\_options} enumerates parametric polytopes
984 in the form of a \rgf/.
985 The polyhedron \verb+P+ is assumed to have only
986 revlex-positive rays.
988 The function \ai[\tt]{barvinok\_enumerate\_e\_series} computes a
989 generating function for the number of point in the parametric set
990 defined by \verb+P+ with \verb+exist+ existentially quantified
991 variables using the \ai{projection theorem}, as explained
992 in \autoref{s:projection}.
993 The function \ai[\tt]{barvinok\_enumerate\_scarf\_series} computes a
994 generating function for the number of point in the parametric set
995 defined by \verb+P+ with \verb+exist+ existentially quantified
996 variables, which is assumed to be 2.
997 This function implements the technique of
998 \shortciteN{Scarf2006Neighborhood} using the \ai{neighborhood complex}
999 description of \shortciteN{Scarf1981indivisibilities:II}.
1000 It is currently restricted to problems with 3 or 4 constraints involving
1001 the existentially quantified variables.
1003 \subsection{Auxiliary Functions}
1005 In this section we briefly mention some auxiliary functions
1006 available in the \barvinok/ library.
1008 \begin{verbatim}
1009 void Polyhedron_Polarize(Polyhedron *P);
1010 \end{verbatim}
1011 The function \ai[\tt]{Polyhedron\_Polarize}
1012 polarizes its argument and is explained
1013 in \citeN[Section~4.4.2]{Verdoolaege2005PhD}.
1015 \begin{verbatim}
1016 int unimodular_complete(Matrix *M, int row);
1017 \end{verbatim}
1018 The function \ai[\tt]{unimodular\_complete} extends
1019 the first \verb+row+ rows of
1020 \verb+M+ with an integral basis of the orthogonal complement
1021 as explained in Section~\ref{s:completion}.
1022 Returns non-zero
1023 if the resulting matrix is unimodular\index{unimodular matrix}.
1025 \begin{verbatim}
1026 int DomainIncludes(Polyhedron *D1, Polyhedron *D2);
1027 \end{verbatim}
1028 The function \ai[\tt]{DomainIncludes} extends
1029 the function \ai[\tt]{PolyhedronIncludes}
1030 provided by \PolyLib/
1031 to unions of polyhedra.
1032 It checks whether every polyhedron in the union {\tt D2}
1033 is included in some polyhedron of {\tt D1}.
1035 \begin{verbatim}
1036 Polyhedron *DomainConstraintSimplify(Polyhedron *P,
1037 unsigned MaxRays);
1038 \end{verbatim}
1039 The value returned by
1040 \ai[\tt]{DomainConstraintSimplify} is a pointer to
1041 a newly allocated \ai[\tt]{Polyhedron} that contains the
1042 same integer points as its first argument but possibly
1043 has simpler constraints.
1044 Each constraint $ g \sp a x \ge c $
1045 is replaced by $ \sp a x \ge \ceil{ \frac c g } $,
1046 where $g$ is the \ac{gcd} of the coefficients in the original
1047 constraint.
1048 The \ai[\tt]{Polyhedron} pointed to by \verb+P+ is destroyed.
1050 \begin{verbatim}
1051 Polyhedron* Polyhedron_Project(Polyhedron *P, int dim);
1052 \end{verbatim}
1053 The function \ai[\tt]{Polyhedron\_Project} projects
1054 \verb+P+ onto its last \verb+dim+ dimensions.
1056 \begin{verbatim}
1057 Matrix *left_inverse(Matrix *M, Matrix **Eq);
1058 \end{verbatim}
1059 The \ai[\tt]{left\_inverse} function computes the left inverse
1060 of \verb+M+ as explained in Section~\ref{s:inverse}.
1062 \sindex{reduced}{basis}
1063 \sindex{generalized}{reduced basis}
1064 \begin{verbatim}
1065 Matrix *Polyhedron_Reduced_Basis(Polyhedron *P,
1066 struct barvinok_options *options);
1067 \end{verbatim}
1068 \ai[\tt]{Polyhedron\_Reduced\_Basis} computes
1069 a \ai{generalized reduced basis} of {\tt P}, which
1070 is assumed to be a polytope, using the algorithm
1071 of~\shortciteN{Cook1993implementation}.
1072 See \autoref{s:feasibility} for more information.
1073 The basis vectors are stored in the rows of the matrix returned.
1075 \begin{verbatim}
1076 Vector *Polyhedron_Sample(Polyhedron *P,
1077 struct barvinok_options *options);
1078 \end{verbatim}
1079 \ai[\tt]{Polyhedron\_Sample} returns an \ai{integer point} of {\tt P}
1080 or {\tt NULL} if {\tt P} contains no integer points.
1081 The integer point is found using the algorithm
1082 of~\shortciteN{Cook1993implementation} and uses
1083 \ai[\tt]{Polyhedron\_Reduced\_Basis} to compute the reduced bases.
1084 See \autoref{s:feasibility} for more information.