Module:JI ratios: Difference between revisions
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
-- In-progress successor to Module:JI ratio finder | -- In-progress successor to Module:JI ratio finder | ||
-- Template for handling multiple entry of JI ratios into a template. | -- 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") | |||
p = {} | p = {} | ||
| Line 91: | Line 93: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- Sorts ratios by closeness to cent values. | |||
function p.sort_by_closeness_to_cent_values(ratios, cent_values, tolerance) | function p.sort_by_closeness_to_cent_values(ratios, cent_values, tolerance) | ||
local sorted_ratios = {} | local sorted_ratios = {} | ||
| Line 120: | Line 123: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-------------------------- | ------------------------- 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) | 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 | |||
if parsed["Prime Limit"] ~= nil then | |||
ratios = p.filter_by_prime_limit(ratios, parsed["Prime Limit"]) | |||
end | |||
end | end | ||
-- Parse search params. | |||
function p.parse_search_params(search_params) | function p.parse_search_params(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["Prime Limit"] ~= nil then | |||
parsed["Prime Limit"] = tonumber(parsed["Prime Limit"]) | |||
end | |||
return parsed | |||
end | end | ||
-------------------------------------------------------------------------------- | |||
---------------------------- RATIO FILTER FUNCTIONS ---------------------------- | |||
-------------------------------------------------------------------------------- | |||
-- Filter ratios by Tenney height. | |||
function p.filter_by_tenney_height(ratios, tenney_height) | function p.filter_by_tenney_height(ratios, tenney_height) | ||
local tenney_height = tenney_height or 8 | local tenney_height = tenney_height or 8 | ||
| Line 144: | Line 173: | ||
end | end | ||
-- Filter ratios by prime limit. | |||
function p.filter_by_prime_limit(ratios, prime_limit) | function p.filter_by_prime_limit(ratios, prime_limit) | ||
local prime_limit = prime_limit or 41 | local prime_limit = prime_limit or 41 | ||
| Line 155: | Line 185: | ||
end | end | ||
return filtered_ratios | return filtered_ratios | ||
end | |||
-- Filter ratios by (prime) subgroup. EG: 2.3.5.7 | |||
function p.filter_by_subgroup(ratios, subgroup) | |||
end | |||
-- Filter ratios by rational/nonprime subgroup. EG, 2.7/2.11/2, or 2.5.7.9 | |||
-- Does not support irrational subgroups. | |||
function p.filter_by_nonprime_subgroup(ratios, subgroup) | |||
end | end | ||
| Line 165: | Line 206: | ||
local add_links = add_links == true | local add_links = add_links == true | ||
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 = add_links and string.format("[[%s]]", rat.as_ratio(ratios[1])) or rat.as_ratio(ratios[1]) | ||
for i = 2, #ratios do | for i = 2, #ratios do | ||
| Line 172: | Line 214: | ||
end | end | ||
-- Convert a table of tables into a table of text | |||
function p.ratios_as_texts(ratios, add_links, delimiter) | |||
local add_links = add_links == true | |||
local delimiter = delimiter or ", " | |||
local texts = {} | |||
for i = 1, #ratios do | |||
local text = p.ratios_as_text(ratios[i], add_links, delimiter) | |||
table.insert(texts, text) | |||
end | |||
return texts | |||
end | |||
function p.tester() | function p.tester() | ||
| Line 177: | Line 231: | ||
ratios = p.filter_by_prime_limit(ratios, 5) | ratios = p.filter_by_prime_limit(ratios, 5) | ||
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) | ||
return | return p.ratios_as_texts(ratios) | ||
end | end | ||
return p | return p | ||