Module:Mediants: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
mNo edit summary |
||
| Line 36: | Line 36: | ||
local ratios = {} | local ratios = {} | ||
local depths = {} | local depths = {} | ||
local | local filter_args = depth | ||
local filter_func = p.search_depth_filter | local filter_func = p.search_depth_filter | ||
ratios, depths = p.find_mediants_by_filter(init_ratios, filter_func, | ratios, depths = p.find_mediants_by_filter(init_ratios, filter_func, filter_args) | ||
return ratios, depths | return ratios, depths | ||
end | end | ||
| Line 45: | Line 45: | ||
-- 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 | ||
-- as part of the function call. | -- as part of the function call. | ||
function p.find_mediants_by_filter(init_ratios, | function p.find_mediants_by_filter(init_ratios, filter_func, filter_args) | ||
local init_ratios = init_ratios or {{1,1}, {1,0}} | local init_ratios = init_ratios or {{1,1}, {1,0}} | ||
| Line 72: | Line 72: | ||
table.insert(new_depths, depth_1) | table.insert(new_depths, depth_1) | ||
if | if filter_func(mediant, new_depth, filter_args) then | ||
table.insert(new_ratios, mediant) | table.insert(new_ratios, mediant) | ||
table.insert(new_depths, new_depth) | table.insert(new_depths, new_depth) | ||
Revision as of 19:06, 13 September 2024
- This module primarily serves as a library for other modules and has no corresponding template.
Module:Mediants is used for finding mediants starting from a set of starting ratios (by default, 1/1 and 1/0), either by search depth, integer limit, or by a custom search function.
| Introspection summary for Module:Mediants | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||||||||||||||||||||
No function descriptions were provided. The Lua code may have further information.
local mos = require("Module:MOS") -- For testing
local utils = require("Module:Utils") -- For testing
local p = {}
-- 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)
local init_ratios = init_ratios or {{1,1}, {1,0}}
local depth = depth or 5
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
-- as part of the function call.
function p.find_mediants_by_filter(init_ratios, filter_func, filter_args)
local init_ratios = init_ratios or {{1,1}, {1,0}}
local ratios = {}
local depths = {}
for i = 1, #init_ratios do
table.insert(ratios, init_ratios[i])
table.insert(depths, 0)
end
local new_ratios_added = true
while new_ratios_added do
new_ratios_added = false
local new_ratios = {}
local new_depths = {}
for i = 1, #ratios-1 do
local ratio_1 = ratios[i]
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)
local depth_1 = depths[i]
local depth_2 = depths[i+1]
local new_depth = math.max(depth_1, depth_2) + 1
table.insert(new_depths, depth_1)
if filter_func(mediant, new_depth, filter_args) then
table.insert(new_ratios, mediant)
table.insert(new_depths, new_depth)
new_ratios_added = true
end
end
table.insert(new_ratios, ratios[#ratios])
table.insert(new_depths, depths[#depths])
ratios = new_ratios
depths = new_depths
end
return ratios, depths
end
--------------------------------------------------------------------------------
----------------------------------- TESTER -------------------------------------
--------------------------------------------------------------------------------
function p.tester()
local func = p.int_limit_filter
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)
local generators = {}
for i = 1, #ratios do
local input_mos = mos.new(5,2)
local gen = mos.bright_gen_to_cents(input_mos, ratios[i])
local gcd = utils._gcd(ratios[i][1], ratios[i][2])
local edo = (ratios[i][1] * 5 + ratios[i][2] * 2)/gcd
local new_string = string.format("%s:%s\t%sedo\t%.3f", ratios[i][1]/gcd, ratios[i][2]/gcd, edo, gen)
table.insert(generators, new_string)
end
return generators
end
return p