+void isl_mat_col_scale(struct isl_mat *mat, unsigned col, isl_int m)
+{
+ int i;
+
+ for (i = 0; i < mat->n_row; ++i)
+ isl_int_mul(mat->row[i][col], mat->row[i][col], m);
+}
+
+void isl_mat_col_combine(struct isl_mat *mat, unsigned dst,
+ isl_int m1, unsigned src1, isl_int m2, unsigned src2)
+{
+ int i;
+ isl_int tmp;
+
+ isl_int_init(tmp);
+ for (i = 0; i < mat->n_row; ++i) {
+ isl_int_mul(tmp, m1, mat->row[i][src1]);
+ isl_int_addmul(tmp, m2, mat->row[i][src2]);
+ isl_int_set(mat->row[i][dst], tmp);
+ }
+ isl_int_clear(tmp);
+}
+
+struct isl_mat *isl_mat_right_inverse(struct isl_mat *mat)
+{
+ struct isl_mat *inv;
+ int row;
+ isl_int a, b;
+
+ mat = isl_mat_cow(mat);
+ if (!mat)
+ return NULL;
+
+ inv = isl_mat_identity(mat->ctx, mat->n_col);
+ inv = isl_mat_cow(inv);
+ if (!inv)
+ goto error;
+
+ isl_int_init(a);
+ isl_int_init(b);
+ for (row = 0; row < mat->n_row; ++row) {
+ int pivot, first, i, off;
+ pivot = isl_seq_abs_min_non_zero(mat->row[row]+row, mat->n_col-row);
+ if (pivot < 0) {
+ isl_int_clear(a);
+ isl_int_clear(b);
+ goto error;
+ }
+ pivot += row;
+ if (pivot != row)
+ exchange(mat, &inv, NULL, row, pivot, row);
+ if (isl_int_is_neg(mat->row[row][row]))
+ oppose(mat, &inv, NULL, row, row);
+ first = row+1;
+ while ((off = isl_seq_first_non_zero(mat->row[row]+first,
+ mat->n_col-first)) != -1) {
+ first += off;
+ isl_int_fdiv_q(a, mat->row[row][first],
+ mat->row[row][row]);
+ subtract(mat, &inv, NULL, row, row, first, a);
+ if (!isl_int_is_zero(mat->row[row][first]))
+ exchange(mat, &inv, NULL, row, row, first);
+ else
+ ++first;
+ }
+ for (i = 0; i < row; ++i) {
+ if (isl_int_is_zero(mat->row[row][i]))
+ continue;
+ isl_int_gcd(a, mat->row[row][row], mat->row[row][i]);
+ isl_int_divexact(b, mat->row[row][i], a);
+ isl_int_divexact(a, mat->row[row][row], a);
+ isl_int_neg(a, a);
+ isl_mat_col_combine(mat, i, a, i, b, row);
+ isl_mat_col_combine(inv, i, a, i, b, row);
+ }
+ }
+ isl_int_clear(b);
+
+ isl_int_set(a, mat->row[0][0]);
+ for (row = 1; row < mat->n_row; ++row)
+ isl_int_lcm(a, a, mat->row[row][row]);
+ if (isl_int_is_zero(a)){
+ isl_int_clear(a);
+ goto error;
+ }
+ for (row = 0; row < mat->n_row; ++row) {
+ isl_int_divexact(mat->row[row][row], a, mat->row[row][row]);
+ if (isl_int_is_one(mat->row[row][row]))
+ continue;
+ isl_mat_col_scale(inv, row, mat->row[row][row]);
+ }
+ isl_int_clear(a);
+
+ isl_mat_free(mat);
+
+ return inv;
+error:
+ isl_mat_free(mat);
+ return NULL;
+}
+
+struct isl_mat *isl_mat_transpose(struct isl_mat *mat)
+{
+ struct isl_mat *transpose = NULL;
+ int i, j;
+
+ if (mat->n_col == mat->n_row) {
+ mat = isl_mat_cow(mat);
+ if (!mat)
+ return NULL;
+ for (i = 0; i < mat->n_row; ++i)
+ for (j = i + 1; j < mat->n_col; ++j)
+ isl_int_swap(mat->row[i][j], mat->row[j][i]);
+ return mat;
+ }
+ transpose = isl_mat_alloc(mat->ctx, mat->n_col, mat->n_row);
+ if (!transpose)
+ goto error;
+ for (i = 0; i < mat->n_row; ++i)
+ for (j = 0; j < mat->n_col; ++j)
+ isl_int_set(transpose->row[j][i], mat->row[i][j]);
+ isl_mat_free(mat);
+ return transpose;
+error:
+ isl_mat_free(mat);
+ return NULL;
+}
+
+struct isl_mat *isl_mat_swap_cols(struct isl_mat *mat, unsigned i, unsigned j)
+{
+ int r;
+
+ mat = isl_mat_cow(mat);
+ if (!mat)
+ return NULL;
+ isl_assert(ctx, i < mat->n_col, goto error);
+ isl_assert(ctx, j < mat->n_col, goto error);
+
+ for (r = 0; r < mat->n_row; ++r)
+ isl_int_swap(mat->row[r][i], mat->row[r][j]);
+ return mat;
+error:
+ isl_mat_free(mat);
+ return NULL;
+}
+
+struct isl_mat *isl_mat_swap_rows(struct isl_mat *mat, unsigned i, unsigned j)