forked from ptigwe/lh-vector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrat.h
295 lines (260 loc) · 6.82 KB
/
rat.h
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
/** \file rat.h
* For the computation of rational numbers. The .h file
* includes definitions for use with both mp and gmp. These
* definitions are seperated with the #ifdef GLEMKE directive
* to avoid a clash in names.
*
* 22 Apr 2000
* Author: Bernhard von Stengel [email protected]
*
* Edited: Tobenna Peter, Igwe [email protected] August 2012.
*/
#ifndef RAT_H
#define RAT_H
#ifdef GLEMKE
#include "gmp.h"
#include "gmpwrap.h"
#endif
#include "mp.h"
#define MAXSTR 100
#ifndef TRUE
#define TRUE 1L
#endif
#ifndef FALSE
#define FALSE 0L
#endif
typedef int Bool; /* Boolean value 0/1 */
/**
* \brief A representation of rational numbers.
*
* A structure which is used to represent rational
* numbers. The numerator and denominator are
* stored as a multiple point precision integer
* using either mp (from mp.h) or gmpt (from gmp.h)
*
*/
typedef struct /* GSoC12: Tobenna Peter, Igwe (Edited) */
{
#ifdef GLEMKE
gmpt num; /*!< \brief Numerator. Used if GLEMKE is defined. */
gmpt den; /*!< \brief Denominator. Used if GLEMKE is defined. */
#else
mp num; /*!< \brief Numerator. Used if GLEMKE is not defined. */
mp den; /*!< \brief Denominator. Used if GLEMKE is not defined. */
#endif
}
Rat;
#ifdef GLEMKE
/**
* \brief Initialise a rational number and returns it.
*
* Creates a Rat and initialises the numerator
* and denominator. To be used when creating a rational
* number with gmp.
* This function is only available if GLEMKE is defined.
*
* \return The initialised Rat structure.
* \sa grat.c
*/
/* GSoC12: Tobenna Peter, Igwe */
Rat ratinit();
/**
* \brief Clears the rational number.
*
* Clears the contents of the rational number which
* has gmp type numerator and denominator.
*
* \param rat The rational number to be cleared.
* \sa grat.c
*/
/* GSoC12: Tobenna Peter, Igwe */
void ratclear(Rat rat);
#endif
/**
* Creates a rational number from two integers.
*
* \param num The numerator.
* \param den The denominator.
* \return The rational representation of the two integers
*/
Rat itorat(int num, int den);
/**
* Converts integer to a rational number.
* This is equivalent to calling itorat(i, 1)
*
* \param i Integer to be converted
* \return The equivalent rational number
* \sa itorat()
*/
Rat ratfromi(int i);
#ifdef GLEMKE
/**
* Create a rational number from two gmp numbers.
* The values of numerator and denominator are copied
* over to a rational structure.
*
* \param num The numerator
* \param den The denominator
* \return The equivalent rational number
*/
/* GSoC12: Tobenna Peter, Igwe */
Rat gmptorat(gmpt num, gmpt den);
/**
* Create a rational number from one gmp number.
*
* \param a the gmp number to be converted.
* \return The equivalent rational number.
*/
/* GSoC12: Tobenna Peter, Igwe */
Rat ratfromgmp(gmpt a);
#else
/**
* Create a rational number from two mp numbers.
* The values of numerator and denominator are copied
* over to a rational structure.
*
* \param num The numerator
* \param den The denominator
* \return The equivalent rational number
*/
/* GSoC12: Tobenna Peter, Igwe */
Rat mptorat(mp num, mp den);
/**
* Create a rational number from one mp number.
*
* \param a the gmp number to be converted.
* \return The equivalent rational number.
*/
/* GSoC12: Tobenna Peter, Igwe */
Rat ratfrommp(mp a);
#endif
/**
* Converts an array of characters to a Rat.
* Parses a string that of the format "x", "x/y" and "x.y"
* and returns the equivalent rational numbers.
*
* \param str The array of charcters.
* \param info Some information about str, to allow the
* function print out some information if there was an error.
* \param j The index of the array it is being read into
* so the correct information can be displayed if an error occurs.
*/
/* GSoC12: Tobenna Peter, Igwe */
Rat ratfroma(char* str, const char* info, int j);
/**
* Returns the normalised sum of a and b.
*
* \returns The normalised sum of a and b.
*/
Rat ratadd (Rat a, Rat b);
/**
* Returns the normalised value of a/b.
*
* \returns The normalise value of a/b
*/
Rat ratdiv (Rat a, Rat b);
/**
* Returns the normalised value of a*b.
*
* \returns The normalised value of a*b
*/
Rat ratmult (Rat a, Rat b);
/**
* Returns -a. If a is normalised, then -a is normalised otherwise
* the returned value is not normalised.
*
* \returns The value of -a
*/
Rat ratneg (Rat a);
/**
* Returns the normalised rational number.
* Normalizes (make den>0, =1 if num==0)
* and reduces by gcd(num,den).
*
* \param a Rational number to be normalised.
* \returns The normalised rational number.
*/
Rat ratreduce (Rat a);
/* GSoC12: Tobenna Peter, Igwe */
#ifdef GLEMKE
/**
* \brief Computes gcd of two integers.
* Computes the gcd of a and b, 0 if both 0 and stores the value in c.
*
* Note: Used if GLEMKE is defined.
*
* \param a First integer.
* \param b Second integer.
* \param c Destination to store the value of gcd(a,b)
*/
void ratgcd(gmpt a, gmpt b, gmpt c);
#else
/**
* \brief Computes gcd of two integers.
* Computes the gcd of a and b, 0 if both 0 and stores the value in c.
*
* Note: Used if GLEMKE is defined.
*
* \param a First integer.
* \param b Second integer.
* \param c Destination to store the value of gcd(a,b)
*/
void ratgcd(mp a, mp b, mp c);
#endif
/**
* Returns the value of 1/a. This is normalised if a is normalised.
*
* \param a The rational number
* \returns The inverse of a
*/
Rat ratinv (Rat a);
/**
* Returns Boolean condition that a > b.
*
* \returns TRUE if a > b otherwise FALSE
*/
Bool ratgreat (Rat a, Rat b);
/**
* Returns Boolean condition that a==b.
* The values a and b are assumed to be normalized.
*
* \returns TRUE if a > b otherwise FALSE
*/
Bool ratiseq (Rat a, Rat b);
/**
* Returns the maximum element in an array of n rational elements
*
* \param rat The array of rational numbers
* \param n The length of the array rat.
* \returns The maximum value in rat
*/
/* GSoC12: Tobenna Peter, Igwe */
Rat maxrow(Rat* rat, int n);
/**
* Returns the maximum element in an mxn matrix of Rat elements.
*
* \param rat The matrix of rational numbers
* \param m The number of rows
* \param n The number of columns
* \returns The maximum value in rat
*/
/* GSoC12: Tobenna Peter, Igwe */
Rat maxMatrix(Rat** rat, int m, int n);
/**
* Converts a rational to a string.
* Omit den == 1, s must be sufficiently long to contain result.
*
* \param r The rational number to be converted
* \param s The array of characters to store the result
* \returns The length of the string
*/
int rattoa (Rat r, char *s);
/**
* Converts a rational to a double. If an overflow occurs, it is
* reported to the standard output.
*
* \param a The rational number to be converted
* \returns The double value of a
*/
double rattodouble (Rat a);
#endif