Module:JI ratios: Difference between revisions

Ganaram inukshuk (talk | contribs)
code for finding products (not yet ratios) within a subgroup, to be expanded to find ratios; comments, rationale; testing
Ganaram inukshuk (talk | contribs)
m comments
 
(89 intermediate revisions by 2 users not shown)
Line 1: Line 1:
-- This module follows [[User:Ganaram inukshuk/Provisional style guide for Lua]]
local getArgs = require("Module:Arguments").getArgs
local med = require("Module:Mediants")
local rat = require("Module:Rational")
local rat = require("Module:Rational")
local tip = require("Module:Template input parse")
local utils = require("Module:Utils")
local utils = require("Module:Utils")
local tip = require("Module:Template input parse")
local yesno = require("Module:Yesno")
local m = require("Module:Mediants")
p = {}


-- TODO:
local p = {}
-- Adopt mediants module


-- Template for handling multiple entry of JI ratios into a template, and for
-- Template for handling multiple entry of JI ratios into a template, and for
Line 12: Line 13:
-- This is a successor/replacement for JI ratio finder.
-- This is a successor/replacement for JI ratio finder.


-- JI ratios are searched by the following params in a hierarchy:
-- TODO: Refactor code such that:
-- - The absolute minimum for ratio search int limit, which limits the maximum
-- - For int-limit search, int limit is the first arg, and equave and min/max
--  size of the numerator and denominator.
--  cents default to 2/1, 0c, and 1200c respectively.
-- - If subgroup is present, ratios are searched by subgroup within an int
--  (int_limit, equave)
--  limit. Subgroup takes precedence over prime limit, as subgroup is
--  (int_limit, min_cents, max_cents)
--  (typically) a subset of prime limit, so prime limit is ignored. (Nonprime
-- - For odd-limit search, odd limit is the first arg, int limit defaults to
--  subgroups take precedence over prime subgroups.)
--  twice the odd limit, and equave and min/max cents default to 2/1, 0c, and
-- - If prime limit is present, ratios are searched by prime limit within an int
--  1200c respectively.
--  limit.
--  (odd_limit, int_limit, equave)
-- NOTES:
--  (odd_limit, int_limit, min_cents, max_cents)
-- - Prime limits are infinite sets, so int limit is used to restrain the set
-- - For prime-limit search, prime-limit is the first arg, int limit defaults to
--  to a finite size. The same is true for subgroup.
--  twice the largest prime, and equave and min/max cents default to 2/1, 0c,
-- - Tenney height is used for further filtering of ratios, and is considered
--  and 1200c respectively.
--   optional.
--  (prime_limit, int_limit, equave)
--  (prime_limit, int_limit, min_cents, max_cents)
-- - For subgroup search, subgroup is the first arg, there's no default value
--  for int limit (due to complexity of subgroups), and equave and min/max
--  cents default to 2/1, 0c, and 1200c respectively.
--   (subgroup, int_limit, equave)
--  (subgroup, int_limit, min_cents, max_cents)
-- - Filter ratios function is split into two:
--  - Filter ratios by complement removes ratios from a table if its complement
--    is missing. Complements are octave-complements by default.
--   - Filter ratios by tenney height removes ratios from a table if its tenney
--     height exceeds a passed-in value.
 
-- TODO: write filter function for cent range


-- INT_LIMIT_MAX is hardcoded to limit the size of output.
-- Module searches for ratios that are, at the minimum, up to an equave and are
-- 400 -> ~24000 ratios
-- up to some integer limit. Search hierarchy is as follows:
-- 300 -> ~14000 ratios
-- - Search by subgroup (subgroup elements may be nonprime or rational)
-- 250 -> ~9500 ratios
-- - Then search by prime limit
-- 200 -> ~6000 ratios
-- - Then search by odd limit
-- 150 -> ~3400 ratios
-- - Then search by int limit
-- 128 -> ~2500 ratios
 
-- 100 -> ~1500 ratios
-- Optional args omit ratios that don't meet certain conditions, and are used
local INT_LIMIT_MAX = 200
-- to further limit the number of ratios found. Current options include:
local DEFAULT_INT_LIMIT = 50
-- - Tenney Height: omits ratios that exceed some max Tenney height. Has no
--  effect if no Tenney height is passed in.
-- - Complements Only: omits ratios and their equave complements if either would
--   be omitted by Tenney height, or if no Tenney height is entered, omits
--   ratios whose complements are missing.
 
