aboutsummaryrefslogtreecommitdiff
path: root/rat2int.c
blob: 24c7ade81d6e751e7992ee23c6471f7c56c32499 (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
/*
 * Reads a polyhedron file on stdin with rationals and outputs an equivalent 
 * system with integer  coefficents.
 * 
 * David Bremner. bremner@unb.ca
 *
 */
static char rcsid[]="$Id: rat2int.c 2055 2006-04-03 19:41:14Z bremner $";
#include <stdlib.h>
#include <stdio.h>

#ifndef LRSMP
#error This file only compiles with LRSMP
#endif

#include "lrsmp.h"
#include "rat2int.h"

static   long int  m,n,type;
#define MP_DIGITS 1000L

static void lcm3(lrs_mp a,lrs_mp b,lrs_mp c);


main(int argc, char **argv)
{


  int i,j;
  lrs_mp l,g,scratch;
  char *type;
  lrs_mp *D, *N;
  char *ok[]={ "integer", "rational"};
  process_args(argc,argv,DOCSTRING);
  lrs_mp_init (MP_DIGITS,stdin,stdout);   
  scan_for_begin(&m,&n,&type);

  check_type(type, ok , 2);

  printf("%d %d integer\n",m,n);
  D=calloc(n,sizeof(lrs_mp));
  N=calloc(n,sizeof(lrs_mp));
  
  for (i=0;i<m;i++)    {
  
      for (j=0; j<n; j++){
	readrat(N[j],D[j]);
      }

      lcm3(D[0],D[1],l);
      
      for(j=2;j<n;j++){
	lcm3(l,D[j],l);
      }
      
      mul_row(N,D,l,0);

      /* reinitialize gcd */
      copy(g,N[0]);
      copy(scratch,N[1]);
      gcd(g,scratch);

      for (j=2; j<n ; j++){
      	copy(scratch,N[j]);
	gcd(g,scratch);
      }

      if (negative(g)) 
	storesign(g,POS);
      
      div_row(N,D,g,0);

      for (j=0; j< n; j++){
	pmp("",N[j]);
      }
      fputs("\n",stdout);
  }
  
  printf("end\n");

}

/* c<- lcm(a,b) */

static void lcm3(lrs_mp a,lrs_mp b,lrs_mp c)
{
  lrs_mp gc,ab,scratch;
  
  copy(gc,a);
  copy(scratch,b);
  gcd(gc,scratch);
  

  mulint(a,b,ab);
    
  divint(ab,gc,c);
    
}


mul_row(N,D,l,offset)
     lrs_mp *N,*D;
     lrs_mp l;
     int offset;
{
    lrs_mp  factor,scratch;
  int j;

  for  (j=offset;j<n;  j++) {

    /* 
     * Now multiply each numerator n_i by lcm/d_i.
     * Implicitly, each denominator is now lcm
     */

    copy(scratch,l);
    divint(scratch,D[j],factor);
    mulint(N[j],factor,N[j]);
  }

}      

div_row(N,D,g,offset)
     lrs_mp *N,*D;
     lrs_mp g;
     int offset;
{
  lrs_mp  scratch;
  int j;

  for  (j=0;j<n;  j++)
    {
      copy(scratch,N[j]);
	
      divint(scratch,g,N[j]);
    }

}