isl_sample.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  1. /*
  2. * Copyright 2008-2009 Katholieke Universiteit Leuven
  3. *
  4. * Use of this software is governed by the MIT license
  5. *
  6. * Written by Sven Verdoolaege, K.U.Leuven, Departement
  7. * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
  8. */
  9. #include <isl_ctx_private.h>
  10. #include <isl_map_private.h>
  11. #include "isl_sample.h"
  12. #include <isl/vec.h>
  13. #include <isl/mat.h>
  14. #include <isl_seq.h>
  15. #include "isl_equalities.h"
  16. #include "isl_tab.h"
  17. #include "isl_basis_reduction.h"
  18. #include <isl_factorization.h>
  19. #include <isl_point_private.h>
  20. #include <isl_options_private.h>
  21. #include <isl_vec_private.h>
  22. #include <bset_from_bmap.c>
  23. #include <set_to_map.c>
  24. static __isl_give isl_vec *empty_sample(__isl_take isl_basic_set *bset)
  25. {
  26. struct isl_vec *vec;
  27. vec = isl_vec_alloc(bset->ctx, 0);
  28. isl_basic_set_free(bset);
  29. return vec;
  30. }
  31. /* Construct a zero sample of the same dimension as bset.
  32. * As a special case, if bset is zero-dimensional, this
  33. * function creates a zero-dimensional sample point.
  34. */
  35. static __isl_give isl_vec *zero_sample(__isl_take isl_basic_set *bset)
  36. {
  37. isl_size dim;
  38. struct isl_vec *sample;
  39. dim = isl_basic_set_dim(bset, isl_dim_all);
  40. if (dim < 0)
  41. goto error;
  42. sample = isl_vec_alloc(bset->ctx, 1 + dim);
  43. if (sample) {
  44. isl_int_set_si(sample->el[0], 1);
  45. isl_seq_clr(sample->el + 1, dim);
  46. }
  47. isl_basic_set_free(bset);
  48. return sample;
  49. error:
  50. isl_basic_set_free(bset);
  51. return NULL;
  52. }
  53. static __isl_give isl_vec *interval_sample(__isl_take isl_basic_set *bset)
  54. {
  55. int i;
  56. isl_int t;
  57. struct isl_vec *sample;
  58. bset = isl_basic_set_simplify(bset);
  59. if (!bset)
  60. return NULL;
  61. if (isl_basic_set_plain_is_empty(bset))
  62. return empty_sample(bset);
  63. if (bset->n_eq == 0 && bset->n_ineq == 0)
  64. return zero_sample(bset);
  65. sample = isl_vec_alloc(bset->ctx, 2);
  66. if (!sample)
  67. goto error;
  68. if (!bset)
  69. return NULL;
  70. isl_int_set_si(sample->block.data[0], 1);
  71. if (bset->n_eq > 0) {
  72. isl_assert(bset->ctx, bset->n_eq == 1, goto error);
  73. isl_assert(bset->ctx, bset->n_ineq == 0, goto error);
  74. if (isl_int_is_one(bset->eq[0][1]))
  75. isl_int_neg(sample->el[1], bset->eq[0][0]);
  76. else {
  77. isl_assert(bset->ctx, isl_int_is_negone(bset->eq[0][1]),
  78. goto error);
  79. isl_int_set(sample->el[1], bset->eq[0][0]);
  80. }
  81. isl_basic_set_free(bset);
  82. return sample;
  83. }
  84. isl_int_init(t);
  85. if (isl_int_is_one(bset->ineq[0][1]))
  86. isl_int_neg(sample->block.data[1], bset->ineq[0][0]);
  87. else
  88. isl_int_set(sample->block.data[1], bset->ineq[0][0]);
  89. for (i = 1; i < bset->n_ineq; ++i) {
  90. isl_seq_inner_product(sample->block.data,
  91. bset->ineq[i], 2, &t);
  92. if (isl_int_is_neg(t))
  93. break;
  94. }
  95. isl_int_clear(t);
  96. if (i < bset->n_ineq) {
  97. isl_vec_free(sample);
  98. return empty_sample(bset);
  99. }
  100. isl_basic_set_free(bset);
  101. return sample;
  102. error:
  103. isl_basic_set_free(bset);
  104. isl_vec_free(sample);
  105. return NULL;
  106. }
  107. /* Find a sample integer point, if any, in bset, which is known
  108. * to have equalities. If bset contains no integer points, then
  109. * return a zero-length vector.
  110. * We simply remove the known equalities, compute a sample
  111. * in the resulting bset, using the specified recurse function,
  112. * and then transform the sample back to the original space.
  113. */
  114. static __isl_give isl_vec *sample_eq(__isl_take isl_basic_set *bset,
  115. __isl_give isl_vec *(*recurse)(__isl_take isl_basic_set *))
  116. {
  117. struct isl_mat *T;
  118. struct isl_vec *sample;
  119. if (!bset)
  120. return NULL;
  121. bset = isl_basic_set_remove_equalities(bset, &T, NULL);
  122. sample = recurse(bset);
  123. if (!sample || sample->size == 0)
  124. isl_mat_free(T);
  125. else
  126. sample = isl_mat_vec_product(T, sample);
  127. return sample;
  128. }
  129. /* Return a matrix containing the equalities of the tableau
  130. * in constraint form. The tableau is assumed to have
  131. * an associated bset that has been kept up-to-date.
  132. */
  133. static struct isl_mat *tab_equalities(struct isl_tab *tab)
  134. {
  135. int i, j;
  136. int n_eq;
  137. struct isl_mat *eq;
  138. struct isl_basic_set *bset;
  139. if (!tab)
  140. return NULL;
  141. bset = isl_tab_peek_bset(tab);
  142. isl_assert(tab->mat->ctx, bset, return NULL);
  143. n_eq = tab->n_var - tab->n_col + tab->n_dead;
  144. if (tab->empty || n_eq == 0)
  145. return isl_mat_alloc(tab->mat->ctx, 0, tab->n_var);
  146. if (n_eq == tab->n_var)
  147. return isl_mat_identity(tab->mat->ctx, tab->n_var);
  148. eq = isl_mat_alloc(tab->mat->ctx, n_eq, tab->n_var);
  149. if (!eq)
  150. return NULL;
  151. for (i = 0, j = 0; i < tab->n_con; ++i) {
  152. if (tab->con[i].is_row)
  153. continue;
  154. if (tab->con[i].index >= 0 && tab->con[i].index >= tab->n_dead)
  155. continue;
  156. if (i < bset->n_eq)
  157. isl_seq_cpy(eq->row[j], bset->eq[i] + 1, tab->n_var);
  158. else
  159. isl_seq_cpy(eq->row[j],
  160. bset->ineq[i - bset->n_eq] + 1, tab->n_var);
  161. ++j;
  162. }
  163. isl_assert(bset->ctx, j == n_eq, goto error);
  164. return eq;
  165. error:
  166. isl_mat_free(eq);
  167. return NULL;
  168. }
  169. /* Compute and return an initial basis for the bounded tableau "tab".
  170. *
  171. * If the tableau is either full-dimensional or zero-dimensional,
  172. * the we simply return an identity matrix.
  173. * Otherwise, we construct a basis whose first directions correspond
  174. * to equalities.
  175. */
  176. static struct isl_mat *initial_basis(struct isl_tab *tab)
  177. {
  178. int n_eq;
  179. struct isl_mat *eq;
  180. struct isl_mat *Q;
  181. tab->n_unbounded = 0;
  182. tab->n_zero = n_eq = tab->n_var - tab->n_col + tab->n_dead;
  183. if (tab->empty || n_eq == 0 || n_eq == tab->n_var)
  184. return isl_mat_identity(tab->mat->ctx, 1 + tab->n_var);
  185. eq = tab_equalities(tab);
  186. eq = isl_mat_left_hermite(eq, 0, NULL, &Q);
  187. if (!eq)
  188. return NULL;
  189. isl_mat_free(eq);
  190. Q = isl_mat_lin_to_aff(Q);
  191. return Q;
  192. }
  193. /* Compute the minimum of the current ("level") basis row over "tab"
  194. * and store the result in position "level" of "min".
  195. *
  196. * This function assumes that at least one more row and at least
  197. * one more element in the constraint array are available in the tableau.
  198. */
  199. static enum isl_lp_result compute_min(isl_ctx *ctx, struct isl_tab *tab,
  200. __isl_keep isl_vec *min, int level)
  201. {
  202. return isl_tab_min(tab, tab->basis->row[1 + level],
  203. ctx->one, &min->el[level], NULL, 0);
  204. }
  205. /* Compute the maximum of the current ("level") basis row over "tab"
  206. * and store the result in position "level" of "max".
  207. *
  208. * This function assumes that at least one more row and at least
  209. * one more element in the constraint array are available in the tableau.
  210. */
  211. static enum isl_lp_result compute_max(isl_ctx *ctx, struct isl_tab *tab,
  212. __isl_keep isl_vec *max, int level)
  213. {
  214. enum isl_lp_result res;
  215. unsigned dim = tab->n_var;
  216. isl_seq_neg(tab->basis->row[1 + level] + 1,
  217. tab->basis->row[1 + level] + 1, dim);
  218. res = isl_tab_min(tab, tab->basis->row[1 + level],
  219. ctx->one, &max->el[level], NULL, 0);
  220. isl_seq_neg(tab->basis->row[1 + level] + 1,
  221. tab->basis->row[1 + level] + 1, dim);
  222. isl_int_neg(max->el[level], max->el[level]);
  223. return res;
  224. }
  225. /* Perform a greedy search for an integer point in the set represented
  226. * by "tab", given that the minimal rational value (rounded up to the
  227. * nearest integer) at "level" is smaller than the maximal rational
  228. * value (rounded down to the nearest integer).
  229. *
  230. * Return 1 if we have found an integer point (if tab->n_unbounded > 0
  231. * then we may have only found integer values for the bounded dimensions
  232. * and it is the responsibility of the caller to extend this solution
  233. * to the unbounded dimensions).
  234. * Return 0 if greedy search did not result in a solution.
  235. * Return -1 if some error occurred.
  236. *
  237. * We assign a value half-way between the minimum and the maximum
  238. * to the current dimension and check if the minimal value of the
  239. * next dimension is still smaller than (or equal) to the maximal value.
  240. * We continue this process until either
  241. * - the minimal value (rounded up) is greater than the maximal value
  242. * (rounded down). In this case, greedy search has failed.
  243. * - we have exhausted all bounded dimensions, meaning that we have
  244. * found a solution.
  245. * - the sample value of the tableau is integral.
  246. * - some error has occurred.
  247. */
  248. static int greedy_search(isl_ctx *ctx, struct isl_tab *tab,
  249. __isl_keep isl_vec *min, __isl_keep isl_vec *max, int level)
  250. {
  251. struct isl_tab_undo *snap;
  252. enum isl_lp_result res;
  253. snap = isl_tab_snap(tab);
  254. do {
  255. isl_int_add(tab->basis->row[1 + level][0],
  256. min->el[level], max->el[level]);
  257. isl_int_fdiv_q_ui(tab->basis->row[1 + level][0],
  258. tab->basis->row[1 + level][0], 2);
  259. isl_int_neg(tab->basis->row[1 + level][0],
  260. tab->basis->row[1 + level][0]);
  261. if (isl_tab_add_valid_eq(tab, tab->basis->row[1 + level]) < 0)
  262. return -1;
  263. isl_int_set_si(tab->basis->row[1 + level][0], 0);
  264. if (++level >= tab->n_var - tab->n_unbounded)
  265. return 1;
  266. if (isl_tab_sample_is_integer(tab))
  267. return 1;
  268. res = compute_min(ctx, tab, min, level);
  269. if (res == isl_lp_error)
  270. return -1;
  271. if (res != isl_lp_ok)
  272. isl_die(ctx, isl_error_internal,
  273. "expecting bounded rational solution",
  274. return -1);
  275. res = compute_max(ctx, tab, max, level);
  276. if (res == isl_lp_error)
  277. return -1;
  278. if (res != isl_lp_ok)
  279. isl_die(ctx, isl_error_internal,
  280. "expecting bounded rational solution",
  281. return -1);
  282. } while (isl_int_le(min->el[level], max->el[level]));
  283. if (isl_tab_rollback(tab, snap) < 0)
  284. return -1;
  285. return 0;
  286. }
  287. /* Given a tableau representing a set, find and return
  288. * an integer point in the set, if there is any.
  289. *
  290. * We perform a depth first search
  291. * for an integer point, by scanning all possible values in the range
  292. * attained by a basis vector, where an initial basis may have been set
  293. * by the calling function. Otherwise an initial basis that exploits
  294. * the equalities in the tableau is created.
  295. * tab->n_zero is currently ignored and is clobbered by this function.
  296. *
  297. * The tableau is allowed to have unbounded direction, but then
  298. * the calling function needs to set an initial basis, with the
  299. * unbounded directions last and with tab->n_unbounded set
  300. * to the number of unbounded directions.
  301. * Furthermore, the calling functions needs to add shifted copies
  302. * of all constraints involving unbounded directions to ensure
  303. * that any feasible rational value in these directions can be rounded
  304. * up to yield a feasible integer value.
  305. * In particular, let B define the given basis x' = B x
  306. * and let T be the inverse of B, i.e., X = T x'.
  307. * Let a x + c >= 0 be a constraint of the set represented by the tableau,
  308. * or a T x' + c >= 0 in terms of the given basis. Assume that
  309. * the bounded directions have an integer value, then we can safely
  310. * round up the values for the unbounded directions if we make sure
  311. * that x' not only satisfies the original constraint, but also
  312. * the constraint "a T x' + c + s >= 0" with s the sum of all
  313. * negative values in the last n_unbounded entries of "a T".
  314. * The calling function therefore needs to add the constraint
  315. * a x + c + s >= 0. The current function then scans the first
  316. * directions for an integer value and once those have been found,
  317. * it can compute "T ceil(B x)" to yield an integer point in the set.
  318. * Note that during the search, the first rows of B may be changed
  319. * by a basis reduction, but the last n_unbounded rows of B remain
  320. * unaltered and are also not mixed into the first rows.
  321. *
  322. * The search is implemented iteratively. "level" identifies the current
  323. * basis vector. "init" is true if we want the first value at the current
  324. * level and false if we want the next value.
  325. *
  326. * At the start of each level, we first check if we can find a solution
  327. * using greedy search. If not, we continue with the exhaustive search.
  328. *
  329. * The initial basis is the identity matrix. If the range in some direction
  330. * contains more than one integer value, we perform basis reduction based
  331. * on the value of ctx->opt->gbr
  332. * - ISL_GBR_NEVER: never perform basis reduction
  333. * - ISL_GBR_ONCE: only perform basis reduction the first
  334. * time such a range is encountered
  335. * - ISL_GBR_ALWAYS: always perform basis reduction when
  336. * such a range is encountered
  337. *
  338. * When ctx->opt->gbr is set to ISL_GBR_ALWAYS, then we allow the basis
  339. * reduction computation to return early. That is, as soon as it
  340. * finds a reasonable first direction.
  341. */
  342. __isl_give isl_vec *isl_tab_sample(struct isl_tab *tab)
  343. {
  344. unsigned dim;
  345. unsigned gbr;
  346. struct isl_ctx *ctx;
  347. struct isl_vec *sample;
  348. struct isl_vec *min;
  349. struct isl_vec *max;
  350. enum isl_lp_result res;
  351. int level;
  352. int init;
  353. int reduced;
  354. struct isl_tab_undo **snap;
  355. if (!tab)
  356. return NULL;
  357. if (tab->empty)
  358. return isl_vec_alloc(tab->mat->ctx, 0);
  359. if (!tab->basis)
  360. tab->basis = initial_basis(tab);
  361. if (!tab->basis)
  362. return NULL;
  363. isl_assert(tab->mat->ctx, tab->basis->n_row == tab->n_var + 1,
  364. return NULL);
  365. isl_assert(tab->mat->ctx, tab->basis->n_col == tab->n_var + 1,
  366. return NULL);
  367. ctx = tab->mat->ctx;
  368. dim = tab->n_var;
  369. gbr = ctx->opt->gbr;
  370. if (tab->n_unbounded == tab->n_var) {
  371. sample = isl_tab_get_sample_value(tab);
  372. sample = isl_mat_vec_product(isl_mat_copy(tab->basis), sample);
  373. sample = isl_vec_ceil(sample);
  374. sample = isl_mat_vec_inverse_product(isl_mat_copy(tab->basis),
  375. sample);
  376. return sample;
  377. }
  378. if (isl_tab_extend_cons(tab, dim + 1) < 0)
  379. return NULL;
  380. min = isl_vec_alloc(ctx, dim);
  381. max = isl_vec_alloc(ctx, dim);
  382. snap = isl_alloc_array(ctx, struct isl_tab_undo *, dim);
  383. if (!min || !max || !snap)
  384. goto error;
  385. level = 0;
  386. init = 1;
  387. reduced = 0;
  388. while (level >= 0) {
  389. if (init) {
  390. int choice;
  391. res = compute_min(ctx, tab, min, level);
  392. if (res == isl_lp_error)
  393. goto error;
  394. if (res != isl_lp_ok)
  395. isl_die(ctx, isl_error_internal,
  396. "expecting bounded rational solution",
  397. goto error);
  398. if (isl_tab_sample_is_integer(tab))
  399. break;
  400. res = compute_max(ctx, tab, max, level);
  401. if (res == isl_lp_error)
  402. goto error;
  403. if (res != isl_lp_ok)
  404. isl_die(ctx, isl_error_internal,
  405. "expecting bounded rational solution",
  406. goto error);
  407. if (isl_tab_sample_is_integer(tab))
  408. break;
  409. choice = isl_int_lt(min->el[level], max->el[level]);
  410. if (choice) {
  411. int g;
  412. g = greedy_search(ctx, tab, min, max, level);
  413. if (g < 0)
  414. goto error;
  415. if (g)
  416. break;
  417. }
  418. if (!reduced && choice &&
  419. ctx->opt->gbr != ISL_GBR_NEVER) {
  420. unsigned gbr_only_first;
  421. if (ctx->opt->gbr == ISL_GBR_ONCE)
  422. ctx->opt->gbr = ISL_GBR_NEVER;
  423. tab->n_zero = level;
  424. gbr_only_first = ctx->opt->gbr_only_first;
  425. ctx->opt->gbr_only_first =
  426. ctx->opt->gbr == ISL_GBR_ALWAYS;
  427. tab = isl_tab_compute_reduced_basis(tab);
  428. ctx->opt->gbr_only_first = gbr_only_first;
  429. if (!tab || !tab->basis)
  430. goto error;
  431. reduced = 1;
  432. continue;
  433. }
  434. reduced = 0;
  435. snap[level] = isl_tab_snap(tab);
  436. } else
  437. isl_int_add_ui(min->el[level], min->el[level], 1);
  438. if (isl_int_gt(min->el[level], max->el[level])) {
  439. level--;
  440. init = 0;
  441. if (level >= 0)
  442. if (isl_tab_rollback(tab, snap[level]) < 0)
  443. goto error;
  444. continue;
  445. }
  446. isl_int_neg(tab->basis->row[1 + level][0], min->el[level]);
  447. if (isl_tab_add_valid_eq(tab, tab->basis->row[1 + level]) < 0)
  448. goto error;
  449. isl_int_set_si(tab->basis->row[1 + level][0], 0);
  450. if (level + tab->n_unbounded < dim - 1) {
  451. ++level;
  452. init = 1;
  453. continue;
  454. }
  455. break;
  456. }
  457. if (level >= 0) {
  458. sample = isl_tab_get_sample_value(tab);
  459. if (!sample)
  460. goto error;
  461. if (tab->n_unbounded && !isl_int_is_one(sample->el[0])) {
  462. sample = isl_mat_vec_product(isl_mat_copy(tab->basis),
  463. sample);
  464. sample = isl_vec_ceil(sample);
  465. sample = isl_mat_vec_inverse_product(
  466. isl_mat_copy(tab->basis), sample);
  467. }
  468. } else
  469. sample = isl_vec_alloc(ctx, 0);
  470. ctx->opt->gbr = gbr;
  471. isl_vec_free(min);
  472. isl_vec_free(max);
  473. free(snap);
  474. return sample;
  475. error:
  476. ctx->opt->gbr = gbr;
  477. isl_vec_free(min);
  478. isl_vec_free(max);
  479. free(snap);
  480. return NULL;
  481. }
  482. static __isl_give isl_vec *sample_bounded(__isl_take isl_basic_set *bset);
  483. /* Internal data for factored_sample.
  484. * "sample" collects the sample and may get reset to a zero-length vector
  485. * signaling the absence of a sample vector.
  486. * "pos" is the position of the contribution of the next factor.
  487. */
  488. struct isl_factored_sample_data {
  489. isl_vec *sample;
  490. int pos;
  491. };
  492. /* isl_factorizer_every_factor_basic_set callback that extends
  493. * the sample in data->sample with the contribution
  494. * of the factor "bset".
  495. * If "bset" turns out to be empty, then the product is empty too and
  496. * no further factors need to be considered.
  497. */
  498. static isl_bool factor_sample(__isl_keep isl_basic_set *bset, void *user)
  499. {
  500. struct isl_factored_sample_data *data = user;
  501. isl_vec *sample;
  502. isl_size n;
  503. n = isl_basic_set_dim(bset, isl_dim_set);
  504. if (n < 0)
  505. return isl_bool_error;
  506. sample = sample_bounded(isl_basic_set_copy(bset));
  507. if (!sample)
  508. return isl_bool_error;
  509. if (sample->size == 0) {
  510. isl_vec_free(data->sample);
  511. data->sample = sample;
  512. return isl_bool_false;
  513. }
  514. isl_seq_cpy(data->sample->el + data->pos, sample->el + 1, n);
  515. isl_vec_free(sample);
  516. data->pos += n;
  517. return isl_bool_true;
  518. }
  519. /* Compute a sample point of the given basic set, based on the given,
  520. * non-trivial factorization.
  521. */
  522. static __isl_give isl_vec *factored_sample(__isl_take isl_basic_set *bset,
  523. __isl_take isl_factorizer *f)
  524. {
  525. struct isl_factored_sample_data data = { NULL };
  526. isl_ctx *ctx;
  527. isl_size total;
  528. isl_bool every;
  529. ctx = isl_basic_set_get_ctx(bset);
  530. total = isl_basic_set_dim(bset, isl_dim_all);
  531. if (!ctx || total < 0)
  532. goto error;
  533. data.sample = isl_vec_alloc(ctx, 1 + total);
  534. if (!data.sample)
  535. goto error;
  536. isl_int_set_si(data.sample->el[0], 1);
  537. data.pos = 1;
  538. every = isl_factorizer_every_factor_basic_set(f, &factor_sample, &data);
  539. if (every < 0) {
  540. data.sample = isl_vec_free(data.sample);
  541. } else if (every) {
  542. isl_morph *morph;
  543. morph = isl_morph_inverse(isl_morph_copy(f->morph));
  544. data.sample = isl_morph_vec(morph, data.sample);
  545. }
  546. isl_basic_set_free(bset);
  547. isl_factorizer_free(f);
  548. return data.sample;
  549. error:
  550. isl_basic_set_free(bset);
  551. isl_factorizer_free(f);
  552. isl_vec_free(data.sample);
  553. return NULL;
  554. }
  555. /* Given a basic set that is known to be bounded, find and return
  556. * an integer point in the basic set, if there is any.
  557. *
  558. * After handling some trivial cases, we construct a tableau
  559. * and then use isl_tab_sample to find a sample, passing it
  560. * the identity matrix as initial basis.
  561. */
  562. static __isl_give isl_vec *sample_bounded(__isl_take isl_basic_set *bset)
  563. {
  564. isl_size dim;
  565. struct isl_vec *sample;
  566. struct isl_tab *tab = NULL;
  567. isl_factorizer *f;
  568. if (!bset)
  569. return NULL;
  570. if (isl_basic_set_plain_is_empty(bset))
  571. return empty_sample(bset);
  572. dim = isl_basic_set_dim(bset, isl_dim_all);
  573. if (dim < 0)
  574. bset = isl_basic_set_free(bset);
  575. if (dim == 0)
  576. return zero_sample(bset);
  577. if (dim == 1)
  578. return interval_sample(bset);
  579. if (bset->n_eq > 0)
  580. return sample_eq(bset, sample_bounded);
  581. f = isl_basic_set_factorizer(bset);
  582. if (!f)
  583. goto error;
  584. if (f->n_group != 0)
  585. return factored_sample(bset, f);
  586. isl_factorizer_free(f);
  587. tab = isl_tab_from_basic_set(bset, 1);
  588. if (tab && tab->empty) {
  589. isl_tab_free(tab);
  590. ISL_F_SET(bset, ISL_BASIC_SET_EMPTY);
  591. sample = isl_vec_alloc(isl_basic_set_get_ctx(bset), 0);
  592. isl_basic_set_free(bset);
  593. return sample;
  594. }
  595. if (!ISL_F_ISSET(bset, ISL_BASIC_SET_NO_IMPLICIT))
  596. if (isl_tab_detect_implicit_equalities(tab) < 0)
  597. goto error;
  598. sample = isl_tab_sample(tab);
  599. if (!sample)
  600. goto error;
  601. if (sample->size > 0) {
  602. isl_vec_free(bset->sample);
  603. bset->sample = isl_vec_copy(sample);
  604. }
  605. isl_basic_set_free(bset);
  606. isl_tab_free(tab);
  607. return sample;
  608. error:
  609. isl_basic_set_free(bset);
  610. isl_tab_free(tab);
  611. return NULL;
  612. }
  613. /* Given a basic set "bset" and a value "sample" for the first coordinates
  614. * of bset, plug in these values and drop the corresponding coordinates.
  615. *
  616. * We do this by computing the preimage of the transformation
  617. *
  618. * [ 1 0 ]
  619. * x = [ s 0 ] x'
  620. * [ 0 I ]
  621. *
  622. * where [1 s] is the sample value and I is the identity matrix of the
  623. * appropriate dimension.
  624. */
  625. static __isl_give isl_basic_set *plug_in(__isl_take isl_basic_set *bset,
  626. __isl_take isl_vec *sample)
  627. {
  628. int i;
  629. isl_size total;
  630. struct isl_mat *T;
  631. total = isl_basic_set_dim(bset, isl_dim_all);
  632. if (total < 0 || !sample)
  633. goto error;
  634. T = isl_mat_alloc(bset->ctx, 1 + total, 1 + total - (sample->size - 1));
  635. if (!T)
  636. goto error;
  637. for (i = 0; i < sample->size; ++i) {
  638. isl_int_set(T->row[i][0], sample->el[i]);
  639. isl_seq_clr(T->row[i] + 1, T->n_col - 1);
  640. }
  641. for (i = 0; i < T->n_col - 1; ++i) {
  642. isl_seq_clr(T->row[sample->size + i], T->n_col);
  643. isl_int_set_si(T->row[sample->size + i][1 + i], 1);
  644. }
  645. isl_vec_free(sample);
  646. bset = isl_basic_set_preimage(bset, T);
  647. return bset;
  648. error:
  649. isl_basic_set_free(bset);
  650. isl_vec_free(sample);
  651. return NULL;
  652. }
  653. /* Given a basic set "bset", return any (possibly non-integer) point
  654. * in the basic set.
  655. */
  656. static __isl_give isl_vec *rational_sample(__isl_take isl_basic_set *bset)
  657. {
  658. struct isl_tab *tab;
  659. struct isl_vec *sample;
  660. if (!bset)
  661. return NULL;
  662. tab = isl_tab_from_basic_set(bset, 0);
  663. sample = isl_tab_get_sample_value(tab);
  664. isl_tab_free(tab);
  665. isl_basic_set_free(bset);
  666. return sample;
  667. }
  668. /* Given a linear cone "cone" and a rational point "vec",
  669. * construct a polyhedron with shifted copies of the constraints in "cone",
  670. * i.e., a polyhedron with "cone" as its recession cone, such that each
  671. * point x in this polyhedron is such that the unit box positioned at x
  672. * lies entirely inside the affine cone 'vec + cone'.
  673. * Any rational point in this polyhedron may therefore be rounded up
  674. * to yield an integer point that lies inside said affine cone.
  675. *
  676. * Denote the constraints of cone by "<a_i, x> >= 0" and the rational
  677. * point "vec" by v/d.
  678. * Let b_i = <a_i, v>. Then the affine cone 'vec + cone' is given
  679. * by <a_i, x> - b/d >= 0.
  680. * The polyhedron <a_i, x> - ceil{b/d} >= 0 is a subset of this affine cone.
  681. * We prefer this polyhedron over the actual affine cone because it doesn't
  682. * require a scaling of the constraints.
  683. * If each of the vertices of the unit cube positioned at x lies inside
  684. * this polyhedron, then the whole unit cube at x lies inside the affine cone.
  685. * We therefore impose that x' = x + \sum e_i, for any selection of unit
  686. * vectors lies inside the polyhedron, i.e.,
  687. *
  688. * <a_i, x'> - ceil{b/d} = <a_i, x> + sum a_i - ceil{b/d} >= 0
  689. *
  690. * The most stringent of these constraints is the one that selects
  691. * all negative a_i, so the polyhedron we are looking for has constraints
  692. *
  693. * <a_i, x> + sum_{a_i < 0} a_i - ceil{b/d} >= 0
  694. *
  695. * Note that if cone were known to have only non-negative rays
  696. * (which can be accomplished by a unimodular transformation),
  697. * then we would only have to check the points x' = x + e_i
  698. * and we only have to add the smallest negative a_i (if any)
  699. * instead of the sum of all negative a_i.
  700. */
  701. static __isl_give isl_basic_set *shift_cone(__isl_take isl_basic_set *cone,
  702. __isl_take isl_vec *vec)
  703. {
  704. int i, j, k;
  705. isl_size total;
  706. struct isl_basic_set *shift = NULL;
  707. total = isl_basic_set_dim(cone, isl_dim_all);
  708. if (total < 0 || !vec)
  709. goto error;
  710. isl_assert(cone->ctx, cone->n_eq == 0, goto error);
  711. shift = isl_basic_set_alloc_space(isl_basic_set_get_space(cone),
  712. 0, 0, cone->n_ineq);
  713. for (i = 0; i < cone->n_ineq; ++i) {
  714. k = isl_basic_set_alloc_inequality(shift);
  715. if (k < 0)
  716. goto error;
  717. isl_seq_cpy(shift->ineq[k] + 1, cone->ineq[i] + 1, total);
  718. isl_seq_inner_product(shift->ineq[k] + 1, vec->el + 1, total,
  719. &shift->ineq[k][0]);
  720. isl_int_cdiv_q(shift->ineq[k][0],
  721. shift->ineq[k][0], vec->el[0]);
  722. isl_int_neg(shift->ineq[k][0], shift->ineq[k][0]);
  723. for (j = 0; j < total; ++j) {
  724. if (isl_int_is_nonneg(shift->ineq[k][1 + j]))
  725. continue;
  726. isl_int_add(shift->ineq[k][0],
  727. shift->ineq[k][0], shift->ineq[k][1 + j]);
  728. }
  729. }
  730. isl_basic_set_free(cone);
  731. isl_vec_free(vec);
  732. return isl_basic_set_finalize(shift);
  733. error:
  734. isl_basic_set_free(shift);
  735. isl_basic_set_free(cone);
  736. isl_vec_free(vec);
  737. return NULL;
  738. }
  739. /* Given a rational point vec in a (transformed) basic set,
  740. * such that cone is the recession cone of the original basic set,
  741. * "round up" the rational point to an integer point.
  742. *
  743. * We first check if the rational point just happens to be integer.
  744. * If not, we transform the cone in the same way as the basic set,
  745. * pick a point x in this cone shifted to the rational point such that
  746. * the whole unit cube at x is also inside this affine cone.
  747. * Then we simply round up the coordinates of x and return the
  748. * resulting integer point.
  749. */
  750. static __isl_give isl_vec *round_up_in_cone(__isl_take isl_vec *vec,
  751. __isl_take isl_basic_set *cone, __isl_take isl_mat *U)
  752. {
  753. isl_size total;
  754. if (!vec || !cone || !U)
  755. goto error;
  756. isl_assert(vec->ctx, vec->size != 0, goto error);
  757. if (isl_int_is_one(vec->el[0])) {
  758. isl_mat_free(U);
  759. isl_basic_set_free(cone);
  760. return vec;
  761. }
  762. total = isl_basic_set_dim(cone, isl_dim_all);
  763. if (total < 0)
  764. goto error;
  765. cone = isl_basic_set_preimage(cone, U);
  766. cone = isl_basic_set_remove_dims(cone, isl_dim_set,
  767. 0, total - (vec->size - 1));
  768. cone = shift_cone(cone, vec);
  769. vec = rational_sample(cone);
  770. vec = isl_vec_ceil(vec);
  771. return vec;
  772. error:
  773. isl_mat_free(U);
  774. isl_vec_free(vec);
  775. isl_basic_set_free(cone);
  776. return NULL;
  777. }
  778. /* Concatenate two integer vectors, i.e., two vectors with denominator
  779. * (stored in element 0) equal to 1.
  780. */
  781. static __isl_give isl_vec *vec_concat(__isl_take isl_vec *vec1,
  782. __isl_take isl_vec *vec2)
  783. {
  784. struct isl_vec *vec;
  785. if (!vec1 || !vec2)
  786. goto error;
  787. isl_assert(vec1->ctx, vec1->size > 0, goto error);
  788. isl_assert(vec2->ctx, vec2->size > 0, goto error);
  789. isl_assert(vec1->ctx, isl_int_is_one(vec1->el[0]), goto error);
  790. isl_assert(vec2->ctx, isl_int_is_one(vec2->el[0]), goto error);
  791. vec = isl_vec_alloc(vec1->ctx, vec1->size + vec2->size - 1);
  792. if (!vec)
  793. goto error;
  794. isl_seq_cpy(vec->el, vec1->el, vec1->size);
  795. isl_seq_cpy(vec->el + vec1->size, vec2->el + 1, vec2->size - 1);
  796. isl_vec_free(vec1);
  797. isl_vec_free(vec2);
  798. return vec;
  799. error:
  800. isl_vec_free(vec1);
  801. isl_vec_free(vec2);
  802. return NULL;
  803. }
  804. /* Give a basic set "bset" with recession cone "cone", compute and
  805. * return an integer point in bset, if any.
  806. *
  807. * If the recession cone is full-dimensional, then we know that
  808. * bset contains an infinite number of integer points and it is
  809. * fairly easy to pick one of them.
  810. * If the recession cone is not full-dimensional, then we first
  811. * transform bset such that the bounded directions appear as
  812. * the first dimensions of the transformed basic set.
  813. * We do this by using a unimodular transformation that transforms
  814. * the equalities in the recession cone to equalities on the first
  815. * dimensions.
  816. *
  817. * The transformed set is then projected onto its bounded dimensions.
  818. * Note that to compute this projection, we can simply drop all constraints
  819. * involving any of the unbounded dimensions since these constraints
  820. * cannot be combined to produce a constraint on the bounded dimensions.
  821. * To see this, assume that there is such a combination of constraints
  822. * that produces a constraint on the bounded dimensions. This means
  823. * that some combination of the unbounded dimensions has both an upper
  824. * bound and a lower bound in terms of the bounded dimensions, but then
  825. * this combination would be a bounded direction too and would have been
  826. * transformed into a bounded dimensions.
  827. *
  828. * We then compute a sample value in the bounded dimensions.
  829. * If no such value can be found, then the original set did not contain
  830. * any integer points and we are done.
  831. * Otherwise, we plug in the value we found in the bounded dimensions,
  832. * project out these bounded dimensions and end up with a set with
  833. * a full-dimensional recession cone.
  834. * A sample point in this set is computed by "rounding up" any
  835. * rational point in the set.
  836. *
  837. * The sample points in the bounded and unbounded dimensions are
  838. * then combined into a single sample point and transformed back
  839. * to the original space.
  840. */
  841. __isl_give isl_vec *isl_basic_set_sample_with_cone(
  842. __isl_take isl_basic_set *bset, __isl_take isl_basic_set *cone)
  843. {
  844. isl_size total;
  845. unsigned cone_dim;
  846. struct isl_mat *M, *U;
  847. struct isl_vec *sample;
  848. struct isl_vec *cone_sample;
  849. struct isl_ctx *ctx;
  850. struct isl_basic_set *bounded;
  851. total = isl_basic_set_dim(cone, isl_dim_all);
  852. if (!bset || total < 0)
  853. goto error;
  854. ctx = isl_basic_set_get_ctx(bset);
  855. cone_dim = total - cone->n_eq;
  856. M = isl_mat_sub_alloc6(ctx, cone->eq, 0, cone->n_eq, 1, total);
  857. M = isl_mat_left_hermite(M, 0, &U, NULL);
  858. if (!M)
  859. goto error;
  860. isl_mat_free(M);
  861. U = isl_mat_lin_to_aff(U);
  862. bset = isl_basic_set_preimage(bset, isl_mat_copy(U));
  863. bounded = isl_basic_set_copy(bset);
  864. bounded = isl_basic_set_drop_constraints_involving(bounded,
  865. total - cone_dim, cone_dim);
  866. bounded = isl_basic_set_drop_dims(bounded, total - cone_dim, cone_dim);
  867. sample = sample_bounded(bounded);
  868. if (!sample || sample->size == 0) {
  869. isl_basic_set_free(bset);
  870. isl_basic_set_free(cone);
  871. isl_mat_free(U);
  872. return sample;
  873. }
  874. bset = plug_in(bset, isl_vec_copy(sample));
  875. cone_sample = rational_sample(bset);
  876. cone_sample = round_up_in_cone(cone_sample, cone, isl_mat_copy(U));
  877. sample = vec_concat(sample, cone_sample);
  878. sample = isl_mat_vec_product(U, sample);
  879. return sample;
  880. error:
  881. isl_basic_set_free(cone);
  882. isl_basic_set_free(bset);
  883. return NULL;
  884. }
  885. static void vec_sum_of_neg(__isl_keep isl_vec *v, isl_int *s)
  886. {
  887. int i;
  888. isl_int_set_si(*s, 0);
  889. for (i = 0; i < v->size; ++i)
  890. if (isl_int_is_neg(v->el[i]))
  891. isl_int_add(*s, *s, v->el[i]);
  892. }
  893. /* Given a tableau "tab", a tableau "tab_cone" that corresponds
  894. * to the recession cone and the inverse of a new basis U = inv(B),
  895. * with the unbounded directions in B last,
  896. * add constraints to "tab" that ensure any rational value
  897. * in the unbounded directions can be rounded up to an integer value.
  898. *
  899. * The new basis is given by x' = B x, i.e., x = U x'.
  900. * For any rational value of the last tab->n_unbounded coordinates
  901. * in the update tableau, the value that is obtained by rounding
  902. * up this value should be contained in the original tableau.
  903. * For any constraint "a x + c >= 0", we therefore need to add
  904. * a constraint "a x + c + s >= 0", with s the sum of all negative
  905. * entries in the last elements of "a U".
  906. *
  907. * Since we are not interested in the first entries of any of the "a U",
  908. * we first drop the columns of U that correpond to bounded directions.
  909. */
  910. static int tab_shift_cone(struct isl_tab *tab,
  911. struct isl_tab *tab_cone, struct isl_mat *U)
  912. {
  913. int i;
  914. isl_int v;
  915. struct isl_basic_set *bset = NULL;
  916. if (tab && tab->n_unbounded == 0) {
  917. isl_mat_free(U);
  918. return 0;
  919. }
  920. isl_int_init(v);
  921. if (!tab || !tab_cone || !U)
  922. goto error;
  923. bset = isl_tab_peek_bset(tab_cone);
  924. U = isl_mat_drop_cols(U, 0, tab->n_var - tab->n_unbounded);
  925. for (i = 0; i < bset->n_ineq; ++i) {
  926. int ok;
  927. struct isl_vec *row = NULL;
  928. if (isl_tab_is_equality(tab_cone, tab_cone->n_eq + i))
  929. continue;
  930. row = isl_vec_alloc(bset->ctx, tab_cone->n_var);
  931. if (!row)
  932. goto error;
  933. isl_seq_cpy(row->el, bset->ineq[i] + 1, tab_cone->n_var);
  934. row = isl_vec_mat_product(row, isl_mat_copy(U));
  935. if (!row)
  936. goto error;
  937. vec_sum_of_neg(row, &v);
  938. isl_vec_free(row);
  939. if (isl_int_is_zero(v))
  940. continue;
  941. if (isl_tab_extend_cons(tab, 1) < 0)
  942. goto error;
  943. isl_int_add(bset->ineq[i][0], bset->ineq[i][0], v);
  944. ok = isl_tab_add_ineq(tab, bset->ineq[i]) >= 0;
  945. isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], v);
  946. if (!ok)
  947. goto error;
  948. }
  949. isl_mat_free(U);
  950. isl_int_clear(v);
  951. return 0;
  952. error:
  953. isl_mat_free(U);
  954. isl_int_clear(v);
  955. return -1;
  956. }
  957. /* Compute and return an initial basis for the possibly
  958. * unbounded tableau "tab". "tab_cone" is a tableau
  959. * for the corresponding recession cone.
  960. * Additionally, add constraints to "tab" that ensure
  961. * that any rational value for the unbounded directions
  962. * can be rounded up to an integer value.
  963. *
  964. * If the tableau is bounded, i.e., if the recession cone
  965. * is zero-dimensional, then we just use inital_basis.
  966. * Otherwise, we construct a basis whose first directions
  967. * correspond to equalities, followed by bounded directions,
  968. * i.e., equalities in the recession cone.
  969. * The remaining directions are then unbounded.
  970. */
  971. int isl_tab_set_initial_basis_with_cone(struct isl_tab *tab,
  972. struct isl_tab *tab_cone)
  973. {
  974. struct isl_mat *eq;
  975. struct isl_mat *cone_eq;
  976. struct isl_mat *U, *Q;
  977. if (!tab || !tab_cone)
  978. return -1;
  979. if (tab_cone->n_col == tab_cone->n_dead) {
  980. tab->basis = initial_basis(tab);
  981. return tab->basis ? 0 : -1;
  982. }
  983. eq = tab_equalities(tab);
  984. if (!eq)
  985. return -1;
  986. tab->n_zero = eq->n_row;
  987. cone_eq = tab_equalities(tab_cone);
  988. eq = isl_mat_concat(eq, cone_eq);
  989. if (!eq)
  990. return -1;
  991. tab->n_unbounded = tab->n_var - (eq->n_row - tab->n_zero);
  992. eq = isl_mat_left_hermite(eq, 0, &U, &Q);
  993. if (!eq)
  994. return -1;
  995. isl_mat_free(eq);
  996. tab->basis = isl_mat_lin_to_aff(Q);
  997. if (tab_shift_cone(tab, tab_cone, U) < 0)
  998. return -1;
  999. if (!tab->basis)
  1000. return -1;
  1001. return 0;
  1002. }
  1003. /* Compute and return a sample point in bset using generalized basis
  1004. * reduction. We first check if the input set has a non-trivial
  1005. * recession cone. If so, we perform some extra preprocessing in
  1006. * sample_with_cone. Otherwise, we directly perform generalized basis
  1007. * reduction.
  1008. */
  1009. static __isl_give isl_vec *gbr_sample(__isl_take isl_basic_set *bset)
  1010. {
  1011. isl_size dim;
  1012. struct isl_basic_set *cone;
  1013. dim = isl_basic_set_dim(bset, isl_dim_all);
  1014. if (dim < 0)
  1015. goto error;
  1016. cone = isl_basic_set_recession_cone(isl_basic_set_copy(bset));
  1017. if (!cone)
  1018. goto error;
  1019. if (cone->n_eq < dim)
  1020. return isl_basic_set_sample_with_cone(bset, cone);
  1021. isl_basic_set_free(cone);
  1022. return sample_bounded(bset);
  1023. error:
  1024. isl_basic_set_free(bset);
  1025. return NULL;
  1026. }
  1027. static __isl_give isl_vec *basic_set_sample(__isl_take isl_basic_set *bset,
  1028. int bounded)
  1029. {
  1030. struct isl_ctx *ctx;
  1031. isl_size dim;
  1032. if (!bset)
  1033. return NULL;
  1034. ctx = bset->ctx;
  1035. if (isl_basic_set_plain_is_empty(bset))
  1036. return empty_sample(bset);
  1037. dim = isl_basic_set_dim(bset, isl_dim_set);
  1038. if (dim < 0 ||
  1039. isl_basic_set_check_no_params(bset) < 0 ||
  1040. isl_basic_set_check_no_locals(bset) < 0)
  1041. goto error;
  1042. if (bset->sample && bset->sample->size == 1 + dim) {
  1043. int contains = isl_basic_set_contains(bset, bset->sample);
  1044. if (contains < 0)
  1045. goto error;
  1046. if (contains) {
  1047. struct isl_vec *sample = isl_vec_copy(bset->sample);
  1048. isl_basic_set_free(bset);
  1049. return sample;
  1050. }
  1051. }
  1052. isl_vec_free(bset->sample);
  1053. bset->sample = NULL;
  1054. if (bset->n_eq > 0)
  1055. return sample_eq(bset, bounded ? isl_basic_set_sample_bounded
  1056. : isl_basic_set_sample_vec);
  1057. if (dim == 0)
  1058. return zero_sample(bset);
  1059. if (dim == 1)
  1060. return interval_sample(bset);
  1061. return bounded ? sample_bounded(bset) : gbr_sample(bset);
  1062. error:
  1063. isl_basic_set_free(bset);
  1064. return NULL;
  1065. }
  1066. __isl_give isl_vec *isl_basic_set_sample_vec(__isl_take isl_basic_set *bset)
  1067. {
  1068. return basic_set_sample(bset, 0);
  1069. }
  1070. /* Compute an integer sample in "bset", where the caller guarantees
  1071. * that "bset" is bounded.
  1072. */
  1073. __isl_give isl_vec *isl_basic_set_sample_bounded(__isl_take isl_basic_set *bset)
  1074. {
  1075. return basic_set_sample(bset, 1);
  1076. }
  1077. __isl_give isl_basic_set *isl_basic_set_from_vec(__isl_take isl_vec *vec)
  1078. {
  1079. int i;
  1080. int k;
  1081. struct isl_basic_set *bset = NULL;
  1082. struct isl_ctx *ctx;
  1083. isl_size dim;
  1084. if (!vec)
  1085. return NULL;
  1086. ctx = vec->ctx;
  1087. isl_assert(ctx, vec->size != 0, goto error);
  1088. bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
  1089. dim = isl_basic_set_dim(bset, isl_dim_set);
  1090. if (dim < 0)
  1091. goto error;
  1092. for (i = dim - 1; i >= 0; --i) {
  1093. k = isl_basic_set_alloc_equality(bset);
  1094. if (k < 0)
  1095. goto error;
  1096. isl_seq_clr(bset->eq[k], 1 + dim);
  1097. isl_int_neg(bset->eq[k][0], vec->el[1 + i]);
  1098. isl_int_set(bset->eq[k][1 + i], vec->el[0]);
  1099. }
  1100. bset->sample = vec;
  1101. return bset;
  1102. error:
  1103. isl_basic_set_free(bset);
  1104. isl_vec_free(vec);
  1105. return NULL;
  1106. }
  1107. __isl_give isl_basic_map *isl_basic_map_sample(__isl_take isl_basic_map *bmap)
  1108. {
  1109. struct isl_basic_set *bset;
  1110. struct isl_vec *sample_vec;
  1111. bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
  1112. sample_vec = isl_basic_set_sample_vec(bset);
  1113. if (!sample_vec)
  1114. goto error;
  1115. if (sample_vec->size == 0) {
  1116. isl_vec_free(sample_vec);
  1117. return isl_basic_map_set_to_empty(bmap);
  1118. }
  1119. isl_vec_free(bmap->sample);
  1120. bmap->sample = isl_vec_copy(sample_vec);
  1121. bset = isl_basic_set_from_vec(sample_vec);
  1122. return isl_basic_map_overlying_set(bset, bmap);
  1123. error:
  1124. isl_basic_map_free(bmap);
  1125. return NULL;
  1126. }
  1127. __isl_give isl_basic_set *isl_basic_set_sample(__isl_take isl_basic_set *bset)
  1128. {
  1129. return isl_basic_map_sample(bset);
  1130. }
  1131. __isl_give isl_basic_map *isl_map_sample(__isl_take isl_map *map)
  1132. {
  1133. int i;
  1134. isl_basic_map *sample = NULL;
  1135. if (!map)
  1136. goto error;
  1137. for (i = 0; i < map->n; ++i) {
  1138. sample = isl_basic_map_sample(isl_basic_map_copy(map->p[i]));
  1139. if (!sample)
  1140. goto error;
  1141. if (!ISL_F_ISSET(sample, ISL_BASIC_MAP_EMPTY))
  1142. break;
  1143. isl_basic_map_free(sample);
  1144. }
  1145. if (i == map->n)
  1146. sample = isl_basic_map_empty(isl_map_get_space(map));
  1147. isl_map_free(map);
  1148. return sample;
  1149. error:
  1150. isl_map_free(map);
  1151. return NULL;
  1152. }
  1153. __isl_give isl_basic_set *isl_set_sample(__isl_take isl_set *set)
  1154. {
  1155. return bset_from_bmap(isl_map_sample(set_to_map(set)));
  1156. }
  1157. __isl_give isl_point *isl_basic_set_sample_point(__isl_take isl_basic_set *bset)
  1158. {
  1159. isl_vec *vec;
  1160. isl_space *space;
  1161. space = isl_basic_set_get_space(bset);
  1162. bset = isl_basic_set_underlying_set(bset);
  1163. vec = isl_basic_set_sample_vec(bset);
  1164. return isl_point_alloc(space, vec);
  1165. }
  1166. __isl_give isl_point *isl_set_sample_point(__isl_take isl_set *set)
  1167. {
  1168. int i;
  1169. isl_point *pnt;
  1170. if (!set)
  1171. return NULL;
  1172. for (i = 0; i < set->n; ++i) {
  1173. pnt = isl_basic_set_sample_point(isl_basic_set_copy(set->p[i]));
  1174. if (!pnt)
  1175. goto error;
  1176. if (!isl_point_is_void(pnt))
  1177. break;
  1178. isl_point_free(pnt);
  1179. }
  1180. if (i == set->n)
  1181. pnt = isl_point_void(isl_set_get_space(set));
  1182. isl_set_free(set);
  1183. return pnt;
  1184. error:
  1185. isl_set_free(set);
  1186. return NULL;
  1187. }