Module:Infobox ET: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
Don't conduct prime test in manual input mode
Sintel (talk | contribs)
merge changes from dev
Line 1: Line 1:
local p = {}
local p = {}
local i = require('Module:Interval')
local utils = require("Module:Utils")
local u = require('Module:Utils')
local rat = require("Module:Rational")
local rat = require('Module:Rational')
local limits = require("Module:Limits")
local l = require('Module:Limits')
local ET = require("Module:ET")
local ET = require('Module:ET')
local infobox = require("Module:Infobox")
local infobox = require('Module:Infobox')


-- check whether the input is a non-empty string
-- check whether the input is a non-empty string
local function value_provided(s)
local function value_provided(s)
return type(s) == 'string' and #s > 0
return type(s) == "string" and #s > 0
end
end


Line 15: Line 14:
local function approximation(et, interval, towards, precomputed_approx)
local function approximation(et, interval, towards, precomputed_approx)
local approx = precomputed_approx or ET.approximate(et, interval, towards or 0)
local approx = precomputed_approx or ET.approximate(et, interval, towards or 0)
 
local tuning = et.size
local tuning = et.size
if not rat.eq(et.equave, 2) then
if not rat.eq(et.equave, 2) then
tuning = tuning .. et.suffix
tuning = tuning .. et.suffix
end
end
 
local ratio = rat.new(approx, et.size)
local ratio = rat.new(approx, et.size)
 
local convergement_notice = ''
local convergement_notice = ""
local converges = rat.converges(ratio, math.log(interval) / math.log(rat.as_float(et.equave)))
local converges = rat.converges(ratio, math.log(interval) / math.log(rat.as_float(et.equave)))
if converges then
if converges then
convergement_notice = '<br/>(' .. converges .. ')'
convergement_notice = "<br/>(" .. converges .. ")"
end
end
 
if rat.as_table(ratio)[1] ~= approx then
if rat.as_table(ratio)[1] ~= approx then
convergement_notice = ''
convergement_notice = ""
local link = rat.as_table(ratio)[2] .. et.suffix
local link = rat.as_table(ratio)[2] .. et.suffix
ratio = ' (→[[' .. link .. '|' .. rat.as_ratio(ratio, '\\')
ratio = " (→[[" .. link .. "|" .. rat.as_ratio(ratio, "\\")
if not rat.eq(et.equave, 2) then
if not rat.eq(et.equave, 2) then
ratio = ratio .. et.suffix
ratio = ratio .. et.suffix
end
end
ratio = ratio .. ']])'
ratio = ratio .. "]])"
else
else
ratio = ''
ratio = ""
end
end
 
local cents = u._round(ET.cents(et, approx), 6)
local cents = utils._round(ET.cents(et, approx), 6)
 
return approx .. '\\' .. tuning .. ' (' .. cents .. '¢)' .. ratio .. convergement_notice
return approx .. "\\" .. tuning .. " (" .. cents .. "¢)" .. ratio .. convergement_notice
end
end


function p.infobox_ET(frame)
function p.infobox_ET(frame)
-- debug mode will disable the categories
-- debug mode will disable the categories
local debug_mode = frame.args['debug']
local debug_mode = frame.args["debug"]
local categories = ''
local categories = ""
 
local tuning = frame.args['tuning']
local tuning = frame.args["tuning"]
local et = ET.parse(tuning) or ET.parse('12edo')
local et = ET.parse(tuning) or ET.parse("12edo")
 
-- category of the main article
-- category of the main article
categories = categories .. '[[Category:' .. tuning .. '| ]]'
categories = categories .. "[[Category:" .. tuning .. "| ]]"
-- category of the equal division
-- category of the equal division
if rat.eq(et.equave, 2) then
if rat.eq(et.equave, 2) then
categories = categories .. '[[Category:Equal divisions of the octave|' .. string.rep ('#', string.len (et.size)) .. ']]'
categories = categories
.. "[[Category:Equal divisions of the octave|"
.. string.rep("#", string.len(et.size))
.. "]]"
else
else
categories = categories .. '[[Category:' .. et.suffix .. '|' .. string.rep ('#', string.len (et.size)) .. ']]'
categories = categories .. "[[Category:" .. et.suffix .. "|" .. string.rep("#", string.len(et.size)) .. "]]"
end
end


