Module:JI ratios: Difference between revisions

Ganaram inukshuk (talk | contribs)
No edit summary
Ganaram inukshuk (talk | contribs)
No edit summary
Line 14: Line 14:
-- 128 -> ~2500 ratios
-- 128 -> ~2500 ratios
-- 100 -> ~1500 ratios
-- 100 -> ~1500 ratios
local SEARCH_MAX = 128
local SEARCH_MAX = 150
 
local DEFAULT_INT_LIMIT = 50


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Line 25: Line 27:
function p.search_by_int_limit(integer_limit, max_cents)
function p.search_by_int_limit(integer_limit, max_cents)
local max_cents = max_cents or 1200
local max_cents = max_cents or 1200
local integer_limit = integer_limit or 21
local integer_limit = integer_limit or DEFAULT_INT_LIMIT
integer_limit = math.max(0, math.min(SEARCH_MAX, integer_limit))
integer_limit = math.max(0, math.min(SEARCH_MAX, integer_limit))
Line 89: Line 91:
end
end


--------------------------------------------------------------------------------
-- Search for ratios based on params passed in. Each param is its own
--------------------------- RATIO SORTING FUNCTIONS ----------------------------
-- function call. Params must be parsed first.
--------------------------------------------------------------------------------
function p.search_by_params(params, max_cents)
 
local max_cents = max_cents or 1200
-- Sorts ratios by closeness to cent values.
function p.sort_by_closeness_to_cent_values(ratios, cent_values, tolerance)
-- First get ratios up to an int limit. If no int limit was passed in, it
local sorted_ratios = {}
-- will default to the hardcoded default value.
local ratios = p.search_by_int_limit(params["Int Limit"], max_cents)
local curr_index = 1 -- Index of current_ratio
if params["Prime Limit"] ~= nil then
for i = 1, #cent_values do
ratios = p.filter_by_prime_limit(ratios, params["Prime Limit"])
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
end
return sorted_ratios
if params["Tenney Height"] ~= nil then
end
ratios = p.filter_by_tenney_height(ratios, params["Tenney Height"])
 
--------------------------------------------------------------------------------
------------------------- RATIO MULTI-SEARCH FUNCTIONS -------------------------
--------------------------------------------------------------------------------
 
-- Filter ratios based on search params entered in. Each param is its own
-- function call.
function p.filter_by_search_params(ratios, search_params)
local parsed = p.parse_search_params(search_params)
if parsed["Tenney Height"] ~= nil then
ratios = p.filter_by_tenney_height(ratios, parsed["Tenney Height"])
end
end
if parsed["Prime Limit"] ~= nil then
return ratios
ratios = p.filter_by_prime_limit(ratios, parsed["Prime Limit"])
end
end
end


Line 143: Line 114:
function p.parse_search_params(search_params)
function p.parse_search_params(search_params)
local parsed = tip.parse_kv_pairs(search_params)
local parsed = tip.parse_kv_pairs(search_params)
if parsed["Tenney Height"] ~= nil then
parsed["Tenney Height"] = tonumber(parsed["Tenney Height"])
end
if parsed["Tenney Height"] ~= nil then
if parsed["Tenney Height"] ~= nil then
Line 196: Line 171:
function p.filter_by_nonprime_subgroup(ratios, subgroup)
function p.filter_by_nonprime_subgroup(ratios, subgroup)
end
--------------------------------------------------------------------------------
--------------------------- RATIO SORTING FUNCTIONS ----------------------------
--------------------------------------------------------------------------------
-- Sorts ratios by closeness to cent values.
function p.sort_by_closeness_to_cent_values(ratios, cent_values, tolerance)
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 207: Line 215:
local delimiter = delimiter or ", "
local delimiter = delimiter or ", "
local text = add_links and string.format("[[%s]]", rat.as_ratio(ratios[1])) or rat.as_ratio(ratios[1])
local text = ""
for i = 2, #ratios do
if #ratios ~= 0 then
text = text .. (add_links and string.format("%s[[%s]]", delimiter, rat.as_ratio(ratios[i])) or string.format("%s%s", delimiter, rat.as_ratio(ratios[i])))
text = add_links and string.format("[[%s]]", rat.as_ratio(ratios[1])) or rat.as_ratio(ratios[1])
for i = 2, #ratios do
text = text .. (add_links and string.format("%s[[%s]]", delimiter, rat.as_ratio(ratios[i])) or string.format("%s%s", delimiter, rat.as_ratio(ratios[i])))
end
end
end
return text
return text
Line 228: Line 239:


function p.tester()
function p.tester()
local ratios = p.search_by_int_limit(50)
local params = p.parse_search_params("Prime Limit: 7; Tenney Height: 11")
ratios = p.filter_by_prime_limit(ratios, 5)
ratios = p.search_by_params(params)
ratios = p.sort_by_closeness_to_cent_values(ratios, {0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200}, 15)
ratios = p.sort_by_closeness_to_cent_values(ratios, {0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200}, 15)