local DEFAULT_EQUAVE = rat.new(2)
local DEFAULT_INT_LIMIT = 30


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
----------------------- INT-LIMIT-BASED SEARCH FUNCTION ------------------------
------------------------------- FILTER FUNCTIONS -------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


-- Find JI ratios up to an integer limit within the octave by finding mediants.
-- Filter function removes certain ratios that don't meet some requirement.
-- A cent value can be passed in to either exclude ratios that are above an
-- Filters currently include:
-- interval below the octave or include ratios above the octave.
-- - Removing ratios that exceed a max Tenney height.
function p.search_by_int_limit(integer_limit, max_cents)
-- - Removing ratios whose complement would exceed a max Tenney height or int limit
local max_cents = max_cents or 1200
function p.filter_ratios(ratios, equave, int_limit, tenney_height, complements_only)
local integer_limit = integer_limit or DEFAULT_INT_LIMIT
local filtered_ratios = {}
for i = 1, #ratios do
local complement = rat.mul(rat.inv(ratios[i]), equave)
local ratio_th  = rat.tenney_height(ratios[i])
local compl_th  = rat.tenney_height(complement)
-- Are the ratios within the Tenney height?
-- Has no effect (defaults to TRUE) if Tenney height is infinity.
local ratio_within_th = ratio_th <= tenney_height
local compl_within_th = compl_th <= tenney_height
-- Is the ratio's complement within the int limit?
local compl_within_int_limit = rat.is_within_int_limit(complement, int_limit)
if complements_only then
if ratio_within_th and compl_within_th and compl_within_int_limit then
table.insert(filtered_ratios, ratios[i])
end
else
if ratio_within_th then
table.insert(filtered_ratios, ratios[i])
end
end
end
integer_limit = math.max(0, math.min(INT_LIMIT_MAX, integer_limit))
return filtered_ratios
end
 
-- Filters ratios from a table of ratios, returning an array of ratios within
-- the cent range and preserving the original table. Meant for searching for
-- multiple ranges. TODO: write
function p.filter_ratios_within_cent_range(ratios, min_cents, max_cents)
local init_ratios = {{1,1}, {2,1}}
end
local func = m.int_limit_search
 
local args = integer_limit
--------------------------------------------------------------------------------
local ratios = m.find_mediants_by_search_func(init_ratios, func, args)
-------------------------- INT-LIMIT SEARCH FUNCTION ---------------------------
--------------------------------------------------------------------------------
 
-- Int limit search finds ratios from 1/1 to an equave, where each ratio's
-- numerator or denominator don't exceed the int limit.
function p.search_by_int_limit(equave, int_limit)
return p.search_by_int_limit_within_cents(0, rat.cents(equave), int_limit)
end
 
