forked from ptigwe/lh-vector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grat.c
417 lines (368 loc) · 8.27 KB
/
grat.c
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/** \file grat.c
*
* For the computation of rational numbers. The grat.c file
* contains the implementation of the methods defined in rat.h
* which use gmp.
*
* Author: Tobenna Peter, Igwe [email protected] August 2012.
*/
#define GLEMKE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/* sprintf */
#include "rat.h"
/* Initialise a rational number and returns it */
Rat ratinit()
{
Rat rat;
ginit(rat.num);
ginit(rat.den);
return rat;
}
/* Creates a clone of the given rational number */
Rat ratclone(Rat rat)
{
Rat r = ratinit();
gset(r.num, rat.num);
gset(r.den, rat.den);
return r;
}
/* Frees the memory occupied by a rational number */
void ratclear(Rat rat)
{
gclear(rat.num);
gclear(rat.den);
}
/* Creates a rational number from two integers */
Rat itorat(int num, int den)
{
Rat r = ratinit();
gitomp(num, r.num);
gitomp(den, r.den);
ratreduce(r);
return r;
}
/* Returns the equivalent rational number of an integer */
Rat ratfromi(int i)
{
Rat tmp = ratinit();
/*tmp.num = i; */
gitomp(i, tmp.num);
/*tmp.den = 1; */
gitomp(1, tmp.den);
return tmp;
}
Rat gmptorat(gmpt num, gmpt den)
{
Rat r = ratinit();
gset(r.num, num);
gset(r.den, den);
return r;
}
Rat ratfromgmp(gmpt a)
{
Rat rat = ratinit();
gset(rat.num, a);
gitomp(1, rat.den);
return rat;
}
/* Parses a string of characters of the format "num" or "num/den"
* and converts it to a rational number */
Rat parseRat(char* srat, const char* info, int j)
{
char snum[MAXSTR], sden[MAXSTR];
gmpt num, den;
ginit(num);
ginit(den);
atoaa(srat, snum, sden);
mpz_set_str(num, snum, 10);
if (sden[0]=='\0')
gitomp(1, den);
else
{
mpz_set_str(den, sden, 10);
if (gnegative(den) || gzero(num))
{
char str[MAXSTR];
gmptoa(den, str);
fprintf(stderr, "Warning: Denominator ");
fprintf(stderr, "%s of %s[%d] set to 1 since not positive\n",
str, info, j+1);
gitomp(1, den);
}
}
Rat r = gmptorat(num, den);
return r;
}
/* Parses a string of characters of the format "i.d" to the a rational number */
Rat parseDecimal(char* srat, const char* info, int j)
{
double x;
int count;
char* sub;
sscanf(srat, "%lf", &x);
sub = strchr(srat, '.');
if(strchr(sub+1, '.') != NULL)
{
fprintf(stderr, "Error: Decimal ");
fprintf(stderr, "%s of %s[%d] has more than one decimal point\n", srat, info, j);
exit(1);
}
count = strlen(sub+1);
char* str = strtok(srat, ".");
strcat(str, strtok(NULL, "."));
/*int num = floor(x * pow(10, count));*/
gmpt num;
mpz_set_str(num, str, 10);
/*int den = pow(10, count);*/
int i;
strcpy(str, "10");
for(i = 1; i < count; ++i)
{
strcat(str, "0");
}
gmpt den;
mpz_set_str(den, str, 10);
Rat rat = gmptorat(num, den);
return rat;
}
/* Parses a string that of the format "x", "x/y" and "x.y"
* and returns the equivalent rational numbers */
Rat ratfroma(char* srat, const char* info, int j)
{
char* pos;
Rat rat;
if((pos = strchr(srat, '.')) != NULL)
{
rat = parseDecimal(srat, info, j);
}
else
{
rat = parseRat(srat, info, j);
}
return rat;
}
/* for improved precision in ratadd(a, b) */
Rat ratadd (Rat a, Rat b)
{
/*
a.num = a.num * b.den + a.den * b.num;
a.den *= b.den;
return ratreduce(a);
*/
gmpt num, den, x, y, t;
Rat c = ratinit();
ginit(num);
ginit(den);
ginit(x);
ginit(y);
ginit(t);
/*itomp (a.num, num) ;*/
gset(num, a.num);
/*itomp (a.den, den) ;*/
gset(den, a.den);
/*itomp (b.num, x) ;*/
gset(x, b.num);
/*itomp (b.den, y) ;*/
gset(y, b.den);
gmulint (y, num, t);
gset(num, t);
gmulint (den, x, x);
/*linint (num, 1, x, 1);*/
gadd(t, num, x);
gset(num, t);
gmulint (y, den, den);
greduce(num, den) ;
/*mptoi( num, &a.num, 1 );
mptoi( den, &a.den, 1 );*/
gset(c.num, num);
gset(c.den, den);
gclear(num);
gclear(den);
gclear(x);
gclear(y);
gclear(t);
return c ;
}
/* Returns the value of "a/b" */
Rat ratdiv (Rat a, Rat b)
{
return ratmult(a, ratinv(b) );
}
/* returns product a*b, normalized */
Rat ratmult (Rat a, Rat b)
{
Rat a1 = ratclone(a);
Rat b1 = ratclone(b);
gmpt x;
ginit(x);
/* avoid overflow in intermediate product by cross-cancelling first
*/
/*x = a.num ; */
gset(x, a.num);
/*a.num = b.num ;*/
gset(a1.num, b.num);
/*b.num = x ;*/
gset(b1.num, x);
a1 = ratreduce(a1);
b1 = ratreduce(b1);
/*a.num *= b.num;*/
gmulint(a1.num, b1.num, x);
gset(a1.num, x);
/*a.den *= b.den;*/
gmulint(a1.den, b1.den, x);
gset(a1.den, x);
gclear(x);
ratclear(b1);
return ratreduce(a1); /* a or b might be non-normalized s*/
}
/* returns -a, normalized only if a normalized */
Rat ratneg (Rat a)
/* returns -a */
{
/*a.num = - a.num;*/
gchangesign(a.num);
return a;
}
/* normalizes (make den>0, =1 if num==0)
* and reduces by gcd(num,den)
*/
Rat ratreduce (Rat a)
{
if (gzero(a.num))
{
/*a.den = 1;*/
gitomp(1, a.den);
}
else
{
gmpt div;
gmpt c;
ginit(div);
ginit(c);
if (gnegative(a.den))
{
/*a.den = -a.den;*/
gchangesign(a.den);
/*a.num = -a.num;*/
gchangesign(a.num);
}
/*div = ratgcd(a.den, a.num);*/
ratgcd(a.den, a.num, div);
/*a.num = a.num/div;*/
gexactdivint(a.num, div, c);
gset(a.num, c);
/*a.den = a.den/div;*/
gexactdivint(a.den, div, c);
gset(a.den, c);
gclear(div);
gclear(c);
}
return a;
}
/* Computes the gcd of a and b and stores it in c */
void ratgcd(gmpt a, gmpt b, gmpt c)
{
gmpt d, e;
ginit(d);
ginit(e);
gset(d, a);
gset(e, b);
mpz_gcd(c, d, e);
gclear(d);
gclear(e);
}
/* Inverts a rational number */
Rat ratinv (Rat a)
{
Rat a1 = ratclone(a);
/*a.num = a.den ;*/
gset(a1.num, a.den);
/*a.den = x ;*/
gset(a1.den, a.num);
return a1;
}
/* returns Boolean condition that a==b
* a, b are assumed to be normalized
*/
Bool ratiseq (Rat a, Rat b)
{
/*return (a.num == b.num && a.den == b.den);*/
Rat a1 = ratclone(a);
Rat b1 = ratclone(b);
Rat c = ratadd(a1, ratneg(b1));
int i = gzero(c.num);
ratclear(a1);
ratclear(b1);
ratclear(c);
return i;
}
/* returns Boolean condition that a > b */
Bool ratgreat (Rat a, Rat b)
{
Rat a1 = ratclone(a);
Rat b1 = ratclone(b);
Rat c = ratadd(a1, ratneg(b1));
int i = gpositive(c.num);
ratclear(a1);
ratclear(b1);
ratclear(c);
return i;
}
/* Returns the maximum element in an array of n Rat elements */
Rat maxrow(Rat* rat, int n)
{
int i;
Rat Mrow = ratfromi(0);
for(i = 0; i < n; ++i)
{
Mrow = ratgreat(Mrow,rat[i]) ? Mrow : rat[i];
}
return Mrow;
}
/* Returns the maximum element in an mxn matrix of Rat elements */
Rat maxMatrix(Rat** rat, int m, int n)
{
int i;
int tmpm = m;
int tmpn = n;
Rat M = ratfromi(0);
for(i = 0; i < tmpm; ++i)
{
Rat r = maxrow(rat[i], tmpn);
M = ratgreat(M, r) ? M : r;
}
return M;
}
/* converts rational r to string s, omit den 1
* s must be sufficiently long to contain result
* returns length of string
*/
int rattoa (Rat r, char *s)
{
char str[MAXSTR];
int l, a;
/*l = sprintf(s, "%d", r.num);*/
gmptoa(r.num, str);
l = sprintf(s, "%s", str);
/*if (r.den != 1)*/
if(!gone(r.den))
{
/*a = sprintf(s+l, "/%d", r.den);*/
gmptoa(r.den, str);
a = sprintf(s+l, "/%s", str);
l += a + 1;
}
return l;
}
/* converts rational a to double */
double rattodouble(Rat a)
{
int num, den;
gmptoi(a.num, &num, 1);
gmptoi(a.den, &den, 1);
/*return (double) a.num / (double) a.den ;*/
return (double)num / (double)den;
}