| 1 |
-- The luafractions module |
| 2 |
-- Authors: Chetan Shirore and Ajit Kumar |
| 3 |
-- version 1.3, Date=23-Aug-2023 |
| 4 |
-- Licensed under LaTeX Project Public License v1.3c or later. The complete license text is available at http://www.latex-project.org/lppl.txt. |
| 5 |
|
| 6 |
M = {} -- the module |
| 7 |
frac_mt = {} -- the metatable |
| 8 |
function M.new (n, d, mode) |
| 9 |
mode = mode or 'fracs' |
| 10 |
if mode == 'nofracs' then |
| 11 |
return (n/d) |
| 12 |
end |
| 13 |
if mode == 'fracs' then |
| 14 |
if n~=math.floor(n) or d~=math.floor(d) then |
| 15 |
error('Only integers are expected.') |
| 16 |
end |
| 17 |
if d == 0 then |
| 18 |
error('Invalid fraction') |
| 19 |
end |
| 20 |
local fr = {} |
| 21 |
local g = M.lgcd(n,d) |
| 22 |
fr = {n=n/g, d=d/g} |
| 23 |
return setmetatable(fr,frac_mt) |
| 24 |
end |
| 25 |
end |
| 26 |
lfrac = M.new |
| 27 |
|
| 28 |
function M.lgcd (a, b) |
| 29 |
local r |
| 30 |
while (b ~= 0) do |
| 31 |
r = a % b |
| 32 |
a = b |
| 33 |
b = r |
| 34 |
end |
| 35 |
return a |
| 36 |
end |
| 37 |
|
| 38 |
function M.simp (num) |
| 39 |
local cf = gcd(num[1], num[2]) |
| 40 |
return M.new(num[1] / cf, num2[2] / cf) |
| 41 |
end |
| 42 |
|
| 43 |
function M.toFnumber(c) |
| 44 |
if getmetatable( c ) == frac_mt then |
| 45 |
return c.n / c.d |
| 46 |
end |
| 47 |
return c |
| 48 |
end |
| 49 |
|
| 50 |
function M.toFrac(x) |
| 51 |
if type(x) == "number" then |
| 52 |
if x==math.floor(x) then |
| 53 |
return M.new(math.floor(x),1) |
| 54 |
else |
| 55 |
return x |
| 56 |
end |
| 57 |
end |
| 58 |
return x |
| 59 |
end |
| 60 |
|
| 61 |
|
| 62 |
function addFracs (c1, c2) |
| 63 |
return M.new(c1.n * c2.d + c1.d * c2.n, c1.d*c2.d) |
| 64 |
end |
| 65 |
|
| 66 |
function subFracs (c1, c2) |
| 67 |
return M.new(c1.n * c2.d - c1.d * c2.n, c1.d*c2.d) |
| 68 |
end |
| 69 |
|
| 70 |
function mulFracs (c1, c2) |
| 71 |
return M.new(c1.n * c2.n, c1.d*c2.d) |
| 72 |
end |
| 73 |
|
| 74 |
function divFracs (c1, c2) |
| 75 |
return M.new(c1.n * c2.d, c1.d*c2.n) |
| 76 |
end |
| 77 |
|
| 78 |
function minusFracs (c1) |
| 79 |
return M.new(-c1.n,c1.d) |
| 80 |
end |
| 81 |
|
| 82 |
function powerFracs (c1,m) |
| 83 |
return M.new((c1.n)^m,(c1.d)^m) |
| 84 |
end |
| 85 |
|
| 86 |
|
| 87 |
function M.add(a, b) |
| 88 |
if type(a) == "number" then |
| 89 |
if a==math.floor(a) then |
| 90 |
return addFracs(M.new(a,1),b) |
| 91 |
else |
| 92 |
return a + M.toFnumber(b) |
| 93 |
end |
| 94 |
end |
| 95 |
|
| 96 |
if type(b) == "number" then |
| 97 |
if b==math.floor(b) then |
| 98 |
return addFracs(a,M.new(b,1)) |
| 99 |
else |
| 100 |
return M.toFnumber(a) + b |
| 101 |
end |
| 102 |
end |
| 103 |
|
| 104 |
if type( a ) == "table" and type(b) =="table" then |
| 105 |
if getmetatable( a ) == frac_mt and getmetatable( b ) == complex_meta then |
| 106 |
return setmetatable( { a+b[1], b[2] }, complex_meta ) |
| 107 |
end |
| 108 |
end |
| 109 |
|
| 110 |
if type( a ) == "table" and type(b) =="table" then |
| 111 |
if getmetatable( b ) == frac_mt and getmetatable( a ) == complex_meta then |
| 112 |
return setmetatable( { b+a[1], a[2] }, complex_meta ) |
| 113 |
end |
| 114 |
end |
| 115 |
return addFracs(a, b) |
| 116 |
end |
| 117 |
|
| 118 |
|
| 119 |
function M.sub(a, b) |
| 120 |
if type(a) == "number" then |
| 121 |
if a==math.floor(a) then |
| 122 |
return subFracs(M.new(a,1),b) |
| 123 |
else |
| 124 |
return a - M.toFnumber(b) |
| 125 |
end |
| 126 |
end |
| 127 |
|
| 128 |
if type(b) == "number" then |
| 129 |
if b==math.floor(b) then |
| 130 |
return subFracs(a,M.new(b,1)) |
| 131 |
else |
| 132 |
return M.toFnumber(a) - b |
| 133 |
end |
| 134 |
end |
| 135 |
|
| 136 |
if type( a ) == "table" and type(b) =="table" then |
| 137 |
if getmetatable( a ) == frac_mt and getmetatable( b ) == complex_meta then |
| 138 |
return setmetatable( { a-b[1], -b[2] }, complex_meta ) |
| 139 |
end |
| 140 |
end |
| 141 |
|
| 142 |
if type( a ) == "table" and type(b) =="table" then |
| 143 |
if getmetatable( b ) == frac_mt and getmetatable( a ) == complex_meta then |
| 144 |
return setmetatable( { a[1]-b, a[2] }, complex_meta ) |
| 145 |
end |
| 146 |
end |
| 147 |
return subFracs(a, b) |
| 148 |
end |
| 149 |
|
| 150 |
|
| 151 |
function M.mul(a, b) |
| 152 |
if type(a) == "number" then |
| 153 |
if a==math.floor(a) then |
| 154 |
return mulFracs(M.new(a,1),b) |
| 155 |
else |
| 156 |
return a * M.toFnumber(b) |
| 157 |
end |
| 158 |
end |
| 159 |
|
| 160 |
if type(b) == "number" then |
| 161 |
if b==math.floor(b) then |
| 162 |
return mulFracs(a,M.new(b,1)) |
| 163 |
else |
| 164 |
return M.toFnumber(a) * b |
| 165 |
end |
| 166 |
end |
| 167 |
|
| 168 |
if type( a ) == "table" and type(b) =="table" then |
| 169 |
if getmetatable( a ) == frac_mt and getmetatable( b ) == complex_meta then |
| 170 |
return setmetatable( { a*b[1], a*b[2] }, complex_meta ) |
| 171 |
end |
| 172 |
end |
| 173 |
|
| 174 |
if type( a ) == "table" and type(b) =="table" then |
| 175 |
if getmetatable( b ) == frac_mt and getmetatable( a ) == complex_meta then |
| 176 |
return setmetatable( { b*a[1], b*a[2] }, complex_meta ) |
| 177 |
end |
| 178 |
end |
| 179 |
return mulFracs(a, b) |
| 180 |
end |
| 181 |
|
| 182 |
|
| 183 |
function M.div(a, b) |
| 184 |
if type(a) == "number" then |
| 185 |
if a==math.floor(a) then |
| 186 |
return divFracs(M.new(a,1),b) |
| 187 |
else |
| 188 |
return a / M.toFnumber(b) |
| 189 |
end |
| 190 |
end |
| 191 |
|
| 192 |
if type(b) == "number" then |
| 193 |
if b==math.floor(b) then |
| 194 |
return divFracs(a,M.new(b,1)) |
| 195 |
else |
| 196 |
return M.toFnumber(a) / b |
| 197 |
end |
| 198 |
end |
| 199 |
|
| 200 |
if type( a ) == "table" and type(b) =="table" then |
| 201 |
if getmetatable( a ) == frac_mt and getmetatable( b ) == complex_meta then |
| 202 |
b= setmetatable( { M.toFrac(b[1]), M.toFrac(b[2]) }, complex_meta ) |
| 203 |
return a*(1/b) |
| 204 |
end |
| 205 |
end |
| 206 |
return divFracs(a, b) |
| 207 |
end |
| 208 |
|
| 209 |
function M.tostring (c) |
| 210 |
if c.n == 0 then |
| 211 |
return string.format("%g",0) |
| 212 |
end |
| 213 |
if c.d == 1 then |
| 214 |
return string.format("%g",c.n) |
| 215 |
end |
| 216 |
if c.d == -1 then |
| 217 |
return string.format("%g",-c.n) |
| 218 |
end |
| 219 |
return string.format("\\frac{%g}{%g}", c.n, c.d) |
| 220 |
end |
| 221 |
|
| 222 |
|
| 223 |
function lnumChqEql(x, y) |
| 224 |
|
| 225 |
if type(x) == "number" and type(y) == "number" then |
| 226 |
return (x == y) |
| 227 |
end |
| 228 |
|
| 229 |
if getmetatable( x ) == frac_mt and getmetatable( y ) == frac_mt then |
| 230 |
return (M.toFnumber(x) == M.toFnumber(y)) |
| 231 |
end |
| 232 |
|
| 233 |
if type(x) == "number" and getmetatable( y ) == frac_mt then |
| 234 |
return (M.toFnumber(y) == x) |
| 235 |
end |
| 236 |
|
| 237 |
if getmetatable( x ) == frac_mt and type(y) == "number" then |
| 238 |
return (M.toFnumber(x) == y) |
| 239 |
end |
| 240 |
|
| 241 |
if getmetatable( x ) == complex_meta and getmetatable( y ) == complex_meta then |
| 242 |
return M.toFnumber(x[1]) == M.toFnumber(y[1]) and M.toFnumber(x[2]) == M.toFnumber(y[2]) |
| 243 |
end |
| 244 |
|
| 245 |
if type(x) == "number" and getmetatable( y ) == complex_meta then |
| 246 |
return (M.toFnumber(y[1]) == x and M.toFnumber(y[2]) == 0) |
| 247 |
end |
| 248 |
|
| 249 |
if getmetatable(x) == complex_meta and type( y ) == "number" then |
| 250 |
return (M.toFnumber(x[1]) == y and M.toFnumber(x[2]) == 0) |
| 251 |
end |
| 252 |
|
| 253 |
if getmetatable( x ) == frac_mt and getmetatable( y ) == complex_meta then |
| 254 |
return (M.toFnumber(x)==M.toFnumber(y[1]) and M.toFnumber(y[2]) == 0) |
| 255 |
end |
| 256 |
|
| 257 |
if getmetatable( x ) == complex_meta and getmetatable( y ) == frac_mt then |
| 258 |
return (M.toFnumber(y)==M.toFnumber(x[1]) and M.toFnumber(x[2]) == 0) |
| 259 |
end |
| 260 |
|
| 261 |
return false |
| 262 |
end |
| 263 |
|
| 264 |
--Setting Metatable operations. |
| 265 |
frac_mt.__add = M.add |
| 266 |
frac_mt.__sub = M.sub |
| 267 |
frac_mt.__mul = M.mul |
| 268 |
frac_mt.__div = M.div |
| 269 |
frac_mt.__unm = minusFracs |
| 270 |
frac_mt.__pow = powerFracs |
| 271 |
frac_mt.__tostring = M.tostring |
| 272 |
frac_mt.__eq = lnumChqEql |
| 273 |
|
| 274 |
return M |