isl_basic_set_swap_vars: finalize result
[isl.git] / isl_tab.h
blobf288c0c7105791a0641d2cb7741300bca559cdfb
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #ifndef ISL_TAB_H
11 #define ISL_TAB_H
13 #include "isl_lp.h"
14 #include "isl_map.h"
15 #include "isl_mat.h"
16 #include "isl_set.h"
18 struct isl_tab_var {
19 int index;
20 unsigned is_row : 1;
21 unsigned is_nonneg : 1;
22 unsigned is_zero : 1;
23 unsigned is_redundant : 1;
24 unsigned marked : 1;
25 unsigned frozen : 1;
26 unsigned negated : 1;
29 enum isl_tab_undo_type {
30 isl_tab_undo_bottom,
31 isl_tab_undo_empty,
32 isl_tab_undo_nonneg,
33 isl_tab_undo_redundant,
34 isl_tab_undo_freeze,
35 isl_tab_undo_zero,
36 isl_tab_undo_allocate,
37 isl_tab_undo_relax,
38 isl_tab_undo_bmap_ineq,
39 isl_tab_undo_bmap_eq,
40 isl_tab_undo_bmap_div,
41 isl_tab_undo_saved_basis,
42 isl_tab_undo_drop_sample,
43 isl_tab_undo_saved_samples,
44 isl_tab_undo_callback,
47 struct isl_tab_callback {
48 int (*run)(struct isl_tab_callback *cb);
51 union isl_tab_undo_val {
52 int var_index;
53 int *col_var;
54 int n;
55 struct isl_tab_callback *callback;
58 struct isl_tab_undo {
59 enum isl_tab_undo_type type;
60 union isl_tab_undo_val u;
61 struct isl_tab_undo *next;
64 /* The tableau maintains equality relations.
65 * Each column and each row is associated to a variable or a constraint.
66 * The "value" of an inequality constraint is the value of the corresponding
67 * slack variable.
68 * The "row_var" and "col_var" arrays map column and row indices
69 * to indices in the "var" and "con" arrays. The elements of these
70 * arrays maintain extra information about the variables and the constraints.
71 * Each row expresses the corresponding row variable as an affine expression
72 * of the column variables.
73 * The first two columns in the matrix contain the common denominator of
74 * the row and the numerator of the constant term.
75 * If "M" is set, then the third column represents the "big parameter".
76 * The third (M = 0) or fourth (M = 1) column
77 * in the matrix is called column 0 with respect to the col_var array.
78 * The sample value of the tableau is the value that assigns zero
79 * to all the column variables and the constant term of each affine
80 * expression to the corresponding row variable.
81 * The operations on the tableau maintain the property that the sample
82 * value satisfies the non-negativity constraints (usually on the slack
83 * variables).
85 * The big parameter represents an arbitrarily big (and divisible)
86 * positive number. If present, then the sign of a row is determined
87 * lexicographically, with the sign of the big parameter coefficient
88 * considered first. The big parameter is only used while
89 * solving PILP problems.
91 * The first n_dead column variables have their values fixed to zero.
92 * The corresponding tab_vars are flagged "is_zero".
93 * Some of the rows that have have zero coefficients in all but
94 * the dead columns are also flagged "is_zero".
96 * The first n_redundant rows correspond to inequality constraints
97 * that are always satisfied for any value satisfying the non-redundant
98 * rows. The corresponding tab_vars are flagged "is_redundant".
99 * A row variable that is flagged "is_zero" is also flagged "is_redundant"
100 * since the constraint has been reduced to 0 = 0 and is therefore always
101 * satisfied.
103 * There are "n_var" variables in total. The first "n_param" of these
104 * are called parameters and the last "n_div" of these are called divs.
105 * The basic tableau operations makes no distinction between different
106 * kinds of variables. These special variables are only used while
107 * solving PILP problems.
109 * Dead columns and redundant rows are detected on the fly.
110 * However, the basic operations do not ensure that all dead columns
111 * or all redundant rows are detected.
112 * isl_tab_detect_implicit_equalities and isl_tab_detect_redundant can be used
113 * to perform and exhaustive search for dead columns and redundant rows.
115 * The samples matrix contains "n_sample" integer points that have at some
116 * point been elements satisfying the tableau. The first "n_outside"
117 * of them no longer satisfy the tableau. They are kept because they
118 * can be reinstated during rollback when the constraint that cut them
119 * out is removed. These samples are only maintained for the context
120 * tableau while solving PILP problems.
122 enum isl_tab_row_sign {
123 isl_tab_row_unknown = 0,
124 isl_tab_row_pos,
125 isl_tab_row_neg,
126 isl_tab_row_any,
128 struct isl_tab {
129 struct isl_mat *mat;
131 unsigned n_row;
132 unsigned n_col;
133 unsigned n_dead;
134 unsigned n_redundant;
136 unsigned n_var;
137 unsigned n_param;
138 unsigned n_div;
139 unsigned max_var;
140 unsigned n_con;
141 unsigned n_eq;
142 unsigned max_con;
143 struct isl_tab_var *var;
144 struct isl_tab_var *con;
145 int *row_var; /* v >= 0 -> var v; v < 0 -> con ~v */
146 int *col_var; /* v >= 0 -> var v; v < 0 -> con ~v */
147 enum isl_tab_row_sign *row_sign;
149 struct isl_tab_undo bottom;
150 struct isl_tab_undo *top;
152 struct isl_vec *dual;
153 struct isl_basic_map *bmap;
155 unsigned n_sample;
156 unsigned n_outside;
157 int *sample_index;
158 struct isl_mat *samples;
160 int n_zero;
161 int n_unbounded;
162 struct isl_mat *basis;
164 unsigned strict_redundant : 1;
165 unsigned need_undo : 1;
166 unsigned rational : 1;
167 unsigned empty : 1;
168 unsigned in_undo : 1;
169 unsigned M : 1;
170 unsigned cone : 1;
173 struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
174 unsigned n_row, unsigned n_var, unsigned M);
175 void isl_tab_free(struct isl_tab *tab);
177 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap);
178 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset);
179 struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_set *bset,
180 int parametric);
181 int isl_tab_cone_is_bounded(struct isl_tab *tab);
182 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
183 struct isl_tab *tab);
184 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
185 struct isl_tab *tab);
186 int isl_tab_detect_implicit_equalities(struct isl_tab *tab) WARN_UNUSED;
187 int isl_tab_detect_redundant(struct isl_tab *tab) WARN_UNUSED;
188 #define ISL_TAB_SAVE_DUAL (1 << 0)
189 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
190 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
191 unsigned flags) WARN_UNUSED;
193 struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new) WARN_UNUSED;
194 int isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq) WARN_UNUSED;
195 int isl_tab_add_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
196 int isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
198 int isl_tab_freeze_constraint(struct isl_tab *tab, int con) WARN_UNUSED;
200 int isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap) WARN_UNUSED;
201 int isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset) WARN_UNUSED;
202 __isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab);
204 int isl_tab_is_equality(struct isl_tab *tab, int con);
205 int isl_tab_is_redundant(struct isl_tab *tab, int con);
207 int isl_tab_sample_is_integer(struct isl_tab *tab);
208 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab);
210 enum isl_ineq_type {
211 isl_ineq_error = -1,
212 isl_ineq_redundant,
213 isl_ineq_separate,
214 isl_ineq_cut,
215 isl_ineq_adj_eq,
216 isl_ineq_adj_ineq,
219 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq);
221 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab);
222 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap) WARN_UNUSED;
224 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con) WARN_UNUSED;
225 int isl_tab_select_facet(struct isl_tab *tab, int con) WARN_UNUSED;
227 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent);
229 struct isl_map *isl_tab_basic_map_partial_lexopt(
230 struct isl_basic_map *bmap, struct isl_basic_set *dom,
231 struct isl_set **empty, int max);
233 /* private */
235 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i);
236 int isl_tab_mark_redundant(struct isl_tab *tab, int row) WARN_UNUSED;
237 int isl_tab_mark_empty(struct isl_tab *tab) WARN_UNUSED;
238 struct isl_tab *isl_tab_dup(struct isl_tab *tab);
239 struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2);
240 int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new) WARN_UNUSED;
241 int isl_tab_allocate_con(struct isl_tab *tab) WARN_UNUSED;
242 int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new) WARN_UNUSED;
243 int isl_tab_allocate_var(struct isl_tab *tab) WARN_UNUSED;
244 int isl_tab_pivot(struct isl_tab *tab, int row, int col) WARN_UNUSED;
245 int isl_tab_add_row(struct isl_tab *tab, isl_int *line) WARN_UNUSED;
246 int isl_tab_row_is_redundant(struct isl_tab *tab, int row);
247 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var);
248 int isl_tab_sign_of_max(struct isl_tab *tab, int con);
249 int isl_tab_kill_col(struct isl_tab *tab, int col) WARN_UNUSED;
251 int isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type) WARN_UNUSED;
252 int isl_tab_push_var(struct isl_tab *tab,
253 enum isl_tab_undo_type type, struct isl_tab_var *var) WARN_UNUSED;
254 int isl_tab_push_basis(struct isl_tab *tab) WARN_UNUSED;
256 struct isl_tab *isl_tab_init_samples(struct isl_tab *tab) WARN_UNUSED;
257 struct isl_tab *isl_tab_add_sample(struct isl_tab *tab,
258 __isl_take isl_vec *sample) WARN_UNUSED;
259 struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s);
260 int isl_tab_save_samples(struct isl_tab *tab) WARN_UNUSED;
262 struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
263 struct isl_tab *tab_cone) WARN_UNUSED;
265 int isl_tab_push_callback(struct isl_tab *tab,
266 struct isl_tab_callback *callback) WARN_UNUSED;
268 int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div,
269 int (*add_ineq)(void *user, isl_int *), void *user);
271 #endif