isl_mat.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110
  1. /*
  2. * Copyright 2008-2009 Katholieke Universiteit Leuven
  3. * Copyright 2010 INRIA Saclay
  4. * Copyright 2014 Ecole Normale Superieure
  5. * Copyright 2017 Sven Verdoolaege
  6. *
  7. * Use of this software is governed by the MIT license
  8. *
  9. * Written by Sven Verdoolaege, K.U.Leuven, Departement
  10. * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
  11. * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
  12. * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
  13. * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
  14. */
  15. #include <isl_ctx_private.h>
  16. #include <isl_map_private.h>
  17. #include <isl/space.h>
  18. #include <isl_seq.h>
  19. #include <isl_mat_private.h>
  20. #include <isl_vec_private.h>
  21. #include <isl_space_private.h>
  22. #include <isl_val_private.h>
  23. isl_ctx *isl_mat_get_ctx(__isl_keep isl_mat *mat)
  24. {
  25. return mat ? mat->ctx : NULL;
  26. }
  27. /* Return a hash value that digests "mat".
  28. */
  29. uint32_t isl_mat_get_hash(__isl_keep isl_mat *mat)
  30. {
  31. int i;
  32. uint32_t hash;
  33. if (!mat)
  34. return 0;
  35. hash = isl_hash_init();
  36. isl_hash_byte(hash, mat->n_row & 0xFF);
  37. isl_hash_byte(hash, mat->n_col & 0xFF);
  38. for (i = 0; i < mat->n_row; ++i) {
  39. uint32_t row_hash;
  40. row_hash = isl_seq_get_hash(mat->row[i], mat->n_col);
  41. isl_hash_hash(hash, row_hash);
  42. }
  43. return hash;
  44. }
  45. __isl_give isl_mat *isl_mat_alloc(isl_ctx *ctx,
  46. unsigned n_row, unsigned n_col)
  47. {
  48. int i;
  49. struct isl_mat *mat;
  50. mat = isl_alloc_type(ctx, struct isl_mat);
  51. if (!mat)
  52. return NULL;
  53. mat->row = NULL;
  54. mat->block = isl_blk_alloc(ctx, n_row * n_col);
  55. if (isl_blk_is_error(mat->block))
  56. goto error;
  57. mat->row = isl_calloc_array(ctx, isl_int *, n_row);
  58. if (n_row && !mat->row)
  59. goto error;
  60. if (n_col != 0) {
  61. for (i = 0; i < n_row; ++i)
  62. mat->row[i] = mat->block.data + i * n_col;
  63. }
  64. mat->ctx = ctx;
  65. isl_ctx_ref(ctx);
  66. mat->ref = 1;
  67. mat->n_row = n_row;
  68. mat->n_col = n_col;
  69. mat->max_col = n_col;
  70. mat->flags = 0;
  71. return mat;
  72. error:
  73. isl_blk_free(ctx, mat->block);
  74. free(mat);
  75. return NULL;
  76. }
  77. __isl_give isl_mat *isl_mat_extend(__isl_take isl_mat *mat,
  78. unsigned n_row, unsigned n_col)
  79. {
  80. int i;
  81. isl_int *old;
  82. isl_int **row;
  83. if (!mat)
  84. return NULL;
  85. if (mat->max_col >= n_col && mat->n_row >= n_row) {
  86. if (mat->n_col < n_col)
  87. mat->n_col = n_col;
  88. return mat;
  89. }
  90. if (mat->max_col < n_col) {
  91. struct isl_mat *new_mat;
  92. if (n_row < mat->n_row)
  93. n_row = mat->n_row;
  94. new_mat = isl_mat_alloc(mat->ctx, n_row, n_col);
  95. if (!new_mat)
  96. goto error;
  97. for (i = 0; i < mat->n_row; ++i)
  98. isl_seq_cpy(new_mat->row[i], mat->row[i], mat->n_col);
  99. isl_mat_free(mat);
  100. return new_mat;
  101. }
  102. mat = isl_mat_cow(mat);
  103. if (!mat)
  104. goto error;
  105. old = mat->block.data;
  106. mat->block = isl_blk_extend(mat->ctx, mat->block, n_row * mat->max_col);
  107. if (isl_blk_is_error(mat->block))
  108. goto error;
  109. row = isl_realloc_array(mat->ctx, mat->row, isl_int *, n_row);
  110. if (n_row && !row)
  111. goto error;
  112. mat->row = row;
  113. for (i = 0; i < mat->n_row; ++i)
  114. mat->row[i] = mat->block.data + (mat->row[i] - old);
  115. for (i = mat->n_row; i < n_row; ++i)
  116. mat->row[i] = mat->block.data + i * mat->max_col;
  117. mat->n_row = n_row;
  118. if (mat->n_col < n_col)
  119. mat->n_col = n_col;
  120. return mat;
  121. error:
  122. isl_mat_free(mat);
  123. return NULL;
  124. }
  125. __isl_give isl_mat *isl_mat_sub_alloc6(isl_ctx *ctx, isl_int **row,
  126. unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
  127. {
  128. int i;
  129. struct isl_mat *mat;
  130. mat = isl_alloc_type(ctx, struct isl_mat);
  131. if (!mat)
  132. return NULL;
  133. mat->row = isl_alloc_array(ctx, isl_int *, n_row);
  134. if (n_row && !mat->row)
  135. goto error;
  136. for (i = 0; i < n_row; ++i)
  137. mat->row[i] = row[first_row+i] + first_col;
  138. mat->ctx = ctx;
  139. isl_ctx_ref(ctx);
  140. mat->ref = 1;
  141. mat->n_row = n_row;
  142. mat->n_col = n_col;
  143. mat->block = isl_blk_empty();
  144. mat->flags = ISL_MAT_BORROWED;
  145. return mat;
  146. error:
  147. free(mat);
  148. return NULL;
  149. }
  150. __isl_give isl_mat *isl_mat_sub_alloc(__isl_keep isl_mat *mat,
  151. unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
  152. {
  153. if (!mat)
  154. return NULL;
  155. return isl_mat_sub_alloc6(mat->ctx, mat->row, first_row, n_row,
  156. first_col, n_col);
  157. }
  158. void isl_mat_sub_copy(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
  159. unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
  160. {
  161. int i;
  162. for (i = 0; i < n_row; ++i)
  163. isl_seq_cpy(dst[i]+dst_col, src[i]+src_col, n_col);
  164. }
  165. void isl_mat_sub_neg(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
  166. unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
  167. {
  168. int i;
  169. for (i = 0; i < n_row; ++i)
  170. isl_seq_neg(dst[i]+dst_col, src[i]+src_col, n_col);
  171. }
  172. __isl_give isl_mat *isl_mat_copy(__isl_keep isl_mat *mat)
  173. {
  174. if (!mat)
  175. return NULL;
  176. mat->ref++;
  177. return mat;
  178. }
  179. __isl_give isl_mat *isl_mat_dup(__isl_keep isl_mat *mat)
  180. {
  181. int i;
  182. struct isl_mat *mat2;
  183. if (!mat)
  184. return NULL;
  185. mat2 = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
  186. if (!mat2)
  187. return NULL;
  188. for (i = 0; i < mat->n_row; ++i)
  189. isl_seq_cpy(mat2->row[i], mat->row[i], mat->n_col);
  190. return mat2;
  191. }
  192. __isl_give isl_mat *isl_mat_cow(__isl_take isl_mat *mat)
  193. {
  194. struct isl_mat *mat2;
  195. if (!mat)
  196. return NULL;
  197. if (mat->ref == 1 && !ISL_F_ISSET(mat, ISL_MAT_BORROWED))
  198. return mat;
  199. mat2 = isl_mat_dup(mat);
  200. isl_mat_free(mat);
  201. return mat2;
  202. }
  203. __isl_null isl_mat *isl_mat_free(__isl_take isl_mat *mat)
  204. {
  205. if (!mat)
  206. return NULL;
  207. if (--mat->ref > 0)
  208. return NULL;
  209. if (!ISL_F_ISSET(mat, ISL_MAT_BORROWED))
  210. isl_blk_free(mat->ctx, mat->block);
  211. isl_ctx_deref(mat->ctx);
  212. free(mat->row);
  213. free(mat);
  214. return NULL;
  215. }
  216. isl_size isl_mat_rows(__isl_keep isl_mat *mat)
  217. {
  218. return mat ? mat->n_row : isl_size_error;
  219. }
  220. isl_size isl_mat_cols(__isl_keep isl_mat *mat)
  221. {
  222. return mat ? mat->n_col : isl_size_error;
  223. }
  224. /* Check that "col" is a valid column position for "mat".
  225. */
  226. static isl_stat check_col(__isl_keep isl_mat *mat, int col)
  227. {
  228. if (!mat)
  229. return isl_stat_error;
  230. if (col < 0 || col >= mat->n_col)
  231. isl_die(isl_mat_get_ctx(mat), isl_error_invalid,
  232. "column out of range", return isl_stat_error);
  233. return isl_stat_ok;
  234. }
  235. /* Check that "row" is a valid row position for "mat".
  236. */
  237. static isl_stat check_row(__isl_keep isl_mat *mat, int row)
  238. {
  239. if (!mat)
  240. return isl_stat_error;
  241. if (row < 0 || row >= mat->n_row)
  242. isl_die(isl_mat_get_ctx(mat), isl_error_invalid,
  243. "row out of range", return isl_stat_error);
  244. return isl_stat_ok;
  245. }
  246. /* Check that there are "n" columns starting at position "first" in "mat".
  247. */
  248. static isl_stat check_col_range(__isl_keep isl_mat *mat, unsigned first,
  249. unsigned n)
  250. {
  251. if (!mat)
  252. return isl_stat_error;
  253. if (first + n > mat->n_col || first + n < first)
  254. isl_die(isl_mat_get_ctx(mat), isl_error_invalid,
  255. "column position or range out of bounds",
  256. return isl_stat_error);
  257. return isl_stat_ok;
  258. }
  259. /* Check that there are "n" rows starting at position "first" in "mat".
  260. */
  261. static isl_stat check_row_range(__isl_keep isl_mat *mat, unsigned first,
  262. unsigned n)
  263. {
  264. if (!mat)
  265. return isl_stat_error;
  266. if (first + n > mat->n_row || first + n < first)
  267. isl_die(isl_mat_get_ctx(mat), isl_error_invalid,
  268. "row position or range out of bounds",
  269. return isl_stat_error);
  270. return isl_stat_ok;
  271. }
  272. int isl_mat_get_element(__isl_keep isl_mat *mat, int row, int col, isl_int *v)
  273. {
  274. if (check_row(mat, row) < 0)
  275. return -1;
  276. if (check_col(mat, col) < 0)
  277. return -1;
  278. isl_int_set(*v, mat->row[row][col]);
  279. return 0;
  280. }
  281. /* Extract the element at row "row", oolumn "col" of "mat".
  282. */
  283. __isl_give isl_val *isl_mat_get_element_val(__isl_keep isl_mat *mat,
  284. int row, int col)
  285. {
  286. isl_ctx *ctx;
  287. if (check_row(mat, row) < 0)
  288. return NULL;
  289. if (check_col(mat, col) < 0)
  290. return NULL;
  291. ctx = isl_mat_get_ctx(mat);
  292. return isl_val_int_from_isl_int(ctx, mat->row[row][col]);
  293. }
  294. __isl_give isl_mat *isl_mat_set_element(__isl_take isl_mat *mat,
  295. int row, int col, isl_int v)
  296. {
  297. mat = isl_mat_cow(mat);
  298. if (check_row(mat, row) < 0)
  299. return isl_mat_free(mat);
  300. if (check_col(mat, col) < 0)
  301. return isl_mat_free(mat);
  302. isl_int_set(mat->row[row][col], v);
  303. return mat;
  304. }
  305. __isl_give isl_mat *isl_mat_set_element_si(__isl_take isl_mat *mat,
  306. int row, int col, int v)
  307. {
  308. mat = isl_mat_cow(mat);
  309. if (check_row(mat, row) < 0)
  310. return isl_mat_free(mat);
  311. if (check_col(mat, col) < 0)
  312. return isl_mat_free(mat);
  313. isl_int_set_si(mat->row[row][col], v);
  314. return mat;
  315. }
  316. /* Replace the element at row "row", column "col" of "mat" by "v".
  317. */
  318. __isl_give isl_mat *isl_mat_set_element_val(__isl_take isl_mat *mat,
  319. int row, int col, __isl_take isl_val *v)
  320. {
  321. if (!v)
  322. return isl_mat_free(mat);
  323. if (!isl_val_is_int(v))
  324. isl_die(isl_val_get_ctx(v), isl_error_invalid,
  325. "expecting integer value", goto error);
  326. mat = isl_mat_set_element(mat, row, col, v->n);
  327. isl_val_free(v);
  328. return mat;
  329. error:
  330. isl_val_free(v);
  331. return isl_mat_free(mat);
  332. }
  333. __isl_give isl_mat *isl_mat_diag(isl_ctx *ctx, unsigned n_row, isl_int d)
  334. {
  335. int i;
  336. struct isl_mat *mat;
  337. mat = isl_mat_alloc(ctx, n_row, n_row);
  338. if (!mat)
  339. return NULL;
  340. for (i = 0; i < n_row; ++i) {
  341. isl_seq_clr(mat->row[i], i);
  342. isl_int_set(mat->row[i][i], d);
  343. isl_seq_clr(mat->row[i]+i+1, n_row-(i+1));
  344. }
  345. return mat;
  346. }
  347. /* Create an "n_row" by "n_col" matrix with zero elements.
  348. */
  349. __isl_give isl_mat *isl_mat_zero(isl_ctx *ctx, unsigned n_row, unsigned n_col)
  350. {
  351. int i;
  352. isl_mat *mat;
  353. mat = isl_mat_alloc(ctx, n_row, n_col);
  354. if (!mat)
  355. return NULL;
  356. for (i = 0; i < n_row; ++i)
  357. isl_seq_clr(mat->row[i], n_col);
  358. return mat;
  359. }
  360. __isl_give isl_mat *isl_mat_identity(isl_ctx *ctx, unsigned n_row)
  361. {
  362. if (!ctx)
  363. return NULL;
  364. return isl_mat_diag(ctx, n_row, ctx->one);
  365. }
  366. /* Is "mat" a (possibly scaled) identity matrix?
  367. */
  368. isl_bool isl_mat_is_scaled_identity(__isl_keep isl_mat *mat)
  369. {
  370. int i;
  371. if (!mat)
  372. return isl_bool_error;
  373. if (mat->n_row != mat->n_col)
  374. return isl_bool_false;
  375. for (i = 0; i < mat->n_row; ++i) {
  376. if (isl_seq_first_non_zero(mat->row[i], i) != -1)
  377. return isl_bool_false;
  378. if (isl_int_ne(mat->row[0][0], mat->row[i][i]))
  379. return isl_bool_false;
  380. if (isl_seq_first_non_zero(mat->row[i] + i + 1,
  381. mat->n_col - (i + 1)) != -1)
  382. return isl_bool_false;
  383. }
  384. return isl_bool_true;
  385. }
  386. __isl_give isl_vec *isl_mat_vec_product(__isl_take isl_mat *mat,
  387. __isl_take isl_vec *vec)
  388. {
  389. int i;
  390. struct isl_vec *prod;
  391. if (!mat || !vec)
  392. goto error;
  393. isl_assert(mat->ctx, mat->n_col == vec->size, goto error);
  394. prod = isl_vec_alloc(mat->ctx, mat->n_row);
  395. if (!prod)
  396. goto error;
  397. for (i = 0; i < prod->size; ++i)
  398. isl_seq_inner_product(mat->row[i], vec->el, vec->size,
  399. &prod->block.data[i]);
  400. isl_mat_free(mat);
  401. isl_vec_free(vec);
  402. return prod;
  403. error:
  404. isl_mat_free(mat);
  405. isl_vec_free(vec);
  406. return NULL;
  407. }
  408. __isl_give isl_vec *isl_mat_vec_inverse_product(__isl_take isl_mat *mat,
  409. __isl_take isl_vec *vec)
  410. {
  411. struct isl_mat *vec_mat;
  412. int i;
  413. if (!mat || !vec)
  414. goto error;
  415. vec_mat = isl_mat_alloc(vec->ctx, vec->size, 1);
  416. if (!vec_mat)
  417. goto error;
  418. for (i = 0; i < vec->size; ++i)
  419. isl_int_set(vec_mat->row[i][0], vec->el[i]);
  420. vec_mat = isl_mat_inverse_product(mat, vec_mat);
  421. isl_vec_free(vec);
  422. if (!vec_mat)
  423. return NULL;
  424. vec = isl_vec_alloc(vec_mat->ctx, vec_mat->n_row);
  425. if (vec)
  426. for (i = 0; i < vec->size; ++i)
  427. isl_int_set(vec->el[i], vec_mat->row[i][0]);
  428. isl_mat_free(vec_mat);
  429. return vec;
  430. error:
  431. isl_mat_free(mat);
  432. isl_vec_free(vec);
  433. return NULL;
  434. }
  435. __isl_give isl_vec *isl_vec_mat_product(__isl_take isl_vec *vec,
  436. __isl_take isl_mat *mat)
  437. {
  438. int i, j;
  439. struct isl_vec *prod;
  440. if (!mat || !vec)
  441. goto error;
  442. isl_assert(mat->ctx, mat->n_row == vec->size, goto error);
  443. prod = isl_vec_alloc(mat->ctx, mat->n_col);
  444. if (!prod)
  445. goto error;
  446. for (i = 0; i < prod->size; ++i) {
  447. isl_int_set_si(prod->el[i], 0);
  448. for (j = 0; j < vec->size; ++j)
  449. isl_int_addmul(prod->el[i], vec->el[j], mat->row[j][i]);
  450. }
  451. isl_mat_free(mat);
  452. isl_vec_free(vec);
  453. return prod;
  454. error:
  455. isl_mat_free(mat);
  456. isl_vec_free(vec);
  457. return NULL;
  458. }
  459. __isl_give isl_mat *isl_mat_aff_direct_sum(__isl_take isl_mat *left,
  460. __isl_take isl_mat *right)
  461. {
  462. int i;
  463. struct isl_mat *sum;
  464. if (!left || !right)
  465. goto error;
  466. isl_assert(left->ctx, left->n_row == right->n_row, goto error);
  467. isl_assert(left->ctx, left->n_row >= 1, goto error);
  468. isl_assert(left->ctx, left->n_col >= 1, goto error);
  469. isl_assert(left->ctx, right->n_col >= 1, goto error);
  470. isl_assert(left->ctx,
  471. isl_seq_first_non_zero(left->row[0]+1, left->n_col-1) == -1,
  472. goto error);
  473. isl_assert(left->ctx,
  474. isl_seq_first_non_zero(right->row[0]+1, right->n_col-1) == -1,
  475. goto error);
  476. sum = isl_mat_alloc(left->ctx, left->n_row, left->n_col + right->n_col - 1);
  477. if (!sum)
  478. goto error;
  479. isl_int_lcm(sum->row[0][0], left->row[0][0], right->row[0][0]);
  480. isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
  481. isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
  482. isl_seq_clr(sum->row[0]+1, sum->n_col-1);
  483. for (i = 1; i < sum->n_row; ++i) {
  484. isl_int_mul(sum->row[i][0], left->row[0][0], left->row[i][0]);
  485. isl_int_addmul(sum->row[i][0],
  486. right->row[0][0], right->row[i][0]);
  487. isl_seq_scale(sum->row[i]+1, left->row[i]+1, left->row[0][0],
  488. left->n_col-1);
  489. isl_seq_scale(sum->row[i]+left->n_col,
  490. right->row[i]+1, right->row[0][0],
  491. right->n_col-1);
  492. }
  493. isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
  494. isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
  495. isl_mat_free(left);
  496. isl_mat_free(right);
  497. return sum;
  498. error:
  499. isl_mat_free(left);
  500. isl_mat_free(right);
  501. return NULL;
  502. }
  503. static void exchange(__isl_keep isl_mat *M, __isl_keep isl_mat **U,
  504. __isl_keep isl_mat **Q, unsigned row, unsigned i, unsigned j)
  505. {
  506. int r;
  507. for (r = row; r < M->n_row; ++r)
  508. isl_int_swap(M->row[r][i], M->row[r][j]);
  509. if (U) {
  510. for (r = 0; r < (*U)->n_row; ++r)
  511. isl_int_swap((*U)->row[r][i], (*U)->row[r][j]);
  512. }
  513. if (Q)
  514. isl_mat_swap_rows(*Q, i, j);
  515. }
  516. static void subtract(__isl_keep isl_mat *M, __isl_keep isl_mat **U,
  517. __isl_keep isl_mat **Q, unsigned row, unsigned i, unsigned j, isl_int m)
  518. {
  519. int r;
  520. for (r = row; r < M->n_row; ++r)
  521. isl_int_submul(M->row[r][j], m, M->row[r][i]);
  522. if (U) {
  523. for (r = 0; r < (*U)->n_row; ++r)
  524. isl_int_submul((*U)->row[r][j], m, (*U)->row[r][i]);
  525. }
  526. if (Q) {
  527. for (r = 0; r < (*Q)->n_col; ++r)
  528. isl_int_addmul((*Q)->row[i][r], m, (*Q)->row[j][r]);
  529. }
  530. }
  531. static void oppose(__isl_keep isl_mat *M, __isl_keep isl_mat **U,
  532. __isl_keep isl_mat **Q, unsigned row, unsigned col)
  533. {
  534. int r;
  535. for (r = row; r < M->n_row; ++r)
  536. isl_int_neg(M->row[r][col], M->row[r][col]);
  537. if (U) {
  538. for (r = 0; r < (*U)->n_row; ++r)
  539. isl_int_neg((*U)->row[r][col], (*U)->row[r][col]);
  540. }
  541. if (Q)
  542. isl_seq_neg((*Q)->row[col], (*Q)->row[col], (*Q)->n_col);
  543. }
  544. /* Given matrix M, compute
  545. *
  546. * M U = H
  547. * M = H Q
  548. *
  549. * with U and Q unimodular matrices and H a matrix in column echelon form
  550. * such that on each echelon row the entries in the non-echelon column
  551. * are non-negative (if neg == 0) or non-positive (if neg == 1)
  552. * and strictly smaller (in absolute value) than the entries in the echelon
  553. * column.
  554. * If U or Q are NULL, then these matrices are not computed.
  555. */
  556. __isl_give isl_mat *isl_mat_left_hermite(__isl_take isl_mat *M, int neg,
  557. __isl_give isl_mat **U, __isl_give isl_mat **Q)
  558. {
  559. isl_int c;
  560. int row, col;
  561. if (U)
  562. *U = NULL;
  563. if (Q)
  564. *Q = NULL;
  565. if (!M)
  566. goto error;
  567. if (U) {
  568. *U = isl_mat_identity(M->ctx, M->n_col);
  569. if (!*U)
  570. goto error;
  571. }
  572. if (Q) {
  573. *Q = isl_mat_identity(M->ctx, M->n_col);
  574. if (!*Q)
  575. goto error;
  576. }
  577. if (M->n_col == 0)
  578. return M;
  579. M = isl_mat_cow(M);
  580. if (!M)
  581. goto error;
  582. col = 0;
  583. isl_int_init(c);
  584. for (row = 0; row < M->n_row; ++row) {
  585. int first, i, off;
  586. first = isl_seq_abs_min_non_zero(M->row[row]+col, M->n_col-col);
  587. if (first == -1)
  588. continue;
  589. first += col;
  590. if (first != col)
  591. exchange(M, U, Q, row, first, col);
  592. if (isl_int_is_neg(M->row[row][col]))
  593. oppose(M, U, Q, row, col);
  594. first = col+1;
  595. while ((off = isl_seq_first_non_zero(M->row[row]+first,
  596. M->n_col-first)) != -1) {
  597. first += off;
  598. isl_int_fdiv_q(c, M->row[row][first], M->row[row][col]);
  599. subtract(M, U, Q, row, col, first, c);
  600. if (!isl_int_is_zero(M->row[row][first]))
  601. exchange(M, U, Q, row, first, col);
  602. else
  603. ++first;
  604. }
  605. for (i = 0; i < col; ++i) {
  606. if (isl_int_is_zero(M->row[row][i]))
  607. continue;
  608. if (neg)
  609. isl_int_cdiv_q(c, M->row[row][i], M->row[row][col]);
  610. else
  611. isl_int_fdiv_q(c, M->row[row][i], M->row[row][col]);
  612. if (isl_int_is_zero(c))
  613. continue;
  614. subtract(M, U, Q, row, col, i, c);
  615. }
  616. ++col;
  617. }
  618. isl_int_clear(c);
  619. return M;
  620. error:
  621. if (Q) {
  622. isl_mat_free(*Q);
  623. *Q = NULL;
  624. }
  625. if (U) {
  626. isl_mat_free(*U);
  627. *U = NULL;
  628. }
  629. isl_mat_free(M);
  630. return NULL;
  631. }
  632. /* Use row "row" of "mat" to eliminate column "col" from all other rows.
  633. */
  634. static __isl_give isl_mat *eliminate(__isl_take isl_mat *mat, int row, int col)
  635. {
  636. int k;
  637. isl_size nr, nc;
  638. isl_ctx *ctx;
  639. nr = isl_mat_rows(mat);
  640. nc = isl_mat_cols(mat);
  641. if (nr < 0 || nc < 0)
  642. return isl_mat_free(mat);
  643. ctx = isl_mat_get_ctx(mat);
  644. for (k = 0; k < nr; ++k) {
  645. if (k == row)
  646. continue;
  647. if (isl_int_is_zero(mat->row[k][col]))
  648. continue;
  649. mat = isl_mat_cow(mat);
  650. if (!mat)
  651. return NULL;
  652. isl_seq_elim(mat->row[k], mat->row[row], col, nc, NULL);
  653. isl_seq_normalize(ctx, mat->row[k], nc);
  654. }
  655. return mat;
  656. }
  657. /* Perform Gaussian elimination on the rows of "mat", but start
  658. * from the final row and the final column.
  659. * Any zero rows that result from the elimination are removed.
  660. *
  661. * In particular, for each column from last to first,
  662. * look for the last row with a non-zero coefficient in that column,
  663. * move it last (but before other rows moved last in previous steps) and
  664. * use it to eliminate the column from the other rows.
  665. */
  666. __isl_give isl_mat *isl_mat_reverse_gauss(__isl_take isl_mat *mat)
  667. {
  668. int k, row, last;
  669. isl_size nr, nc;
  670. nr = isl_mat_rows(mat);
  671. nc = isl_mat_cols(mat);
  672. if (nr < 0 || nc < 0)
  673. return isl_mat_free(mat);
  674. last = nc - 1;
  675. for (row = nr - 1; row >= 0; --row) {
  676. for (; last >= 0; --last) {
  677. for (k = row; k >= 0; --k)
  678. if (!isl_int_is_zero(mat->row[k][last]))
  679. break;
  680. if (k >= 0)
  681. break;
  682. }
  683. if (last < 0)
  684. break;
  685. if (k != row)
  686. mat = isl_mat_swap_rows(mat, k, row);
  687. if (!mat)
  688. return NULL;
  689. if (isl_int_is_neg(mat->row[row][last]))
  690. mat = isl_mat_row_neg(mat, row);
  691. mat = eliminate(mat, row, last);
  692. if (!mat)
  693. return NULL;
  694. }
  695. mat = isl_mat_drop_rows(mat, 0, row + 1);
  696. return mat;
  697. }
  698. /* Negate the lexicographically negative rows of "mat" such that
  699. * all rows in the result are lexicographically non-negative.
  700. */
  701. __isl_give isl_mat *isl_mat_lexnonneg_rows(__isl_take isl_mat *mat)
  702. {
  703. int i;
  704. isl_size nr, nc;
  705. nr = isl_mat_rows(mat);
  706. nc = isl_mat_cols(mat);
  707. if (nr < 0 || nc < 0)
  708. return isl_mat_free(mat);
  709. for (i = 0; i < nr; ++i) {
  710. int pos;
  711. pos = isl_seq_first_non_zero(mat->row[i], nc);
  712. if (pos < 0)
  713. continue;
  714. if (isl_int_is_nonneg(mat->row[i][pos]))
  715. continue;
  716. mat = isl_mat_row_neg(mat, i);
  717. if (!mat)
  718. return NULL;
  719. }
  720. return mat;
  721. }
  722. /* Given a matrix "H" is column echelon form, what is the first
  723. * zero column? That is how many initial columns are non-zero?
  724. * Start looking at column "first_col" and only consider
  725. * the columns to be of size "n_row".
  726. * "H" is assumed to be non-NULL.
  727. *
  728. * Since "H" is in column echelon form, the first non-zero entry
  729. * in a column is always in a later position compared to the previous column.
  730. */
  731. static int hermite_first_zero_col(__isl_keep isl_mat *H, int first_col,
  732. int n_row)
  733. {
  734. int row, col;
  735. for (col = first_col, row = 0; col < H->n_col; ++col) {
  736. for (; row < n_row; ++row)
  737. if (!isl_int_is_zero(H->row[row][col]))
  738. break;
  739. if (row == n_row)
  740. return col;
  741. }
  742. return H->n_col;
  743. }
  744. /* Return the rank of "mat", or isl_size_error in case of error.
  745. */
  746. isl_size isl_mat_rank(__isl_keep isl_mat *mat)
  747. {
  748. int rank;
  749. isl_mat *H;
  750. H = isl_mat_left_hermite(isl_mat_copy(mat), 0, NULL, NULL);
  751. if (!H)
  752. return isl_size_error;
  753. rank = hermite_first_zero_col(H, 0, H->n_row);
  754. isl_mat_free(H);
  755. return rank;
  756. }
  757. __isl_give isl_mat *isl_mat_right_kernel(__isl_take isl_mat *mat)
  758. {
  759. int rank;
  760. struct isl_mat *U = NULL;
  761. struct isl_mat *K;
  762. mat = isl_mat_left_hermite(mat, 0, &U, NULL);
  763. if (!mat || !U)
  764. goto error;
  765. rank = hermite_first_zero_col(mat, 0, mat->n_row);
  766. K = isl_mat_alloc(U->ctx, U->n_row, U->n_col - rank);
  767. if (!K)
  768. goto error;
  769. isl_mat_sub_copy(K->ctx, K->row, U->row, U->n_row, 0, rank, U->n_col-rank);
  770. isl_mat_free(mat);
  771. isl_mat_free(U);
  772. return K;
  773. error:
  774. isl_mat_free(mat);
  775. isl_mat_free(U);
  776. return NULL;
  777. }
  778. __isl_give isl_mat *isl_mat_lin_to_aff(__isl_take isl_mat *mat)
  779. {
  780. int i;
  781. struct isl_mat *mat2;
  782. if (!mat)
  783. return NULL;
  784. mat2 = isl_mat_alloc(mat->ctx, 1+mat->n_row, 1+mat->n_col);
  785. if (!mat2)
  786. goto error;
  787. isl_int_set_si(mat2->row[0][0], 1);
  788. isl_seq_clr(mat2->row[0]+1, mat->n_col);
  789. for (i = 0; i < mat->n_row; ++i) {
  790. isl_int_set_si(mat2->row[1+i][0], 0);
  791. isl_seq_cpy(mat2->row[1+i]+1, mat->row[i], mat->n_col);
  792. }
  793. isl_mat_free(mat);
  794. return mat2;
  795. error:
  796. isl_mat_free(mat);
  797. return NULL;
  798. }
  799. /* Given two matrices M1 and M2, return the block matrix
  800. *
  801. * [ M1 0 ]
  802. * [ 0 M2 ]
  803. */
  804. __isl_give isl_mat *isl_mat_diagonal(__isl_take isl_mat *mat1,
  805. __isl_take isl_mat *mat2)
  806. {
  807. int i;
  808. isl_mat *mat;
  809. if (!mat1 || !mat2)
  810. goto error;
  811. mat = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
  812. mat1->n_col + mat2->n_col);
  813. if (!mat)
  814. goto error;
  815. for (i = 0; i < mat1->n_row; ++i) {
  816. isl_seq_cpy(mat->row[i], mat1->row[i], mat1->n_col);
  817. isl_seq_clr(mat->row[i] + mat1->n_col, mat2->n_col);
  818. }
  819. for (i = 0; i < mat2->n_row; ++i) {
  820. isl_seq_clr(mat->row[mat1->n_row + i], mat1->n_col);
  821. isl_seq_cpy(mat->row[mat1->n_row + i] + mat1->n_col,
  822. mat2->row[i], mat2->n_col);
  823. }
  824. isl_mat_free(mat1);
  825. isl_mat_free(mat2);
  826. return mat;
  827. error:
  828. isl_mat_free(mat1);
  829. isl_mat_free(mat2);
  830. return NULL;
  831. }
  832. static int row_first_non_zero(isl_int **row, unsigned n_row, unsigned col)
  833. {
  834. int i;
  835. for (i = 0; i < n_row; ++i)
  836. if (!isl_int_is_zero(row[i][col]))
  837. return i;
  838. return -1;
  839. }
  840. static int row_abs_min_non_zero(isl_int **row, unsigned n_row, unsigned col)
  841. {
  842. int i, min = row_first_non_zero(row, n_row, col);
  843. if (min < 0)
  844. return -1;
  845. for (i = min + 1; i < n_row; ++i) {
  846. if (isl_int_is_zero(row[i][col]))
  847. continue;
  848. if (isl_int_abs_lt(row[i][col], row[min][col]))
  849. min = i;
  850. }
  851. return min;
  852. }
  853. static isl_stat inv_exchange(__isl_keep isl_mat **left,
  854. __isl_keep isl_mat **right, unsigned i, unsigned j)
  855. {
  856. *left = isl_mat_swap_rows(*left, i, j);
  857. *right = isl_mat_swap_rows(*right, i, j);
  858. if (!*left || !*right)
  859. return isl_stat_error;
  860. return isl_stat_ok;
  861. }
  862. static void inv_oppose(
  863. __isl_keep isl_mat *left, __isl_keep isl_mat *right, unsigned row)
  864. {
  865. isl_seq_neg(left->row[row]+row, left->row[row]+row, left->n_col-row);
  866. isl_seq_neg(right->row[row], right->row[row], right->n_col);
  867. }
  868. static void inv_subtract(__isl_keep isl_mat *left, __isl_keep isl_mat *right,
  869. unsigned row, unsigned i, isl_int m)
  870. {
  871. isl_int_neg(m, m);
  872. isl_seq_combine(left->row[i]+row,
  873. left->ctx->one, left->row[i]+row,
  874. m, left->row[row]+row,
  875. left->n_col-row);
  876. isl_seq_combine(right->row[i], right->ctx->one, right->row[i],
  877. m, right->row[row], right->n_col);
  878. }
  879. /* Compute inv(left)*right
  880. */
  881. __isl_give isl_mat *isl_mat_inverse_product(__isl_take isl_mat *left,
  882. __isl_take isl_mat *right)
  883. {
  884. int row;
  885. isl_int a, b;
  886. if (!left || !right)
  887. goto error;
  888. isl_assert(left->ctx, left->n_row == left->n_col, goto error);
  889. isl_assert(left->ctx, left->n_row == right->n_row, goto error);
  890. if (left->n_row == 0) {
  891. isl_mat_free(left);
  892. return right;
  893. }
  894. left = isl_mat_cow(left);
  895. right = isl_mat_cow(right);
  896. if (!left || !right)
  897. goto error;
  898. isl_int_init(a);
  899. isl_int_init(b);
  900. for (row = 0; row < left->n_row; ++row) {
  901. int pivot, first, i, off;
  902. pivot = row_abs_min_non_zero(left->row+row, left->n_row-row, row);
  903. if (pivot < 0) {
  904. isl_int_clear(a);
  905. isl_int_clear(b);
  906. isl_assert(left->ctx, pivot >= 0, goto error);
  907. }
  908. pivot += row;
  909. if (pivot != row)
  910. if (inv_exchange(&left, &right, pivot, row) < 0)
  911. goto error;
  912. if (isl_int_is_neg(left->row[row][row]))
  913. inv_oppose(left, right, row);
  914. first = row+1;
  915. while ((off = row_first_non_zero(left->row+first,
  916. left->n_row-first, row)) != -1) {
  917. first += off;
  918. isl_int_fdiv_q(a, left->row[first][row],
  919. left->row[row][row]);
  920. inv_subtract(left, right, row, first, a);
  921. if (!isl_int_is_zero(left->row[first][row])) {
  922. if (inv_exchange(&left, &right, row, first) < 0)
  923. goto error;
  924. } else {
  925. ++first;
  926. }
  927. }
  928. for (i = 0; i < row; ++i) {
  929. if (isl_int_is_zero(left->row[i][row]))
  930. continue;
  931. isl_int_gcd(a, left->row[row][row], left->row[i][row]);
  932. isl_int_divexact(b, left->row[i][row], a);
  933. isl_int_divexact(a, left->row[row][row], a);
  934. isl_int_neg(b, b);
  935. isl_seq_combine(left->row[i] + i,
  936. a, left->row[i] + i,
  937. b, left->row[row] + i,
  938. left->n_col - i);
  939. isl_seq_combine(right->row[i], a, right->row[i],
  940. b, right->row[row], right->n_col);
  941. }
  942. }
  943. isl_int_clear(b);
  944. isl_int_set(a, left->row[0][0]);
  945. for (row = 1; row < left->n_row; ++row)
  946. isl_int_lcm(a, a, left->row[row][row]);
  947. if (isl_int_is_zero(a)){
  948. isl_int_clear(a);
  949. isl_assert(left->ctx, 0, goto error);
  950. }
  951. for (row = 0; row < left->n_row; ++row) {
  952. isl_int_divexact(left->row[row][row], a, left->row[row][row]);
  953. if (isl_int_is_one(left->row[row][row]))
  954. continue;
  955. isl_seq_scale(right->row[row], right->row[row],
  956. left->row[row][row], right->n_col);
  957. }
  958. isl_int_clear(a);
  959. isl_mat_free(left);
  960. return right;
  961. error:
  962. isl_mat_free(left);
  963. isl_mat_free(right);
  964. return NULL;
  965. }
  966. void isl_mat_col_scale(__isl_keep isl_mat *mat, unsigned col, isl_int m)
  967. {
  968. int i;
  969. for (i = 0; i < mat->n_row; ++i)
  970. isl_int_mul(mat->row[i][col], mat->row[i][col], m);
  971. }
  972. void isl_mat_col_combine(__isl_keep isl_mat *mat, unsigned dst,
  973. isl_int m1, unsigned src1, isl_int m2, unsigned src2)
  974. {
  975. int i;
  976. isl_int tmp;
  977. isl_int_init(tmp);
  978. for (i = 0; i < mat->n_row; ++i) {
  979. isl_int_mul(tmp, m1, mat->row[i][src1]);
  980. isl_int_addmul(tmp, m2, mat->row[i][src2]);
  981. isl_int_set(mat->row[i][dst], tmp);
  982. }
  983. isl_int_clear(tmp);
  984. }
  985. __isl_give isl_mat *isl_mat_right_inverse(__isl_take isl_mat *mat)
  986. {
  987. struct isl_mat *inv;
  988. int row;
  989. isl_int a, b;
  990. mat = isl_mat_cow(mat);
  991. if (!mat)
  992. return NULL;
  993. inv = isl_mat_identity(mat->ctx, mat->n_col);
  994. inv = isl_mat_cow(inv);
  995. if (!inv)
  996. goto error;
  997. isl_int_init(a);
  998. isl_int_init(b);
  999. for (row = 0; row < mat->n_row; ++row) {
  1000. int pivot, first, i, off;
  1001. pivot = isl_seq_abs_min_non_zero(mat->row[row]+row, mat->n_col-row);
  1002. if (pivot < 0) {
  1003. isl_int_clear(a);
  1004. isl_int_clear(b);
  1005. isl_assert(mat->ctx, pivot >= 0, goto error);
  1006. }
  1007. pivot += row;
  1008. if (pivot != row)
  1009. exchange(mat, &inv, NULL, row, pivot, row);
  1010. if (isl_int_is_neg(mat->row[row][row]))
  1011. oppose(mat, &inv, NULL, row, row);
  1012. first = row+1;
  1013. while ((off = isl_seq_first_non_zero(mat->row[row]+first,
  1014. mat->n_col-first)) != -1) {
  1015. first += off;
  1016. isl_int_fdiv_q(a, mat->row[row][first],
  1017. mat->row[row][row]);
  1018. subtract(mat, &inv, NULL, row, row, first, a);
  1019. if (!isl_int_is_zero(mat->row[row][first]))
  1020. exchange(mat, &inv, NULL, row, row, first);
  1021. else
  1022. ++first;
  1023. }
  1024. for (i = 0; i < row; ++i) {
  1025. if (isl_int_is_zero(mat->row[row][i]))
  1026. continue;
  1027. isl_int_gcd(a, mat->row[row][row], mat->row[row][i]);
  1028. isl_int_divexact(b, mat->row[row][i], a);
  1029. isl_int_divexact(a, mat->row[row][row], a);
  1030. isl_int_neg(a, a);
  1031. isl_mat_col_combine(mat, i, a, i, b, row);
  1032. isl_mat_col_combine(inv, i, a, i, b, row);
  1033. }
  1034. }
  1035. isl_int_clear(b);
  1036. isl_int_set(a, mat->row[0][0]);
  1037. for (row = 1; row < mat->n_row; ++row)
  1038. isl_int_lcm(a, a, mat->row[row][row]);
  1039. if (isl_int_is_zero(a)){
  1040. isl_int_clear(a);
  1041. goto error;
  1042. }
  1043. for (row = 0; row < mat->n_row; ++row) {
  1044. isl_int_divexact(mat->row[row][row], a, mat->row[row][row]);
  1045. if (isl_int_is_one(mat->row[row][row]))
  1046. continue;
  1047. isl_mat_col_scale(inv, row, mat->row[row][row]);
  1048. }
  1049. isl_int_clear(a);
  1050. isl_mat_free(mat);
  1051. return inv;
  1052. error:
  1053. isl_mat_free(mat);
  1054. isl_mat_free(inv);
  1055. return NULL;
  1056. }
  1057. __isl_give isl_mat *isl_mat_transpose(__isl_take isl_mat *mat)
  1058. {
  1059. struct isl_mat *transpose = NULL;
  1060. int i, j;
  1061. if (!mat)
  1062. return NULL;
  1063. if (mat->n_col == mat->n_row) {
  1064. mat = isl_mat_cow(mat);
  1065. if (!mat)
  1066. return NULL;
  1067. for (i = 0; i < mat->n_row; ++i)
  1068. for (j = i + 1; j < mat->n_col; ++j)
  1069. isl_int_swap(mat->row[i][j], mat->row[j][i]);
  1070. return mat;
  1071. }
  1072. transpose = isl_mat_alloc(mat->ctx, mat->n_col, mat->n_row);
  1073. if (!transpose)
  1074. goto error;
  1075. for (i = 0; i < mat->n_row; ++i)
  1076. for (j = 0; j < mat->n_col; ++j)
  1077. isl_int_set(transpose->row[j][i], mat->row[i][j]);
  1078. isl_mat_free(mat);
  1079. return transpose;
  1080. error:
  1081. isl_mat_free(mat);
  1082. return NULL;
  1083. }
  1084. __isl_give isl_mat *isl_mat_swap_cols(__isl_take isl_mat *mat,
  1085. unsigned i, unsigned j)
  1086. {
  1087. int r;
  1088. mat = isl_mat_cow(mat);
  1089. if (check_col_range(mat, i, 1) < 0 ||
  1090. check_col_range(mat, j, 1) < 0)
  1091. return isl_mat_free(mat);
  1092. for (r = 0; r < mat->n_row; ++r)
  1093. isl_int_swap(mat->row[r][i], mat->row[r][j]);
  1094. return mat;
  1095. }
  1096. __isl_give isl_mat *isl_mat_swap_rows(__isl_take isl_mat *mat,
  1097. unsigned i, unsigned j)
  1098. {
  1099. isl_int *t;
  1100. if (!mat)
  1101. return NULL;
  1102. mat = isl_mat_cow(mat);
  1103. if (check_row_range(mat, i, 1) < 0 ||
  1104. check_row_range(mat, j, 1) < 0)
  1105. return isl_mat_free(mat);
  1106. t = mat->row[i];
  1107. mat->row[i] = mat->row[j];
  1108. mat->row[j] = t;
  1109. return mat;
  1110. }
  1111. /* Calculate the product of two matrices.
  1112. *
  1113. * This function is optimized for operand matrices that contain many zeros and
  1114. * skips multiplications where we know one of the operands is zero.
  1115. */
  1116. __isl_give isl_mat *isl_mat_product(__isl_take isl_mat *left,
  1117. __isl_take isl_mat *right)
  1118. {
  1119. int i, j, k;
  1120. struct isl_mat *prod;
  1121. if (!left || !right)
  1122. goto error;
  1123. isl_assert(left->ctx, left->n_col == right->n_row, goto error);
  1124. prod = isl_mat_alloc(left->ctx, left->n_row, right->n_col);
  1125. if (!prod)
  1126. goto error;
  1127. if (left->n_col == 0) {
  1128. for (i = 0; i < prod->n_row; ++i)
  1129. isl_seq_clr(prod->row[i], prod->n_col);
  1130. isl_mat_free(left);
  1131. isl_mat_free(right);
  1132. return prod;
  1133. }
  1134. for (i = 0; i < prod->n_row; ++i) {
  1135. for (j = 0; j < prod->n_col; ++j)
  1136. isl_int_mul(prod->row[i][j],
  1137. left->row[i][0], right->row[0][j]);
  1138. for (k = 1; k < left->n_col; ++k) {
  1139. if (isl_int_is_zero(left->row[i][k]))
  1140. continue;
  1141. for (j = 0; j < prod->n_col; ++j)
  1142. isl_int_addmul(prod->row[i][j],
  1143. left->row[i][k], right->row[k][j]);
  1144. }
  1145. }
  1146. isl_mat_free(left);
  1147. isl_mat_free(right);
  1148. return prod;
  1149. error:
  1150. isl_mat_free(left);
  1151. isl_mat_free(right);
  1152. return NULL;
  1153. }
  1154. /* Replace the variables x in the rows q by x' given by x = M x',
  1155. * with M the matrix mat.
  1156. *
  1157. * If the number of new variables is greater than the original
  1158. * number of variables, then the rows q have already been
  1159. * preextended. If the new number is smaller, then the coefficients
  1160. * of the divs, which are not changed, need to be shifted down.
  1161. * The row q may be the equalities, the inequalities or the
  1162. * div expressions. In the latter case, has_div is true and
  1163. * we need to take into account the extra denominator column.
  1164. */
  1165. static int preimage(struct isl_ctx *ctx, isl_int **q, unsigned n,
  1166. unsigned n_div, int has_div, struct isl_mat *mat)
  1167. {
  1168. int i;
  1169. struct isl_mat *t;
  1170. int e;
  1171. if (mat->n_col >= mat->n_row)
  1172. e = 0;
  1173. else
  1174. e = mat->n_row - mat->n_col;
  1175. if (has_div)
  1176. for (i = 0; i < n; ++i)
  1177. isl_int_mul(q[i][0], q[i][0], mat->row[0][0]);
  1178. t = isl_mat_sub_alloc6(mat->ctx, q, 0, n, has_div, mat->n_row);
  1179. t = isl_mat_product(t, mat);
  1180. if (!t)
  1181. return -1;
  1182. for (i = 0; i < n; ++i) {
  1183. isl_seq_swp_or_cpy(q[i] + has_div, t->row[i], t->n_col);
  1184. isl_seq_cpy(q[i] + has_div + t->n_col,
  1185. q[i] + has_div + t->n_col + e, n_div);
  1186. isl_seq_clr(q[i] + has_div + t->n_col + n_div, e);
  1187. }
  1188. isl_mat_free(t);
  1189. return 0;
  1190. }
  1191. /* Replace the variables x in bset by x' given by x = M x', with
  1192. * M the matrix mat.
  1193. *
  1194. * If there are fewer variables x' then there are x, then we perform
  1195. * the transformation in place, which means that, in principle,
  1196. * this frees up some extra variables as the number
  1197. * of columns remains constant, but we would have to extend
  1198. * the div array too as the number of rows in this array is assumed
  1199. * to be equal to extra.
  1200. */
  1201. __isl_give isl_basic_set *isl_basic_set_preimage(
  1202. __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
  1203. {
  1204. struct isl_ctx *ctx;
  1205. if (!bset || !mat)
  1206. goto error;
  1207. ctx = bset->ctx;
  1208. bset = isl_basic_set_cow(bset);
  1209. if (isl_basic_set_check_no_params(bset) < 0)
  1210. goto error;
  1211. isl_assert(ctx, 1+bset->dim->n_out == mat->n_row, goto error);
  1212. isl_assert(ctx, mat->n_col > 0, goto error);
  1213. if (mat->n_col > mat->n_row) {
  1214. bset = isl_basic_set_add_dims(bset, isl_dim_set,
  1215. mat->n_col - mat->n_row);
  1216. if (!bset)
  1217. goto error;
  1218. } else if (mat->n_col < mat->n_row) {
  1219. bset->dim = isl_space_cow(bset->dim);
  1220. if (!bset->dim)
  1221. goto error;
  1222. bset->dim->n_out -= mat->n_row - mat->n_col;
  1223. }
  1224. if (preimage(ctx, bset->eq, bset->n_eq, bset->n_div, 0,
  1225. isl_mat_copy(mat)) < 0)
  1226. goto error;
  1227. if (preimage(ctx, bset->ineq, bset->n_ineq, bset->n_div, 0,
  1228. isl_mat_copy(mat)) < 0)
  1229. goto error;
  1230. if (preimage(ctx, bset->div, bset->n_div, bset->n_div, 1, mat) < 0)
  1231. goto error2;
  1232. ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
  1233. ISL_F_CLR(bset, ISL_BASIC_SET_NO_REDUNDANT);
  1234. ISL_F_CLR(bset, ISL_BASIC_SET_SORTED);
  1235. ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS);
  1236. ISL_F_CLR(bset, ISL_BASIC_SET_ALL_EQUALITIES);
  1237. bset = isl_basic_set_simplify(bset);
  1238. bset = isl_basic_set_finalize(bset);
  1239. return bset;
  1240. error:
  1241. isl_mat_free(mat);
  1242. error2:
  1243. isl_basic_set_free(bset);
  1244. return NULL;
  1245. }
  1246. __isl_give isl_set *isl_set_preimage(
  1247. __isl_take isl_set *set, __isl_take isl_mat *mat)
  1248. {
  1249. int i;
  1250. set = isl_set_cow(set);
  1251. if (!set)
  1252. goto error;
  1253. for (i = 0; i < set->n; ++i) {
  1254. set->p[i] = isl_basic_set_preimage(set->p[i],
  1255. isl_mat_copy(mat));
  1256. if (!set->p[i])
  1257. goto error;
  1258. }
  1259. if (mat->n_col != mat->n_row) {
  1260. set->dim = isl_space_cow(set->dim);
  1261. if (!set->dim)
  1262. goto error;
  1263. set->dim->n_out += mat->n_col;
  1264. set->dim->n_out -= mat->n_row;
  1265. }
  1266. isl_mat_free(mat);
  1267. ISL_F_CLR(set, ISL_SET_NORMALIZED);
  1268. return set;
  1269. error:
  1270. isl_set_free(set);
  1271. isl_mat_free(mat);
  1272. return NULL;
  1273. }
  1274. /* Replace the variables x starting at "first_col" in the rows "rows"
  1275. * of some coefficient matrix by x' with x = M x' with M the matrix mat.
  1276. * That is, replace the corresponding coefficients c by c M.
  1277. */
  1278. isl_stat isl_mat_sub_transform(isl_int **row, unsigned n_row,
  1279. unsigned first_col, __isl_take isl_mat *mat)
  1280. {
  1281. int i;
  1282. isl_ctx *ctx;
  1283. isl_mat *t;
  1284. if (!mat)
  1285. return isl_stat_error;
  1286. ctx = isl_mat_get_ctx(mat);
  1287. t = isl_mat_sub_alloc6(ctx, row, 0, n_row, first_col, mat->n_row);
  1288. t = isl_mat_product(t, mat);
  1289. if (!t)
  1290. return isl_stat_error;
  1291. for (i = 0; i < n_row; ++i)
  1292. isl_seq_swp_or_cpy(row[i] + first_col, t->row[i], t->n_col);
  1293. isl_mat_free(t);
  1294. return isl_stat_ok;
  1295. }
  1296. void isl_mat_print_internal(__isl_keep isl_mat *mat, FILE *out, int indent)
  1297. {
  1298. int i, j;
  1299. if (!mat) {
  1300. fprintf(out, "%*snull mat\n", indent, "");
  1301. return;
  1302. }
  1303. if (mat->n_row == 0)
  1304. fprintf(out, "%*s[]\n", indent, "");
  1305. for (i = 0; i < mat->n_row; ++i) {
  1306. if (!i)
  1307. fprintf(out, "%*s[[", indent, "");
  1308. else
  1309. fprintf(out, "%*s[", indent+1, "");
  1310. for (j = 0; j < mat->n_col; ++j) {
  1311. if (j)
  1312. fprintf(out, ",");
  1313. isl_int_print(out, mat->row[i][j], 0);
  1314. }
  1315. if (i == mat->n_row-1)
  1316. fprintf(out, "]]\n");
  1317. else
  1318. fprintf(out, "]\n");
  1319. }
  1320. }
  1321. void isl_mat_dump(__isl_keep isl_mat *mat)
  1322. {
  1323. isl_mat_print_internal(mat, stderr, 0);
  1324. }
  1325. __isl_give isl_mat *isl_mat_drop_cols(__isl_take isl_mat *mat,
  1326. unsigned col, unsigned n)
  1327. {
  1328. int r;
  1329. if (n == 0)
  1330. return mat;
  1331. mat = isl_mat_cow(mat);
  1332. if (check_col_range(mat, col, n) < 0)
  1333. return isl_mat_free(mat);
  1334. if (col != mat->n_col-n) {
  1335. for (r = 0; r < mat->n_row; ++r)
  1336. isl_seq_cpy(mat->row[r]+col, mat->row[r]+col+n,
  1337. mat->n_col - col - n);
  1338. }
  1339. mat->n_col -= n;
  1340. return mat;
  1341. }
  1342. __isl_give isl_mat *isl_mat_drop_rows(__isl_take isl_mat *mat,
  1343. unsigned row, unsigned n)
  1344. {
  1345. int r;
  1346. mat = isl_mat_cow(mat);
  1347. if (check_row_range(mat, row, n) < 0)
  1348. return isl_mat_free(mat);
  1349. for (r = row; r+n < mat->n_row; ++r)
  1350. mat->row[r] = mat->row[r+n];
  1351. mat->n_row -= n;
  1352. return mat;
  1353. }
  1354. __isl_give isl_mat *isl_mat_insert_cols(__isl_take isl_mat *mat,
  1355. unsigned col, unsigned n)
  1356. {
  1357. isl_mat *ext;
  1358. if (check_col_range(mat, col, 0) < 0)
  1359. return isl_mat_free(mat);
  1360. if (n == 0)
  1361. return mat;
  1362. ext = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col + n);
  1363. if (!ext)
  1364. goto error;
  1365. isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row, 0, 0, col);
  1366. isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row,
  1367. col + n, col, mat->n_col - col);
  1368. isl_mat_free(mat);
  1369. return ext;
  1370. error:
  1371. isl_mat_free(mat);
  1372. return NULL;
  1373. }
  1374. __isl_give isl_mat *isl_mat_insert_zero_cols(__isl_take isl_mat *mat,
  1375. unsigned first, unsigned n)
  1376. {
  1377. int i;
  1378. if (!mat)
  1379. return NULL;
  1380. mat = isl_mat_insert_cols(mat, first, n);
  1381. if (!mat)
  1382. return NULL;
  1383. for (i = 0; i < mat->n_row; ++i)
  1384. isl_seq_clr(mat->row[i] + first, n);
  1385. return mat;
  1386. }
  1387. __isl_give isl_mat *isl_mat_add_zero_cols(__isl_take isl_mat *mat, unsigned n)
  1388. {
  1389. if (!mat)
  1390. return NULL;
  1391. return isl_mat_insert_zero_cols(mat, mat->n_col, n);
  1392. }
  1393. __isl_give isl_mat *isl_mat_insert_rows(__isl_take isl_mat *mat,
  1394. unsigned row, unsigned n)
  1395. {
  1396. isl_mat *ext;
  1397. if (check_row_range(mat, row, 0) < 0)
  1398. return isl_mat_free(mat);
  1399. if (n == 0)
  1400. return mat;
  1401. ext = isl_mat_alloc(mat->ctx, mat->n_row + n, mat->n_col);
  1402. if (!ext)
  1403. goto error;
  1404. isl_mat_sub_copy(mat->ctx, ext->row, mat->row, row, 0, 0, mat->n_col);
  1405. isl_mat_sub_copy(mat->ctx, ext->row + row + n, mat->row + row,
  1406. mat->n_row - row, 0, 0, mat->n_col);
  1407. isl_mat_free(mat);
  1408. return ext;
  1409. error:
  1410. isl_mat_free(mat);
  1411. return NULL;
  1412. }
  1413. __isl_give isl_mat *isl_mat_add_rows(__isl_take isl_mat *mat, unsigned n)
  1414. {
  1415. if (!mat)
  1416. return NULL;
  1417. return isl_mat_insert_rows(mat, mat->n_row, n);
  1418. }
  1419. __isl_give isl_mat *isl_mat_insert_zero_rows(__isl_take isl_mat *mat,
  1420. unsigned row, unsigned n)
  1421. {
  1422. int i;
  1423. mat = isl_mat_insert_rows(mat, row, n);
  1424. if (!mat)
  1425. return NULL;
  1426. for (i = 0; i < n; ++i)
  1427. isl_seq_clr(mat->row[row + i], mat->n_col);
  1428. return mat;
  1429. }
  1430. __isl_give isl_mat *isl_mat_add_zero_rows(__isl_take isl_mat *mat, unsigned n)
  1431. {
  1432. if (!mat)
  1433. return NULL;
  1434. return isl_mat_insert_zero_rows(mat, mat->n_row, n);
  1435. }
  1436. void isl_mat_col_submul(__isl_keep isl_mat *mat,
  1437. int dst_col, isl_int f, int src_col)
  1438. {
  1439. int i;
  1440. for (i = 0; i < mat->n_row; ++i)
  1441. isl_int_submul(mat->row[i][dst_col], f, mat->row[i][src_col]);
  1442. }
  1443. void isl_mat_col_add(__isl_keep isl_mat *mat, int dst_col, int src_col)
  1444. {
  1445. int i;
  1446. if (!mat)
  1447. return;
  1448. for (i = 0; i < mat->n_row; ++i)
  1449. isl_int_add(mat->row[i][dst_col],
  1450. mat->row[i][dst_col], mat->row[i][src_col]);
  1451. }
  1452. void isl_mat_col_mul(__isl_keep isl_mat *mat, int dst_col, isl_int f,
  1453. int src_col)
  1454. {
  1455. int i;
  1456. for (i = 0; i < mat->n_row; ++i)
  1457. isl_int_mul(mat->row[i][dst_col], f, mat->row[i][src_col]);
  1458. }
  1459. /* Add "f" times column "src_col" to column "dst_col" of "mat" and
  1460. * return the result.
  1461. */
  1462. __isl_give isl_mat *isl_mat_col_addmul(__isl_take isl_mat *mat, int dst_col,
  1463. isl_int f, int src_col)
  1464. {
  1465. int i;
  1466. if (check_col(mat, dst_col) < 0 || check_col(mat, src_col) < 0)
  1467. return isl_mat_free(mat);
  1468. for (i = 0; i < mat->n_row; ++i) {
  1469. if (isl_int_is_zero(mat->row[i][src_col]))
  1470. continue;
  1471. mat = isl_mat_cow(mat);
  1472. if (!mat)
  1473. return NULL;
  1474. isl_int_addmul(mat->row[i][dst_col], f, mat->row[i][src_col]);
  1475. }
  1476. return mat;
  1477. }
  1478. /* Negate column "col" of "mat" and return the result.
  1479. */
  1480. __isl_give isl_mat *isl_mat_col_neg(__isl_take isl_mat *mat, int col)
  1481. {
  1482. int i;
  1483. if (check_col(mat, col) < 0)
  1484. return isl_mat_free(mat);
  1485. for (i = 0; i < mat->n_row; ++i) {
  1486. if (isl_int_is_zero(mat->row[i][col]))
  1487. continue;
  1488. mat = isl_mat_cow(mat);
  1489. if (!mat)
  1490. return NULL;
  1491. isl_int_neg(mat->row[i][col], mat->row[i][col]);
  1492. }
  1493. return mat;
  1494. }
  1495. /* Negate row "row" of "mat" and return the result.
  1496. */
  1497. __isl_give isl_mat *isl_mat_row_neg(__isl_take isl_mat *mat, int row)
  1498. {
  1499. if (check_row(mat, row) < 0)
  1500. return isl_mat_free(mat);
  1501. if (isl_seq_first_non_zero(mat->row[row], mat->n_col) == -1)
  1502. return mat;
  1503. mat = isl_mat_cow(mat);
  1504. if (!mat)
  1505. return NULL;
  1506. isl_seq_neg(mat->row[row], mat->row[row], mat->n_col);
  1507. return mat;
  1508. }
  1509. __isl_give isl_mat *isl_mat_unimodular_complete(__isl_take isl_mat *M, int row)
  1510. {
  1511. int r;
  1512. struct isl_mat *H = NULL, *Q = NULL;
  1513. if (!M)
  1514. return NULL;
  1515. isl_assert(M->ctx, M->n_row == M->n_col, goto error);
  1516. M->n_row = row;
  1517. H = isl_mat_left_hermite(isl_mat_copy(M), 0, NULL, &Q);
  1518. M->n_row = M->n_col;
  1519. if (!H)
  1520. goto error;
  1521. for (r = 0; r < row; ++r)
  1522. isl_assert(M->ctx, isl_int_is_one(H->row[r][r]), goto error);
  1523. for (r = row; r < M->n_row; ++r)
  1524. isl_seq_cpy(M->row[r], Q->row[r], M->n_col);
  1525. isl_mat_free(H);
  1526. isl_mat_free(Q);
  1527. return M;
  1528. error:
  1529. isl_mat_free(H);
  1530. isl_mat_free(Q);
  1531. isl_mat_free(M);
  1532. return NULL;
  1533. }
  1534. __isl_give isl_mat *isl_mat_concat(__isl_take isl_mat *top,
  1535. __isl_take isl_mat *bot)
  1536. {
  1537. struct isl_mat *mat;
  1538. if (!top || !bot)
  1539. goto error;
  1540. isl_assert(top->ctx, top->n_col == bot->n_col, goto error);
  1541. if (top->n_row == 0) {
  1542. isl_mat_free(top);
  1543. return bot;
  1544. }
  1545. if (bot->n_row == 0) {
  1546. isl_mat_free(bot);
  1547. return top;
  1548. }
  1549. mat = isl_mat_alloc(top->ctx, top->n_row + bot->n_row, top->n_col);
  1550. if (!mat)
  1551. goto error;
  1552. isl_mat_sub_copy(mat->ctx, mat->row, top->row, top->n_row,
  1553. 0, 0, mat->n_col);
  1554. isl_mat_sub_copy(mat->ctx, mat->row + top->n_row, bot->row, bot->n_row,
  1555. 0, 0, mat->n_col);
  1556. isl_mat_free(top);
  1557. isl_mat_free(bot);
  1558. return mat;
  1559. error:
  1560. isl_mat_free(top);
  1561. isl_mat_free(bot);
  1562. return NULL;
  1563. }
  1564. isl_bool isl_mat_is_equal(__isl_keep isl_mat *mat1, __isl_keep isl_mat *mat2)
  1565. {
  1566. int i;
  1567. if (!mat1 || !mat2)
  1568. return isl_bool_error;
  1569. if (mat1->n_row != mat2->n_row)
  1570. return isl_bool_false;
  1571. if (mat1->n_col != mat2->n_col)
  1572. return isl_bool_false;
  1573. for (i = 0; i < mat1->n_row; ++i)
  1574. if (!isl_seq_eq(mat1->row[i], mat2->row[i], mat1->n_col))
  1575. return isl_bool_false;
  1576. return isl_bool_true;
  1577. }
  1578. __isl_give isl_mat *isl_mat_from_row_vec(__isl_take isl_vec *vec)
  1579. {
  1580. struct isl_mat *mat;
  1581. if (!vec)
  1582. return NULL;
  1583. mat = isl_mat_alloc(vec->ctx, 1, vec->size);
  1584. if (!mat)
  1585. goto error;
  1586. isl_seq_cpy(mat->row[0], vec->el, vec->size);
  1587. isl_vec_free(vec);
  1588. return mat;
  1589. error:
  1590. isl_vec_free(vec);
  1591. return NULL;
  1592. }
  1593. /* Return a copy of row "row" of "mat" as an isl_vec.
  1594. */
  1595. __isl_give isl_vec *isl_mat_get_row(__isl_keep isl_mat *mat, unsigned row)
  1596. {
  1597. isl_vec *v;
  1598. if (!mat)
  1599. return NULL;
  1600. if (row >= mat->n_row)
  1601. isl_die(mat->ctx, isl_error_invalid, "row out of range",
  1602. return NULL);
  1603. v = isl_vec_alloc(isl_mat_get_ctx(mat), mat->n_col);
  1604. if (!v)
  1605. return NULL;
  1606. isl_seq_cpy(v->el, mat->row[row], mat->n_col);
  1607. return v;
  1608. }
  1609. __isl_give isl_mat *isl_mat_vec_concat(__isl_take isl_mat *top,
  1610. __isl_take isl_vec *bot)
  1611. {
  1612. return isl_mat_concat(top, isl_mat_from_row_vec(bot));
  1613. }
  1614. __isl_give isl_mat *isl_mat_move_cols(__isl_take isl_mat *mat,
  1615. unsigned dst_col, unsigned src_col, unsigned n)
  1616. {
  1617. isl_mat *res;
  1618. if (!mat)
  1619. return NULL;
  1620. if (n == 0 || dst_col == src_col)
  1621. return mat;
  1622. res = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
  1623. if (!res)
  1624. goto error;
  1625. if (dst_col < src_col) {
  1626. isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
  1627. 0, 0, dst_col);
  1628. isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
  1629. dst_col, src_col, n);
  1630. isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
  1631. dst_col + n, dst_col, src_col - dst_col);
  1632. isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
  1633. src_col + n, src_col + n,
  1634. res->n_col - src_col - n);
  1635. } else {
  1636. isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
  1637. 0, 0, src_col);
  1638. isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
  1639. src_col, src_col + n, dst_col - src_col);
  1640. isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
  1641. dst_col, src_col, n);
  1642. isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
  1643. dst_col + n, dst_col + n,
  1644. res->n_col - dst_col - n);
  1645. }
  1646. isl_mat_free(mat);
  1647. return res;
  1648. error:
  1649. isl_mat_free(mat);
  1650. return NULL;
  1651. }
  1652. /* Return the gcd of the elements in row "row" of "mat" in *gcd.
  1653. * Return isl_stat_ok on success and isl_stat_error on failure.
  1654. */
  1655. isl_stat isl_mat_row_gcd(__isl_keep isl_mat *mat, int row, isl_int *gcd)
  1656. {
  1657. if (check_row(mat, row) < 0)
  1658. return isl_stat_error;
  1659. isl_seq_gcd(mat->row[row], mat->n_col, gcd);
  1660. return isl_stat_ok;
  1661. }
  1662. void isl_mat_gcd(__isl_keep isl_mat *mat, isl_int *gcd)
  1663. {
  1664. int i;
  1665. isl_int g;
  1666. isl_int_set_si(*gcd, 0);
  1667. if (!mat)
  1668. return;
  1669. isl_int_init(g);
  1670. for (i = 0; i < mat->n_row; ++i) {
  1671. isl_seq_gcd(mat->row[i], mat->n_col, &g);
  1672. isl_int_gcd(*gcd, *gcd, g);
  1673. }
  1674. isl_int_clear(g);
  1675. }
  1676. /* Return the result of scaling "mat" by a factor of "m".
  1677. */
  1678. __isl_give isl_mat *isl_mat_scale(__isl_take isl_mat *mat, isl_int m)
  1679. {
  1680. int i;
  1681. if (isl_int_is_one(m))
  1682. return mat;
  1683. mat = isl_mat_cow(mat);
  1684. if (!mat)
  1685. return NULL;
  1686. for (i = 0; i < mat->n_row; ++i)
  1687. isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
  1688. return mat;
  1689. }
  1690. __isl_give isl_mat *isl_mat_scale_down(__isl_take isl_mat *mat, isl_int m)
  1691. {
  1692. int i;
  1693. if (isl_int_is_one(m))
  1694. return mat;
  1695. mat = isl_mat_cow(mat);
  1696. if (!mat)
  1697. return NULL;
  1698. for (i = 0; i < mat->n_row; ++i)
  1699. isl_seq_scale_down(mat->row[i], mat->row[i], m, mat->n_col);
  1700. return mat;
  1701. }
  1702. __isl_give isl_mat *isl_mat_scale_down_row(__isl_take isl_mat *mat, int row,
  1703. isl_int m)
  1704. {
  1705. if (isl_int_is_one(m))
  1706. return mat;
  1707. mat = isl_mat_cow(mat);
  1708. if (!mat)
  1709. return NULL;
  1710. isl_seq_scale_down(mat->row[row], mat->row[row], m, mat->n_col);
  1711. return mat;
  1712. }
  1713. __isl_give isl_mat *isl_mat_normalize(__isl_take isl_mat *mat)
  1714. {
  1715. isl_int gcd;
  1716. if (!mat)
  1717. return NULL;
  1718. isl_int_init(gcd);
  1719. isl_mat_gcd(mat, &gcd);
  1720. mat = isl_mat_scale_down(mat, gcd);
  1721. isl_int_clear(gcd);
  1722. return mat;
  1723. }
  1724. __isl_give isl_mat *isl_mat_normalize_row(__isl_take isl_mat *mat, int row)
  1725. {
  1726. mat = isl_mat_cow(mat);
  1727. if (!mat)
  1728. return NULL;
  1729. isl_seq_normalize(mat->ctx, mat->row[row], mat->n_col);
  1730. return mat;
  1731. }
  1732. /* Number of initial non-zero columns.
  1733. */
  1734. int isl_mat_initial_non_zero_cols(__isl_keep isl_mat *mat)
  1735. {
  1736. int i;
  1737. if (!mat)
  1738. return -1;
  1739. for (i = 0; i < mat->n_col; ++i)
  1740. if (row_first_non_zero(mat->row, mat->n_row, i) < 0)
  1741. break;
  1742. return i;
  1743. }
  1744. /* Return a basis for the space spanned by the rows of "mat".
  1745. * Any basis will do, so simply perform Gaussian elimination and
  1746. * remove the empty rows.
  1747. */
  1748. __isl_give isl_mat *isl_mat_row_basis(__isl_take isl_mat *mat)
  1749. {
  1750. return isl_mat_reverse_gauss(mat);
  1751. }
  1752. /* Return rows that extend a basis of "mat1" to one
  1753. * that covers both "mat1" and "mat2".
  1754. * The Hermite normal form of the concatenation of the two matrices is
  1755. *
  1756. * [ Q1 ]
  1757. * [ M1 ] = [ H1 0 0 ] [ Q2 ]
  1758. * [ M2 ] = [ H2 H3 0 ] [ Q3 ]
  1759. *
  1760. * The number of columns in H1 and H3 determine the number of rows
  1761. * in Q1 and Q2. Q1 is a basis for M1, while Q2 extends this basis
  1762. * to also cover M2.
  1763. */
  1764. __isl_give isl_mat *isl_mat_row_basis_extension(
  1765. __isl_take isl_mat *mat1, __isl_take isl_mat *mat2)
  1766. {
  1767. isl_size n_row;
  1768. int r1, r;
  1769. isl_size n1;
  1770. isl_mat *H, *Q;
  1771. n1 = isl_mat_rows(mat1);
  1772. H = isl_mat_concat(mat1, mat2);
  1773. H = isl_mat_left_hermite(H, 0, NULL, &Q);
  1774. if (n1 < 0 || !H || !Q)
  1775. goto error;
  1776. r1 = hermite_first_zero_col(H, 0, n1);
  1777. r = hermite_first_zero_col(H, r1, H->n_row);
  1778. n_row = isl_mat_rows(Q);
  1779. if (n_row < 0)
  1780. goto error;
  1781. Q = isl_mat_drop_rows(Q, r, n_row - r);
  1782. Q = isl_mat_drop_rows(Q, 0, r1);
  1783. isl_mat_free(H);
  1784. return Q;
  1785. error:
  1786. isl_mat_free(H);
  1787. isl_mat_free(Q);
  1788. return NULL;
  1789. }
  1790. /* Are the rows of "mat1" linearly independent of those of "mat2"?
  1791. * That is, is there no linear dependence among the combined rows
  1792. * that is not already present in either "mat1" or "mat2"?
  1793. * In other words, is the rank of "mat1" and "mat2" combined equal
  1794. * to the sum of the ranks of "mat1" and "mat2"?
  1795. */
  1796. isl_bool isl_mat_has_linearly_independent_rows(__isl_keep isl_mat *mat1,
  1797. __isl_keep isl_mat *mat2)
  1798. {
  1799. isl_size r1, r2, r;
  1800. isl_mat *mat;
  1801. r1 = isl_mat_rank(mat1);
  1802. if (r1 < 0)
  1803. return isl_bool_error;
  1804. if (r1 == 0)
  1805. return isl_bool_true;
  1806. r2 = isl_mat_rank(mat2);
  1807. if (r2 < 0)
  1808. return isl_bool_error;
  1809. if (r2 == 0)
  1810. return isl_bool_true;
  1811. mat = isl_mat_concat(isl_mat_copy(mat1), isl_mat_copy(mat2));
  1812. r = isl_mat_rank(mat);
  1813. isl_mat_free(mat);
  1814. if (r < 0)
  1815. return isl_bool_error;
  1816. return isl_bool_ok(r == r1 + r2);
  1817. }