Module:Mediants: Difference between revisions

Ganaram inukshuk (talk | contribs)
Simplify code
Ganaram inukshuk (talk | contribs)
No edit summary
Line 5: Line 5:
-- Module for finding mediants, either by search depth or by search function.
-- Module for finding mediants, either by search depth or by search function.


--------------------------------------------------------------------------------
------------------------------- FILTER FUNCTIONS -------------------------------
--------------------------------------------------------------------------------
-- Filter functions determine whether a mediant meets a specific criteria (eg,
-- int limit), based on either the mediant itself, its search depth, or both.
-- This filter adds (or excludes) mediants based on int limit, and does nothing
-- with the depth param.
function p.int_limit_filter(mediant, depth, int_limit)
local int_max = math.max(mediant[1], mediant[2])
return int_max <= int_limit
end
-- This filter adds (or excludes) mediants based on search depth, and does
-- nothing with the mediant itself.
function p.search_depth_filter(mediant, depth, search_depth)
return depth <= search_depth
end
--------------------------------------------------------------------------------
------------------------------- SEARCH FUNCTIONS -------------------------------
--------------------------------------------------------------------------------
-- Find mediants by depth, how many times mediants are found in a set of ratios.
function p.find_mediants_by_depth(init_ratios, depth)
function p.find_mediants_by_depth(init_ratios, depth)
local init_ratios = init_ratios or {{1,1}, {1,0}}
local init_ratios = init_ratios or {{1,1}, {1,0}}
local depth = depth or 5
local depth = depth or 5
 
local ratios = {}
local ratios = {}
local depths = {}
local depths = {}
for i = 1, #init_ratios do
local filter_arg = depth
table.insert(ratios, init_ratios[i])
local filter_func = p.search_depth_filter
table.insert(depths, 0)
end
for i = 1, depth do
ratios, depths = p.find_mediants_by_filter(init_ratios, filter_func, filter_arg)
local new_ratios = {}
local new_depths = {}
for j = 1, #ratios - 1 do
local ratio_1 = ratios[j]
local ratio_2 = ratios[j+1]
local mediant = { ratio_1[1] + ratio_2[1], ratio_1[2] + ratio_2[2] }
table.insert(new_ratios, ratio_1)
table.insert(new_ratios, mediant)
local depth_1 = depths[j]
local depth_2 = depths[j+1]
table.insert(new_depths, depth_1)
table.insert(new_depths, math.max(depth_1, depth_2) + 1)
end
table.insert(new_ratios, ratios[#ratios])
table.insert(new_depths, depths[#depths])
ratios = new_ratios
depths = new_depths
end
return ratios, depths
return ratios, depths
end
end


-- Filter functions calculate whether a mediant is allowed to be added to the
-- Find mediants by filter, where the filter function and its args are passed in
-- running set of mediants, and are passed to the find-mediants-by-filter
-- as part of the function call.
-- function as a parameter, followed by its arg(s).
function p.int_limit_filter(ratio_1, ratio_2, int_limit)
local mediant = { ratio_1[1] + ratio_2[1], ratio_1[2] + ratio_2[2] }
local int_max = math.max(mediant[1], mediant[2])
 
return int_max <= int_limit
end
 
function p.find_mediants_by_filter(init_ratios, filter, filter_arg)
function p.find_mediants_by_filter(init_ratios, filter, filter_arg)
local init_ratios = init_ratios or {{1,1}, {1,0}}
local init_ratios = init_ratios or {{1,1}, {1,0}}
Line 70: Line 64:
local ratio_1 = ratios[i]
local ratio_1 = ratios[i]
local ratio_2 = ratios[i+1]
local ratio_2 = ratios[i+1]
local mediant = { ratio_1[1] + ratio_2[1], ratio_1[2] + ratio_2[2] }
table.insert(new_ratios, ratio_1)
table.insert(new_ratios, ratio_1)
if filter(ratio_1, ratio_2, filter_arg) then
local depth_1 = depths[i]
local mediant = { ratio_1[1] + ratio_2[1], ratio_1[2] + ratio_2[2] }
local depth_2 = depths[i+1]
local new_depth = math.max(depth_1, depth_2) + 1
table.insert(new_depths, depth_1)
if filter(mediant, new_depth, filter_arg) then
table.insert(new_ratios, mediant)
table.insert(new_ratios, mediant)
table.insert(new_depths, new_depth)
new_ratios_added = true
new_ratios_added = true
end
end
Line 86: Line 86:
return ratios, depths
return ratios, depths
end
end
--------------------------------------------------------------------------------
----------------------------------- TESTER -------------------------------------
--------------------------------------------------------------------------------


function p.tester()
function p.tester()