aboutsummaryrefslogtreecommitdiff
path: root/rat2canon.c
blob: 03555c6f59f097bae0866b3a4b4fe903e722c7aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * 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 "rat2canon.h"

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);
      for (cursor=buf; isblank(*cursor); cursor++);
      if (strncmp(cursor,"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);

	    if (negative(num)) {
	      changesign(num0);
	      itomp(-1,An[i][j]);
	    } else {
	      itomp(1,An[i][j]);
	    }

	    copy(den0,den);
	    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);
    }
}