Module:JI ratios: Difference between revisions

Ganaram inukshuk (talk | contribs)
No edit summary
Ganaram inukshuk (talk | contribs)
cleanup before rewriting
Line 8: Line 8:


-- TODO
-- TODO
-- - Move filtering functions to separate module?
-- - Transfer control over to new "main" function: p.ji_ratios()
-- - Transfer control over to new "main" function: p.ji_ratios()


Line 56: Line 57:
end
end
return found
return found
end
-- Preprocess fine-search args
function p.preprocess_fine_search_args(fine_search_args)
local fine_search_args = fine_search_args or DEFAULT_FINE_SEARCH_ARGS
local tenney_height = fine_search_args["Tenney Height"] or 1/0
local comps_only    = fine_search_args["Complements Only"] or false
local int_limit    = fine_search_args["Int Limit"] or DEFAULT_INT_LIMIT
return {
["Tenney Height"]    = tenney_height,
["Complements Only"] = comps_only,
["Int Limit"]        = int_limit
}
end
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------------------- FILTER FUNCTIONS -------------------------------
--------------------------- FILTER/SORTING FUNCTIONS ---------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


Line 104: Line 91:
end
end
return filtered_ratios
return filtered_ratios
end
-- Sorts ratios by closeness to cent values.
function p.sort_by_closeness_to_cent_values(ratios, cent_values, tolerance)
local tolerance = tolerance or 30
local sorted_ratios = {}
local curr_index = 1 -- Index of current_ratio
for i = 1, #cent_values do
local lower_bound = cent_values[i] - tolerance
local upper_bound = cent_values[i] + tolerance
local cents_within_range = true
local curr_ratios = {}
for j = curr_index, #ratios do
local curr_ratio = ratios[j]
local curr_cents = rat.cents(curr_ratio)
if lower_bound < curr_cents and curr_cents < upper_bound then
table.insert(curr_ratios, curr_ratio)
--elseif curr_cents > upper_bound then
-- curr_index = j
-- break
end
end
table.insert(sorted_ratios, curr_ratios)
end
return sorted_ratios
end
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
----------------------- INT-LIMIT-BASED SEARCH FUNCTION ------------------------
-------------------------- INT-LIMIT SEARCH FUNCTION ---------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


Line 140: Line 157:


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------------ SUBGROUP-BASED SEARCH FUNCTION ------------------------
------------------------- PRIME-LIMIT SEARCH FUNCTION --------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


Line 168: Line 185:
end
end


-- WORK-IN-PROGRESS!!
--------------------------------------------------------------------------------
---------------------------- SUBGROUP SEARCH FUNCTION --------------------------
--------------------------------------------------------------------------------
 
function p.search_by_subgroup(subgroup, equave, fine_search_args)
function p.search_by_subgroup(subgroup, equave, fine_search_args)
local subgroup = subgroup or {rat.new(2), rat.new(3), rat.new(7)}
local subgroup = subgroup or {rat.new(2), rat.new(3), rat.new(7)}
Line 239: Line 259:
--------------------------- REVERSE-SEARCH FUNCTIONS ---------------------------
--------------------------- REVERSE-SEARCH FUNCTIONS ---------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
 
--[[
function p.int_limit_of_ratios(ratios)
function p.int_limit_of_ratios(ratios)
Line 254: Line 274:
end
end
 
]]--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-------------------------- ARG-BASED SEARCH FUNCTIONS --------------------------
-------------------------- ARG-BASED SEARCH FUNCTIONS --------------------------
Line 309: Line 329:
return parsed
return parsed
end
function p.search_footnotes(search_args)
local result = "Other interpretations are possible."
local autosearch_text = "Automatic search may be inexact."
local search_text = ""
if search_args["Prime Limit"] ~= nil then
search_text = string.format("Ratios shown are within the %s-prime limit.", search_args["Prime Limit"])
.. " " .. autosearch_text
elseif search_args["Subgroup"] ~= nil then
local subgroup_members = {}
for i = 1, #search_args["Subgroup"] do
local a, b = rat.as_pair(search_args["Subgroup"][i])
table.insert(subgroup_members, (b == 1 and string.format("%s", a) or string.format("%s/%s", a, b)))
end
search_text = string.format("Ratios shown are within the %s subgroup.", table.concat(subgroup_members, "."))
.. " " .. autosearch_text
elseif search_args["Int Limit"] ~= nil then
search_text = string.format("Ratios shown are within the %s-integer limit.", search_args["Int Limit"])
.. " " .. autosearch_text
end
result = search_text .. " " .. result
return result
end
--------------------------------------------------------------------------------
--------------------------- RATIO SORTING FUNCTIONS ----------------------------
--------------------------------------------------------------------------------
-- Sorts ratios by closeness to cent values.
function p.sort_by_closeness_to_cent_values(ratios, cent_values, tolerance)
local tolerance = tolerance or 30
local sorted_ratios = {}
local curr_index = 1 -- Index of current_ratio
for i = 1, #cent_values do
local lower_bound = cent_values[i] - tolerance
local upper_bound = cent_values[i] + tolerance
local cents_within_range = true
local curr_ratios = {}
for j = curr_index, #ratios do
local curr_ratio = ratios[j]
local curr_cents = rat.cents(curr_ratio)
if lower_bound < curr_cents and curr_cents < upper_bound then
table.insert(curr_ratios, curr_ratio)
--elseif curr_cents > upper_bound then
-- curr_index = j
-- break
end
end
table.insert(sorted_ratios, curr_ratios)
end
return sorted_ratios
end
end


Line 414: Line 375:
end
end
return texts
return texts
end
function p.tester()
local equave = rat.new(2,1)
local fine_search_args = p.parse_search_args("Subgroup: 2.5.9/7.21; Int Limit: 50; Complements Only: 0; Tenney Height: 10")
local subgroup = {rat.new(2), rat.new(3), rat.new(5), rat.new(7)}
return p.ratios_as_string(p.search_by_args_within_equave(equave, fine_search_args))
end
end