aboutsummaryrefslogtreecommitdiff
path: root/rat2canon.c
blob: 4b2d4e21d2442e5b089b4875ed2603c549a857ad (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
/*
 * Reads a polyhedron file on stdin with rationals and outputs 
 * a canonical form with normalized rhs and sorted constraints.
 * 
 * David Bremner. bremner@unb.ca
 *
 */

#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"
#include "ine_io.h"
#include "process_args.h"

typedef  lrs_mp integer_t;
#define MP_DIGITS 1000L


#include "rat2canon.h"


static int *rows=NULL;
static 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 *ok[]={ "integer", "rational" };
  char *type;
  char buf[BUFSIZ];

  process_args(argc,argv,DOCSTRING);
  lrs_mp_init (MP_DIGITS,stdin,stdout);   
  scan_for_begin(&m,&n,&type);
  check_type(type,  ok, 2);
  
  An=calloc(m,sizeof(lrs_mp*));
  Ad=calloc(m,sizeof(lrs_mp*));
  rows=calloc(m,sizeof(int));


  for (i=0; i<m; i++){
    rows[i]=i;
    An[i]=calloc(n,sizeof(lrs_mp));
    Ad[i]=calloc(n,sizeof(lrs_mp));
  }

  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);
    }
}