Module:JI ratios: Difference between revisions

Ganaram inukshuk (talk | contribs)
No edit summary
Ganaram inukshuk (talk | contribs)
comments; todo
Line 1: Line 1:
-- In-progress successor to Module:JI ratio finder
-- Template for handling multiple entry of JI ratios into a template, and for
-- searching for JI ratios if automatic entry is desired.
local rat = require("Module:Rational")
local rat = require("Module:Rational")
local utils = require("Module:Utils")
local utils = require("Module:Utils")
local tip = require("Module:Template input parse")
local tip = require("Module:Template input parse")
local m = require("Module:Mediants")
p = {}
p = {}
-- TODO:
-- Adopt mediants module for all filtering needs
-- Template for handling multiple entry of JI ratios into a template, and for
-- searching for JI ratios if automatic entry is desired.
-- This is a successor/replacement for JI ratio finder.
-- JI ratios are searched by the following params in a hierarchy:
-- - The absolute minimum for ratio search int limit, which limits the maximum
--  size of the numerator and denominator.
-- - If subgroup is present, ratios are searched by subgroup within an int
--  limit. Subgroup takes precedence over prime limit, as subgroup is
--  (typically) a subset of prime limit, so prime limit is ignored. (Nonprime
--  subgroups take precedence over prime subgroups.)
-- - If prime limit is present, ratios are searched by prime limit within an int
--  limit.
-- NOTES:
-- - Prime limits are infinite sets, so int limit is used to restrain the set
--  to a finite size. The same is true for subgroup.
-- - Tenney height is used for further filtering of ratios, and is considered
--  optional.


-- INT_LIMIT_MAX is hardcoded to limit the size of output.
-- INT_LIMIT_MAX is hardcoded to limit the size of output.
Line 17: Line 37:
local INT_LIMIT_MAX = 200
local INT_LIMIT_MAX = 200
local DEFAULT_INT_LIMIT = 50
local DEFAULT_INT_LIMIT = 50
--------------------------------------------------------------------------------
----------------------- MEDIANT SEARCH FILTER FUNCTIONS ------------------------
--------------------------------------------------------------------------------
-- Mediants module is used to find JI ratios, so this section is for filter
-- functions.
-- 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)
return math.max(mediant[1], mediant[2]) <= int_limit
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


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Line 31: Line 73:
integer_limit = math.max(0, math.min(INT_LIMIT_MAX, integer_limit))
integer_limit = math.max(0, math.min(INT_LIMIT_MAX, integer_limit))
local ratios = {{1,1}, {2,1}}
local init_ratios = {{1,1}, {2,1}}
local filter_func = p.int_limit_filter
-- Find ratios by finding the mediants between existing ratios, only adding
local filter_args = integer_limit
-- those that do not exceed the integer limit.
local ratios = m.find_mediants_by_filter(init_ratios, filter_func, filter_args)
local new_ratios_added = true
while new_ratios_added do
new_ratios_added = false
local new_ratios = {}
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]}
local INT_LIMIT_MAX = math.max(mediant[1], mediant[2])
table.insert(new_ratios, ratios[i])
if INT_LIMIT_MAX <= integer_limit then
new_ratios_added = true
table.insert(new_ratios, mediant)
end
end
table.insert(new_ratios, ratios[#ratios])
ratios = new_ratios
end
-- If the max cents is greater than the octave, duplicate all existing
-- If the max cents is greater than the octave, duplicate all existing
Line 133: Line 156:
end
end


-- Footnote generation. Must take in parsed params. There's a hierarchy for
-- search params, as follows:
-- - Go by subgroup. If not, then go by prime limit. If not, then go by int limit.
function p.search_param_footnotes(search_params)
function p.search_param_footnotes(search_params)
local result = "Not all notable ratios may be shown, and other interpretations are possible."
local result = "Not all notable ratios may be shown, and other interpretations are possible."