Module:Mediants: Difference between revisions

Ganaram inukshuk (talk | contribs)
mNo edit summary
Ganaram inukshuk (talk | contribs)
Comments
Line 9: Line 9:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


-- Filter functions determine whether a mediant meets a specific criteria (eg,
-- Filter functions determine whether a mediant meets a specific criteria for
-- int limit), based on either the mediant itself, its search depth, or both.
-- being added to a set of mediants, be it based on something about the mediant,
-- This filter adds (or excludes) mediants based on int limit, and does nothing
-- its search depth, or both.
-- with the depth param.
 
-- A filter function has three params: mediant, depth, and a set of args, which
-- can be a single value or a table of values.
 
-- Int limit filter determines whether a ratio is within an int limit. Does not
-- use depth.
function p.int_limit_filter(mediant, depth, int_limit)
function p.int_limit_filter(mediant, depth, int_limit)
local int_max = math.max(mediant[1], mediant[2])
return math.max(mediant[1], mediant[2]) <= int_limit
 
return int_max <= int_limit
end
end


-- This filter adds (or excludes) mediants based on search depth, and does
-- Depth filter determines whether a ratio is within a target depth. Does not
-- nothing with the mediant itself.
-- use the mediant itself.
function p.search_depth_filter(mediant, depth, search_depth)
function p.search_depth_filter(mediant, depth, search_depth)
return depth <= search_depth
return depth <= search_depth
end
-- Tenney height filter determines whether a ratio is within a target Tenney
-- height. Does not use depth.
function p.tenney_height_filter(mediant, depth, tenney_height)
return math.log(mediant[1] * mediant[2]) / math.log(2) <= tenney_height
end
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------------------- SEARCH FUNCTIONS -------------------------------
---------------------------- GENERAL SEARCH FUNCTION ---------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


-- Find mediants by depth, how many times mediants are found in a set of ratios.
-- General search function searches for mediants using a filter function. A
function p.find_mediants_by_depth(init_ratios, depth)
-- custom filter function can be passed in to "filter" out mediants. Ratios
local init_ratios = init_ratios or {{1,1}, {1,0}}
-- are added using a while loop, which exits if a loop iteration adds no new
local depth = depth or 5
-- ratios.
 
local ratios = {}
local depths = {}
local filter_args = depth
local filter_func = p.search_depth_filter
ratios, depths = p.find_mediants_by_filter(init_ratios, filter_func, filter_args)
return ratios, depths
end


-- Find mediants by filter, where the filter function and its args are passed in
-- Find mediants by filter, where the filter function and its args are passed in
Line 85: Line 85:
end
end
return ratios, depths
return ratios, depths
end
-- Find mediants by filter, where the filter function and its args are passed in
-- as part of the function call. Only returns mediants, not depths.
function p.find_only_mediants_by_filter(init_ratios, filter_func, filter_args)
local init_ratios = init_ratios or {{1,1}, {1,0}}
local ratios, depths
ratios, depths = p.find_mediants_by_filter(init_ratios, filter_func, filter_args)
return ratios
end
--------------------------------------------------------------------------------
------------------------- DEPTH-BASED SEARCH FUNCTION --------------------------
--------------------------------------------------------------------------------
-- Depth-based search finds mediants by building a tree of mediants up to a
-- specified depth. This is made a standalone function under the reasoning that
-- depth-based search is a common enough operation (EG, JI ratio search, tuning
-- spectrum step ratio search).
-- Find mediants by depth, how many times mediants are found in a set of ratios.
function p.find_mediants(init_ratios, depth)
local init_ratios = init_ratios or {{1,1}, {1,0}}
local depth = depth or 5
local ratios, depths
ratios, depths = p.find_mediants_by_filter(init_ratios, p.search_depth_filter, depth)
return ratios, depths
end
-- Find mediants by depth, how many times mediants are found in a set of ratios.
-- Does not return depths.
function p.find_only_mediants(init_ratios, depth)
local init_ratios = init_ratios or {{1,1}, {1,0}}
local depth = depth or 5
local ratios, depths
ratios, depths = p.find_mediants_by_filter(init_ratios, p.search_depth_filter, depth)
return ratios
end
end


Line 95: Line 137:
local ratios, depths = p.find_mediants_by_filter({{1,1}, {1,0}}, func, 12)
local ratios, depths = p.find_mediants_by_filter({{1,1}, {1,0}}, func, 12)
ratios = p.find_mediants_by_depth({{1,1}, {1,0}}, 6)
ratios = p.find_mediants({{1,1}, {1,0}}, 6)
local generators = {}
local generators = {}
for i = 1, #ratios do
for i = 1, #ratios do