-- prime factorization
-- prime factorization
local prime_factorization_override = frame.args['Prime factorization']
local prime_factorization_override = frame.args["Prime factorization"]
local prime_factorization = ""
local prime_factorization
if not value_provided(prime_factorization_override) then
if not value_provided(prime_factorization_override) then
prime_factorization = u._prime_factorization(et.size)
prime_factorization = utils._prime_factorization(et.size)
if u.is_prime(et.size) then
if utils.is_prime(et.size) then
prime_factorization = prime_factorization .. " (prime)"
prime_factorization = prime_factorization .. " (prime)"
if rat.eq(et.equave, 2) then
if rat.eq(et.equave, 2) then
categories = categories .. '[[Category:Prime EDO|' .. string.rep ('#', string.len (et.size)) .. ']]'
categories = categories .. "[[Category:Prime EDO|" .. string.rep("#", string.len(et.size)) .. "]]"
end
end
end
end
Line 79: Line 81:


-- zeta test
-- zeta test
local zeta_override = frame.args['Zeta']
local zeta_override = frame.args["Zeta"]
local zeta_switch = nil
local zeta_switch
if value_provided(zeta_override) then
if value_provided(zeta_override) then
zeta_switch = not zeta_override:match('^[Nn][Oo]$')
zeta_switch = not zeta_override:match("^[Nn][Oo]$")
else
else
zeta_switch = rat.eq(et.equave, 2) and ET.is_zeta(et)
zeta_switch = rat.eq(et.equave, 2) and ET.is_zeta(et)
Line 89: Line 91:
-- navigation arrows
-- navigation arrows
local increment = 1
local increment = 1
if rat.eq(et.equave, 9/4) or rat.eq(et.equave, 4) or rat.eq(et.equave, 9) then
if rat.eq(et.equave, 9 / 4) or rat.eq(et.equave, 4) or rat.eq(et.equave, 9) then
increment = 2
increment = 2
end
end
local prev_one = ''
local prev_one = ""
if et.size >= increment then
if et.size >= increment then
prev_one = '[[' .. (et.size - increment) .. et.suffix .. '|← ' .. (et.size - increment) .. et.suffix .. ']]'
prev_one = "[[" .. (et.size - increment) .. et.suffix .. "|← " .. (et.size - increment) .. et.suffix .. "]]"
end
end
local next_one = '[[' .. (et.size + increment) .. et.suffix .. '|' .. (et.size + increment) .. et.suffix .. ' →]]'
local next_one = "[[" .. (et.size + increment) .. et.suffix .. "|" .. (et.size + increment) .. et.suffix .. " →]]"


-- step size
-- step size
local step_size = ET.cents(et, 1)
local step_size = ET.cents(et, 1)
local note_12edo = ''
local note_12edo = ""
if rat.eq(et.equave, 2) and et.size == 12 then
if rat.eq(et.equave, 2) and et.size == 12 then
note_12edo = '<sup>by definition</sup>'
note_12edo = "<sup>by definition</sup>"
end
end


-- octave, twelfth, and fifth sizes
-- octave, twelfth, and fifth sizes
local octave = ET.approximate(et, 2)
local octave = ET.approximate(et, 2)
local twelfth = ET.approximate(et, 3)
local twelfth = ET.approximate(et, 3)
 
