22 for (temp = Head; temp != NULL; temp = temp->
next)
34 while (Head != NULL) {
66 fp = fopen(
"_debug",
"a");
67 fprintf(fp,
"\nEntered SAMEAFFINEPART \n");
71 for (i = 0; i < A->NbRows; i++)
72 if (
value_ne(A->p[i][A->NbColumns - 1], B->p[i][B->NbColumns - 1]))
88 fp = fopen(
"_debug",
"a");
89 fprintf(fp,
"\nEntered NULLATTICE \n");
95 for (j = 0; j < dimension; j++)
107 if(A == NULL || A->NbColumns == 0) {
111 for (
int j = 0; j < A->NbRows-1; j++) {
131 fp = fopen(
"_debug",
"a");
132 fprintf(fp,
"\nEntered ISLINEAR \n");
136 for (i = 0; i < A->NbRows - 1; i++)
313 fp = fopen(
"_debug",
"a");
314 fprintf(fp,
"\nEntered HOMOGENISE \n");
319 if (Forward ==
True) {
341 fp = fopen(
"_debug",
"a");
342 fprintf(fp,
"\nEntered LATTICE INCLUDES \n");
374 fp = fopen(
"_debug",
"a");
375 fprintf(fp,
"\nEntered SAME LATTICE \n");
379 if(A->NbRows != B->NbRows || A->NbColumns != B->NbColumns)
385 for (i = 0; i < A->NbRows; i++)
386 for (j = 0; j < A->NbColumns; j++)
387 if (
value_ne(HA->p[i][j], HB->p[i][j])) {
411 if (dimension <= A->NbRows) {
412 for (i = 0; i < dimension; i++)
413 for (j = 0; j < dimension; j++)
417 for (i = 0; i < A->NbRows; i++)
418 for (j = 0; j < A->NbRows; j++)
421 for (i = A->NbRows; i < dimension; i++)
422 for (j = 0; j < dimension; j++) {
426 for (i = A->NbRows; i < dimension; i++)
439 Result = (Lattice *)
Matrix_Alloc(A->NbRows - 1, A->NbColumns - 1);
440 for (i = 0; i < A->NbRows - 1; i++)
441 for (j = 0; j < A->NbColumns - 1; j++)
687 Lattice *B1 = NULL, *B2 = NULL, *newB1 = NULL, *newB2 = NULL,
688 *Intersection = NULL;
689 Matrix *U = NULL, *M1 = NULL, *M2 = NULL, *M1Inverse = NULL,
691 Matrix *Vinv, *V, *temp, *DiagMatrix;
699 fprintf(stderr,
"Lattice intersection = ");
704 fprintf(stderr,
"\nIn Lattice2LatticeUnion : the input lattices X and Y do "
705 "not have any common part\n");
715 fprintf(stderr,
"M1 = ");
717 fprintf(stderr,
"M2 = ");
721 int maxdim = (M1->NbColumns > M1->NbRows)? M1->NbColumns : M1->NbRows;
728 for (i = 0; i < M1->NbRows; i++) {
730 for (j = 0; j < M1->NbColumns; j++)
732 for( ; j < maxdim; j++)
736 for (
int j = 0; j < M1->NbColumns; j++)
743 M1Inverse->NbColumns = M1->NbRows;
744 M1Inverse->NbRows = M1->NbColumns;
750 fprintf(stderr,
"M1Inverse = ");
754 MtProduct =
Matrix_Alloc(M1Inverse->NbRows, M2->NbColumns);
768 for (i = 0; i < DiagMatrix->
NbRows; i++)
772 fprintf(stderr,
"B1 = ");
774 fprintf(stderr,
"B2 = ");
788 for(i=0 ; i<B1->NbRows; i++) {
789 for(
int j=0 ; j<B1->NbColumns; j++) {
795 for(
int j=0 ; j<B1->NbColumns; j++) {
800 for (i = 0; i < newB2->NbRows; i++)
802 value_set_si(newB1->p[i][newB2->NbColumns - 1], (i==newB2->NbColumns - 1));
804 Intersection->p[i][Intersection->NbColumns - 1]);
808 fprintf(stderr,
"newB2 = ");
810 fprintf(stderr,
"Diag = ");
841 fp = fopen(
"_debug",
"a");
842 fprintf(fp,
"\nEntered LATTICEDIFFERENCE \n");
846 if (A->NbRows != B->NbRows) {
847 errormsg1(
"LatticeDifference",
"dimincomp",
848 "input lattices A and B have incompatible dimensions (rows)");
851 if (A->NbColumns != B->NbColumns) {
852 errormsg1(
"LatticeDifference",
"dimincomp",
853 "input lattices A and B have incompatible dimensions (columns)");
877 fprintf(stderr,
"Entering LatDiff. X = ");
879 fprintf(stderr,
"Y = ");
888 fprintf(stderr,
"Inter = ");
924 for (
int line = 0; line < Inter->NbRows; line++) {
931 fprintf(stderr,
"Empty result\n");
946 fprintf(stderr,
"Raw result = ");
950 fprintf(stderr,
"empty\n");
953 if ((Head != NULL)) {
956 fprintf(stderr,
"Simplified result = ");
985 for (i = 0; i < C->
NbRows; i++)
1005 Value NumofTimes,
int Colnumber) {
1015 while (tail->
next != NULL)
1019 for (temp = Head; temp != NULL; temp = temp->
next) {
1021 Lattice *tempMatrix, *H;
1024 for (j = 0; j < B2->
NbRows; j++) {
1026 B1->
p[j][Colnumber]);
1035 tempMatrix->p[j][B2->
NbColumns - 1], maxval);
1340 fp = fopen(
"_debug",
"a");
1341 fprintf(fp,
"\nEntered ISLATTICE \n");
1348 if (
m->NbRows !=
m->NbColumns)
1351 for (i = 0; i <
m->NbColumns - 1; i++)
1409 Value cnt, aux, k, fac, tmp, foobar;
1411 if ((*InputList == NULL) || (InputList[0]->next == NULL))
1422 width = InputList[0]->
M->
NbRows - 1;
1425 for (temp = InputList[0]; temp != NULL; temp = temp->
next)
1427 for (i = 0; i < allfac.
count; i++) {
1433 if (i == allfac.
count) {
1444 for (; i < allfac.
count; i++) {
1448 if (*InputList == NULL) {
1464 for (temp = *InputList; temp != NULL; temp = temp->
next) {
1469 if (
value_ge(fac, (*InputList)->M->p[dim][dim])) {
1478 if (Present ==
True) {
1480 if (*ResultList == NULL)
1483 for (temp = *ResultList; temp->
next != NULL; temp = temp->
next)
1498 temp = InputList[0];
1499 while (temp != NULL) {
1500 if (
value_eq(temp->
M->
p[width][width], tmp)) {
1501 if (temp == InputList[0]) {
1503 temp = InputList[0] = temp->
next;
1549 for (i = 0; i < L1[0]->NbRows - 1; i++)
1550 for (j = 0; j <= i; j++) {
1551 if (
value_gt(L1[0]->p[i][j], L2[0]->p[i][j]))
1553 if (
value_lt(L1[0]->p[i][j], L2[0]->p[i][j]))
1571 for (temp = Head; temp != NULL; temp = temp->
next)
1574 Latlist = (Lattice **)malloc(
sizeof(Lattice *) * cnt);
1577 for (temp = Head; temp != NULL; temp = temp->
next)
1578 Latlist[cnt++] = temp->
M;
1583 for (temp = Head; temp != NULL; temp = temp->
next)
1584 temp->
M = Latlist[cnt++];
1604 for (i = 0; i < L1[0]->NbRows; i++) {
1605 if (
value_gt(L1[0]->p[i][L1[0]->NbColumns - 1],
1606 L2[0]->p[i][L2[0]->NbColumns - 1]))
1609 if (
value_lt(L1[0]->p[i][L1[0]->NbColumns - 1],
1610 L2[0]->p[i][L2[0]->NbColumns - 1]))
1627 for (tmp = List; tmp != NULL; tmp = tmp->
next)
1630 LatList = malloc(
sizeof(Lattice *) * cnt);
1633 for (tmp = List; tmp != NULL; tmp = tmp->
next)
1634 LatList[cnt++] = tmp->
M;
1639 for (tmp = List; tmp != NULL; tmp = tmp->
next)
1640 tmp->
M = LatList[cnt++];
1649 if ((A == NULL) || (B == NULL))
1652 for (i = 0; i < A->
M->
NbRows - 1; i++)
1673 if (curlist == NULL)
1676 if (curlist->
next == NULL) {
1677 curlist->
next = newlist[0];
1678 newlist[0] = curlist;
1683 for (i = 0; i < curlist->
M->
NbRows - 1; i++) {
1688 for (temp = curlist; temp != NULL; temp = temp->
next) {
1698 while (curr != NULL) {
1702 chng =
Simplify(&curlist, newlist, i);
1703 if (nextlist == NULL)
1707 for (tmp = nextlist; tmp->
next; tmp = tmp->
next)
1709 tmp->
next = curlist;
1711 change = (
Bool)(change | chng);
1721 for (temp = curlist; temp != NULL; temp = temp->
next) {
1728 if (curlist == NULL)
1731 if (*newlist == NULL)
1732 *newlist = nextlist;
1734 for (curr = *newlist; curr->
next != NULL; curr = curr->
next)
1736 curr->
next = nextlist;
1745 if ((A == NULL) || (B == NULL))
1747 for (i = 0; i < A->
M->
NbRows - 1; i++)
1748 for (j = 0; j <= i; j++)
1766 while (change ==
True) {
1771 while (curr != NULL) {
1808 result.
fac = malloc(tabsize *
sizeof(
int));
1810 while(div*div <= rest)
1815 if(result.
count == tabsize)
1818 result.
fac = realloc(result.
fac, tabsize *
sizeof(
int));
1825 div += (1 + (1&div));
1830 if(result.
count == tabsize)
1833 result.
fac = realloc(result.
fac, tabsize *
sizeof(
int));
1847 int size = (1<<(primes.
count));
1849 result.
fac = malloc(size *
sizeof(
int));
1852 for(
int mask=1; mask < size; mask++) {
1855 for(
int bits=0, rest=mask; rest ; bits++, rest>>=1) {
1857 val *= primes.
fac[bits];
1861 if(val > result.
fac[result.
count-1]) {
1944 Lattice *Tmp, *H, *Res;
1945 if(A->NbRows != B->NbRows){
1946 errormsg1(
"LatticeIntersection",
"dimincomp",
"incompatible dimensions!");
1949 #ifdef LATINTER_DEBUG
1950 fprintf(stderr,
"---Entering LatInter---\nMatrix A = ");
1952 fprintf(stderr,
"Matrix B = ");
1964 Tmp =
Matrix_Alloc(A->NbRows*2, A->NbColumns+B->NbColumns);
1967 errormsg1(
"LatticeIntersection",
"outofmem",
"Not enough memory space!");
1974 value_assign(Tmp->p[0][0], A->p[A->NbRows-1][A->NbColumns-1]);
1975 value_assign(Tmp->p[A->NbRows][0], A->p[A->NbRows-1][A->NbColumns-1]);
1978 for(
int i=1; i<A->NbRows; i++) {
1980 value_assign(Tmp->p[i+A->NbRows][0], A->p[i-1][A->NbColumns-1]);
1983 for(
int i = 1 ; i < A->NbRows; i++) {
1984 for(
int j = 1; j < A->NbColumns; j++){
1991 value_assign(Tmp->p[0][A->NbColumns], B->p[B->NbRows-1][B->NbColumns-1]);
1994 for (
int i = 1; i < B->NbRows; i++) {
1995 value_assign(Tmp->p[i][A->NbColumns], B->p[i-1][B->NbColumns-1]);
1998 for (
int i = 1; i < B->NbRows; i++){
1999 for (
int j = 1; j < B->NbColumns; j++) {
2000 value_assign(Tmp->p[i][j+A->NbColumns], B->p[i-1][j-1]);
2004 #ifdef LATINTER_DEBUG
2005 fprintf(stderr,
"H init = ");
2026 #ifdef LATINTER_DEBUG
2027 fprintf(stderr,
"\nH = ");
2037 for(
int col_num = H->NbColumns-1 ; col_num >= 0; col_num--) {
2039 for(i = 0; i < A->NbRows; i++) {
2043 if(i != A->NbRows) {
2051 #ifdef LATINTER_DEBUG
2052 fprintf(stderr,
"\nEmpty intersection\n");
2059 for (
int i = 0; i < Res->NbRows; i++) {
2060 for (
int j = 0; j < Res->NbColumns; j++) {
2061 value_assign(Res->p[i][j], H->p[i + H->NbRows - Res->NbRows][j + H->NbColumns - Res->NbColumns]);
2068 #ifdef LATINTER_DEBUG
2069 fprintf(stderr,
"\nLatticeIntersection result = ");
2071 fprintf(stderr,
"---Exiting LatInter---\n\n");
2093 for(
int i=0; i<A->
NbRows; i++) {
2096 for(
int j = A->
NbColumns-1; j > 0; j--) {
2104 for(
int i = A->
NbRows-1; i > 0; i--) {
2129 for (
int i = 0; i < A->
NbRows; i++) {
2131 for (
int j = 0; j < A->
NbColumns-1; j++) {
2136 for (
int j = 0; j < A->
NbColumns; j++) {
2138 for (
int i = 0; i < A->
NbRows-1; i++) {
2149 for(
int i=0; i < A->
NbRows ; i++) {
2169 newResult->
next = Result;
2171 newResult->
M->
p[line_nb][newResult->
M->
NbColumns-1], cst);
2174 newResult->
M->
p[line_nb][newResult->
M->
NbColumns-1], diag_Inter->
p[line_nb]);
Bool isNormalLattice(Matrix *A)
void PutRowLast(Matrix *X, int Rownumber)
void PutColumnLast(Matrix *X, int Columnnumber)
void PutRowFirst(Matrix *X, int Rownumber)
void PutColumnFirst(Matrix *X, int Columnnumber)
Matrix * Matrix_Copy(Matrix const *Src)
Lattice * EmptyLattice(int dimension)
LatticeUnion * LatticeUnion_Alloc(void)
Lattice * ExtractLinearPart(Lattice *A)
static void LinearPartSort(LatticeUnion *Head)
static factor prime_factors(int n)
Bool IsLattice(Matrix *m)
Lattice * LatticeIntersection(Lattice *A, Lattice *B)
void Matrix_Move_Homogeneous_Dim_First(Matrix *A)
Vector * get_pivots(Matrix *A)
static Bool Simplify(LatticeUnion **InputList, LatticeUnion **ResultList, int dim)
void AffineHermite(Lattice *A, Lattice **H, Matrix **U)
LatticeUnion * LatticeDifference(Lattice *A, Lattice *B)
Bool isLinear(Lattice *A)
static factor allfactors(int num)
Bool sameAffinepart(Lattice *A, Lattice *B)
static int AffinePartCompare(const void *A, const void *B)
Lattice * Homogenise(Lattice *A, Bool Forward)
Bool isEmptyLattice(Lattice *A)
static Bool SameLinearPart(LatticeUnion *A, LatticeUnion *B)
Lattice * ChangeLatticeDimension(Lattice *A, int dimension)
LatticeUnion * Lattice2LatticeUnion(Lattice *X, Lattice *Y)
static Bool AffinePartSimplify(LatticeUnion *curlist, LatticeUnion **newlist)
static Bool AlmostSameAffinePart(LatticeUnion *A, LatticeUnion *B)
static LatticeUnion * generate_lattice_union_line(int line_nb, Vector *diag_A, Vector *diag_Inter, Lattice *Intersection, LatticeUnion *Result)
static void AffinePartSort(LatticeUnion *List)
int intcompare(const void *a, const void *b)
static int LinearPartCompare(const void *A, const void *B)
Bool LatticeIncludes(Lattice *A, Lattice *B)
static void AddLattice(LatticeUnion *, Matrix *, Matrix *, Value, int)
LatticeUnion * SplitLattice(Matrix *, Matrix *, Matrix *)
LatticeUnion * LatticeSimplify(LatticeUnion *latlist)
void LatticeUnion_Free(LatticeUnion *Head)
void PrintLatticeUnion(FILE *fp, char *format, LatticeUnion *Head)
Bool sameLattice(Lattice *A, Lattice *B)
void Matrix_Move_Homogeneous_Dim_Last(Matrix *A)
#define VALUE_TO_INT(val)
#define value_notzero_p(val)
#define value_notone_p(val)
#define value_zero_p(val)
#define value_assign(v1, v2)
#define value_increment(ref, val)
#define value_set_si(val, i)
#define value_addmul(ref, val1, val2)
#define value_division(ref, val1, val2)
#define value_multiply(ref, val1, val2)
#define value_modulus(ref, val1, val2)
#define value_addto(ref, val1, val2)
void errormsg1(const char *f, const char *msgname, const char *msg)
void Matrix_Product(Matrix *Mat1, Matrix *Mat2, Matrix *Mat3)
Matrix * Matrix_Alloc(unsigned NbRows, unsigned NbColumns)
int Matrix_Inverse(Matrix *Mat, Matrix *MatInv)
void Matrix_Print(FILE *Dst, const char *Format, Matrix *Mat)
void left_hermite(Matrix *A, Matrix **Hp, Matrix **Qp, Matrix **Up)
void Matrix_Free(Matrix *Mat)
struct lattice_union * next
void Vector_Free(Vector *vector)
Vector * Vector_Alloc(unsigned length)