diff options
author | bremner <bremner@09fa754a-f411-0410-976a-da6bfa213b30> | 2006-06-01 15:21:54 +0000 |
---|---|---|
committer | bremner <bremner@09fa754a-f411-0410-976a-da6bfa213b30> | 2006-06-01 15:21:54 +0000 |
commit | 0adbe5a920fe6106109f14d6bdb28dee49cb8774 (patch) | |
tree | f672c1a2023f0da4c7c4f0508fa6d53b4ff44e0f | |
parent | a468544d39ef18c6e47fd59cf427a4e25fe22b7f (diff) |
first version of rat2canon
git-svn-id: file:///export/data/bremner/svn/trunk/inetools@5354 09fa754a-f411-0410-976a-da6bfa213b30
-rw-r--r-- | Makefile | 9 | ||||
-rw-r--r-- | rat2canon.c | 178 | ||||
-rw-r--r-- | rat2canon.doc | 10 |
3 files changed, 196 insertions, 1 deletions
@@ -2,7 +2,7 @@ .SUFFIXES: .doc .ds -BIN= rat2float float2rat +BIN= rat2float float2rat rat2canon MPOBJ= CC=gcc @@ -39,6 +39,13 @@ rat2float: rat2float.c rat2float.ds $(MPOBJ) $(CC) $(CPPFLAGS) $(CFLAGS) -o $@ $(MPOBJ) $(LDFLAGS) $< +rat2canon.ds: rat2canon.doc + +rat2canon: rat2canon.c rat2canon.ds $(MPOBJ) + $(CC) $(CPPFLAGS) $(CFLAGS) -o $@ $(MPOBJ) $(LDFLAGS) $< + + + float2rat.ds: float2rat.doc float2rat: float2rat.c float2rat.ds diff --git a/rat2canon.c b/rat2canon.c new file mode 100644 index 0000000..88b72b3 --- /dev/null +++ b/rat2canon.c @@ -0,0 +1,178 @@ +/* + * Reads a polyhedron file on stdin with rationals and outputs + * a canonical form with normalized rhs and sorted constraints. + * + * David Bremner. bremner@cs.mcgill.ca + * + */ + +static char rcsid[]="$Id: rat2float.c 2343 2006-04-04 12:34:35Z bremner $"; + +#ifndef LRSMP +#error This file only compiles with LRSMP +#endif + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <float.h> +#include <errno.h> +#include "lrsmp.h" + + + +typedef lrs_mp integer_t; +#define MP_DIGITS 1000L + + +#include "rat2float.ds" + +static int *rows=NULL; +static long int m,n; +static lrs_mp **An; +static lrs_mp **Ad; + +int cmprow(const void *i_p,const void *j_p){ + int i=*(int *)i_p; + int j=*(int *)j_p; + int cval; + int k=0; + /* the trick here is that the denominators are always positive */ + while( k< n && + ( cval=comprod(An[i][k],Ad[j][k],An[j][k],Ad[i][k])) == 0){ + k++; + } + return cval; +} + +int main(argc,argv) + int argc; + char **argv; +{ + int i,j; + + + char format[BUFSIZ]; + char buf[BUFSIZ]; + + + char *cursor; + + + CHECK_HELP; + + lrs_mp_init (MP_DIGITS,stdin,stdout); + while ( fgets(buf,BUFSIZ,stdin) !=NULL ) + { + fputs(buf,stdout); + if (strncmp(buf,"begin",5)==0) break; + } + + if (fgets(buf,BUFSIZ,stdin)==NULL) { + fprintf(stderr,"No parameter line"); + exit(1); + } + + m=strtol(buf,&cursor,10); + if (errno==ERANGE || m==0){ + fprintf(stderr,"Missing or absurd row count"); + exit(1); + } + + An=calloc(m,sizeof(lrs_mp*)); + Ad=calloc(m,sizeof(lrs_mp*)); + rows=calloc(m,sizeof(int)); + + n=strtol(cursor,&cursor,10); + if (errno==ERANGE || m==0){ + fprintf(stderr,"Missing or absurd column count"); + exit(1); + } + + for (i=0; i<m; i++){ + rows[i]=i; + An[i]=calloc(n,sizeof(lrs_mp)); + Ad[i]=calloc(n,sizeof(lrs_mp)); + } + + while (isblank(*cursor)){ + cursor++; + } + + if (strncmp(cursor,"integer",7) != 0 && strncmp(cursor,"rational",8)){ + fprintf(stderr,"oops. I don't know what to do with a file of type %s\n", + cursor); + exit(1); + } + + printf("%ld %ld rational\n",m,n); + + + for (i=0;i<m;i++) + { + + /* read in numbers */ + + int looking=1; + + lrs_mp num0,den0; + + for(j=0;j<n;j++) + { + char *p; + + integer_t num,den; + double out; + + readrat(num,den); + + if (looking==1 && !zero(num)){ + copy(num0,num); + copy(den0,den); + itomp(1,An[i][j]); + itomp(1,Ad[i][j]); + + looking=0; + } else { + + if (zero(num)) { + itomp(0,An[i][j]); + itomp(1,Ad[i][j]); + } else { + lrs_mp outnum,outden; + + if (!zero(num0)){ + divrat(num,den,num0,den0,An[i][j],Ad[i][j]); + } else { + copy(An[i][j],num); + copy(Ad[i][j],den); + } + } + + } + } + } + + + fgets(buf,BUFSIZ,stdin); /* clean off last line */ + + qsort(rows,m,sizeof(int), cmprow); + + for (i=0; i< m; i++){ + for(j=0; j<n; j++){ + prat("",An[rows[i]][j],Ad[rows[i]][j]); + } + fputs("\n",stdout); + } + + while (fgets(buf,BUFSIZ,stdin) !=NULL ) + { + fputs(buf,stdout); + } +} + + + + + + diff --git a/rat2canon.doc b/rat2canon.doc new file mode 100644 index 0000000..905071c --- /dev/null +++ b/rat2canon.doc @@ -0,0 +1,10 @@ + +$Id: rat2float.doc 2343 2006-04-04 12:34:35Z bremner $ + +rat2canon takes a polytope file with rational or integer coefficents, +and outputs an equivelent one with normalized right hand side. + + + + + |