Module:Mediants: Difference between revisions
Jump to navigation
Jump to search
Changed code to accept initial ratios in an array |
Simplify code |
||
| Line 10: | Line 10: | ||
local ratios = {} | local ratios = {} | ||
local depths = {} | |||
for i = 1, #init_ratios do | for i = 1, #init_ratios do | ||
table.insert(ratios, init_ratios[i]) | table.insert(ratios, init_ratios[i]) | ||
table.insert(depths, 0) | |||
end | end | ||
for i = 1, depth do | for i = 1, depth do | ||
local new_ratios = {} | local new_ratios = {} | ||
local new_depths = {} | |||
for j = 1, #ratios - 1 do | for j = 1, #ratios - 1 do | ||
| Line 23: | Line 26: | ||
table.insert(new_ratios, ratio_1) | table.insert(new_ratios, ratio_1) | ||
table.insert(new_ratios, mediant) | 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 | end | ||
table.insert(new_ratios, ratios[#ratios]) | table.insert(new_ratios, ratios[#ratios]) | ||
table.insert(new_depths, depths[#depths]) | |||
ratios = new_ratios | ratios = new_ratios | ||
depths = new_depths | |||
end | end | ||
return ratios | return ratios, depths | ||
end | end | ||
function p.int_limit_filter( | -- Filter functions calculate whether a mediant is allowed to be added to the | ||
-- running set of mediants, and are passed to the find-mediants-by-filter | |||
-- 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 mediant = { ratio_1[1] + ratio_2[1], ratio_1[2] + ratio_2[2] } | ||
local int_max = math.max(mediant[1], mediant[2]) | local int_max = math.max(mediant[1], mediant[2]) | ||
return int_max <= int_limit | |||
end | end | ||
| Line 63: | Line 55: | ||
local ratios = {} | local ratios = {} | ||
local depths = {} | |||
for i = 1, #init_ratios do | for i = 1, #init_ratios do | ||
table.insert(ratios, init_ratios[i]) | table.insert(ratios, init_ratios[i]) | ||
table.insert(depths, 0) | |||
end | end | ||
| Line 71: | Line 65: | ||
new_ratios_added = false | new_ratios_added = false | ||
local new_ratios = {} | local new_ratios = {} | ||
local new_depths = {} | |||
for i = 1, #ratios-1 do | for i = 1, #ratios-1 do | ||
| Line 76: | Line 71: | ||
local ratio_2 = ratios[i+1] | local ratio_2 = ratios[i+1] | ||
table.insert(new_ratios, ratio_1) | table.insert(new_ratios, ratio_1) | ||
if filter(ratio_1, ratio_2, filter_arg) then | |||
local mediant = { ratio_1[1] + ratio_2[1], ratio_1[2] + ratio_2[2] } | |||
table.insert(new_ratios, mediant) | |||
new_ratios_added = true | |||
end | |||
end | end | ||
table.insert(new_ratios, ratios[#ratios]) | |||
table.insert(new_depths, depths[#depths]) | |||
ratios = new_ratios | ratios = new_ratios | ||
depths = new_depths | |||
end | end | ||
return ratios | return ratios, depths | ||
end | end | ||
| Line 88: | Line 90: | ||
local func = p.int_limit_filter | local func = p.int_limit_filter | ||
local ratios = 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}}, | ratios = p.find_mediants_by_depth({{1,1}, {1,0}}, 6) | ||
local generators = {} | local generators = {} | ||
for i = 1, #ratios do | for i = 1, #ratios do | ||
Revision as of 09:13, 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.
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 = {}
for i = 1, #init_ratios do
table.insert(ratios, init_ratios[i])
table.insert(depths, 0)
end
for i = 1, depth do
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
end
-- Filter functions calculate whether a mediant is allowed to be added to the
-- running set of mediants, and are passed to the find-mediants-by-filter
-- 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)
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]
table.insert(new_ratios, ratio_1)
if filter(ratio_1, ratio_2, filter_arg) then
local mediant = { ratio_1[1] + ratio_2[1], ratio_1[2] + ratio_2[2] }
table.insert(new_ratios, mediant)
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
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