rename value_ macros to entier_ to avoid confusion with PolyLib's
[piplib.git] / include / piplib / piplib.h
bloba1a57f5cda55e570769b064cce5b6420edff7e79
1 /******************************************************************************
2 * PIP : Parametric Integer Programming *
3 ******************************************************************************
4 * piplib.h *
5 ******************************************************************************
6 * *
7 * Copyright Paul Feautrier, 1988-2005 *
8 * *
9 * This is free software; you can redistribute it and/or modify it under the *
10 * terms of the GNU General Public License as published by the Free Software *
11 * Foundation; either version 2 of the License, or (at your option) any later *
12 * version. *
13 * *
14 * This software is distributed in the hope that it will be useful, but *
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. *
18 * *
19 * You should have received a copy of the GNU General Public License along *
20 * with software; if not, write to the Free Software Foundation, Inc., *
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
22 * *
23 * Written by Cedric Bastoul *
24 * *
25 ******************************************************************************/
27 /* Premiere version du 18 septembre 2002. */
29 #if !defined(LINEAR_VALUE_IS_LONGLONG) && !defined(LINEAR_VALUE_IS_INT)
30 #if !defined(LINEAR_VALUE_IS_MP)
31 # error Please define LINEAR_VALUE_IS_* or #include polylib32.h or polylib64.h
32 #endif
33 #endif
35 #if defined(LINEAR_VALUE_IS_LONGLONG)
37 # define Entier long long
38 # define FORMAT "%lld"
39 # define VAL_UN 1LL
40 # define VAL_ZERO 0LL
42 #define VALUE_TO_INT(val) ((int)(val))
44 #elif defined(LINEAR_VALUE_IS_INT)
46 # define Entier long int
47 # define FORMAT "%ld"
48 # define VAL_UN 1L
49 # define VAL_ZERO 0L
51 #define VALUE_TO_INT(val) ((int)(val))
53 #elif defined(LINEAR_VALUE_IS_MP)
55 # include <gmp.h>
56 # define Entier mpz_t
57 # define FORMAT "%d"
58 # define GMP_INPUT_FORMAT "%lZd"
60 #define VALUE_TO_INT(val) ((int)mpz_get_si(val))
62 #endif
64 #if defined(LINEAR_VALUE_IS_MP)
66 #define entier_addto(ref,val1,val2) (mpz_add((ref),(val1),(val2)))
67 #define entier_assign(v1,v2) (mpz_set((v1),(v2)))
68 #define entier_clear(val) (mpz_clear((val)))
69 #define entier_divexact(d,v1,v2) (mpz_divexact((d),(v1),(v2)))
70 #define entier_gcd(g,v1,v2) (mpz_gcd((g),(v1),(v2)))
71 #define entier_init(val) (mpz_init((val)))
72 #define entier_init_set(v1,v2) (mpz_init_set((v1),(v2)))
73 #define entier_pmodulus(ref,val1,val2) (mpz_fdiv_r((ref),(val1),(val2)))
74 #define entier_oppose(ref,val) (mpz_neg((ref),(val)))
75 #define entier_set_si(val,i) (mpz_set_si((val),(i)))
76 #define entier_subtract(ref,val1,val2) (mpz_sub((ref),(val1),(val2)))
77 #define entier_eq(v1,v2) (mpz_cmp((v1),(v2)) == 0)
78 #define entier_ne(v1,v2) (mpz_cmp((v1),(v2)) != 0)
79 #define entier_notzero_p(val) (mpz_sgn(val) != 0)
80 #define entier_pos_p(val) (mpz_sgn(val) > 0)
82 #else
84 #define entier_addto(ref,val1,val2) ((ref) = (val1)+(val2))
85 #define entier_assign(v1,v2) ((v1) = (v2))
86 #define entier_clear(val) do { } while(0)
87 #define entier_divexact(d,v1,v2) ((d) = (v1) / (v2))
88 #define entier_gcd(g,v1,v2) ((g) = pgcd((v1),(v2)))
89 #define entier_init(val) do { } while(0)
90 #define entier_init_set(v1,v2) ((v1) = (v2))
91 #define entier_pmodulus(ref,val1,val2) ((ref) = mod((val1),(val2)))
92 #define entier_oppose(ref,val) ((ref) = -(val))
93 #define entier_set_si(val,i) ((val) = (Entier)(i))
94 #define entier_subtract(ref,val1,val2) ((ref) = (val1)-(val2))
95 #define entier_eq(v1,v2) ((v1) == (v2))
96 #define entier_ne(v1,v2) ((v1) != (v2))
97 #define entier_notzero_p(val) ((val) != 0)
98 #define entier_pos_p(val) ((val) > 0)
100 #endif
103 #ifndef PIPLIB_H
104 #define PIPLIB_H
105 #if defined(__cplusplus)
106 extern "C"
108 #endif
111 /* Structure PipMatrix :
112 * Structure de matrice au format PolyLib. Le premier element d'une ligne
113 * indique quand il vaut 1 que la ligne decrit une inequation de la forme
114 * p(x)>=0 et quand il vaut 0, que la ligne decrit une egalite de la forme
115 * p(x)=0. Le dernier element de chaque ligne correspond au coefficient
116 * constant.
118 struct pipmatrix
119 { unsigned NbRows, NbColumns ;
120 Entier **p ;
121 Entier *p_Init ;
122 int p_Init_size; /* Only for PolyLib compatibility under MP
123 * version: PolyLib makes sometimes
124 * overestimates on the size of the matrices,
125 * in order to go faster. Thus
126 * NbRows*NbColumns is not the number of
127 * allocated elements. With MP version, we
128 * have to think to mpz_clear() all the
129 * initialized elements before freing, then
130 * we need to know the number of allocated
131 * elements: p_Init_size.
134 typedef struct pipmatrix PipMatrix ;
137 /* Structure PipVector :
138 * Cette structure contient un Vector de 'nb_elements' la ieme composante de
139 * ce vecteur vaut the_vector[i]/the_deno[i].
141 struct pipvector
142 { int nb_elements ; /* Nombre d'elements du vecteur. */
143 Entier * the_vector ; /* Numerateurs du vecteur. */
144 Entier * the_deno ; /* Denominateurs du vecteur. */
146 typedef struct pipvector PipVector ;
149 /* Structure PipNewparm :
150 * Liste chainee de Newparm, les informations d'un newparm etant son rang, un
151 * vecteur de coefficients et un denominateur. Le newparm est egal a la division
152 * du vecteur par le denominateur.
154 struct pipnewparm
155 { int rank ; /* Rang du 'newparm'. */
156 PipVector * vector ; /* Le vector decrivant le newparm. */
157 Entier deno ; /* Denominateur du 'newparm'. */
158 struct pipnewparm * next ; /* Pointeur vers le newparm suivant. */
160 typedef struct pipnewparm PipNewparm ;
163 /* Structure PipList :
164 * Liste chainee de Vector.
166 struct piplist
167 { PipVector * vector ; /* Le vector contenant la partie de solution. */
168 struct piplist * next ; /* Pointeur vers l'element suivant. */
170 typedef struct piplist PipList ;
173 /* Structure pipquast :
174 * Arbre binaire. Conformement a la grammaire de sortie (voir mode d'emploi), un
175 * noeud de l'arbre des solutions debute par une liste de 'newparm'. Il continue
176 * ensuite soit par une 'list' (alors condition vaut null), soit par un 'if'
177 * (alors le champ condition contient la condition).
179 struct pipquast
180 { PipNewparm * newparm ; /* Les 'newparm'. */
181 PipList * list ; /* La 'list' si pas de 'if'. */
182 PipVector * condition ; /* La condition si 'if'. */
183 struct pipquast * next_then ; /* Noeud si condition et si verifiee. */
184 struct pipquast * next_else ; /* Noeud si condition et si non verifiee. */
185 struct pipquast * father ; /* Pointeur vers le quast pere. */
186 } ;
187 typedef struct pipquast PipQuast ;
190 /* Structure pipoptions:
191 * This structure contains each option that can be set to change the PIP
192 * behaviour.
194 struct pipoptions
195 { int Nq ; /* 1 if an integer solution is needed,
196 * 0 otherwise.
198 int Verbose ; /* -1 -> absolute silence,
199 * 0 -> relative silence,
200 * 1 -> information on cuts when an integer
201 * solution is needed,
202 * 2 -> information sur les pivots et les
203 * déterminants,
204 * 3 -> information on arrays,
205 * Each option include the preceding.
207 int Simplify ; /* Set to 1 to eliminate some trivial
208 * solutions, 0 otherwise.
210 int Deepest_cut ; /* Set to 1 to include deepest cut
211 * algorithm.
213 int Maximize; /* Set to 1 if maximum is needed. */
214 int Urs_parms; /* -1 -> all parameters may be negative
215 * 0 -> all parameters are non-negative
217 int Urs_unknowns; /* -1 -> all unknowns may be negative
218 * 0 -> all unknowns are non-negative
220 } ;
221 typedef struct pipoptions PipOptions ;
224 /* Prototypes des fonctions d'affichages des structures de la PipLib. */
225 void pip_matrix_print(FILE *, PipMatrix *) ;
226 void pip_vector_print(FILE *, PipVector *) ;
227 void pip_newparm_print(FILE * foo, PipNewparm *, int indent) ;
228 void pip_list_print(FILE * foo, PipList *, int indent) ;
229 void pip_quast_print(FILE *, PipQuast *, int) ;
232 /* Prototypes des fonctions de liberation memoire des structures de la PipLib.*/
233 void pip_matrix_free(PipMatrix *) ;
234 void pip_vector_free(PipVector *) ;
235 void pip_newparm_free(PipNewparm *) ;
236 void pip_list_free(PipList *) ;
237 void pip_quast_free(PipQuast *) ;
238 void pip_options_free(PipOptions *) ;
241 /* Prototypes des fonctions d'acquisition de matrices de contraintes et
242 * options.
244 PipMatrix * pip_matrix_alloc(unsigned, unsigned) ;
245 PipMatrix * pip_matrix_read(FILE *) ;
246 PipOptions * pip_options_init(void) ;
249 /* initialization of pip library */
250 void pip_init();
251 void pip_close();
254 /* Prototype de la fonction de resolution :
255 * pip_solve resoud le probleme qu'on lui passe en parametre, suivant les
256 * options elles aussi en parametre. Elle renvoie la solution sous forme
257 * d'un arbre de PipQuast. Parametres :
258 * - probleme :
259 * 1 PipMatrix : systeme des inequations definissant le domaine des inconnues,
260 * 2 PipMatrix : systeme des inequations satisfaites par les parametres,
261 * 3 int : column rank of the bignum, or negative value if there
262 * is no big parameter.
263 * 4 PipOptions : options for PIP.
265 PipQuast * pip_solve(PipMatrix *, PipMatrix *, int, PipOptions *) ;
267 #define SOL_SHIFT (1 << 0) /* Shift solution over -bigparam */
268 #define SOL_NEGATE (1 << 1) /* Negate solution */
269 #define SOL_REMOVE (1 << 2) /* Remove big parameter */
270 #define SOL_MAX (SOL_SHIFT | SOL_NEGATE)
271 /* Maximum was computed */
272 PipQuast *sol_quast_edit(int *i, PipQuast *father, int Bg, int Urs_p, int flags);
274 #if defined(__cplusplus)
276 #endif
277 #endif /* define PIPLIB_H */