Module:Mediants: Difference between revisions

Ganaram inukshuk (talk | contribs)
m Cleanup
Ganaram inukshuk (talk | contribs)
update code to be documentation
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
-- This module follows [[User:Ganaram inukshuk/Provisional style guide for Lua]]
local rat  = require("Module:Rational")
local utils = require("Module:Utils")
local p = {}
local p = {}


-- Module for finding mediants, either by search depth, int limit, or by a
-- Mediants consists of code used to find a tree of mediants, starting from a
-- custom search function. Note that ratios produced by this module are NOT the
-- set of starting ratios (default 1/1 and 1/0). Search can be by int limit,
-- same as that defined by Module:Rational and will require conversion to that
-- depth, or a custom search function.
-- form. This is because some situations, such as with step ratios, don't need
-- Ratios produced this way are a table consisting of the numerator and
-- the factorization process that Module:Rational uses.
-- denominator, which allows for non-simplified ratios to be represented.
 
--------------------------------------------------------------------------------
------------------------------ UTILITY FUNCTIONS -------------------------------
--------------------------------------------------------------------------------
 
-- Given a table of depths, return the deepest depth
function p.deepest_depth(depths)
local deepest = nil
for _, value in ipairs(depths) do
if not deepest or value > deepest then
deepest = value
end
end
return deepest
end
 
-- Given a ratio, return its simplified form.
function p.simplify_ratio(ratio)
local gcd = utils._gcd(ratio[1], ratio[2])
return { ratio[1] / gcd, ratio[2] / gcd }
end
 
-- Sort ratios in ascending order. Comparison function is built-in.
function p.sort_ratios(ratios)
table.sort(ratios, function(ratio_1, ratio_2)
return ratio_1[1] / ratio_1[2] < ratio_2[1] / ratio_2[2]
end
)
end
 
--------------------------------------------------------------------------------
----------------------------- CONVERTER FUNCTIONS ------------------------------
--------------------------------------------------------------------------------


-- TODO:
-- Converts ratios into the form defined by [[Module:Rational]], a table
-- Add a convert-to-rational function.
-- consisting of its prime factorization.
 
-- Given a single ratio, as a table of two numbers, convert to rational and
-- return it.
function p.to_rational(ratio)
return rat.new(ratio[1], ratio[2])
end
 
-- Given a table of ratios, each a table of two numbers, return an array of
-- ratios in the form as defined by module:Rational.
function p.to_rationals(ratios)
local rats = {}
for i = 1, #ratios do
table.insert(rats, p.to_rational(ratios[i]))
end
return rats
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Line 174: Line 228:


function p.tester()
function p.tester()
return p.find_only_mediants_by_int_limit()
--return p.find_only_mediants_by_int_limit()
local ratios = {{4,3}, {5,1}, {3,2}}
p.sort_ratios(ratios)
return p.to_rationals(ratios)
end
end


return p
return p