Module:JI ratios in ED: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
Ganaram inukshuk (talk | contribs)
Moved subgroup footnote to table title
Ganaram inukshuk (talk | contribs)
Added et template (as it can parse ets)
Line 4: Line 4:
local jiraf = require('Module:JI ratio finder')
local jiraf = require('Module:JI ratio finder')
local tip = require('Module:Template input parse')
local tip = require('Module:Template input parse')
local et = require('Module:ET')
local p = {}
local p = {}


function p.find_ratios_in_ed_by_primes(ed, primes, equave, tenney_height, denominator_limit)
-- Helper function
local ed = ed or 7
-- Converts prime limit to subgroup
local primes = primes or { 2, 3, 7, 11 }
-- EG, 7-limit becomes 2.3.5.7
local equave = equave or rat.new(3, 2)
function p.prime_limit_to_subgroup(prime_limit)
local prime_limit = prime_limit or 7
local subgroup = {}
for i = 2, prime_limit do
if utils.is_prime(i) then
table.insert(subgroup, i)
end
end
return subgroup
end
 
function p.subgroup_as_string(subgroup)
local subgroup = subgroup or { 2, 3, 5, 7 }
local str = ""
for i = 1, #subgroup do
if i ~= #subgroup then
str = str .. string.format("%d.", subgroup[i])
else
str = str .. subgroup[i]
end
end
return str
end
 
