Module:Rational: Difference between revisions

All the special cases in as_ket make no sense
Document this mess, starting with the first function
Line 3: Line 3:
local p = {}
local p = {}


-- construct a rational number n/m
-- enter a numerator n and denominator m
-- returns a table of prime factors
-- similar to a monzo, but the indices are prime numbers.
function p.new(n, m)
function p.new(n, m)
m = m or 1
m = m or 1
Line 14: Line 16:
end
end
local sign = utils.signum(n) * utils.signum(m)
local sign = utils.signum(n) * utils.signum(m)
-- ensure n and m are positive
n = n * utils.signum(n)
n = n * utils.signum(n)
m = m * utils.signum(m)
m = m * utils.signum(m)
-- factorize n and m separately
local n_factors = utils.prime_factorization_raw(n)
local n_factors = utils.prime_factorization_raw(n)
local m_factors = utils.prime_factorization_raw(m)
local m_factors = utils.prime_factorization_raw(m)
local factors = n_factors
local factors = n_factors
factors.sign = sign
factors.sign = sign
-- subtract the factors of m from the factors of n
for factor, power in pairs(m_factors) do
for factor, power in pairs(m_factors) do
factors[factor] = factors[factor] or 0
factors[factor] = factors[factor] or 0
factors[factor] = factors[factor] - power
factors[factor] = factors[factor] - power
if factors[factor] == 0 then
if factors[factor] == 0 then
factors[factor] = nil
factors[factor] = nil -- clear the zeros
end
end
end
end