Blame isl-0.14/isl_equalities.c

Packit fb9d21
/*
Packit fb9d21
 * Copyright 2008-2009 Katholieke Universiteit Leuven
Packit fb9d21
 * Copyright 2010      INRIA Saclay
Packit fb9d21
 *
Packit fb9d21
 * Use of this software is governed by the MIT license
Packit fb9d21
 *
Packit fb9d21
 * Written by Sven Verdoolaege, K.U.Leuven, Departement
Packit fb9d21
 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
Packit fb9d21
 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
Packit fb9d21
 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
Packit fb9d21
 */
Packit fb9d21
Packit fb9d21
#include <isl_mat_private.h>
Packit fb9d21
#include <isl_vec_private.h>
Packit fb9d21
#include <isl_seq.h>
Packit fb9d21
#include "isl_map_private.h"
Packit fb9d21
#include "isl_equalities.h"
Packit fb9d21
#include <isl_val_private.h>
Packit fb9d21
Packit fb9d21
/* Given a set of modulo constraints
Packit fb9d21
 *
Packit fb9d21
 *		c + A y = 0 mod d
Packit fb9d21
 *
Packit fb9d21
 * this function computes a particular solution y_0
Packit fb9d21
 *
Packit fb9d21
 * The input is given as a matrix B = [ c A ] and a vector d.
Packit fb9d21
 *
Packit fb9d21
 * The output is matrix containing the solution y_0 or
Packit fb9d21
 * a zero-column matrix if the constraints admit no integer solution.
Packit fb9d21
 *
Packit fb9d21
 * The given set of constrains is equivalent to
Packit fb9d21
 *
Packit fb9d21
 *		c + A y = -D x
Packit fb9d21
 *
Packit fb9d21
 * with D = diag d and x a fresh set of variables.
Packit fb9d21
 * Reducing both c and A modulo d does not change the
Packit fb9d21
 * value of y in the solution and may lead to smaller coefficients.
Packit fb9d21
 * Let M = [ D A ] and [ H 0 ] = M U, the Hermite normal form of M.
Packit fb9d21
 * Then
Packit fb9d21
 *		  [ x ]
Packit fb9d21
 *		M [ y ] = - c
Packit fb9d21
 * and so
Packit fb9d21
 *		               [ x ]
Packit fb9d21
 *		[ H 0 ] U^{-1} [ y ] = - c
Packit fb9d21
 * Let
Packit fb9d21
 *		[ A ]          [ x ]
Packit fb9d21
 *		[ B ] = U^{-1} [ y ]
Packit fb9d21
 * then
Packit fb9d21
 *		H A + 0 B = -c
Packit fb9d21
 *
Packit fb9d21
 * so B may be chosen arbitrarily, e.g., B = 0, and then
Packit fb9d21
 *
Packit fb9d21
 *		       [ x ] = [ -c ]
Packit fb9d21
 *		U^{-1} [ y ] = [  0 ]
Packit fb9d21
 * or
Packit fb9d21
 *		[ x ]     [ -c ]
Packit fb9d21
 *		[ y ] = U [  0 ]
Packit fb9d21
 * specifically,
Packit fb9d21
 *
Packit fb9d21
 *		y = U_{2,1} (-c)
Packit fb9d21
 *
Packit fb9d21
 * If any of the coordinates of this y are non-integer
Packit fb9d21
 * then the constraints admit no integer solution and
Packit fb9d21
 * a zero-column matrix is returned.
Packit fb9d21
 */