function p.find_ratios_in_ed(input_et, primes, tenney_height, denominator_limit)
local input_et = input_et or et.parse("12edo")
local primes = primes or { 2, 3, 5, 7 }
local tenney_height = tenney_height or 10
local tenney_height = tenney_height or 10
local denominator_limit = denominator_limit or 99
local denominator_limit = denominator_limit or 99
-- Get the number of divisions, equave, and et as text (eg edo, edt, etc)
local steps = input_et['size']
local equave = input_et['equave']
local et_as_string = et.as_string(input_et)
local equave_in_cents = rat.cents(equave)
local equave_in_cents = rat.cents(equave)
local tolerance = equave_in_cents / ed * 0.4
local tolerance = equave_in_cents / steps * 0.4
-- Temperament_interpretation
local subgroup_as_string = p.subgroup_as_string(primes)
-- Find candidate ratios; filter later
-- Find candidate ratios; filter later
local max_prime = primes[#primes]
local max_prime = primes[#primes]
local candidate_ratios = jiraf.find_candidate_ratios_within_subgroup(equave_in_cents, denominator_limit, primes)
local candidate_ratios = jiraf.find_candidate_ratios_within_subgroup(equave_in_cents, denominator_limit, primes)
-- Equave as text
local equave_as_text = rat.as_ratio(equave)
if equave_as_text == "2/1" then
equave_as_text = "edo"
elseif equave_as_text == "3/1" then
equave_as_text = "edt"
elseif equave_as_text == "3/2" then
equave_as_text = "edf"
end
-- Subgroup as text
local subgroup_as_text = ""
for i = 1, #primes do
if i ~= #primes then
subgroup_as_text = subgroup_as_text .. string.format("%d.", primes[i])
else
subgroup_as_text = subgroup_as_text .. primes[i]
end
end
-- Build table headers
-- Build table headers
local result = string.format('{| class="wikitable center-all"\n')
local result = string.format('{| class="wikitable center-all"\n')
result = result .. string.format('|+ Intervals of %d%s (as a %s subgroup temperament)\n', ed, equave_as_text, subgroup_as_text)
result = result .. string.format('|+ Intervals of %s (as a %s subgroup temperament)\n', et_as_string, subgroup_as_string)
result = result .. string.format('|-\n')
result = result .. string.format('|-\n')
result = result .. string.format('! rowspan="2" | [[Degree]]\n')
result = result .. string.format('! rowspan="2" | [[Degree]]\n')
Line 56: Line 73:
result = result .. string.format('|-\n')
result = result .. string.format('|-\n')
-- Build the rows for each edstep, showing ratios by limit
-- Build the rows for each step, showing ratios by limit
for i = 1, ed + 1 do
for i = 1, steps + 1 do
local edstep = i - 1
local step = i - 1
local edstep_in_cents = (edstep / ed) * equave_in_cents
local filtered_ratios = {}
-- Table headers
local step_in_cents = et.cents(input_et, step)
result = result .. string.format('| %d\n', step)
result = result .. string.format('| %.3f\n', step_in_cents)
-- Override ratios for edsteps for the unison and equave
-- Override ratios for steps for the unison and equave
-- Otherwise, filter ratios by range
-- Otherwise, filter ratios by range
local filtered_ratios = {}
if i == 1 then
if i == 1 then
filtered_ratios = { rat.new(1, 1) }
filtered_ratios = { rat.new(1, 1) }
elseif i == ed + 1 then
elseif i == steps + 1 then
filtered_ratios = { rat.copy(equave) }
filtered_ratios = { equave }
else
else
filtered_ratios = jiraf.filter_ratios_by_range(candidate_ratios, edstep_in_cents - tolerance, edstep_in_cents + tolerance)
filtered_ratios = jiraf.filter_ratios_by_range(candidate_ratios, step_in_cents - tolerance, step_in_cents + tolerance)
end
end
result = result .. string.format('| %d\n', edstep)
result = result .. string.format('| %.3f\n', edstep_in_cents)
-- Add ratios according to harmonic class
-- Add ratios according to harmonic class
Line 80: Line 98:
local prime_filtered_ratios = {}
local prime_filtered_ratios = {}
-- The first column filters by prime limit
-- Filter ratios by harmonic class
-- Successive columns are filered by harmonic class
prime_filtered_ratios = jiraf.filter_ratios_by_harmonic_class(filtered_ratios, current_prime)
if j == 1 then
prime_filtered_ratios = jiraf.filter_ratios_by_prime_limit(filtered_ratios, current_prime)
else
prime_filtered_ratios = jiraf.filter_ratios_by_harmonic_class(filtered_ratios, current_prime)
end
-- Filter ratios by complement-agnostic tenney height
-- Filter ratios by complement-agnostic tenney height
Line 92: Line 105:
-- Add ratios to cells
-- Add ratios to cells
local ratios_as_text = jiraf.ratios_to_text_with_error(prime_filtered_ratios, edstep_in_cents, "<br>", true)
local ratios_as_text = jiraf.ratios_to_text_with_error(prime_filtered_ratios, step_in_cents, "<br>", true)
result = result .. string.format('| %s\n', ratios_as_text)
result = result .. string.format('| %s\n', ratios_as_text)
end
end
Line 100: Line 113:
result = result .. string.format('|}\n')
result = result .. string.format('|}\n')
return result
return result
end
end


function p.parse_ed(unparsed)
function p.ji_ratios_in_ed_frame(frame)
local unparsed = unparsed or "12edo"
local ed = tonumber(unparsed) or unparsed:match('(%d+)ed')
 
return ed
end
 
function p.parse_equave(unparsed)
local unparsed = unparsed or "10ed4"
local equave_unparsed = ""
local input_et = et.parse(frame.args["ED"])
if string.match(unparsed, "edo") or tonumber(unparsed) ~= nil then
if tonumber(et) ~= nil then
equave_unparsed = "2/1"
input_et = input_et .. "edo"
elseif string.match(unparsed, "edt") then
equave_unparsed = "3/1"
elseif string.match(unparsed, "edf") then
equave_unparsed = "3/2"
else
equave_unparsed = unparsed:match('[%d]+ed([%d]+/[%d]+)') or unparsed:match('[%d]+ed([%d]+)')
end
end
return rat.parse(equave_unparsed)
end
function p.ji_ratios_in_ed_frame(frame)
local ed = p.parse_ed(frame.args["ED"])
local equave = p.parse_equave(frame.args["ED"])


local primes_as_text = "2.3.5.7.11.13"
local primes_as_text = "2.3.5.7.11.13"
Line 143: Line 132:
local denominator_limit = tonumber(frame.args["Denominator Limit"]) or 99
local denominator_limit = tonumber(frame.args["Denominator Limit"]) or 99
local result = p.find_ratios_in_ed_by_primes(ed, primes, equave, tenney_height, denominator_limit)
local result = p.find_ratios_in_ed(input_et, primes, tenney_height, denominator_limit)
return result
return result
end
end


return p
return p

Revision as of 03:43, 25 January 2024

Module documentation[view] [edit] [history] [purge]
This module should not be invoked directly; use its corresponding template instead: Template:JI ratios in ED.
Module:JI ratios in ED is deprecated and has no replacement. Further use of this module is not advised. This module is kept for historical purposes and should not be deleted.
Introspection summary for Module:JI ratios in ED 
Functions provided (4)
Line Function Params
12 prime_limit_to_subgroup (prime_limit)
24 subgroup_as_string (subgroup)
38 find_ratios_in_ed (input_et, primes, tenney_height, denominator_limit)
118 ji_ratios_in_ed_frame (invokable) (frame)
Lua modules required (6)
Variable Module Functions used
et Module:ET parse
as_string
cents
interval Module:Interval dependency not used
jiraf Module:JI ratio finder find_candidate_ratios_within_subgroup
filter_ratios_by_range
filter_ratios_by_harmonic_class
filter_ratios_by_complement_agnostic_tenney_height
ratios_to_text_with_error
rat Module:Rational cents
new
tip Module:Template input parse parse_numeric_entries
utils Module:Utils is_prime

No function descriptions were provided. The Lua code may have further information.


local utils = require('Module:Utils')
local interval = require('Module:Interval')
local rat = require('Module:Rational')
local jiraf = require('Module:JI ratio finder')
local tip = require('Module:Template input parse')
local et = require('Module:ET')
local p = {}

-- Helper function
-- Converts prime limit to subgroup
-- EG, 7-limit becomes 2.3.5.7
function p.prime_limit_to_subgroup(prime_limit)
	local prime_limit = prime_limit or 7
	
	local subgroup = {}
	for i = 2, prime_limit do
		if utils.is_prime(i) then
			table.insert(subgroup, i)
		end
	end
	return subgroup
end

function p.subgroup_as_string(subgroup)
	local subgroup = subgroup or { 2, 3, 5, 7 }
	
	local str = ""
	for i = 1, #subgroup do
		if i ~= #subgroup then
			str = str .. string.format("%d.", subgroup[i])
		else
			str = str .. subgroup[i]
		end
	end
	return str
end

function p.find_ratios_in_ed(input_et, primes, tenney_height, denominator_limit)
	local input_et = input_et or et.parse("12edo")
	local primes = primes or { 2, 3, 5, 7 }
	local tenney_height = tenney_height or 10
	local denominator_limit = denominator_limit or 99
	
	-- Get the number of divisions, equave, and et as text (eg edo, edt, etc)
	local steps = input_et['size']
	local equave = input_et['equave']
	local et_as_string = et.as_string(input_et)
	
	local equave_in_cents = rat.cents(equave)
	local tolerance = equave_in_cents / steps * 0.4
	
	-- Temperament_interpretation
	local subgroup_as_string = p.subgroup_as_string(primes)
	
	-- Find candidate ratios; filter later
	local max_prime = primes[#primes]
	local candidate_ratios = jiraf.find_candidate_ratios_within_subgroup(equave_in_cents, denominator_limit, primes)
	
	-- Build table headers
	local result = string.format('{| class="wikitable center-all"\n')
	result = result .. string.format('|+ Intervals of %s (as a %s subgroup temperament)\n', et_as_string, subgroup_as_string)
	result = result .. string.format('|-\n')
	result = result .. string.format('! rowspan="2" | [[Degree]]\n')
	result = result .. string.format('! rowspan="2" | [[Cent]]s\n')
	result = result .. string.format('! colspan="%d" | Approximated [[JI]] intervals ([[error]] in cents)\n', #primes)
	result = result .. string.format('|-\n')
	
	-- Add table headers for prime limits (technically harmonic classes)
	for i = 1, #primes do
		local current_prime = primes[i]	
		result = result .. string.format('! [[%d-limit]]\n', current_prime)
	end
	result = result .. string.format('|-\n')
	
	-- Build the rows for each step, showing ratios by limit
	for i = 1, steps + 1 do
		local step = i - 1
		
		-- Table headers
		local step_in_cents = et.cents(input_et, step)
		result = result .. string.format('| %d\n', step)
		result = result .. string.format('| %.3f\n', step_in_cents)
		
		-- Override ratios for steps for the unison and equave
		-- Otherwise, filter ratios by range
		local filtered_ratios = {}
		if i == 1 then
			filtered_ratios = { rat.new(1, 1) }
		elseif i == steps + 1 then
			filtered_ratios = { equave }
		else
			filtered_ratios = jiraf.filter_ratios_by_range(candidate_ratios, step_in_cents - tolerance, step_in_cents + tolerance)
		end
		
		-- Add ratios according to harmonic class
		for j = 1, #primes do
			local current_prime = primes[j]
			local prime_filtered_ratios = {}
			
			-- Filter ratios by harmonic class
			prime_filtered_ratios = jiraf.filter_ratios_by_harmonic_class(filtered_ratios, current_prime)
			
			-- Filter ratios by complement-agnostic tenney height
			prime_filtered_ratios = jiraf.filter_ratios_by_complement_agnostic_tenney_height(prime_filtered_ratios, tenney_height, equave)
			
			-- Add ratios to cells
			local ratios_as_text = jiraf.ratios_to_text_with_error(prime_filtered_ratios, step_in_cents, "<br>", true)
			result = result .. string.format('| %s\n', ratios_as_text)
		end
		
		result = result .. string.format('|-\n')
	end
	
	result = result .. string.format('|}\n')
	return result
end

function p.ji_ratios_in_ed_frame(frame)
	
	local input_et = et.parse(frame.args["ED"])
	if tonumber(et) ~= nil then
		input_et = input_et .. "edo"
	end

	local primes_as_text = "2.3.5.7.11.13"
	if string.len(frame.args["Primes"]) > 0 then
		primes_as_text = frame.args["Primes"]
	end
	local primes = tip.parse_numeric_entries(primes_as_text, '.') or tip.parse_numeric_entries(primes_as_text, ',')
	
	local tenney_height = tonumber(frame.args["Tenney Height"]) or 10
	local denominator_limit = tonumber(frame.args["Denominator Limit"]) or 99
	
	local result = p.find_ratios_in_ed(input_et, primes, tenney_height, denominator_limit)
	
	return result
end

return p