Module:JI ratios: Difference between revisions

Ganaram inukshuk (talk | contribs)
cleanup before rewriting
Ganaram inukshuk (talk | contribs)
reorganize
Line 35: Line 35:


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------------------ HELPER FUNCTIONS --------------------------------
------------------------------- FILTER FUNCTIONS -------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


-- Does not return anything; entries in source are added to destination.
-- Filter functions remove certain ratios that don't meet some requirement.
function p.merge_ratio_tables_without_duplicates(dest_table, source_table)
-- Filters currently include:
for i = 1, #source_table do
-- - Removing ratios that exceed a max Tenney height.
if not p.find_ratio_in_table(dest_table, source_table[i]) then
-- - Removing ratios whose complement would exceed a max Tenney height.
table.insert(dest_table, source_table[i])
end
end
end
 
-- Checks whether a ratio was already in a table
function p.find_ratio_in_table(table_, ratio)
local found = false
for i = 1, #table_ do
if rat.as_float(table_[i]) == rat.as_float(ratio) then
found = true
break
end
end
return found
end
 
--------------------------------------------------------------------------------
--------------------------- FILTER/SORTING FUNCTIONS ---------------------------
--------------------------------------------------------------------------------


-- Remove ratios whose complements exceed the int limit and Tenney height.
-- Remove ratios whose complements exceed the int limit and Tenney height.
Line 91: Line 71:
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


Line 155: Line 105:
return ratios
return ratios
end
end
--------------------------------------------------------------------------------
-------------------------- ODD-LIMIT SEARCH FUNCTION ---------------------------
--------------------------------------------------------------------------------
-- to be implemented


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Line 256: Line 212:
end
end


--------------------------------------------------------------------------------
-- Heleper function; merges tables while disallowing duplicates
--------------------------- REVERSE-SEARCH FUNCTIONS ---------------------------
function p.merge_ratio_tables_without_duplicates(dest_table, source_table)
--------------------------------------------------------------------------------
for i = 1, #source_table do
--[[
if not p.find_ratio_in_table(dest_table, source_table[i]) then
function p.int_limit_of_ratios(ratios)
table.insert(dest_table, source_table[i])
end
end
end
end


function p.prime_limit_of_ratios(ratios)
-- Helper function for table merge function
function p.find_ratio_in_table(table_, ratio)
local found = false
for i = 1, #table_ do
if rat.as_float(table_[i]) == rat.as_float(ratio) then
found = true
break
end
end
return found
end
end


function p.subgroup_of_ratios(ratios, is_nonprime)
end
]]--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-------------------------- ARG-BASED SEARCH FUNCTIONS --------------------------
-------------------------- ARG-BASED SEARCH FUNCTIONS --------------------------
Line 328: Line 286:
end
end
return parsed
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
return parsed
end
end
Line 421: Line 365:
return p.ratios_as_string(ratios)
return p.ratios_as_string(ratios)
end
end
--------------------------------------------------------------------------------
---------------------------- FUNCTIONS TO BE MOVED -----------------------------
--------------------------------------------------------------------------------
-- 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)
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


return p
return p