local fifth = -octave + twelfth -- 3/2 = [-1 1>
local fifth = -octave + twelfth -- 3/2 = [-1 1>
local fifth_error = ET.cents(et, fifth) - i._to_cents(3/2)
local fifth_error = ET.cents(et, fifth) - rat.cents(rat.new(3, 2))
local dual_fifth = math.abs(fifth_error) > step_size / 3
local dual_fifth = math.abs(fifth_error) > step_size / 3


local A1 = -11 * octave + 7 * twelfth -- 2187/2048 = [-11 7>
local A1 = -11 * octave + 7 * twelfth -- 2187/2048 = [-11 7>
local m2 = 8 * octave - 5 * twelfth -- 256/243 = [8 -5>
local m2 = 8 * octave - 5 * twelfth -- 256/243 = [8 -5>
local A1_cents = u._round(ET.cents(et, A1), 4)
local A1_cents = utils._round(ET.cents(et, A1), 4)
local m2_cents = u._round(ET.cents(et, m2), 4)
local m2_cents = utils._round(ET.cents(et, m2), 4)


-- display
-- display
Line 122: Line 124:


table.insert(infobox_data, {
table.insert(infobox_data, {
    'Prime factorization',
"Prime factorization",
    prime_factorization
prime_factorization,
    })
})


table.insert(infobox_data, {
table.insert(infobox_data, {
'Step size',
"Step size",
u._round(step_size, 6) .. '¢' .. note_12edo
utils._round(step_size, 6) .. "¢" .. note_12edo,
})
})


if not rat.eq(et.equave, 2) then
if not rat.eq(et.equave, 2) then
table.insert(infobox_data, {
table.insert(infobox_data, {
'Octave',
"Octave",
approximation(et, 2)
approximation(et, 2),
})
})
if not rat.eq(et.equave, 3) then
if not rat.eq(et.equave, 3) then
table.insert(infobox_data, {
table.insert(infobox_data, {
'Twelfth',
"Twelfth",
approximation(et, 3)
approximation(et, 3),
})
})
end
end
else
else
table.insert(infobox_data, {
table.insert(infobox_data, {
'Fifth',
"Fifth",
approximation(et, 3/2)
approximation(et, 3 / 2),
})
})
table.insert(infobox_data, {
table.insert(infobox_data, {
'Semitones (A1:m2)',
"Semitones (A1:m2)",
A1 .. ':' .. m2 .. ' (' .. A1_cents .. '¢ : ' .. m2_cents .. '¢)'
A1 .. ":" .. m2 .. " (" .. A1_cents .. "¢ : " .. m2_cents .. "¢)",
})
})
if dual_fifth and et.size > 0 then
if dual_fifth and et.size > 0 then
table.insert(infobox_data, {
table.insert(infobox_data, {
'Dual sharp fifth',
"Dual sharp fifth",
approximation(et, 3/2, 1)
approximation(et, 3 / 2, 1),
})
})
table.insert(infobox_data, {
table.insert(infobox_data, {
'Dual flat fifth',
"Dual flat fifth",
approximation(et, 3/2, -1)
approximation(et, 3 / 2, -1),
})
})
local sharp = ET.approximate(et, 3/2, 1)
local sharp = ET.approximate(et, 3 / 2, 1)
local flat = ET.approximate(et, 3/2, -1)
local flat = ET.approximate(et, 3 / 2, -1)
table.insert(infobox_data, {
table.insert(infobox_data, {
'Dual major 2nd',
"Dual major 2nd",
approximation(et, 9/8, 0, sharp + flat - octave)
approximation(et, 9 / 8, 0, sharp + flat - octave),
})
})
categories = categories .. '[[Category:Dual-fifth temperaments|' .. string.rep ('#', string.len (et.size)) .. ']]'
categories = categories
.. "[[Category:Dual-fifth temperaments|"
.. string.rep("#", string.len(et.size))
.. "]]"
end
end
end
end


local consistency = tonumber(frame.args['Consistency'])
local consistency = tonumber(frame.args["Consistency"])
if consistency == nil then
if consistency == nil then
consistency = l.consistency_limit(et, false, 43)
consistency = limits.consistency_limit(et, false, 43)
end
end
if consistency == nil then
if consistency == nil then
consistency = 'at least 43'
consistency = "at least 43"
end
end
if consistency ~= nil then
if consistency ~= nil then
table.insert(infobox_data, {
table.insert(infobox_data, {
'Consistency limit',
"Consistency limit",
consistency
consistency,
})
})
end
end
local distinct_consistency = tonumber(frame.args['Distinct consistency'])
local distinct_consistency = tonumber(frame.args["Distinct consistency"])
if distinct_consistency == nil then
if distinct_consistency == nil then
distinct_consistency = l.consistency_limit(et, consistency or true, 43)
distinct_consistency = limits.consistency_limit(et, consistency or true, 43)
end
end
if distinct_consistency == nil then
if distinct_consistency == nil then
distinct_consistency = 'at least 43'
distinct_consistency = "at least 43"
end
end
if distinct_consistency ~= nil then
if distinct_consistency ~= nil then
table.insert(infobox_data, {
table.insert(infobox_data, {
'Distinct consistency limit',
"Distinct consistency limit",
distinct_consistency
distinct_consistency,
})
})
end
end
Line 199: Line 204:
-- special properties
-- special properties
if ET.is_highly_composite(et) or zeta_switch then
if ET.is_highly_composite(et) or zeta_switch then
local text = ''
local text = ""
if ET.is_highly_composite(et) then
if ET.is_highly_composite(et) then
text = text .. '[[Highly composite equal division|highly composite]]'
text = text .. "[[Highly composite equal division|highly composite]]"
if rat.eq(et.equave, 2) then
if rat.eq(et.equave, 2) then
categories = categories .. '[[Category:Highly composite EDO|' .. string.rep ('#', string.len (et.size)) .. ']]'
categories = categories
.. "[[Category:Highly composite EDO|"
.. string.rep("#", string.len(et.size))
.. "]]"
end
end
end
end
if zeta_switch then
if zeta_switch then
if #text > 0 then text = text .. '<br>' end
if #text > 0 then
text = text .. "<br>"
end
if not value_provided(zeta_override) then
if not value_provided(zeta_override) then
text = text .. ET.why_zeta(et)
text = text .. ET.why_zeta(et)
Line 213: Line 223:
text = text .. zeta_override
text = text .. zeta_override
end
end
categories = categories .. '[[Category:Zeta|' .. string.rep ('#', string.len (et.size)) .. ']]'
categories = categories .. "[[Category:Zeta|" .. string.rep("#", string.len(et.size)) .. "]]"
end
end
table.insert(infobox_data, {
table.insert(infobox_data, {
'Special properties',
"Special properties",
'<div style="max-width: 270px;">' .. text .. '</div>'
'<div style="max-width: 270px;">' .. text .. "</div>",
})
})
end
end


result = infobox.build(
local result = infobox.build("[[" .. et.suffix .. "|" .. tuning .. "]]", infobox_data, prev_one, next_one)
'[[' .. et.suffix .. '|' .. tuning .. ']]',
infobox_data,
prev_one,
next_one
)
if not value_provided(debug_mode) then
if not value_provided(debug_mode) then
result = result .. categories
result = result .. categories

Revision as of 20:20, 1 April 2024

Module documentation[view] [edit] [history] [purge]
Note: Do not invoke this module directly; use the corresponding template instead: Template:Infobox ET.

This module automatically fills in information about a specified equal temperament tuning.


local p = {}
local utils = require("Module:Utils")
local rat = require("Module:Rational")
local limits = require("Module:Limits")
local ET = require("Module:ET")
local infobox = require("Module:Infobox")

-- check whether the input is a non-empty string
local function value_provided(s)
	return type(s) == "string" and #s > 0
end

-- towards is one of: -1 (floor), 0 (nearest), 1 (ceil)
local function approximation(et, interval, towards, precomputed_approx)
	local approx = precomputed_approx or ET.approximate(et, interval, towards or 0)

	local tuning = et.size
	if not rat.eq(et.equave, 2) then
		tuning = tuning .. et.suffix
	end

	local ratio = rat.new(approx, et.size)

	local convergement_notice = ""
	local converges = rat.converges(ratio, math.log(interval) / math.log(rat.as_float(et.equave)))
	if converges then
		convergement_notice = "<br/>(" .. converges .. ")"
	end

	if rat.as_table(ratio)[1] ~= approx then
		convergement_notice = ""
		local link = rat.as_table(ratio)[2] .. et.suffix
		ratio = " (→[[" .. link .. "|" .. rat.as_ratio(ratio, "\\")
		if not rat.eq(et.equave, 2) then
			ratio = ratio .. et.suffix
		end
		ratio = ratio .. "]])"
	else
		ratio = ""
	end

	local cents = utils._round(ET.cents(et, approx), 6)

	return approx .. "\\" .. tuning .. " (" .. cents .. "¢)" .. ratio .. convergement_notice
end

function p.infobox_ET(frame)
	-- debug mode will disable the categories
	local debug_mode = frame.args["debug"]
	local categories = ""

	local tuning = frame.args["tuning"]
	local et = ET.parse(tuning) or ET.parse("12edo")

	-- category of the main article
	categories = categories .. "[[Category:" .. tuning .. "| ]]"
	-- category of the equal division
	if rat.eq(et.equave, 2) then
		categories = categories
			.. "[[Category:Equal divisions of the octave|"
			.. string.rep("#", string.len(et.size))
			.. "]]"
	else
		categories = categories .. "[[Category:" .. et.suffix .. "|" .. string.rep("#", string.len(et.size)) .. "]]"
	end

	-- prime factorization
	local prime_factorization_override = frame.args["Prime factorization"]
	local prime_factorization
	if not value_provided(prime_factorization_override) then
		prime_factorization = utils._prime_factorization(et.size)
		if utils.is_prime(et.size) then
			prime_factorization = prime_factorization .. " (prime)"
			if rat.eq(et.equave, 2) then
				categories = categories .. "[[Category:Prime EDO|" .. string.rep("#", string.len(et.size)) .. "]]"
			end
		end
	else
		prime_factorization = prime_factorization_override
	end

	-- zeta test
	local zeta_override = frame.args["Zeta"]
	local zeta_switch
	if value_provided(zeta_override) then
		zeta_switch = not zeta_override:match("^[Nn][Oo]$")
	else
		zeta_switch = rat.eq(et.equave, 2) and ET.is_zeta(et)
	end

	-- navigation arrows
	local increment = 1
	if rat.eq(et.equave, 9 / 4) or rat.eq(et.equave, 4) or rat.eq(et.equave, 9) then
		increment = 2
	end
	local prev_one = ""
	if et.size >= increment then
		prev_one = "[[" .. (et.size - increment) .. et.suffix .. "|← " .. (et.size - increment) .. et.suffix .. "]]"
	end
	local next_one = "[[" .. (et.size + increment) .. et.suffix .. "|" .. (et.size + increment) .. et.suffix .. " →]]"

	-- step size
	local step_size = ET.cents(et, 1)
	local note_12edo = ""
	if rat.eq(et.equave, 2) and et.size == 12 then
		note_12edo = "<sup>by definition</sup>"
	end

	-- octave, twelfth, and fifth sizes
	local octave = ET.approximate(et, 2)
	local twelfth = ET.approximate(et, 3)

	local fifth = -octave + twelfth -- 3/2 = [-1 1>
	local fifth_error = ET.cents(et, fifth) - rat.cents(rat.new(3, 2))
	local dual_fifth = math.abs(fifth_error) > step_size / 3

	local A1 = -11 * octave + 7 * twelfth -- 2187/2048 = [-11 7>
	local m2 = 8 * octave - 5 * twelfth -- 256/243 = [8 -5>
	local A1_cents = utils._round(ET.cents(et, A1), 4)
	local m2_cents = utils._round(ET.cents(et, m2), 4)

	-- display
	local infobox_data = {}

	table.insert(infobox_data, {
		"Prime factorization",
		prime_factorization,
	})

	table.insert(infobox_data, {
		"Step size",
		utils._round(step_size, 6) .. "¢" .. note_12edo,
	})

	if not rat.eq(et.equave, 2) then
		table.insert(infobox_data, {
			"Octave",
			approximation(et, 2),
		})
		if not rat.eq(et.equave, 3) then
			table.insert(infobox_data, {
				"Twelfth",
				approximation(et, 3),
			})
		end
	else
		table.insert(infobox_data, {
			"Fifth",
			approximation(et, 3 / 2),
		})
		table.insert(infobox_data, {
			"Semitones (A1:m2)",
			A1 .. ":" .. m2 .. " (" .. A1_cents .. "¢ : " .. m2_cents .. "¢)",
		})
		if dual_fifth and et.size > 0 then
			table.insert(infobox_data, {
				"Dual sharp fifth",
				approximation(et, 3 / 2, 1),
			})
			table.insert(infobox_data, {
				"Dual flat fifth",
				approximation(et, 3 / 2, -1),
			})
			local sharp = ET.approximate(et, 3 / 2, 1)
			local flat = ET.approximate(et, 3 / 2, -1)
			table.insert(infobox_data, {
				"Dual major 2nd",
				approximation(et, 9 / 8, 0, sharp + flat - octave),
			})
			categories = categories
				.. "[[Category:Dual-fifth temperaments|"
				.. string.rep("#", string.len(et.size))
				.. "]]"
		end
	end

	local consistency = tonumber(frame.args["Consistency"])
	if consistency == nil then
		consistency = limits.consistency_limit(et, false, 43)
	end
	if consistency == nil then
		consistency = "at least 43"
	end
	if consistency ~= nil then
		table.insert(infobox_data, {
			"Consistency limit",
			consistency,
		})
	end
	local distinct_consistency = tonumber(frame.args["Distinct consistency"])
	if distinct_consistency == nil then
		distinct_consistency = limits.consistency_limit(et, consistency or true, 43)
	end
	if distinct_consistency == nil then
		distinct_consistency = "at least 43"
	end
	if distinct_consistency ~= nil then
		table.insert(infobox_data, {
			"Distinct consistency limit",
			distinct_consistency,
		})
	end

	-- special properties
	if ET.is_highly_composite(et) or zeta_switch then
		local text = ""
		if ET.is_highly_composite(et) then
			text = text .. "[[Highly composite equal division|highly composite]]"
			if rat.eq(et.equave, 2) then
				categories = categories
					.. "[[Category:Highly composite EDO|"
					.. string.rep("#", string.len(et.size))
					.. "]]"
			end
		end
		if zeta_switch then
			if #text > 0 then
				text = text .. "<br>"
			end
			if not value_provided(zeta_override) then
				text = text .. ET.why_zeta(et)
			else
				text = text .. zeta_override
			end
			categories = categories .. "[[Category:Zeta|" .. string.rep("#", string.len(et.size)) .. "]]"
		end
		table.insert(infobox_data, {
			"Special properties",
			'<div style="max-width: 270px;">' .. text .. "</div>",
		})
	end

	local result = infobox.build("[[" .. et.suffix .. "|" .. tuning .. "]]", infobox_data, prev_one, next_one)
	if not value_provided(debug_mode) then
		result = result .. categories
	end
	return result
end

return p