forked from ptigwe/lh-vector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rat.c
359 lines (327 loc) · 7.69 KB
/
rat.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
/** \file rat.c
*
* For the computation of rational numbers. The rat.c file
* contains the implementation of the methods defined in rat.h
* which use mp.
*
* 27 Apr 2000
* Author: Bernhard von Stengel stengel@maths.lse.ac.uk
*
* Edited: Tobenna Peter, Igwe ptigwe@gmail.com August 2012.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/* sprintf */
#include "rat.h"
/* Creates a rational number from two integers */
Rat itorat(int num, int den)
{
Rat r;
itomp(num, r.num);
itomp(den, r.den);
ratreduce(r);
return r;
}
/* converts integer i to rational */
/* GSoC12: Tobenna Peter, Igwe (Edited)*/
Rat ratfromi(int i)
{
Rat tmp;
/*tmp.num = i; */
itomp(i, tmp.num);
/*tmp.den = 1; */
itomp(1, tmp.den);
return tmp;
}
/* Create a rational number from two mp numbers */
/* GSoC12: Tobenna Peter, Igwe */
Rat mptorat(mp num, mp den)
{
Rat rat;
copy(rat.num, num);
copy(rat.den, den);
return rat;
}
/* Create a rational number from one mp number */
/* GSoC12: Tobenna Peter, Igwe */
Rat ratfrommp(mp num)
{
mp den;
itomp(1, den);
return mptorat(num, den);
}
/* Parses a string that of the format "x" and "x/y"
* and returns the equivalent rational numbers */
/* GSoC12: Tobenna Peter, Igwe */
Rat parseRat(char* srat, const char* info, int j)
{
char snum[MAXSTR], sden[MAXSTR];
mp num, den;
atoaa(srat, snum, sden);
atomp(snum, num);
if (sden[0]=='\0')
itomp(1, den);
else
{
atomp(sden, den);
if (negative(den) || zero(den))
{
char str[MAXSTR];
mptoa(den, str);
fprintf(stderr, "Warning: Denominator ");
fprintf(stderr, "%s of %s[%d] set to 1 since not positive\n",
str, info, j+1);
itomp(1, den);
}
}
Rat r = mptorat(num, den);
return r;
}
/* Parses a string that of the format "x.y"
* and returns the equivalent rational numbers */
/* GSoC12: Tobenna Peter, Igwe */
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));*/
mp num;
atomp(str, num);
/*int den = pow(10, count);*/
int i;
strcpy(str, "10");
for(i = 1; i < count; ++i)
{
strcat(str, "0");
}
mp den;
atomp(str, den);
Rat rat = mptorat(num, den);
return rat;
}
/* Parses a string that of the format "x", "x/y" and "x.y"
* and returns the equivalent rational numbers */
/* GSoC12: Tobenna Peter, Igwe */
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;
}
/* returns sum a+b, normalized */
/* GSoC12: Tobenna Peter, Igwe (Edited) */
Rat ratadd (Rat a, Rat b)
{
/*
a.num = a.num * b.den + a.den * b.num;
a.den *= b.den;
return ratreduce(a);
*/
mp num, den, x, y, t;
/*itomp (a.num, num) ;*/
copy(num, a.num);
/*itomp (a.den, den) ;*/
copy(den, a.den);
/*itomp (b.num, x) ;*/
copy(x, b.num);
/*itomp (b.den, y) ;*/
copy(y, b.den);
mulint (y, num, t);
copy(num, t);
mulint (den, x, x);
linint (num, 1, x, 1);
mulint (y, den, den);
reduce(num, den) ;
/*mptoi( num, &a.num, 1 );
mptoi( den, &a.den, 1 );*/
copy(a.num, num);
copy(a.den, den);
return a ;
}
/* returns quotient a/b, normalized */
Rat ratdiv (Rat a, Rat b)
{
return ratmult(a, ratinv(b) );
}
/* returns product a*b, normalized */
/* GSoC12: Tobenna Peter, Igwe (Edited) */
Rat ratmult (Rat a, Rat b)
{
mp x;
/* avoid overflow in intermediate product by cross-cancelling first
*/
/*x = a.num ; */
copy(x, a.num);
/*a.num = b.num ;*/
copy(a.num, b.num);
/*b.num = x ;*/
copy(b.num, x);
a = ratreduce(a);
b = ratreduce(b);
/*a.num *= b.num;*/
mulint(a.num, b.num, x);
copy(a.num, x);
/*a.den *= b.den;*/
mulint(a.den, b.den, x);
copy(a.den, x);
return ratreduce(a); /* a or b might be non-normalized s*/
}
/* returns -a, normalized only if a normalized */
/* GSoC12: Tobenna Peter, Igwe (Edited)*/
Rat ratneg (Rat a)
{
/*a.num = - a.num;*/
changesign(a.num);
return a;
}
/* normalizes (make den>0, =1 if num==0)
* and reduces by gcd(num,den)
*/
/* GSoC12: Tobenna Peter, Igwe (Edited) */
Rat ratreduce (Rat a)
{
if (zero(a.num))
{
/*a.den = 1;*/
itomp(1, a.den);
}
else
{
mp div;
mp c;
if (negative(a.den))
{
/*a.den = -a.den;*/
changesign(a.den);
/*a.num = -a.num;*/
changesign(a.num);
}
/*div = ratgcd(a.den, a.num);*/
ratgcd(a.den, a.num, div);
/*a.num = a.num/div;*/
divint(a.num, div, c);
copy(a.num, c);
/*a.den = a.den/div;*/
divint(a.den, div, c);
copy(a.den, c);
}
return a;
}
/* computes gcd of integers a and b, 0 if both 0 and stores the value in c*/
void ratgcd(mp a, mp b, mp c)
{
mp d;
copy(c, a);
copy(d, b);
gcd(c, d);
}
/* GSoC12: Tobenna Peter, Igwe (Edited)*/
Rat ratinv (Rat a)
{
mp x;
/*x = a.num ;*/
copy(x, a.num);
/*a.num = a.den ;*/
copy(a.num, a.den);
/*a.den = x ;*/
copy(a.den, x);
return a;
}
/* returns Boolean condition that a > b */
Bool ratgreat (Rat a, Rat b)
{
Rat c = ratadd(a, ratneg(b));
return (positive(c.num));
}
/* returns Boolean condition that a==b
* a, b are assumed to be normalized
*/
/* GSoC12: Tobenna Peter, Igwe (Edited) */
Bool ratiseq (Rat a, Rat b)
{
/*return (a.num == b.num && a.den == b.den);*/
mp c;
itomp(1, c);
int x = comprod(a.num, c, b.num, c);
int y = comprod(a.den, c, b.den, c);
return ((x == 0) && (y == 0));
}
/* Returns the maximum element in an array of n Rat elements */
/* GSoC12: Tobenna Peter, Igwe */
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 */
/* GSoC12: Tobenna Peter, Igwe */
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;
/* GSoC12: Tobenna Peter, Igwe */
mptoa(r.num, str);
l = sprintf(s, "%s", str);
if(!one(r.den))
{
/*a = sprintf(s+l, "/%d", r.den);*/
mptoa(r.den, str);
a = sprintf(s+l, "/%s", str);
l += a + 1;
}
return l;
}
/* converts rational a to double */
/* GSoC12: Tobenna Peter, Igwe (Edited) */
double rattodouble(Rat a)
{
/*return (double)a.num/(double)a.den*/
int num, den;
mptoi(a.num, &num, 1);
mptoi(a.den, &den, 1);
return (double)num / (double)den;
}