-- Cent range search finds ratios within a cent range. Meant for searching for
-- ratios within a single interval range. If searching for ratios within many
-- interval ranges, then try a broad search first.
function p.search_by_int_limit_within_cents(min_cents, max_cents, int_limit)
-- If the max cents is greater than the octave, duplicate all existing
local init_ratios = {{1,1}, {1,0}}
-- ratios and raise them by the required number of octaves.
local ratios = med.find_only_mediants(init_ratios, 2)
if max_cents > 1200 then
for i = 3, int_limit do
local new_ratios = {}
ratios = med.find_mediants_by_int_limit(ratios, i)
local num_octaves_up = math.ceil(max_cents / 1200)
for j = 1, num_octaves_up do
-- Purge ratios from the beginning.
for i = 2, #ratios do
-- If the first and second ratio are smaller than min_cents, and smaller
local num = ratios[i][1] * math.pow(2, j)
-- than max_cents, then remove the first ratio. Keeping the first ratio
local den = ratios[i][2]
-- would add mediants outside the cent range.
local cents_1 = utils.log2(ratios[1][1] / ratios[1][2]) * 1200
local gcd = utils._gcd(num, den)
local cents_2 = utils.log2(ratios[2][1] / ratios[2][2]) * 1200
num = num / gcd
if cents_1 < min_cents and cents_2 <= min_cents and cents_1 < max_cents and cents_2 < max_cents then
den = den / gcd
table.remove(ratios, 1)
if math.max(num, den) <= integer_limit then
table.insert(new_ratios, {num, den})
end
end
end
end
for i = 1, #new_ratios do
-- Purge ratios from the end.
table.insert(ratios, new_ratios[i])
-- If the 2nd-last ratio and last ratio are greater than max_cents, and
-- larger than min_cents, then remove the last ratio. Keeping the last
-- ratio would add mediants outside the cent range.
local cents_3 = utils.log2(ratios[#ratios-1][1] / ratios[#ratios-1][2]) * 1200
local cents_4 = utils.log2(ratios[#ratios  ][1] / ratios[#ratios  ][2]) * 1200
if cents_3 > max_cents and cents_4 >= max_cents and cents_3 > min_cents and cents_4 > min_cents then
table.remove(ratios, #ratios)
end
end
end
end
-- Remove any ratios that exceed the max cents
-- Convert to ratios that Module:Rational can work with
-- Convert to ratios that Module:Rational can work with
for i = 1, #ratios do
for i = 1, #ratios do
ratios[i] = rat.new(ratios[i][1], ratios[i][2])
ratios[i] = rat.new(ratios[i][1], ratios[i][2])
end
-- Remove any remaining ratios that fall outside the cent range.
while rat.cents(ratios[1]) < min_cents do
table.remove(ratios, 1)
end
while rat.cents(ratios[#ratios]) > max_cents do
table.remove(ratios, #ratios)
end
end
Line 93: Line 162:


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------------ SUBGROUP-BASED SEARCH FUNCTION ------------------------
-------------------------- ODD-LIMIT SEARCH FUNCTION ---------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


-- Requires helper functions
-- Convert odd limit into equivalent subgroup.
function p.search_by_subgroup(subgroup, int_limit, max_cents)
-- EG, 11-odd-limit becomes 2.3.5.7.9.11
local subgroup = subgroup or { 2, 3, 7, 11 }
-- 2 is part of the subgroup by definition.
local int_limit = int_limit or 50
function p.odd_limit_to_subgroup(odd_limit)
local max_cents = max_cents or 1200
local subgroup = { rat.new(2) }
for i = 3, odd_limit, 2 do
table.insert(subgroup, rat.new(i))
end
return subgroup
end
 
function p.search_by_odd_limit(equave, int_limit, odd_limit)
local subgroup = p.odd_limit_to_subgroup(odd_limit)
return p.search_by_subgroup_within_cents(0, rat.cents(equave), int_limit, subgroup)
end
 
function p.search_by_odd_limit_within_cents(min_cents, max_cents, odd_limit)
local subgroup = p.odd_limit_to_subgroup(odd_limit)
return p.search_by_subgroup_within_cents(min_cents, max_cents, int_limit, subgroup)
end
end


-- Helper function
--------------------------------------------------------------------------------
-- Given a set of factors and a max product, find all values up to the max
------------------------- PRIME-LIMIT SEARCH FUNCTION --------------------------
-- product that is divisible by at least one of those primes.
--------------------------------------------------------------------------------
-- Factors are either prime or coprime, thus handling nonprime subgroups.
 
function p.find_products(factors, max_product)
-- Convert prime limit into equivalent subgroup.
local factors = factors or { 2, 3, 7, 11 }
-- EG, 11-prime-limit becomes 2.3.5.7.11
local max_product = max_product or 100
function p.prime_limit_to_subgroup(prime_limit)
local subgroup = {}
for i = 3, prime_limit do
local is_prime = true
for j = 2, math.floor(math.sqrt(i)) do
if i % j == 0 then
is_prime = false
break
end
end
if is_prime then
table.insert(subgroup, rat.new(i))
end
end
return subgroup
end


local products = {  }
-- Prime limit search finds ratios with prime factors that don't exceed some
-- prime limit.
-- Upper bounds for searching is the equave and int limit.
function p.search_by_prime_limit(equave, int_limit, prime_limit)
local subgroup = p.prime_limit_to_subgroup(prime_limit)
return p.search_by_subgroup_within_cents(0, rat.cents(equave), int_limit, subgroup)
end


-- A naive approach is to iterate through all numbers 2..max_product and in-
-- Prime limit search finds ratios with prime factors that don't exceed some
-- sert them in a table if it consists of at least one of the given factors.
-- prime limit. Searches within a cent range.
-- Using these numbers, make every possible pair from these numbers to
function p.search_by_prime_limit_within_cents(min_cents, max_cents, int_limit, prime_limit)
-- produce a table of ratios within the desired subgroup, given the factors
local subgroup = p.prime_limit_to_subgroup(prime_limit)
-- represent a subgroup. This is super inefficient as just the process of
local ratios = p.search_by_subgroup_within_cents(min_cents, max_cents, int_limit, subgroup)
-- producing products involves checking numbers that are produced by factors
while rat.cents(ratios[1]) < min_cents do
-- outside the set of desired factors, which is the vast majority of num-
table.remove(ratios, 1)
-- bers. A more targeted approach is to do the following:
-- - For each factor fi, calculate the largest power pi of that factor that
--   is just less than the max product. Keep track of these powers as a
--  vector of powers.
-- - For factors f1, f2, ... fn, and powers p1, p2, ... pn, represent the
--  potential powers as sets {0,1,...,p1}, {0,1,...,p2}, ..., {0,1,...,pn}.
--  The set of all possible vectors of powers is the cartesian product of
--  these sets.
-- - These vectors encode prime factorizations, and to reduce the quantity
--  of products, only permit products that are less than that of the max
--  product. Additionally, since these encode prime factorizations, the use
--  of negative exponents produces ratios for free!
-- - NOTE ABOUT VECTORS OF POWERS: these vectors may be thought of as monzos
--  but without the potential sparseness. EG, whereas [2 0 0 1 1> is a
--  shorthand for 308, in the context of the 2.7.11 subgroup, the lack of
--  3's and 5's makes the inclusion of these zeroes wasteful; hence the
--  factors are denoted as a vector {2,7,11} and the powers {2,1,1}.
--  Another way to think of these vectors is as a number where each
--  position is a different base; thus, producing every possible vector is
--  as simple as counting up, which is how these vectors are produced in
--  code.
local max_powers = {}
local curr_powers = {}
for i = 1, #factors do
local curr_factor = factors[i]
local curr_max_multiplier = math.floor(max_product/curr_factor)
local max_power = math.log(max_product) / math.log(curr_factor)
table.insert(max_powers, math.floor(max_power))
table.insert(curr_powers, 0)
end
end
return ratios
end
--------------------------------------------------------------------------------
---------------------------- SUBGROUP SEARCH FUNCTION --------------------------
--------------------------------------------------------------------------------
-- Subgroup search find ratios that are products of at least two non-unique
-- elements from the subgroup.
function p.search_by_subgroup(equave, int_limit, subgroup)
local ratios = p.search_by_subgroup_within_cents(0, rat.cents(equave), int_limit, subgroup)
return ratios
end
function p.search_by_subgroup_within_cents(min_cents, max_cents, int_limit, subgroup)
--local equave    = equave or rat.new(2,1) -- Defualt equave is 2/1.
--local int_limit = int_limit or 50 -- Default is 50
--local subgroup  = subgroup or {rat.new(2), rat.new(3), rat.new(7)} -- Default is 2.3.7 subgroup
-- Increment current powers one by one
-- Find all possible ways to multiply subgroup elements with one another
while curr_powers[#curr_powers] < max_powers[#max_powers] do
-- using breadth-first-search. Products found this way should not exceed the
curr_powers[1] = curr_powers[1] + 1
-- int limit, and if a subgroup element is rational, neither its numerator
-- nor denominator should exceed the int limit.
for i = 1, #factors - 1 do
local products = { rat.new(1) }
if curr_powers[i] > max_powers[i] then
local i = 1
curr_powers[i] = 0
while i <= #products do
curr_powers[i+1] = curr_powers[i+1] + 1
-- Multiply each subgroup element by the current ratio. The table of
-- product ratios created this way is merged with the running table of
-- ratios. This is the Cartesian product of the single ratio as a set,
-- with the subgroup elements as a set, or {p/q} X subgroup.
local new_products = {}
for j = 1, #subgroup do
local new_ratio = rat.mul(products[i], subgroup[j])
if rat.is_within_int_limit(new_ratio, int_limit) and not p.find_ratio_in_table(new_products, new_ratio) then
table.insert(new_products, new_ratio)
end
end
end
end
local curr_product = 1
-- Merge new products with the table of products, omitting duplicates.
for i = 1, #curr_powers do
p.merge_tables(products, new_products)
curr_product = curr_product * math.pow(factors[i], curr_powers[i])
i = i + 1
end
end
-- Sort for next step
table.sort(products, rat.lt)
-- Use the products found to find all ratios between 1 and the equave.
-- For each ratio in the table of products, create a set of new ratios by
-- having that ratio be the numerator and all successive ratios be possible
-- denominators. Store these new ratios in a table, and repeat with all
-- successive products, omitting duplicats. From earlier testing, this is
-- faster than performing BFS on each ratio, and yields the same results.
local ratios = {}
for i = 1, #products do
local new_ratios = {}
for j = i, #products do
local new_ratio = rat.div(products[j], products[i])
if rat.cents(new_ratio) > max_cents then break end
if curr_product <= max_product then
if not p.find_ratio_in_table(new_ratios, new_ratio) and rat.is_within_int_limit(new_ratio, int_limit) then
table.insert(products, curr_product)
table.insert(new_ratios, new_ratio)
end
end
end
-- Merge new ratios with the table of ratios, omitting duplicates.
p.merge_tables(ratios, new_ratios)
end
end
table.sort(products)
-- Sort
table.sort(ratios, rat.lt)
-- Remove ratios less than minimum
while rat.cents(ratios[1]) < min_cents do
table.remove(ratios, 1)
end
return max_powers
return ratios
end
 
--------------------------------------------------------------------------------
------------------------------- HELPER FUNCTIONS -------------------------------
--------------------------------------------------------------------------------
 
-- Heleper function; merges elements from source table with destination table
-- while disallowing duplicates.
function p.merge_tables(dest_table, source_table)
for i = 1, #source_table do
if not p.find_ratio_in_table(dest_table, source_table[i]) then
table.insert(dest_table, source_table[i])
end
end
end
end


-- Check whether a number is composed of the following factors.
-- Helper function for merge function.
function p.is_divisible_by_factors(number, factors)
function p.find_ratio_in_table(table_, ratio)
local test_number = number
local found = false
for i = 1, #factors do
for i = 1, #table_ do
local is_divisible = true
if rat.as_float(table_[i]) == rat.as_float(ratio) then
while test_number ~= 0 and is_divisible do
found = true
is_divisible = false
break
if test_number % factors[i] == 0 then
test_number = test_number / factors[i]
is_divisible = true
end
end
end
end
end
return found
return test_number == 1
end
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------------- PARAM-BASED SEARCH FUNCTIONS -------------------------
---------------------------- RATIO STRING FUNCTIONS ----------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


-- Search for ratios based on params passed in. Each param is its own
-- Convert a table of ratios into a string, with options for links and delimiter
-- function call. Params must be parsed first.
function p.ratios_as_string(ratios, add_links, delimiter)
function p.search_by_params(params, max_cents)
local add_links = add_links == true
local max_cents = max_cents or 1200
local delimiter = delimiter or ", "
-- First get ratios up to an int limit. If no int limit was passed in, it
local text = ""
-- will default to the hardcoded default value.
if #ratios ~= 0 then
local ratios = {}
text = add_links and string.format("[[%s]]", rat.as_ratio(ratios[1])) or rat.as_ratio(ratios[1])
if params["Int Limit"] ~= nil then
for i = 2, #ratios do
ratios = p.search_by_int_limit(params["Int Limit"], max_cents)
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
end
-- Convert a jagged array of ratios into an array of strings
function p.ratios_as_strings(ratios, add_links, delimiter)
local add_links = add_links == true
local delimiter = delimiter or ", "
if params["Prime Limit"] ~= nil then
local texts = {}
ratios = p.filter_by_prime_limit(ratios, params["Prime Limit"])
for i = 1, #ratios do
local text = p.ratios_as_string(ratios[i], add_links, delimiter)
table.insert(texts, text)
end
end
return texts
end
--------------------------------------------------------------------------------
---------------------------- ARG-PARSING FUNCTION ------------------------------
--------------------------------------------------------------------------------
-- Parse search args if entered as one string. Use is to be determined.
function p.parse_args(search_args)
local parsed = tip.parse_kv_pairs(search_args)
if params["Tenney Height"] ~= nil then
if parsed["Equave"] ~= nil then
ratios = p.filter_by_tenney_height(ratios, params["Tenney Height"])
parsed["Equave"] = rat.parse(parsed["Equave"])
end
end
return ratios
end
-- Parse search params.
function p.parse_search_params(search_params)
local parsed = tip.parse_kv_pairs(search_params)
if parsed["Int Limit"] ~= nil then
if parsed["Int Limit"] ~= nil then
Line 241: Line 386:
end
end
return parsed
if parsed["Subgroup"] ~= nil then
end
local subgroup_elements = tip.parse_numeric_pairs(parsed["Subgroup"], ".", "/", true)
 
for i = 1, #subgroup_elements do
function p.search_param_footnotes(search_params)
subgroup_elements[i] = rat.new(subgroup_elements[i][1], subgroup_elements[i][2])
local result = "Not all notable ratios may be shown, and other interpretations are possible."
end
parsed["Subgroup"] = subgroup_elements
end
if search_params["Prime Limit"] ~= nil then
if parsed["Complements Only"] ~= nil then
result = string.format("Ratios shown are within the [[%s-limit]]. %s", search_params["Prime Limit"], result)
parsed["Complements Only"] = yesno(parsed["Complements Only"])
elseif search_params["Int Limit"] ~= nil then
result = string.format("Ratios shown are %s-[[integer-limit|integer limit]]. %s", search_params["Int Limit"], result)
end
end
return result
return parsed
end
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
---------------------------- RATIO FILTER FUNCTIONS ----------------------------
----------------------------- INVOKABLE FUNCTIONS ------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


-- Filter ratios by Tenney height.
-- Function callable by other modules
function p.filter_by_tenney_height(ratios, tenney_height)
-- Ratios are returned as a table, for use with other modules.
local tenney_height = tenney_height or 10
function p._ji_ratios(args)
local filtered_ratios = {}
-- Args for ease of access
equave      = args["Equave"    ] or DEFAULT_EQUAVE
int_limit  = args["Int Limit"  ] or DEFAULT_INT_LIMIT
odd_limit  = args["Odd Limit"  ]
prime_limit = args["Prime Limit"]
subgroup    = args["Subgroup"  ]
-- Filtering args
tenney_height   = args["Tenney Height"  ] or 1/0 -- Default Tenney height is infinity
complements_only = args["Complements Only"] or false -- Default is to include all ratios
for i = 1, #ratios do
local ratios = {}
local curr_tenney_height = rat.tenney_height(ratios[i])
if subgroup ~= nil then
if curr_tenney_height <= tenney_height then
ratios = p.search_by_subgroup(equave, int_limit, subgroup)
table.insert(filtered_ratios, ratios[i])
elseif prime_limit ~= nil then
end
ratios = p.search_by_prime_limit(equave, int_limit, prime_limit)
elseif int_limit ~= nil then
ratios = p.search_by_int_limit(equave, int_limit)
end
end
return filtered_ratios
-- Filter ratios
ratios = p.filter_ratios(ratios, equave, int_limit, tenney_height, complements_only)
return ratios
end
end


-- Filter ratios by prime limit.
-- Invokable function; for templates
function p.filter_by_prime_limit(ratios, prime_limit)
-- Ratios are returned as a comma-delimited list. For finer control, it's
local prime_limit = prime_limit or 41
-- necessary to call the "main" function, then further process the results.
local filtered_ratios = {}
function p.ji_ratios(frame)
args = getArgs(frame)
-- Preprocess equave
-- Ratios are searched from 1/1 to some equave (default 2/1), so an equave
-- must be passed in.
args["Equave"] = args["Equave"] ~= nil and rat.parse(args["Equave"])
-- Preprocess int limit
-- Ratios are searched up to some int limit (default 50), so an int limit
-- must be passed in.
args["Int Limit"] = args["Int Limit"] ~= nil and tonumber(args["Int Limit"])
 
-- Preprocess Tenney height
if args["Tenney Height"] ~= nil then
args["Tenney Height"] = tonumber(args["Tenney Height"])
end
-- Preprocess prime limit
if args["Prime Limit"] ~= nil then
args["Prime Limit"] = tonumber(args["Prime Limit"])
end
for i = 1, #ratios do
-- Preprocess subgroup
local curr_max_prime = rat.max_prime(ratios[i])
if args["Subgroup"] ~= nil then
if curr_max_prime <= prime_limit then
local subgroup_elements = tip.parse_numeric_pairs(args["Subgroup"], ".", "/", true)
table.insert(filtered_ratios, ratios[i])
for i = 1, #subgroup_elements do
subgroup_elements[i] = rat.new(subgroup_elements[i][1], subgroup_elements[i][2])
end
end
args["Subgroup"] = subgroup_elements
end
end
return filtered_ratios
end
if args["Complements Only"] ~= nil then
args["Complements Only"] = yesno(args["Complements Only"], false)
end
-- Find and return ratios
local result = p.ratios_as_string(p._ji_ratios(args))
local debugg = yesno(frame.args["debug"])
if debugg == true then
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
end
return frame:preprocess(result)


-- Filter ratios by (prime) subgroup. EG: 2.3.5.7
function p.filter_by_subgroup(ratios, subgroup)
end
end


-- Filter ratios by rational/nonprime subgroup. EG, 2.7/2.11/2, or 2.5.7.9
function p.tester()
-- Does not support irrational subgroups.
--return p.ratios_as_string(p._ji_ratios(p.parse_args("Int Limit: 16; Equave: 3/1; Complements Only: 0")))
function p.filter_by_nonprime_subgroup(ratios, subgroup)
--return p.ratios_as_string(p.search_by_prime_limit_within_cents(372, 440, 17, 30))
return p.ratios_as_string(p.search_by_odd_limit(rat.new(2), 15, 15*2))
end
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------- RATIO SORTING FUNCTIONS ----------------------------
---------------------------- FUNCTIONS TO BE MOVED -----------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


-- Sorts ratios by closeness to cent values.
-- Parse a list of ratios from a string. String is formatted as follows:
-- "a/b; c/d; e/f; g/h"
function p.parse_ratios(unparsed)
local parsed = tip.parse_numeric_pairs(unparsed)
for i = 1, #parsed do
parsed[i] = rat.new(parsed[i][1], parsed[i][2])
end
return parsed
end
 
-- Sorts ratios by closeness to cent values. Move to new module?
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 tolerance = tolerance or 30
local tolerance = tolerance or 30
Line 330: Line 533:
return sorted_ratios
return sorted_ratios
end
--------------------------------------------------------------------------------
------------------------ RATIO PARSING/INPUT FUNCTIONS -------------------------
--------------------------------------------------------------------------------
-- Parse a list of ratios from a string. String is formatted as follows:
-- "a/b; c/d; e/f; g/h"
function p.parse_ratios(unparsed)
local parsed = tip.parse_numeric_pairs(unparsed)
for i = 1, #parsed do
parsed[i] = rat.new(parsed[i][1], parsed[i][2])
end
return parsed
end
--------------------------------------------------------------------------------
---------------------------- RATIO STRING FUNCTIONS ----------------------------
--------------------------------------------------------------------------------
-- Convert a table of ratios into a string, with options for links and delimiter
function p.ratios_as_text(ratios, add_links, delimiter)
local add_links = add_links == true
local delimiter = delimiter or ", "
local text = ""
if #ratios ~= 0 then
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
return text
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()
local params = p.parse_search_params("Int Limit: 30; Prime Limit: 17")
--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)
--return p.ratios_as_texts(ratios)
--local ratios = p.search_by_int_limit(250)
--return p.ratios_as_text(ratios) .. " " .. #ratios
-- Using these params with the naive search algorithm takes several seconds
-- to return only 1563 results: factors 2, 3, 7, 11; max product: 10 million
local factors = { 2, 3, 7, 11 }
local max_product = 20
return p.find_products(factors, max_product)
end
end


return p
return p