Packit fb9d21
static struct isl_mat *particular_solution(struct isl_mat *B, struct isl_vec *d)
Packit fb9d21
{
Packit fb9d21
	int i, j;
Packit fb9d21
	struct isl_mat *M = NULL;
Packit fb9d21
	struct isl_mat *C = NULL;
Packit fb9d21
	struct isl_mat *U = NULL;
Packit fb9d21
	struct isl_mat *H = NULL;
Packit fb9d21
	struct isl_mat *cst = NULL;
Packit fb9d21
	struct isl_mat *T = NULL;
Packit fb9d21
Packit fb9d21
	M = isl_mat_alloc(B->ctx, B->n_row, B->n_row + B->n_col - 1);
Packit fb9d21
	C = isl_mat_alloc(B->ctx, 1 + B->n_row, 1);
Packit fb9d21
	if (!M || !C)
Packit fb9d21
		goto error;
Packit fb9d21
	isl_int_set_si(C->row[0][0], 1);
Packit fb9d21
	for (i = 0; i < B->n_row; ++i) {
Packit fb9d21
		isl_seq_clr(M->row[i], B->n_row);
Packit fb9d21
		isl_int_set(M->row[i][i], d->block.data[i]);
Packit fb9d21
		isl_int_neg(C->row[1 + i][0], B->row[i][0]);
Packit fb9d21
		isl_int_fdiv_r(C->row[1+i][0], C->row[1+i][0], M->row[i][i]);
Packit fb9d21
		for (j = 0; j < B->n_col - 1; ++j)
Packit fb9d21
			isl_int_fdiv_r(M->row[i][B->n_row + j],
Packit fb9d21
					B->row[i][1 + j], M->row[i][i]);
Packit fb9d21
	}
Packit fb9d21
	M = isl_mat_left_hermite(M, 0, &U, NULL);
Packit fb9d21
	if (!M || !U)
Packit fb9d21
		goto error;
Packit fb9d21
	H = isl_mat_sub_alloc(M, 0, B->n_row, 0, B->n_row);
Packit fb9d21
	H = isl_mat_lin_to_aff(H);
Packit fb9d21
	C = isl_mat_inverse_product(H, C);
Packit fb9d21
	if (!C)
Packit fb9d21
		goto error;
Packit fb9d21
	for (i = 0; i < B->n_row; ++i) {
Packit fb9d21
		if (!isl_int_is_divisible_by(C->row[1+i][0], C->row[0][0]))
Packit fb9d21
			break;
Packit fb9d21
		isl_int_divexact(C->row[1+i][0], C->row[1+i][0], C->row[0][0]);
Packit fb9d21
	}
Packit fb9d21
	if (i < B->n_row)
Packit fb9d21
		cst = isl_mat_alloc(B->ctx, B->n_row, 0);
Packit fb9d21
	else
Packit fb9d21
		cst = isl_mat_sub_alloc(C, 1, B->n_row, 0, 1);
Packit fb9d21
	T = isl_mat_sub_alloc(U, B->n_row, B->n_col - 1, 0, B->n_row);
Packit fb9d21
	cst = isl_mat_product(T, cst);
Packit fb9d21
	isl_mat_free(M);
Packit fb9d21
	isl_mat_free(C);
Packit fb9d21
	isl_mat_free(U);
Packit fb9d21
	return cst;
Packit fb9d21
error:
Packit fb9d21
	isl_mat_free(M);
Packit fb9d21
	isl_mat_free(C);
Packit fb9d21
	isl_mat_free(U);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute and return the matrix
Packit fb9d21
 *
Packit fb9d21
 *		U_1^{-1} diag(d_1, 1, ..., 1)
Packit fb9d21
 *
Packit fb9d21
 * with U_1 the unimodular completion of the first (and only) row of B.
Packit fb9d21
 * The columns of this matrix generate the lattice that satisfies
Packit fb9d21
 * the single (linear) modulo constraint.
Packit fb9d21
 */
Packit fb9d21
static struct isl_mat *parameter_compression_1(
Packit fb9d21
			struct isl_mat *B, struct isl_vec *d)
Packit fb9d21
{
Packit fb9d21
	struct isl_mat *U;
Packit fb9d21
Packit fb9d21
	U = isl_mat_alloc(B->ctx, B->n_col - 1, B->n_col - 1);
Packit fb9d21
	if (!U)
Packit fb9d21
		return NULL;
Packit fb9d21
	isl_seq_cpy(U->row[0], B->row[0] + 1, B->n_col - 1);
Packit fb9d21
	U = isl_mat_unimodular_complete(U, 1);
Packit fb9d21
	U = isl_mat_right_inverse(U);
Packit fb9d21
	if (!U)
Packit fb9d21
		return NULL;
Packit fb9d21
	isl_mat_col_mul(U, 0, d->block.data[0], 0);
Packit fb9d21
	U = isl_mat_lin_to_aff(U);
Packit fb9d21
	return U;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute a common lattice of solutions to the linear modulo
Packit fb9d21
 * constraints specified by B and d.
Packit fb9d21
 * See also the documentation of isl_mat_parameter_compression.
Packit fb9d21
 * We put the matrix
Packit fb9d21
 * 
Packit fb9d21
 *		A = [ L_1^{-T} L_2^{-T} ... L_k^{-T} ]
Packit fb9d21
 *
Packit fb9d21
 * on a common denominator.  This denominator D is the lcm of modulos d.
Packit fb9d21
 * Since L_i = U_i^{-1} diag(d_i, 1, ... 1), we have
Packit fb9d21
 * L_i^{-T} = U_i^T diag(d_i, 1, ... 1)^{-T} = U_i^T diag(1/d_i, 1, ..., 1).
Packit fb9d21
 * Putting this on the common denominator, we have
Packit fb9d21
 * D * L_i^{-T} = U_i^T diag(D/d_i, D, ..., D).
Packit fb9d21
 */
Packit fb9d21
static struct isl_mat *parameter_compression_multi(
Packit fb9d21
			struct isl_mat *B, struct isl_vec *d)
Packit fb9d21
{
Packit fb9d21
	int i, j, k;
Packit fb9d21
	isl_int D;
Packit fb9d21
	struct isl_mat *A = NULL, *U = NULL;
Packit fb9d21
	struct isl_mat *T;
Packit fb9d21
	unsigned size;
Packit fb9d21
Packit fb9d21
	isl_int_init(D);
Packit fb9d21
Packit fb9d21
	isl_vec_lcm(d, &D);
Packit fb9d21
Packit fb9d21
	size = B->n_col - 1;
Packit fb9d21
	A = isl_mat_alloc(B->ctx, size, B->n_row * size);
Packit fb9d21
	U = isl_mat_alloc(B->ctx, size, size);
Packit fb9d21
	if (!U || !A)
Packit fb9d21
		goto error;
Packit fb9d21
	for (i = 0; i < B->n_row; ++i) {
Packit fb9d21
		isl_seq_cpy(U->row[0], B->row[i] + 1, size);
Packit fb9d21
		U = isl_mat_unimodular_complete(U, 1);
Packit fb9d21
		if (!U)
Packit fb9d21
			goto error;
Packit fb9d21
		isl_int_divexact(D, D, d->block.data[i]);
Packit fb9d21
		for (k = 0; k < U->n_col; ++k)
Packit fb9d21
			isl_int_mul(A->row[k][i*size+0], D, U->row[0][k]);
Packit fb9d21
		isl_int_mul(D, D, d->block.data[i]);
Packit fb9d21
		for (j = 1; j < U->n_row; ++j)
Packit fb9d21
			for (k = 0; k < U->n_col; ++k)
Packit fb9d21
				isl_int_mul(A->row[k][i*size+j],
Packit fb9d21
						D, U->row[j][k]);
Packit fb9d21
	}
Packit fb9d21
	A = isl_mat_left_hermite(A, 0, NULL, NULL);
Packit fb9d21
	T = isl_mat_sub_alloc(A, 0, A->n_row, 0, A->n_row);
Packit fb9d21
	T = isl_mat_lin_to_aff(T);
Packit fb9d21
	if (!T)
Packit fb9d21
		goto error;
Packit fb9d21
	isl_int_set(T->row[0][0], D);
Packit fb9d21
	T = isl_mat_right_inverse(T);
Packit fb9d21
	if (!T)
Packit fb9d21
		goto error;
Packit fb9d21
	isl_assert(T->ctx, isl_int_is_one(T->row[0][0]), goto error);
Packit fb9d21
	T = isl_mat_transpose(T);
Packit fb9d21
	isl_mat_free(A);
Packit fb9d21
	isl_mat_free(U);
Packit fb9d21
Packit fb9d21
	isl_int_clear(D);
Packit fb9d21
	return T;
Packit fb9d21
error:
Packit fb9d21
	isl_mat_free(A);
Packit fb9d21
	isl_mat_free(U);
Packit fb9d21
	isl_int_clear(D);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given a set of modulo constraints
Packit fb9d21
 *
Packit fb9d21
 *		c + A y = 0 mod d
Packit fb9d21
 *
Packit fb9d21
 * this function returns an affine transformation T,
Packit fb9d21
 *
Packit fb9d21
 *		y = T y'
Packit fb9d21
 *
Packit fb9d21
 * that bijectively maps the integer vectors y' to integer
Packit fb9d21
 * vectors y that satisfy the modulo constraints.
Packit fb9d21
 *
Packit fb9d21
 * This function is inspired by Section 2.5.3
Packit fb9d21
 * of B. Meister, "Stating and Manipulating Periodicity in the Polytope
Packit fb9d21
 * Model.  Applications to Program Analysis and Optimization".
Packit fb9d21
 * However, the implementation only follows the algorithm of that
Packit fb9d21
 * section for computing a particular solution and not for computing
Packit fb9d21
 * a general homogeneous solution.  The latter is incomplete and
Packit fb9d21
 * may remove some valid solutions.
Packit fb9d21
 * Instead, we use an adaptation of the algorithm in Section 7 of
Packit fb9d21
 * B. Meister, S. Verdoolaege, "Polynomial Approximations in the Polytope
Packit fb9d21
 * Model: Bringing the Power of Quasi-Polynomials to the Masses".
Packit fb9d21
 *
Packit fb9d21
 * The input is given as a matrix B = [ c A ] and a vector d.
Packit fb9d21
 * Each element of the vector d corresponds to a row in B.
Packit fb9d21
 * The output is a lower triangular matrix.
Packit fb9d21
 * If no integer vector y satisfies the given constraints then
Packit fb9d21
 * a matrix with zero columns is returned.
Packit fb9d21
 *
Packit fb9d21
 * We first compute a particular solution y_0 to the given set of
Packit fb9d21
 * modulo constraints in particular_solution.  If no such solution
Packit fb9d21
 * exists, then we return a zero-columned transformation matrix.
Packit fb9d21
 * Otherwise, we compute the generic solution to
Packit fb9d21
 *
Packit fb9d21
 *		A y = 0 mod d
Packit fb9d21
 *
Packit fb9d21
 * That is we want to compute G such that
Packit fb9d21
 *
Packit fb9d21
 *		y = G y''
Packit fb9d21
 *
Packit fb9d21
 * with y'' integer, describes the set of solutions.
Packit fb9d21
 *
Packit fb9d21
 * We first remove the common factors of each row.
Packit fb9d21
 * In particular if gcd(A_i,d_i) != 1, then we divide the whole
Packit fb9d21
 * row i (including d_i) by this common factor.  If afterwards gcd(A_i) != 1,
Packit fb9d21
 * then we divide this row of A by the common factor, unless gcd(A_i) = 0.
Packit fb9d21
 * In the later case, we simply drop the row (in both A and d).
Packit fb9d21
 *
Packit fb9d21
 * If there are no rows left in A, then G is the identity matrix. Otherwise,
Packit fb9d21
 * for each row i, we now determine the lattice of integer vectors
Packit fb9d21
 * that satisfies this row.  Let U_i be the unimodular extension of the
Packit fb9d21
 * row A_i.  This unimodular extension exists because gcd(A_i) = 1.
Packit fb9d21
 * The first component of
Packit fb9d21
 *
Packit fb9d21
 *		y' = U_i y
Packit fb9d21
 *
Packit fb9d21
 * needs to be a multiple of d_i.  Let y' = diag(d_i, 1, ..., 1) y''.
Packit fb9d21
 * Then,
Packit fb9d21
 *
Packit fb9d21
 *		y = U_i^{-1} diag(d_i, 1, ..., 1) y''
Packit fb9d21
 *
Packit fb9d21
 * for arbitrary integer vectors y''.  That is, y belongs to the lattice
Packit fb9d21
 * generated by the columns of L_i = U_i^{-1} diag(d_i, 1, ..., 1).
Packit fb9d21
 * If there is only one row, then G = L_1.
Packit fb9d21
 *
Packit fb9d21
 * If there is more than one row left, we need to compute the intersection
Packit fb9d21
 * of the lattices.  That is, we need to compute an L such that
Packit fb9d21
 *
Packit fb9d21
 *		L = L_i L_i'	for all i
Packit fb9d21
 *
Packit fb9d21
 * with L_i' some integer matrices.  Let A be constructed as follows
Packit fb9d21
 *
Packit fb9d21
 *		A = [ L_1^{-T} L_2^{-T} ... L_k^{-T} ]
Packit fb9d21
 *
Packit fb9d21
 * and computed the Hermite Normal Form of A = [ H 0 ] U
Packit fb9d21
 * Then,
Packit fb9d21
 *
Packit fb9d21
 *		L_i^{-T} = H U_{1,i}
Packit fb9d21
 *
Packit fb9d21
 * or
Packit fb9d21
 *
Packit fb9d21
 *		H^{-T} = L_i U_{1,i}^T
Packit fb9d21
 *
Packit fb9d21
 * In other words G = L = H^{-T}.
Packit fb9d21
 * To ensure that G is lower triangular, we compute and use its Hermite
Packit fb9d21
 * normal form.
Packit fb9d21
 *
Packit fb9d21
 * The affine transformation matrix returned is then
Packit fb9d21
 *
Packit fb9d21
 *		[  1   0  ]
Packit fb9d21
 *		[ y_0  G  ]
Packit fb9d21
 *
Packit fb9d21
 * as any y = y_0 + G y' with y' integer is a solution to the original
Packit fb9d21
 * modulo constraints.
Packit fb9d21
 */
Packit fb9d21
struct isl_mat *isl_mat_parameter_compression(
Packit fb9d21
			struct isl_mat *B, struct isl_vec *d)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	struct isl_mat *cst = NULL;
Packit fb9d21
	struct isl_mat *T = NULL;
Packit fb9d21
	isl_int D;
Packit fb9d21
Packit fb9d21
	if (!B || !d)
Packit fb9d21
		goto error;
Packit fb9d21
	isl_assert(B->ctx, B->n_row == d->size, goto error);
Packit fb9d21
	cst = particular_solution(B, d);
Packit fb9d21
	if (!cst)
Packit fb9d21
		goto error;
Packit fb9d21
	if (cst->n_col == 0) {
Packit fb9d21
		T = isl_mat_alloc(B->ctx, B->n_col, 0);
Packit fb9d21
		isl_mat_free(cst);
Packit fb9d21
		isl_mat_free(B);
Packit fb9d21
		isl_vec_free(d);
Packit fb9d21
		return T;
Packit fb9d21
	}
Packit fb9d21
	isl_int_init(D);
Packit fb9d21
	/* Replace a*g*row = 0 mod g*m by row = 0 mod m */
Packit fb9d21
	for (i = 0; i < B->n_row; ++i) {
Packit fb9d21
		isl_seq_gcd(B->row[i] + 1, B->n_col - 1, &D);
Packit fb9d21
		if (isl_int_is_one(D))
Packit fb9d21
			continue;
Packit fb9d21
		if (isl_int_is_zero(D)) {
Packit fb9d21
			B = isl_mat_drop_rows(B, i, 1);
Packit fb9d21
			d = isl_vec_cow(d);
Packit fb9d21
			if (!B || !d)
Packit fb9d21
				goto error2;
Packit fb9d21
			isl_seq_cpy(d->block.data+i, d->block.data+i+1,
Packit fb9d21
							d->size - (i+1));
Packit fb9d21
			d->size--;
Packit fb9d21
			i--;
Packit fb9d21
			continue;
Packit fb9d21
		}
Packit fb9d21
		B = isl_mat_cow(B);
Packit fb9d21
		if (!B)
Packit fb9d21
			goto error2;
Packit fb9d21
		isl_seq_scale_down(B->row[i] + 1, B->row[i] + 1, D, B->n_col-1);
Packit fb9d21
		isl_int_gcd(D, D, d->block.data[i]);
Packit fb9d21
		d = isl_vec_cow(d);
Packit fb9d21
		if (!d)
Packit fb9d21
			goto error2;
Packit fb9d21
		isl_int_divexact(d->block.data[i], d->block.data[i], D);
Packit fb9d21
	}
Packit fb9d21
	isl_int_clear(D);
Packit fb9d21
	if (B->n_row == 0)
Packit fb9d21
		T = isl_mat_identity(B->ctx, B->n_col);
Packit fb9d21
	else if (B->n_row == 1)
Packit fb9d21
		T = parameter_compression_1(B, d);
Packit fb9d21
	else
Packit fb9d21
		T = parameter_compression_multi(B, d);
Packit fb9d21
	T = isl_mat_left_hermite(T, 0, NULL, NULL);
Packit fb9d21
	if (!T)
Packit fb9d21
		goto error;
Packit fb9d21
	isl_mat_sub_copy(T->ctx, T->row + 1, cst->row, cst->n_row, 0, 0, 1);
Packit fb9d21
	isl_mat_free(cst);
Packit fb9d21
	isl_mat_free(B);
Packit fb9d21
	isl_vec_free(d);
Packit fb9d21
	return T;
Packit fb9d21
error2:
Packit fb9d21
	isl_int_clear(D);
Packit fb9d21
error:
Packit fb9d21
	isl_mat_free(cst);
Packit fb9d21
	isl_mat_free(B);
Packit fb9d21
	isl_vec_free(d);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given a set of equalities
Packit fb9d21
 *
Packit fb9d21
 *		B(y) + A x = 0						(*)
Packit fb9d21
 *
Packit fb9d21
 * compute and return an affine transformation T,
Packit fb9d21
 *
Packit fb9d21
 *		y = T y'
Packit fb9d21
 *
Packit fb9d21
 * that bijectively maps the integer vectors y' to integer
Packit fb9d21
 * vectors y that satisfy the modulo constraints for some value of x.
Packit fb9d21
 *
Packit fb9d21
 * Let [H 0] be the Hermite Normal Form of A, i.e.,
Packit fb9d21
 *
Packit fb9d21
 *		A = [H 0] Q
Packit fb9d21
 *
Packit fb9d21
 * Then y is a solution of (*) iff
Packit fb9d21
 *
Packit fb9d21
 *		H^-1 B(y) (= - [I 0] Q x)
Packit fb9d21
 *
Packit fb9d21
 * is an integer vector.  Let d be the common denominator of H^-1.
Packit fb9d21
 * We impose
Packit fb9d21
 *
Packit fb9d21
 *		d H^-1 B(y) = 0 mod d
Packit fb9d21
 *
Packit fb9d21
 * and compute the solution using isl_mat_parameter_compression.
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_mat *isl_mat_parameter_compression_ext(__isl_take isl_mat *B,
Packit fb9d21
	__isl_take isl_mat *A)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_vec *d;
Packit fb9d21
	int n_row, n_col;
Packit fb9d21
Packit fb9d21
	if (!A)
Packit fb9d21
		return isl_mat_free(B);
Packit fb9d21
Packit fb9d21
	ctx = isl_mat_get_ctx(A);
Packit fb9d21
	n_row = A->n_row;
Packit fb9d21
	n_col = A->n_col;
Packit fb9d21
	A = isl_mat_left_hermite(A, 0, NULL, NULL);
Packit fb9d21
	A = isl_mat_drop_cols(A, n_row, n_col - n_row);
Packit fb9d21
	A = isl_mat_lin_to_aff(A);
Packit fb9d21
	A = isl_mat_right_inverse(A);
Packit fb9d21
	d = isl_vec_alloc(ctx, n_row);
Packit fb9d21
	if (A)
Packit fb9d21
		d = isl_vec_set(d, A->row[0][0]);
Packit fb9d21
	A = isl_mat_drop_rows(A, 0, 1);
Packit fb9d21
	A = isl_mat_drop_cols(A, 0, 1);
Packit fb9d21
	B = isl_mat_product(A, B);
Packit fb9d21
Packit fb9d21
	return isl_mat_parameter_compression(B, d);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Given a set of equalities
Packit fb9d21
 *
Packit fb9d21
 *		M x - c = 0
Packit fb9d21
 *
Packit fb9d21
 * this function computes a unimodular transformation from a lower-dimensional
Packit fb9d21
 * space to the original space that bijectively maps the integer points x'
Packit fb9d21
 * in the lower-dimensional space to the integer points x in the original
Packit fb9d21
 * space that satisfy the equalities.
Packit fb9d21
 *
Packit fb9d21
 * The input is given as a matrix B = [ -c M ] and the output is a
Packit fb9d21
 * matrix that maps [1 x'] to [1 x].
Packit fb9d21
 * If T2 is not NULL, then *T2 is set to a matrix mapping [1 x] to [1 x'].
Packit fb9d21
 *
Packit fb9d21
 * First compute the (left) Hermite normal form of M,
Packit fb9d21
 *
Packit fb9d21
 *		M [U1 U2] = M U = H = [H1 0]
Packit fb9d21
 * or
Packit fb9d21
 *		              M = H Q = [H1 0] [Q1]
Packit fb9d21
 *                                             [Q2]
Packit fb9d21
 *
Packit fb9d21
 * with U, Q unimodular, Q = U^{-1} (and H lower triangular).
Packit fb9d21
 * Define the transformed variables as
Packit fb9d21
 *
Packit fb9d21
 *		x = [U1 U2] [ x1' ] = [U1 U2] [Q1] x
Packit fb9d21
 *		            [ x2' ]           [Q2]
Packit fb9d21
 *
Packit fb9d21
 * The equalities then become
Packit fb9d21
 *
Packit fb9d21
 *		H1 x1' - c = 0   or   x1' = H1^{-1} c = c'
Packit fb9d21
 *
Packit fb9d21
 * If any of the c' is non-integer, then the original set has no
Packit fb9d21
 * integer solutions (since the x' are a unimodular transformation
Packit fb9d21
 * of the x) and a zero-column matrix is returned.
Packit fb9d21
 * Otherwise, the transformation is given by
Packit fb9d21
 *
Packit fb9d21
 *		x = U1 H1^{-1} c + U2 x2'
Packit fb9d21
 *
Packit fb9d21
 * The inverse transformation is simply
Packit fb9d21
 *
Packit fb9d21
 *		x2' = Q2 x
Packit fb9d21
 */
Packit fb9d21
__isl_give isl_mat *isl_mat_variable_compression(__isl_take isl_mat *B,
Packit fb9d21
	__isl_give isl_mat **T2)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	struct isl_mat *H = NULL, *C = NULL, *H1, *U = NULL, *U1, *U2, *TC;
Packit fb9d21
	unsigned dim;
Packit fb9d21
Packit fb9d21
	if (T2)
Packit fb9d21
		*T2 = NULL;
Packit fb9d21
	if (!B)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	dim = B->n_col - 1;
Packit fb9d21
	H = isl_mat_sub_alloc(B, 0, B->n_row, 1, dim);
Packit fb9d21
	H = isl_mat_left_hermite(H, 0, &U, T2);
Packit fb9d21
	if (!H || !U || (T2 && !*T2))
Packit fb9d21
		goto error;
Packit fb9d21
	if (T2) {
Packit fb9d21
		*T2 = isl_mat_drop_rows(*T2, 0, B->n_row);
Packit fb9d21
		*T2 = isl_mat_lin_to_aff(*T2);
Packit fb9d21
		if (!*T2)
Packit fb9d21
			goto error;
Packit fb9d21
	}
Packit fb9d21
	C = isl_mat_alloc(B->ctx, 1+B->n_row, 1);
Packit fb9d21
	if (!C)
Packit fb9d21
		goto error;
Packit fb9d21
	isl_int_set_si(C->row[0][0], 1);
Packit fb9d21
	isl_mat_sub_neg(C->ctx, C->row+1, B->row, B->n_row, 0, 0, 1);
Packit fb9d21
	H1 = isl_mat_sub_alloc(H, 0, H->n_row, 0, H->n_row);
Packit fb9d21
	H1 = isl_mat_lin_to_aff(H1);
Packit fb9d21
	TC = isl_mat_inverse_product(H1, C);
Packit fb9d21
	if (!TC)
Packit fb9d21
		goto error;
Packit fb9d21
	isl_mat_free(H);
Packit fb9d21
	if (!isl_int_is_one(TC->row[0][0])) {
Packit fb9d21
		for (i = 0; i < B->n_row; ++i) {
Packit fb9d21
			if (!isl_int_is_divisible_by(TC->row[1+i][0], TC->row[0][0])) {
Packit fb9d21
				struct isl_ctx *ctx = B->ctx;
Packit fb9d21
				isl_mat_free(B);
Packit fb9d21
				isl_mat_free(TC);
Packit fb9d21
				isl_mat_free(U);
Packit fb9d21
				if (T2) {
Packit fb9d21
					isl_mat_free(*T2);
Packit fb9d21
					*T2 = NULL;
Packit fb9d21
				}
Packit fb9d21
				return isl_mat_alloc(ctx, 1 + dim, 0);
Packit fb9d21
			}
Packit fb9d21
			isl_seq_scale_down(TC->row[1+i], TC->row[1+i], TC->row[0][0], 1);
Packit fb9d21
		}
Packit fb9d21
		isl_int_set_si(TC->row[0][0], 1);
Packit fb9d21
	}
Packit fb9d21
	U1 = isl_mat_sub_alloc(U, 0, U->n_row, 0, B->n_row);
Packit fb9d21
	U1 = isl_mat_lin_to_aff(U1);
Packit fb9d21
	U2 = isl_mat_sub_alloc(U, 0, U->n_row, B->n_row, U->n_row - B->n_row);
Packit fb9d21
	U2 = isl_mat_lin_to_aff(U2);
Packit fb9d21
	isl_mat_free(U);
Packit fb9d21
	TC = isl_mat_product(U1, TC);
Packit fb9d21
	TC = isl_mat_aff_direct_sum(TC, U2);
Packit fb9d21
Packit fb9d21
	isl_mat_free(B);
Packit fb9d21
Packit fb9d21
	return TC;
Packit fb9d21
error:
Packit fb9d21
	isl_mat_free(B);
Packit fb9d21
	isl_mat_free(H);
Packit fb9d21
	isl_mat_free(U);
Packit fb9d21
	if (T2) {
Packit fb9d21
		isl_mat_free(*T2);
Packit fb9d21
		*T2 = NULL;
Packit fb9d21
	}
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Use the n equalities of bset to unimodularly transform the
Packit fb9d21
 * variables x such that n transformed variables x1' have a constant value
Packit fb9d21
 * and rewrite the constraints of bset in terms of the remaining
Packit fb9d21
 * transformed variables x2'.  The matrix pointed to by T maps
Packit fb9d21
 * the new variables x2' back to the original variables x, while T2
Packit fb9d21
 * maps the original variables to the new variables.
Packit fb9d21
 */
Packit fb9d21
static struct isl_basic_set *compress_variables(
Packit fb9d21
	struct isl_basic_set *bset, struct isl_mat **T, struct isl_mat **T2)
Packit fb9d21
{
Packit fb9d21
	struct isl_mat *B, *TC;
Packit fb9d21
	unsigned dim;
Packit fb9d21
Packit fb9d21
	if (T)
Packit fb9d21
		*T = NULL;
Packit fb9d21
	if (T2)
Packit fb9d21
		*T2 = NULL;
Packit fb9d21
	if (!bset)
Packit fb9d21
		goto error;
Packit fb9d21
	isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
Packit fb9d21
	isl_assert(bset->ctx, bset->n_div == 0, goto error);
Packit fb9d21
	dim = isl_basic_set_n_dim(bset);
Packit fb9d21
	isl_assert(bset->ctx, bset->n_eq <= dim, goto error);
Packit fb9d21
	if (bset->n_eq == 0)
Packit fb9d21
		return bset;
Packit fb9d21
Packit fb9d21
	B = isl_mat_sub_alloc6(bset->ctx, bset->eq, 0, bset->n_eq, 0, 1 + dim);
Packit fb9d21
	TC = isl_mat_variable_compression(B, T2);
Packit fb9d21
	if (!TC)
Packit fb9d21
		goto error;
Packit fb9d21
	if (TC->n_col == 0) {
Packit fb9d21
		isl_mat_free(TC);
Packit fb9d21
		if (T2) {
Packit fb9d21
			isl_mat_free(*T2);
Packit fb9d21
			*T2 = NULL;
Packit fb9d21
		}
Packit fb9d21
		return isl_basic_set_set_to_empty(bset);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	bset = isl_basic_set_preimage(bset, T ? isl_mat_copy(TC) : TC);
Packit fb9d21
	if (T)
Packit fb9d21
		*T = TC;
Packit fb9d21
	return bset;
Packit fb9d21
error:
Packit fb9d21
	isl_basic_set_free(bset);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
struct isl_basic_set *isl_basic_set_remove_equalities(
Packit fb9d21
	struct isl_basic_set *bset, struct isl_mat **T, struct isl_mat **T2)
Packit fb9d21
{
Packit fb9d21
	if (T)
Packit fb9d21
		*T = NULL;
Packit fb9d21
	if (T2)
Packit fb9d21
		*T2 = NULL;
Packit fb9d21
	if (!bset)
Packit fb9d21
		return NULL;
Packit fb9d21
	isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
Packit fb9d21
	bset = isl_basic_set_gauss(bset, NULL);
Packit fb9d21
	if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
Packit fb9d21
		return bset;
Packit fb9d21
	bset = compress_variables(bset, T, T2);
Packit fb9d21
	return bset;
Packit fb9d21
error:
Packit fb9d21
	isl_basic_set_free(bset);
Packit fb9d21
	*T = NULL;
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Check if dimension dim belongs to a residue class
Packit fb9d21
 *		i_dim \equiv r mod m
Packit fb9d21
 * with m != 1 and if so return m in *modulo and r in *residue.
Packit fb9d21
 * As a special case, when i_dim has a fixed value v, then
Packit fb9d21
 * *modulo is set to 0 and *residue to v.
Packit fb9d21
 *
Packit fb9d21
 * If i_dim does not belong to such a residue class, then *modulo
Packit fb9d21
 * is set to 1 and *residue is set to 0.
Packit fb9d21
 */
Packit fb9d21
int isl_basic_set_dim_residue_class(struct isl_basic_set *bset,
Packit fb9d21
	int pos, isl_int *modulo, isl_int *residue)
Packit fb9d21
{
Packit fb9d21
	struct isl_ctx *ctx;
Packit fb9d21
	struct isl_mat *H = NULL, *U = NULL, *C, *H1, *U1;
Packit fb9d21
	unsigned total;
Packit fb9d21
	unsigned nparam;
Packit fb9d21
Packit fb9d21
	if (!bset || !modulo || !residue)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	if (isl_basic_set_plain_dim_is_fixed(bset, pos, residue)) {
Packit fb9d21
		isl_int_set_si(*modulo, 0);
Packit fb9d21
		return 0;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	ctx = bset->ctx;
Packit fb9d21
	total = isl_basic_set_total_dim(bset);
Packit fb9d21
	nparam = isl_basic_set_n_param(bset);
Packit fb9d21
	H = isl_mat_sub_alloc6(bset->ctx, bset->eq, 0, bset->n_eq, 1, total);
Packit fb9d21
	H = isl_mat_left_hermite(H, 0, &U, NULL);
Packit fb9d21
	if (!H)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	isl_seq_gcd(U->row[nparam + pos]+bset->n_eq,
Packit fb9d21
			total-bset->n_eq, modulo);
Packit fb9d21
	if (isl_int_is_zero(*modulo))
Packit fb9d21
		isl_int_set_si(*modulo, 1);
Packit fb9d21
	if (isl_int_is_one(*modulo)) {
Packit fb9d21
		isl_int_set_si(*residue, 0);
Packit fb9d21
		isl_mat_free(H);
Packit fb9d21
		isl_mat_free(U);
Packit fb9d21
		return 0;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	C = isl_mat_alloc(bset->ctx, 1+bset->n_eq, 1);
Packit fb9d21
	if (!C)
Packit fb9d21
		goto error;
Packit fb9d21
	isl_int_set_si(C->row[0][0], 1);
Packit fb9d21
	isl_mat_sub_neg(C->ctx, C->row+1, bset->eq, bset->n_eq, 0, 0, 1);
Packit fb9d21
	H1 = isl_mat_sub_alloc(H, 0, H->n_row, 0, H->n_row);
Packit fb9d21
	H1 = isl_mat_lin_to_aff(H1);
Packit fb9d21
	C = isl_mat_inverse_product(H1, C);
Packit fb9d21
	isl_mat_free(H);
Packit fb9d21
	U1 = isl_mat_sub_alloc(U, nparam+pos, 1, 0, bset->n_eq);
Packit fb9d21
	U1 = isl_mat_lin_to_aff(U1);
Packit fb9d21
	isl_mat_free(U);
Packit fb9d21
	C = isl_mat_product(U1, C);
Packit fb9d21
	if (!C)
Packit fb9d21
		return -1;
Packit fb9d21
	if (!isl_int_is_divisible_by(C->row[1][0], C->row[0][0])) {
Packit fb9d21
		bset = isl_basic_set_copy(bset);
Packit fb9d21
		bset = isl_basic_set_set_to_empty(bset);
Packit fb9d21
		isl_basic_set_free(bset);
Packit fb9d21
		isl_int_set_si(*modulo, 1);
Packit fb9d21
		isl_int_set_si(*residue, 0);
Packit fb9d21
		return 0;
Packit fb9d21
	}
Packit fb9d21
	isl_int_divexact(*residue, C->row[1][0], C->row[0][0]);
Packit fb9d21
	isl_int_fdiv_r(*residue, *residue, *modulo);
Packit fb9d21
	isl_mat_free(C);
Packit fb9d21
	return 0;
Packit fb9d21
error:
Packit fb9d21
	isl_mat_free(H);
Packit fb9d21
	isl_mat_free(U);
Packit fb9d21
	return -1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Check if dimension dim belongs to a residue class
Packit fb9d21
 *		i_dim \equiv r mod m
Packit fb9d21
 * with m != 1 and if so return m in *modulo and r in *residue.
Packit fb9d21
 * As a special case, when i_dim has a fixed value v, then
Packit fb9d21
 * *modulo is set to 0 and *residue to v.
Packit fb9d21
 *
Packit fb9d21
 * If i_dim does not belong to such a residue class, then *modulo
Packit fb9d21
 * is set to 1 and *residue is set to 0.
Packit fb9d21
 */
Packit fb9d21
int isl_set_dim_residue_class(struct isl_set *set,
Packit fb9d21
	int pos, isl_int *modulo, isl_int *residue)
Packit fb9d21
{
Packit fb9d21
	isl_int m;
Packit fb9d21
	isl_int r;
Packit fb9d21
	int i;
Packit fb9d21
Packit fb9d21
	if (!set || !modulo || !residue)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	if (set->n == 0) {
Packit fb9d21
		isl_int_set_si(*modulo, 0);
Packit fb9d21
		isl_int_set_si(*residue, 0);
Packit fb9d21
		return 0;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	if (isl_basic_set_dim_residue_class(set->p[0], pos, modulo, residue)<0)
Packit fb9d21
		return -1;
Packit fb9d21
Packit fb9d21
	if (set->n == 1)
Packit fb9d21
		return 0;
Packit fb9d21
Packit fb9d21
	if (isl_int_is_one(*modulo))
Packit fb9d21
		return 0;
Packit fb9d21
Packit fb9d21
	isl_int_init(m);
Packit fb9d21
	isl_int_init(r);
Packit fb9d21
Packit fb9d21
	for (i = 1; i < set->n; ++i) {
Packit fb9d21
		if (isl_basic_set_dim_residue_class(set->p[i], pos, &m, &r) < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		isl_int_gcd(*modulo, *modulo, m);
Packit fb9d21
		isl_int_sub(m, *residue, r);
Packit fb9d21
		isl_int_gcd(*modulo, *modulo, m);
Packit fb9d21
		if (!isl_int_is_zero(*modulo))
Packit fb9d21
			isl_int_fdiv_r(*residue, *residue, *modulo);
Packit fb9d21
		if (isl_int_is_one(*modulo))
Packit fb9d21
			break;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	isl_int_clear(m);
Packit fb9d21
	isl_int_clear(r);
Packit fb9d21
Packit fb9d21
	return 0;
Packit fb9d21
error:
Packit fb9d21
	isl_int_clear(m);
Packit fb9d21
	isl_int_clear(r);
Packit fb9d21
	return -1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Check if dimension "dim" belongs to a residue class
Packit fb9d21
 *		i_dim \equiv r mod m
Packit fb9d21
 * with m != 1 and if so return m in *modulo and r in *residue.
Packit fb9d21
 * As a special case, when i_dim has a fixed value v, then
Packit fb9d21
 * *modulo is set to 0 and *residue to v.
Packit fb9d21
 *
Packit fb9d21
 * If i_dim does not belong to such a residue class, then *modulo
Packit fb9d21
 * is set to 1 and *residue is set to 0.
Packit fb9d21
 */
Packit fb9d21
int isl_set_dim_residue_class_val(__isl_keep isl_set *set,
Packit fb9d21
	int pos, __isl_give isl_val **modulo, __isl_give isl_val **residue)
Packit fb9d21
{
Packit fb9d21
	*modulo = NULL;
Packit fb9d21
	*residue = NULL;
Packit fb9d21
	if (!set)
Packit fb9d21
		return -1;
Packit fb9d21
	*modulo = isl_val_alloc(isl_set_get_ctx(set));
Packit fb9d21
	*residue = isl_val_alloc(isl_set_get_ctx(set));
Packit fb9d21
	if (!*modulo || !*residue)
Packit fb9d21
		goto error;
Packit fb9d21
	if (isl_set_dim_residue_class(set, pos,
Packit fb9d21
					&(*modulo)->n, &(*residue)->n) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
	isl_int_set_si((*modulo)->d, 1);
Packit fb9d21
	isl_int_set_si((*residue)->d, 1);
Packit fb9d21
	return 0;
Packit fb9d21
error:
Packit fb9d21
	isl_val_free(*modulo);
Packit fb9d21
	isl_val_free(*residue);
Packit fb9d21
	return -1;
Packit fb9d21
}