Module:Mediants: Difference between revisions
m Cleanup |
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 = {} | ||
-- | -- Mediants consists of code used to find a tree of mediants, starting from a | ||
-- custom search function. | -- set of starting ratios (default 1/1 and 1/0). Search can be by int limit, | ||
-- | -- depth, or a custom search function. | ||
-- form. | -- Ratios produced this way are a table consisting of the numerator and | ||
-- | -- 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 ------------------------------ | |||
-------------------------------------------------------------------------------- | |||
-- | -- Converts ratios into the form defined by [[Module:Rational]], a table | ||
-- | -- 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 | ||