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
|
-- The luafractions module
-- Authors: Chetan Shirore and Ajit Kumar
-- version 1.3, Date=23-Aug-2023
-- Licensed under LaTeX Project Public License v1.3c or later. The complete license text is available at http://www.latex-project.org/lppl.txt.
M = {} -- the module
frac_mt = {} -- the metatable
function M.new (n, d, mode)
mode = mode or 'fracs'
if mode == 'nofracs' then
return (n/d)
end
if mode == 'fracs' then
if n~=math.floor(n) or d~=math.floor(d) then
error('Only integers are expected.')
end
if d == 0 then
error('Invalid fraction')
end
local fr = {}
local g = M.lgcd(n,d)
fr = {n=n/g, d=d/g}
return setmetatable(fr,frac_mt)
end
end
lfrac = M.new
function M.lgcd (a, b)
local r
while (b ~= 0) do
r = a % b
a = b
b = r
end
return a
end
function M.simp (num)
local cf = gcd(num[1], num[2])
return M.new(num[1] / cf, num2[2] / cf)
end
function M.toFnumber(c)
if getmetatable( c ) == frac_mt then
return c.n / c.d
end
return c
end
function M.toFrac(x)
if type(x) == "number" then
if x==math.floor(x) then
return M.new(math.floor(x),1)
else
return x
end
end
return x
end
function addFracs (c1, c2)
return M.new(c1.n * c2.d + c1.d * c2.n, c1.d*c2.d)
end
function subFracs (c1, c2)
return M.new(c1.n * c2.d - c1.d * c2.n, c1.d*c2.d)
end
function mulFracs (c1, c2)
return M.new(c1.n * c2.n, c1.d*c2.d)
end
function divFracs (c1, c2)
return M.new(c1.n * c2.d, c1.d*c2.n)
end
function minusFracs (c1)
return M.new(-c1.n,c1.d)
end
function powerFracs (c1,m)
return M.new((c1.n)^m,(c1.d)^m)
end
function M.add(a, b)
if type(a) == "number" then
if a==math.floor(a) then
return addFracs(M.new(a,1),b)
else
return a + M.toFnumber(b)
end
end
if type(b) == "number" then
if b==math.floor(b) then
return addFracs(a,M.new(b,1))
else
return M.toFnumber(a) + b
end
end
if type( a ) == "table" and type(b) =="table" then
if getmetatable( a ) == frac_mt and getmetatable( b ) == complex_meta then
return setmetatable( { a+b[1], b[2] }, complex_meta )
end
end
if type( a ) == "table" and type(b) =="table" then
if getmetatable( b ) == frac_mt and getmetatable( a ) == complex_meta then
return setmetatable( { b+a[1], a[2] }, complex_meta )
end
end
return addFracs(a, b)
end
function M.sub(a, b)
if type(a) == "number" then
if a==math.floor(a) then
return subFracs(M.new(a,1),b)
else
return a - M.toFnumber(b)
end
end
if type(b) == "number" then
if b==math.floor(b) then
return subFracs(a,M.new(b,1))
else
return M.toFnumber(a) - b
end
end
if type( a ) == "table" and type(b) =="table" then
if getmetatable( a ) == frac_mt and getmetatable( b ) == complex_meta then
return setmetatable( { a-b[1], -b[2] }, complex_meta )
end
end
if type( a ) == "table" and type(b) =="table" then
if getmetatable( b ) == frac_mt and getmetatable( a ) == complex_meta then
return setmetatable( { a[1]-b, a[2] }, complex_meta )
end
end
return subFracs(a, b)
end
function M.mul(a, b)
if type(a) == "number" then
if a==math.floor(a) then
return mulFracs(M.new(a,1),b)
else
return a * M.toFnumber(b)
end
end
if type(b) == "number" then
if b==math.floor(b) then
return mulFracs(a,M.new(b,1))
else
return M.toFnumber(a) * b
end
end
if type( a ) == "table" and type(b) =="table" then
if getmetatable( a ) == frac_mt and getmetatable( b ) == complex_meta then
return setmetatable( { a*b[1], a*b[2] }, complex_meta )
end
end
if type( a ) == "table" and type(b) =="table" then
if getmetatable( b ) == frac_mt and getmetatable( a ) == complex_meta then
return setmetatable( { b*a[1], b*a[2] }, complex_meta )
end
end
return mulFracs(a, b)
end
function M.div(a, b)
if type(a) == "number" then
if a==math.floor(a) then
return divFracs(M.new(a,1),b)
else
return a / M.toFnumber(b)
end
end
if type(b) == "number" then
if b==math.floor(b) then
return divFracs(a,M.new(b,1))
else
return M.toFnumber(a) / b
end
end
if type( a ) == "table" and type(b) =="table" then
if getmetatable( a ) == frac_mt and getmetatable( b ) == complex_meta then
b= setmetatable( { M.toFrac(b[1]), M.toFrac(b[2]) }, complex_meta )
return a*(1/b)
end
end
return divFracs(a, b)
end
function M.tostring (c)
if c.n == 0 then
return string.format("%g",0)
end
if c.d == 1 then
return string.format("%g",c.n)
end
if c.d == -1 then
return string.format("%g",-c.n)
end
return string.format("\\frac{%g}{%g}", c.n, c.d)
end
function lnumChqEql(x, y)
if type(x) == "number" and type(y) == "number" then
return (x == y)
end
if getmetatable( x ) == frac_mt and getmetatable( y ) == frac_mt then
return (M.toFnumber(x) == M.toFnumber(y))
end
if type(x) == "number" and getmetatable( y ) == frac_mt then
return (M.toFnumber(y) == x)
end
if getmetatable( x ) == frac_mt and type(y) == "number" then
return (M.toFnumber(x) == y)
end
if getmetatable( x ) == complex_meta and getmetatable( y ) == complex_meta then
return M.toFnumber(x[1]) == M.toFnumber(y[1]) and M.toFnumber(x[2]) == M.toFnumber(y[2])
end
if type(x) == "number" and getmetatable( y ) == complex_meta then
return (M.toFnumber(y[1]) == x and M.toFnumber(y[2]) == 0)
end
if getmetatable(x) == complex_meta and type( y ) == "number" then
return (M.toFnumber(x[1]) == y and M.toFnumber(x[2]) == 0)
end
if getmetatable( x ) == frac_mt and getmetatable( y ) == complex_meta then
return (M.toFnumber(x)==M.toFnumber(y[1]) and M.toFnumber(y[2]) == 0)
end
if getmetatable( x ) == complex_meta and getmetatable( y ) == frac_mt then
return (M.toFnumber(y)==M.toFnumber(x[1]) and M.toFnumber(x[2]) == 0)
end
return false
end
--Setting Metatable operations.
frac_mt.__add = M.add
frac_mt.__sub = M.sub
frac_mt.__mul = M.mul
frac_mt.__div = M.div
frac_mt.__unm = minusFracs
frac_mt.__pow = powerFracs
frac_mt.__tostring = M.tostring
frac_mt.__eq = lnumChqEql
return M
|