isl_farkas.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. /*
  2. * Copyright 2010 INRIA Saclay
  3. *
  4. * Use of this software is governed by the MIT license
  5. *
  6. * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
  7. * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
  8. * 91893 Orsay, France
  9. */
  10. #include <isl_map_private.h>
  11. #include <isl/set.h>
  12. #include <isl_space_private.h>
  13. #include <isl_seq.h>
  14. #include <isl_aff_private.h>
  15. #include <isl_mat_private.h>
  16. #include <isl_factorization.h>
  17. /*
  18. * Let C be a cone and define
  19. *
  20. * C' := { y | forall x in C : y x >= 0 }
  21. *
  22. * C' contains the coefficients of all linear constraints
  23. * that are valid for C.
  24. * Furthermore, C'' = C.
  25. *
  26. * If C is defined as { x | A x >= 0 }
  27. * then any element in C' must be a non-negative combination
  28. * of the rows of A, i.e., y = t A with t >= 0. That is,
  29. *
  30. * C' = { y | exists t >= 0 : y = t A }
  31. *
  32. * If any of the rows in A actually represents an equality, then
  33. * also negative combinations of this row are allowed and so the
  34. * non-negativity constraint on the corresponding element of t
  35. * can be dropped.
  36. *
  37. * A polyhedron P = { x | b + A x >= 0 } can be represented
  38. * in homogeneous coordinates by the cone
  39. * C = { [z,x] | b z + A x >= and z >= 0 }
  40. * The valid linear constraints on C correspond to the valid affine
  41. * constraints on P.
  42. * This is essentially Farkas' lemma.
  43. *
  44. * Since
  45. * [ 1 0 ]
  46. * [ w y ] = [t_0 t] [ b A ]
  47. *
  48. * we have
  49. *
  50. * C' = { w, y | exists t_0, t >= 0 : y = t A and w = t_0 + t b }
  51. * or
  52. *
  53. * C' = { w, y | exists t >= 0 : y = t A and w - t b >= 0 }
  54. *
  55. * In practice, we introduce an extra variable (w), shifting all
  56. * other variables to the right, and an extra inequality
  57. * (w - t b >= 0) corresponding to the positivity constraint on
  58. * the homogeneous coordinate.
  59. *
  60. * When going back from coefficients to solutions, we immediately
  61. * plug in 1 for z, which corresponds to shifting all variables
  62. * to the left, with the leftmost ending up in the constant position.
  63. */
  64. /* Add the given prefix to all named isl_dim_set dimensions in "space".
  65. */
  66. static __isl_give isl_space *isl_space_prefix(__isl_take isl_space *space,
  67. const char *prefix)
  68. {
  69. int i;
  70. isl_ctx *ctx;
  71. isl_size nvar;
  72. size_t prefix_len = strlen(prefix);
  73. if (!space)
  74. return NULL;
  75. ctx = isl_space_get_ctx(space);
  76. nvar = isl_space_dim(space, isl_dim_set);
  77. if (nvar < 0)
  78. return isl_space_free(space);
  79. for (i = 0; i < nvar; ++i) {
  80. const char *name;
  81. char *prefix_name;
  82. name = isl_space_get_dim_name(space, isl_dim_set, i);
  83. if (!name)
  84. continue;
  85. prefix_name = isl_alloc_array(ctx, char,
  86. prefix_len + strlen(name) + 1);
  87. if (!prefix_name)
  88. goto error;
  89. memcpy(prefix_name, prefix, prefix_len);
  90. strcpy(prefix_name + prefix_len, name);
  91. space = isl_space_set_dim_name(space,
  92. isl_dim_set, i, prefix_name);
  93. free(prefix_name);
  94. }
  95. return space;
  96. error:
  97. isl_space_free(space);
  98. return NULL;
  99. }
  100. /* Given a dimension specification of the solutions space, construct
  101. * a dimension specification for the space of coefficients.
  102. *
  103. * In particular transform
  104. *
  105. * [params] -> { S }
  106. *
  107. * to
  108. *
  109. * { coefficients[[cst, params] -> S] }
  110. *
  111. * and prefix each dimension name with "c_".
  112. */
  113. static __isl_give isl_space *isl_space_coefficients(__isl_take isl_space *space)
  114. {
  115. isl_space *space_param;
  116. isl_size nvar;
  117. isl_size nparam;
  118. nvar = isl_space_dim(space, isl_dim_set);
  119. nparam = isl_space_dim(space, isl_dim_param);
  120. if (nvar < 0 || nparam < 0)
  121. return isl_space_free(space);
  122. space_param = isl_space_copy(space);
  123. space_param = isl_space_drop_dims(space_param, isl_dim_set, 0, nvar);
  124. space_param = isl_space_move_dims(space_param, isl_dim_set, 0,
  125. isl_dim_param, 0, nparam);
  126. space_param = isl_space_prefix(space_param, "c_");
  127. space_param = isl_space_insert_dims(space_param, isl_dim_set, 0, 1);
  128. space_param = isl_space_set_dim_name(space_param,
  129. isl_dim_set, 0, "c_cst");
  130. space = isl_space_drop_dims(space, isl_dim_param, 0, nparam);
  131. space = isl_space_prefix(space, "c_");
  132. space = isl_space_join(isl_space_from_domain(space_param),
  133. isl_space_from_range(space));
  134. space = isl_space_wrap(space);
  135. space = isl_space_set_tuple_name(space, isl_dim_set, "coefficients");
  136. return space;
  137. }
  138. /* Drop the given prefix from all named dimensions of type "type" in "space".
  139. */
  140. static __isl_give isl_space *isl_space_unprefix(__isl_take isl_space *space,
  141. enum isl_dim_type type, const char *prefix)
  142. {
  143. int i;
  144. isl_size n;
  145. size_t prefix_len = strlen(prefix);
  146. n = isl_space_dim(space, type);
  147. if (n < 0)
  148. return isl_space_free(space);
  149. for (i = 0; i < n; ++i) {
  150. const char *name;
  151. name = isl_space_get_dim_name(space, type, i);
  152. if (!name)
  153. continue;
  154. if (strncmp(name, prefix, prefix_len))
  155. continue;
  156. space = isl_space_set_dim_name(space,
  157. type, i, name + prefix_len);
  158. }
  159. return space;
  160. }
  161. /* Given a dimension specification of the space of coefficients, construct
  162. * a dimension specification for the space of solutions.
  163. *
  164. * In particular transform
  165. *
  166. * { coefficients[[cst, params] -> S] }
  167. *
  168. * to
  169. *
  170. * [params] -> { S }
  171. *
  172. * and drop the "c_" prefix from the dimension names.
  173. */
  174. static __isl_give isl_space *isl_space_solutions(__isl_take isl_space *space)
  175. {
  176. isl_size nparam;
  177. space = isl_space_unwrap(space);
  178. space = isl_space_drop_dims(space, isl_dim_in, 0, 1);
  179. space = isl_space_unprefix(space, isl_dim_in, "c_");
  180. space = isl_space_unprefix(space, isl_dim_out, "c_");
  181. nparam = isl_space_dim(space, isl_dim_in);
  182. if (nparam < 0)
  183. return isl_space_free(space);
  184. space = isl_space_move_dims(space,
  185. isl_dim_param, 0, isl_dim_in, 0, nparam);
  186. space = isl_space_range(space);
  187. return space;
  188. }
  189. /* Return the rational universe basic set in the given space.
  190. */
  191. static __isl_give isl_basic_set *rational_universe(__isl_take isl_space *space)
  192. {
  193. isl_basic_set *bset;
  194. bset = isl_basic_set_universe(space);
  195. bset = isl_basic_set_set_rational(bset);
  196. return bset;
  197. }
  198. /* Compute the dual of "bset" by applying Farkas' lemma.
  199. * As explained above, we add an extra dimension to represent
  200. * the coefficient of the constant term when going from solutions
  201. * to coefficients (shift == 1) and we drop the extra dimension when going
  202. * in the opposite direction (shift == -1).
  203. * The dual can be created in an arbitrary space.
  204. * The caller is responsible for putting the result in the appropriate space.
  205. *
  206. * If "bset" is (obviously) empty, then the way this emptiness
  207. * is represented by the constraints does not allow for the application
  208. * of the standard farkas algorithm. We therefore handle this case
  209. * specifically and return the universe basic set.
  210. */
  211. static __isl_give isl_basic_set *farkas(__isl_take isl_basic_set *bset,
  212. int shift)
  213. {
  214. int i, j, k;
  215. isl_ctx *ctx;
  216. isl_space *space;
  217. isl_basic_set *dual = NULL;
  218. isl_size total;
  219. total = isl_basic_set_dim(bset, isl_dim_all);
  220. if (total < 0)
  221. return isl_basic_set_free(bset);
  222. ctx = isl_basic_set_get_ctx(bset);
  223. space = isl_space_set_alloc(ctx, 0, total + shift);
  224. if (isl_basic_set_plain_is_empty(bset)) {
  225. isl_basic_set_free(bset);
  226. return rational_universe(space);
  227. }
  228. dual = isl_basic_set_alloc_space(space, bset->n_eq + bset->n_ineq,
  229. total, bset->n_ineq + (shift > 0));
  230. dual = isl_basic_set_set_rational(dual);
  231. for (i = 0; i < bset->n_eq + bset->n_ineq; ++i) {
  232. k = isl_basic_set_alloc_div(dual);
  233. if (k < 0)
  234. goto error;
  235. isl_int_set_si(dual->div[k][0], 0);
  236. }
  237. for (i = 0; i < total; ++i) {
  238. k = isl_basic_set_alloc_equality(dual);
  239. if (k < 0)
  240. goto error;
  241. isl_seq_clr(dual->eq[k], 1 + shift + total);
  242. isl_int_set_si(dual->eq[k][1 + shift + i], -1);
  243. for (j = 0; j < bset->n_eq; ++j)
  244. isl_int_set(dual->eq[k][1 + shift + total + j],
  245. bset->eq[j][1 + i]);
  246. for (j = 0; j < bset->n_ineq; ++j)
  247. isl_int_set(dual->eq[k][1 + shift + total + bset->n_eq + j],
  248. bset->ineq[j][1 + i]);
  249. }
  250. for (i = 0; i < bset->n_ineq; ++i) {
  251. k = isl_basic_set_alloc_inequality(dual);
  252. if (k < 0)
  253. goto error;
  254. isl_seq_clr(dual->ineq[k],
  255. 1 + shift + total + bset->n_eq + bset->n_ineq);
  256. isl_int_set_si(dual->ineq[k][1 + shift + total + bset->n_eq + i], 1);
  257. }
  258. if (shift > 0) {
  259. k = isl_basic_set_alloc_inequality(dual);
  260. if (k < 0)
  261. goto error;
  262. isl_seq_clr(dual->ineq[k], 2 + total);
  263. isl_int_set_si(dual->ineq[k][1], 1);
  264. for (j = 0; j < bset->n_eq; ++j)
  265. isl_int_neg(dual->ineq[k][2 + total + j],
  266. bset->eq[j][0]);
  267. for (j = 0; j < bset->n_ineq; ++j)
  268. isl_int_neg(dual->ineq[k][2 + total + bset->n_eq + j],
  269. bset->ineq[j][0]);
  270. }
  271. dual = isl_basic_set_remove_divs(dual);
  272. dual = isl_basic_set_simplify(dual);
  273. dual = isl_basic_set_finalize(dual);
  274. isl_basic_set_free(bset);
  275. return dual;
  276. error:
  277. isl_basic_set_free(bset);
  278. isl_basic_set_free(dual);
  279. return NULL;
  280. }
  281. /* Construct a basic set containing the tuples of coefficients of all
  282. * valid affine constraints on the given basic set, ignoring
  283. * the space of input and output and without any further decomposition.
  284. */
  285. static __isl_give isl_basic_set *isl_basic_set_coefficients_base(
  286. __isl_take isl_basic_set *bset)
  287. {
  288. return farkas(bset, 1);
  289. }
  290. /* Return the inverse mapping of "morph".
  291. */
  292. static __isl_give isl_mat *peek_inv(__isl_keep isl_morph *morph)
  293. {
  294. return morph ? morph->inv : NULL;
  295. }
  296. /* Return a copy of the inverse mapping of "morph".
  297. */
  298. static __isl_give isl_mat *get_inv(__isl_keep isl_morph *morph)
  299. {
  300. return isl_mat_copy(peek_inv(morph));
  301. }
  302. /* Information about a single factor within isl_basic_set_coefficients_product.
  303. *
  304. * "start" is the position of the first coefficient (beyond
  305. * the one corresponding to the constant term) in this factor.
  306. * "dim" is the number of coefficients (other than
  307. * the one corresponding to the constant term) in this factor.
  308. * "n_line" is the number of lines in "coeff".
  309. * "n_ray" is the number of rays (other than lines) in "coeff".
  310. * "n_vertex" is the number of vertices in "coeff".
  311. *
  312. * While iterating over the vertices,
  313. * "pos" represents the inequality constraint corresponding
  314. * to the current vertex.
  315. */
  316. struct isl_coefficients_factor_data {
  317. isl_basic_set *coeff;
  318. int start;
  319. int dim;
  320. int n_line;
  321. int n_ray;
  322. int n_vertex;
  323. int pos;
  324. };
  325. /* Internal data structure for isl_basic_set_coefficients_product.
  326. * "n" is the number of factors in the factorization.
  327. * "pos" is the next factor that will be considered.
  328. * "start_next" is the position of the first coefficient (beyond
  329. * the one corresponding to the constant term) in the next factor.
  330. * "factors" contains information about the individual "n" factors.
  331. */
  332. struct isl_coefficients_product_data {
  333. int n;
  334. int pos;
  335. int start_next;
  336. struct isl_coefficients_factor_data *factors;
  337. };
  338. /* Initialize the internal data structure for
  339. * isl_basic_set_coefficients_product.
  340. */
  341. static isl_stat isl_coefficients_product_data_init(isl_ctx *ctx,
  342. struct isl_coefficients_product_data *data, int n)
  343. {
  344. data->n = n;
  345. data->pos = 0;
  346. data->start_next = 0;
  347. data->factors = isl_calloc_array(ctx,
  348. struct isl_coefficients_factor_data, n);
  349. if (!data->factors)
  350. return isl_stat_error;
  351. return isl_stat_ok;
  352. }
  353. /* Free all memory allocated in "data".
  354. */
  355. static void isl_coefficients_product_data_clear(
  356. struct isl_coefficients_product_data *data)
  357. {
  358. int i;
  359. if (data->factors) {
  360. for (i = 0; i < data->n; ++i) {
  361. isl_basic_set_free(data->factors[i].coeff);
  362. }
  363. }
  364. free(data->factors);
  365. }
  366. /* Does inequality "ineq" in the (dual) basic set "bset" represent a ray?
  367. * In particular, does it have a zero denominator
  368. * (i.e., a zero coefficient for the constant term)?
  369. */
  370. static int is_ray(__isl_keep isl_basic_set *bset, int ineq)
  371. {
  372. return isl_int_is_zero(bset->ineq[ineq][1]);
  373. }
  374. /* isl_factorizer_every_factor_basic_set callback that
  375. * constructs a basic set containing the tuples of coefficients of all
  376. * valid affine constraints on the factor "bset" and
  377. * extracts further information that will be used
  378. * when combining the results over the different factors.
  379. */
  380. static isl_bool isl_basic_set_coefficients_factor(
  381. __isl_keep isl_basic_set *bset, void *user)
  382. {
  383. struct isl_coefficients_product_data *data = user;
  384. isl_basic_set *coeff;
  385. isl_size n_eq, n_ineq, dim;
  386. int i, n_ray, n_vertex;
  387. coeff = isl_basic_set_coefficients_base(isl_basic_set_copy(bset));
  388. data->factors[data->pos].coeff = coeff;
  389. if (!coeff)
  390. return isl_bool_error;
  391. dim = isl_basic_set_dim(bset, isl_dim_set);
  392. n_eq = isl_basic_set_n_equality(coeff);
  393. n_ineq = isl_basic_set_n_inequality(coeff);
  394. if (dim < 0 || n_eq < 0 || n_ineq < 0)
  395. return isl_bool_error;
  396. n_ray = n_vertex = 0;
  397. for (i = 0; i < n_ineq; ++i) {
  398. if (is_ray(coeff, i))
  399. n_ray++;
  400. else
  401. n_vertex++;
  402. }
  403. data->factors[data->pos].start = data->start_next;
  404. data->factors[data->pos].dim = dim;
  405. data->factors[data->pos].n_line = n_eq;
  406. data->factors[data->pos].n_ray = n_ray;
  407. data->factors[data->pos].n_vertex = n_vertex;
  408. data->pos++;
  409. data->start_next += dim;
  410. return isl_bool_true;
  411. }
  412. /* Clear an entry in the product, given that there is a "total" number
  413. * of coefficients (other than that of the constant term).
  414. */
  415. static void clear_entry(isl_int *entry, int total)
  416. {
  417. isl_seq_clr(entry, 1 + 1 + total);
  418. }
  419. /* Set the part of the entry corresponding to factor "data",
  420. * from the factor coefficients in "src".
  421. */
  422. static void set_factor(isl_int *entry, isl_int *src,
  423. struct isl_coefficients_factor_data *data)
  424. {
  425. isl_seq_cpy(entry + 1 + 1 + data->start, src + 1 + 1, data->dim);
  426. }
  427. /* Set the part of the entry corresponding to factor "data",
  428. * from the factor coefficients in "src" multiplied by "f".
  429. */
  430. static void scale_factor(isl_int *entry, isl_int *src, isl_int f,
  431. struct isl_coefficients_factor_data *data)
  432. {
  433. isl_seq_scale(entry + 1 + 1 + data->start, src + 1 + 1, f, data->dim);
  434. }
  435. /* Add all lines from the given factor to "bset",
  436. * given that there is a "total" number of coefficients
  437. * (other than that of the constant term).
  438. */
  439. static __isl_give isl_basic_set *add_lines(__isl_take isl_basic_set *bset,
  440. struct isl_coefficients_factor_data *factor, int total)
  441. {
  442. int i;
  443. for (i = 0; i < factor->n_line; ++i) {
  444. int k;
  445. k = isl_basic_set_alloc_equality(bset);
  446. if (k < 0)
  447. return isl_basic_set_free(bset);
  448. clear_entry(bset->eq[k], total);
  449. set_factor(bset->eq[k], factor->coeff->eq[i], factor);
  450. }
  451. return bset;
  452. }
  453. /* Add all rays (other than lines) from the given factor to "bset",
  454. * given that there is a "total" number of coefficients
  455. * (other than that of the constant term).
  456. */
  457. static __isl_give isl_basic_set *add_rays(__isl_take isl_basic_set *bset,
  458. struct isl_coefficients_factor_data *data, int total)
  459. {
  460. int i;
  461. int n_ineq = data->n_ray + data->n_vertex;
  462. for (i = 0; i < n_ineq; ++i) {
  463. int k;
  464. if (!is_ray(data->coeff, i))
  465. continue;
  466. k = isl_basic_set_alloc_inequality(bset);
  467. if (k < 0)
  468. return isl_basic_set_free(bset);
  469. clear_entry(bset->ineq[k], total);
  470. set_factor(bset->ineq[k], data->coeff->ineq[i], data);
  471. }
  472. return bset;
  473. }
  474. /* Move to the first vertex of the given factor starting
  475. * at inequality constraint "start", setting factor->pos and
  476. * returning 1 if a vertex is found.
  477. */
  478. static int factor_first_vertex(struct isl_coefficients_factor_data *factor,
  479. int start)
  480. {
  481. int j;
  482. int n = factor->n_ray + factor->n_vertex;
  483. for (j = start; j < n; ++j) {
  484. if (is_ray(factor->coeff, j))
  485. continue;
  486. factor->pos = j;
  487. return 1;
  488. }
  489. return 0;
  490. }
  491. /* Move to the first constraint in each factor starting at "first"
  492. * that represents a vertex.
  493. * In particular, skip the initial constraints that correspond to rays.
  494. */
  495. static void first_vertex(struct isl_coefficients_product_data *data, int first)
  496. {
  497. int i;
  498. for (i = first; i < data->n; ++i)
  499. factor_first_vertex(&data->factors[i], 0);
  500. }
  501. /* Move to the next vertex in the product.
  502. * In particular, move to the next vertex of the last factor.
  503. * If all vertices of this last factor have already been considered,
  504. * then move to the next vertex of the previous factor(s)
  505. * until a factor is found that still has a next vertex.
  506. * Once such a next vertex has been found, the subsequent
  507. * factors are reset to the first vertex.
  508. * Return 1 if any next vertex was found.
  509. */
  510. static int next_vertex(struct isl_coefficients_product_data *data)
  511. {
  512. int i;
  513. for (i = data->n - 1; i >= 0; --i) {
  514. struct isl_coefficients_factor_data *factor = &data->factors[i];
  515. if (!factor_first_vertex(factor, factor->pos + 1))
  516. continue;
  517. first_vertex(data, i + 1);
  518. return 1;
  519. }
  520. return 0;
  521. }
  522. /* Add a vertex to the product "bset" combining the currently selected
  523. * vertices of the factors.
  524. *
  525. * In the dual representation, the constant term is always zero.
  526. * The vertex itself is the sum of the contributions of the factors
  527. * with a shared denominator in position 1.
  528. *
  529. * First compute the shared denominator (lcm) and
  530. * then scale the numerators to this shared denominator.
  531. */
  532. static __isl_give isl_basic_set *add_vertex(__isl_take isl_basic_set *bset,
  533. struct isl_coefficients_product_data *data)
  534. {
  535. int i;
  536. int k;
  537. isl_int lcm, f;
  538. k = isl_basic_set_alloc_inequality(bset);
  539. if (k < 0)
  540. return isl_basic_set_free(bset);
  541. isl_int_init(lcm);
  542. isl_int_init(f);
  543. isl_int_set_si(lcm, 1);
  544. for (i = 0; i < data->n; ++i) {
  545. struct isl_coefficients_factor_data *factor = &data->factors[i];
  546. isl_basic_set *coeff = factor->coeff;
  547. int pos = factor->pos;
  548. isl_int_lcm(lcm, lcm, coeff->ineq[pos][1]);
  549. }
  550. isl_int_set_si(bset->ineq[k][0], 0);
  551. isl_int_set(bset->ineq[k][1], lcm);
  552. for (i = 0; i < data->n; ++i) {
  553. struct isl_coefficients_factor_data *factor = &data->factors[i];
  554. isl_basic_set *coeff = factor->coeff;
  555. int pos = factor->pos;
  556. isl_int_divexact(f, lcm, coeff->ineq[pos][1]);
  557. scale_factor(bset->ineq[k], coeff->ineq[pos], f, factor);
  558. }
  559. isl_int_clear(f);
  560. isl_int_clear(lcm);
  561. return bset;
  562. }
  563. /* Combine the duals of the factors in the factorization of a basic set
  564. * to form the dual of the entire basic set.
  565. * The dual share the coefficient of the constant term.
  566. * All other coefficients are specific to a factor.
  567. * Any constraint not involving the coefficient of the constant term
  568. * can therefor simply be copied into the appropriate position.
  569. * This includes all equality constraints since the coefficient
  570. * of the constant term can always be increased and therefore
  571. * never appears in an equality constraint.
  572. * The inequality constraints involving the coefficient of
  573. * the constant term need to be combined across factors.
  574. * In particular, if this coefficient needs to be greater than or equal
  575. * to some linear combination of the other coefficients in each factor,
  576. * then it needs to be greater than or equal to the sum of
  577. * these linear combinations across the factors.
  578. *
  579. * Alternatively, the constraints of the dual can be seen
  580. * as the vertices, rays and lines of the original basic set.
  581. * Clearly, rays and lines can simply be copied,
  582. * while vertices needs to be combined across factors.
  583. * This means that the number of rays and lines in the product
  584. * is equal to the sum of the numbers in the factors,
  585. * while the number of vertices is the product
  586. * of the number of vertices in the factors. Note that each
  587. * factor has at least one vertex.
  588. * The only exception is when the factor is the dual of an obviously empty set,
  589. * in which case a universe dual is created.
  590. * In this case, return a universe dual for the product as well.
  591. *
  592. * While constructing the vertices, look for the first combination
  593. * of inequality constraints that represent a vertex,
  594. * construct the corresponding vertex and then move on
  595. * to the next combination of inequality constraints until
  596. * all combinations have been considered.
  597. */
  598. static __isl_give isl_basic_set *construct_product(isl_ctx *ctx,
  599. struct isl_coefficients_product_data *data)
  600. {
  601. int i;
  602. int n_line, n_ray, n_vertex;
  603. int total;
  604. isl_space *space;
  605. isl_basic_set *product;
  606. if (!data->factors)
  607. return NULL;
  608. total = data->start_next;
  609. n_line = 0;
  610. n_ray = 0;
  611. n_vertex = 1;
  612. for (i = 0; i < data->n; ++i) {
  613. n_line += data->factors[i].n_line;
  614. n_ray += data->factors[i].n_ray;
  615. n_vertex *= data->factors[i].n_vertex;
  616. }
  617. space = isl_space_set_alloc(ctx, 0, 1 + total);
  618. if (n_vertex == 0)
  619. return rational_universe(space);
  620. product = isl_basic_set_alloc_space(space, 0, n_line, n_ray + n_vertex);
  621. product = isl_basic_set_set_rational(product);
  622. for (i = 0; i < data->n; ++i)
  623. product = add_lines(product, &data->factors[i], total);
  624. for (i = 0; i < data->n; ++i)
  625. product = add_rays(product, &data->factors[i], total);
  626. first_vertex(data, 0);
  627. do {
  628. product = add_vertex(product, data);
  629. } while (next_vertex(data));
  630. return product;
  631. }
  632. /* Given a factorization "f" of a basic set,
  633. * construct a basic set containing the tuples of coefficients of all
  634. * valid affine constraints on the product of the factors, ignoring
  635. * the space of input and output.
  636. * Note that this product may not be equal to the original basic set,
  637. * if a non-trivial transformation is involved.
  638. * This is handled by the caller.
  639. *
  640. * Compute the tuples of coefficients for each factor separately and
  641. * then combine the results.
  642. */
  643. static __isl_give isl_basic_set *isl_basic_set_coefficients_product(
  644. __isl_take isl_factorizer *f)
  645. {
  646. struct isl_coefficients_product_data data;
  647. isl_ctx *ctx;
  648. isl_basic_set *coeff;
  649. isl_bool every;
  650. ctx = isl_factorizer_get_ctx(f);
  651. if (isl_coefficients_product_data_init(ctx, &data, f->n_group) < 0)
  652. f = isl_factorizer_free(f);
  653. every = isl_factorizer_every_factor_basic_set(f,
  654. &isl_basic_set_coefficients_factor, &data);
  655. isl_factorizer_free(f);
  656. if (every >= 0)
  657. coeff = construct_product(ctx, &data);
  658. else
  659. coeff = NULL;
  660. isl_coefficients_product_data_clear(&data);
  661. return coeff;
  662. }
  663. /* Given a factorization "f" of a basic set,
  664. * construct a basic set containing the tuples of coefficients of all
  665. * valid affine constraints on the basic set, ignoring
  666. * the space of input and output.
  667. *
  668. * The factorization may involve a linear transformation of the basic set.
  669. * In particular, the transformed basic set is formulated
  670. * in terms of x' = U x, i.e., x = V x', with V = U^{-1}.
  671. * The dual is then computed in terms of y' with y'^t [z; x'] >= 0.
  672. * Plugging in y' = [1 0; 0 V^t] y yields
  673. * y^t [1 0; 0 V] [z; x'] >= 0, i.e., y^t [z; x] >= 0, which is
  674. * the desired set of coefficients y.
  675. * Note that this transformation to y' only needs to be applied
  676. * if U is not the identity matrix.
  677. */
  678. static __isl_give isl_basic_set *isl_basic_set_coefficients_morphed_product(
  679. __isl_take isl_factorizer *f)
  680. {
  681. isl_bool is_identity;
  682. isl_space *space;
  683. isl_mat *inv;
  684. isl_multi_aff *ma;
  685. isl_basic_set *coeff;
  686. if (!f)
  687. goto error;
  688. is_identity = isl_mat_is_scaled_identity(peek_inv(f->morph));
  689. if (is_identity < 0)
  690. goto error;
  691. if (is_identity)
  692. return isl_basic_set_coefficients_product(f);
  693. inv = get_inv(f->morph);
  694. inv = isl_mat_transpose(inv);
  695. inv = isl_mat_lin_to_aff(inv);
  696. coeff = isl_basic_set_coefficients_product(f);
  697. space = isl_space_map_from_set(isl_basic_set_get_space(coeff));
  698. ma = isl_multi_aff_from_aff_mat(space, inv);
  699. coeff = isl_basic_set_preimage_multi_aff(coeff, ma);
  700. return coeff;
  701. error:
  702. isl_factorizer_free(f);
  703. return NULL;
  704. }
  705. /* Construct a basic set containing the tuples of coefficients of all
  706. * valid affine constraints on the given basic set, ignoring
  707. * the space of input and output.
  708. *
  709. * The caller has already checked that "bset" does not involve
  710. * any local variables. It may have parameters, though.
  711. * Treat them as regular variables internally.
  712. * This is especially important for the factorization,
  713. * since the (original) parameters should be taken into account
  714. * explicitly in this factorization.
  715. *
  716. * Check if the basic set can be factorized.
  717. * If so, compute constraints on the coefficients of the factors
  718. * separately and combine the results.
  719. * Otherwise, compute the results for the input basic set as a whole.
  720. */
  721. static __isl_give isl_basic_set *basic_set_coefficients(
  722. __isl_take isl_basic_set *bset)
  723. {
  724. isl_factorizer *f;
  725. isl_size nparam;
  726. nparam = isl_basic_set_dim(bset, isl_dim_param);
  727. if (nparam < 0)
  728. return isl_basic_set_free(bset);
  729. bset = isl_basic_set_move_dims(bset, isl_dim_set, 0,
  730. isl_dim_param, 0, nparam);
  731. f = isl_basic_set_factorizer(bset);
  732. if (!f)
  733. return isl_basic_set_free(bset);
  734. if (f->n_group > 0) {
  735. isl_basic_set_free(bset);
  736. return isl_basic_set_coefficients_morphed_product(f);
  737. }
  738. isl_factorizer_free(f);
  739. return isl_basic_set_coefficients_base(bset);
  740. }
  741. /* Construct a basic set containing the tuples of coefficients of all
  742. * valid affine constraints on the given basic set.
  743. */
  744. __isl_give isl_basic_set *isl_basic_set_coefficients(
  745. __isl_take isl_basic_set *bset)
  746. {
  747. isl_space *space;
  748. if (!bset)
  749. return NULL;
  750. if (bset->n_div)
  751. isl_die(bset->ctx, isl_error_invalid,
  752. "input set not allowed to have local variables",
  753. goto error);
  754. space = isl_basic_set_get_space(bset);
  755. space = isl_space_coefficients(space);
  756. bset = basic_set_coefficients(bset);
  757. bset = isl_basic_set_reset_space(bset, space);
  758. return bset;
  759. error:
  760. isl_basic_set_free(bset);
  761. return NULL;
  762. }
  763. /* Construct a basic set containing the elements that satisfy all
  764. * affine constraints whose coefficient tuples are
  765. * contained in the given basic set.
  766. */
  767. __isl_give isl_basic_set *isl_basic_set_solutions(
  768. __isl_take isl_basic_set *bset)
  769. {
  770. isl_space *space;
  771. if (!bset)
  772. return NULL;
  773. if (bset->n_div)
  774. isl_die(bset->ctx, isl_error_invalid,
  775. "input set not allowed to have local variables",
  776. goto error);
  777. space = isl_basic_set_get_space(bset);
  778. space = isl_space_solutions(space);
  779. bset = farkas(bset, -1);
  780. bset = isl_basic_set_reset_space(bset, space);
  781. return bset;
  782. error:
  783. isl_basic_set_free(bset);
  784. return NULL;
  785. }
  786. /* Construct a basic set containing the tuples of coefficients of all
  787. * valid affine constraints on the given set.
  788. */
  789. __isl_give isl_basic_set *isl_set_coefficients(__isl_take isl_set *set)
  790. {
  791. int i;
  792. isl_basic_set *coeff;
  793. if (!set)
  794. return NULL;
  795. if (set->n == 0) {
  796. isl_space *space = isl_set_get_space(set);
  797. space = isl_space_coefficients(space);
  798. isl_set_free(set);
  799. return rational_universe(space);
  800. }
  801. coeff = isl_basic_set_coefficients(isl_basic_set_copy(set->p[0]));
  802. for (i = 1; i < set->n; ++i) {
  803. isl_basic_set *bset, *coeff_i;
  804. bset = isl_basic_set_copy(set->p[i]);
  805. coeff_i = isl_basic_set_coefficients(bset);
  806. coeff = isl_basic_set_intersect(coeff, coeff_i);
  807. }
  808. isl_set_free(set);
  809. return coeff;
  810. }
  811. /* Wrapper around isl_basic_set_coefficients for use
  812. * as a isl_basic_set_list_map callback.
  813. */
  814. static __isl_give isl_basic_set *coefficients_wrap(
  815. __isl_take isl_basic_set *bset, void *user)
  816. {
  817. return isl_basic_set_coefficients(bset);
  818. }
  819. /* Replace the elements of "list" by the result of applying
  820. * isl_basic_set_coefficients to them.
  821. */
  822. __isl_give isl_basic_set_list *isl_basic_set_list_coefficients(
  823. __isl_take isl_basic_set_list *list)
  824. {
  825. return isl_basic_set_list_map(list, &coefficients_wrap, NULL);
  826. }
  827. /* Construct a basic set containing the elements that satisfy all
  828. * affine constraints whose coefficient tuples are
  829. * contained in the given set.
  830. */
  831. __isl_give isl_basic_set *isl_set_solutions(__isl_take isl_set *set)
  832. {
  833. int i;
  834. isl_basic_set *sol;
  835. if (!set)
  836. return NULL;
  837. if (set->n == 0) {
  838. isl_space *space = isl_set_get_space(set);
  839. space = isl_space_solutions(space);
  840. isl_set_free(set);
  841. return rational_universe(space);
  842. }
  843. sol = isl_basic_set_solutions(isl_basic_set_copy(set->p[0]));
  844. for (i = 1; i < set->n; ++i) {
  845. isl_basic_set *bset, *sol_i;
  846. bset = isl_basic_set_copy(set->p[i]);
  847. sol_i = isl_basic_set_solutions(bset);
  848. sol = isl_basic_set_intersect(sol, sol_i);
  849. }
  850. isl_set_free(set);
  851. return sol;
  852. }