Module:Infobox ET: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
Plumtree (talk | contribs)
m Links for simplified ratios
Fredg999 (talk | contribs)
Make zeta properties display opt-in, but always include zeta categories nonetheless
 
(153 intermediate revisions by 8 users not shown)
Line 1: Line 1:
local p = {}
local p = {}
local i = require('Module:Interval')
local u = require('Module:Utils')
local rat = require('Module:Rational')
local l = require('Module:Limits')


-- towards is one of: -1 (floor), 0 (nearest), 1 (ceil)
local ET = require("Module:ET")
local function approximate(size, equave, interval, towards)
local infobox = require("Module:Infobox")
towards = towards or 0
local limits = require("Module:Limits")
local exact = math.log(interval) / math.log(rat.as_float(equave)) * size
local rat = require("Module:Rational")
local approx = nil
local utils = require("Module:Utils")
if towards < 0 then
 
approx = math.floor(exact)
-- check whether the input is a non-empty string
elseif towards > 0 then
local function value_provided(s)
approx = math.ceil(exact)
return type(s) == "string" and #s > 0
else
approx = math.floor(exact + 0.5)
end
return approx
end
end


-- towards is one of: -1 (floor), 0 (nearest), 1 (ceil)
-- towards is one of: -1 (floor), 0 (nearest), 1 (ceil)
local function approximation(suffix, size, equave, interval, towards, precomputed_approx)
local function approximation(et, interval, towards, precomputed_approx)
local approx = approximate(size, equave, interval, towards or 0)
local approx = precomputed_approx or ET.approximate(et, interval, towards or 0)
if precomputed_approx then
 
approx = precomputed_approx
-- string for backslash notation
-- "edo" is omitted
local tuning = et.size
if not rat.eq(et.equave, 2) then
tuning = tuning .. et.suffix
end
end
local tuning = size
 
if not rat.eq(equave, 2) then
local ratio = rat.new(approx, et.size)
tuning = tuning .. suffix
 
-- convergence notice, suppressed for 1ed's
local convergement_notice = ""
local converges = rat.converges(ratio, math.log(interval) / math.log(rat.as_float(et.equave)))
if et.size > 1 and converges then
convergement_notice = "<br>(" .. converges .. ")"
end
end
local ratio = rat.new(approx, size)
 
if rat.as_table(ratio)[1] ~= approx then
if rat.as_table(ratio)[1] ~= approx then
local link = rat.as_table(ratio)[2] .. suffix
convergement_notice = ""
ratio = ' ([[' .. link .. '|' .. rat.as_ratio(ratio, '\\')
local link = rat.as_table(ratio)[2] .. et.suffix
if not rat.eq(equave, 2) then
ratio = string.format(" (&rarr;&nbsp;[[%s|%s%s]])",
ratio = ratio .. suffix
link,
end
rat.as_ratio(ratio, "\\"),
ratio = ratio .. ']])'
(rat.eq(et.equave, 2) == false and et.suffix or ""))
else
else
ratio = ''
ratio = ""
end
end
local cents = i._to_cents(i._backslash_ratio(approx .. '\\' .. tuning), 6)
 
return approx .. '\\' .. tuning .. ' (' .. cents .. )' .. ratio
local cents = utils._round(ET.cents(et, approx), 6)
 
return approx .. "\\" .. tuning .. " (" .. cents .. "{{c}})" .. ratio .. convergement_notice
end
end


function p.infobox_ET(frame)
function p.infobox_ET(frame)
local tuning = frame.args['tuning']
-- debug mode will disable the categories
local size, equave = i.parse_ET(tuning)
local debug_mode = frame.args["debug"]
local prime = ""
local categories = ""
if u.is_prime(size) then
 
prime = " (prime)"
local tuning = frame.args["tuning"]
local et = ET.parse(tuning) or ET.parse(tuning .. "edo") or ET.parse("12edo")
 
-- category of the main article
categories = categories .. "{{#ifexist: Category:" .. tuning .. "|[[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))
.. "]]"
elseif rat.eq(et.equave, 3) then
categories = categories .. "[[Category:Edts|" .. string.rep("#", string.len(et.size)) .. "]]"
elseif rat.eq(et.equave, rat.new (3, 2)) then
categories = categories .. "[[Category:Edfs|" .. string.rep("#", string.len(et.size)) .. "]]"
else
categories = categories .. "[[Category:" .. et.suffix .. "'s|" .. string.rep("#", string.len(et.size)) .. "]]"
end
end
local suffix = tuning:match('%d+(ed.+)')
local step_size = i._backslash_ratio('1\\' .. tuning)
local fifth = approximate(size, equave, 3/2)
local fifth_error = i._to_cents(i._backslash_ratio(fifth .. '\\' .. tuning)) - i._to_cents(3/2)
local dual_fifth = math.abs(fifth_error) > i._to_cents(step_size) / 3


local note_12edo = ''
-- prime factorization
if rat.eq(equave, 2) and size == 12 then
local prime_factorization_override = frame.args["Prime factorization"]
note_12edo = '<sup>by definition</sup>'
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 EDOs|" .. 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 = zeta_override:match("^[Yy][Ee][Ss]$") and ET.is_zeta(et)
else
zeta_switch = false
end
 
-- navigation arrows
local increment = 1
if rat.eq(et.equave, rat.new(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 .. "|&larr;&nbsp;" .. (et.size - increment) .. et.suffix .. "]]"
end
local next_one = "[[" .. (et.size + increment) .. et.suffix .. "|" .. (et.size + increment) .. et.suffix .. "&nbsp;&rarr;]]"
 
-- step size in cents
local step_size = ET.cents(et, 1)
if step_size > 100 then
categories = categories .. "[[Category:Macrotonal|" .. string.rep("#", string.len(et.size)) .. "]]"
end
end
local note_12edo = ""
local octave = approximate(size, equave, 2)
if rat.eq(et.equave, 2) and et.size == 12 then
local A1 = 7 * fifth - 4 * octave
note_12edo = " (by&nbsp;definition)"
local m2 = 3 * octave - 5 * fifth
end
local A1_cents = i._to_cents(i._backslash_ratio(A1 .. '\\' .. tuning), 2)
 
local m2_cents = i._to_cents(i._backslash_ratio(m2 .. '\\' .. tuning), 2)
-- octave, twelfth, and fifth in steps
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 is_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 = {}
local infobox_data = {}
table.insert(infobox_data, {
table.insert(infobox_data, {
'Prime factorization',
"Prime factorization",
u._prime_factorization(size) .. prime
prime_factorization,
})
})
table.insert(infobox_data, {
table.insert(infobox_data, {
'Step size',
"Step size",
i._to_cents(step_size, 6) .. '¢' .. note_12edo
utils._round(step_size, 6) .. "{{c}}" .. note_12edo .. "&nbsp;",
})
})
if not dual_fifth then
 
if not rat.eq(et.equave, 2) then
table.insert(infobox_data, {
table.insert(infobox_data, {
'Fifth',
"Octave",
approximation(suffix, size, equave, 3/2)
approximation(et, 2),
})
})
end
if not rat.eq(et.equave, 3) then
table.insert(infobox_data, {
table.insert(infobox_data, {
'Semitones (A1:m2)',
"Twelfth",
A1 .. ':' .. m2 .. ' (' .. A1_cents .. '¢ : ' .. m2_cents .. '¢)'
approximation(et, 3),
})
})
if dual_fifth then
end
else
table.insert(infobox_data, {
table.insert(infobox_data, {
'Sharp fifth',
"Fifth",
approximation(suffix, size, equave, 3/2, 1)
approximation(et, 3 / 2),
})
})
table.insert(infobox_data, {
table.insert(infobox_data, {
'Flat fifth',
"Semitones (A1:m2)",
approximation(suffix, size, equave, 3/2, -1)
A1 .. ":" .. m2 .. " (" .. A1_cents .. "{{c}} : " .. m2_cents .. "{{c}})",
})
})
local sharp = approximate(size, equave, 3/2, 1)
if is_dual_fifth and et.size > 0 then
local flat = approximate(size, equave, 3/2, -1)
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
 
-- consistency and distinct consistency
-- max_limit is used to prevent timeout
local consistency = tonumber(frame.args["Consistency"])
local max_limit = rat.eq(et.equave, 2) and 43 or 32
if consistency == nil then
consistency = limits.consistency_limit(et, false, max_limit)
end
if consistency == nil then
consistency = "at least " .. max_limit
end
if consistency ~= nil then
table.insert(infobox_data, {
table.insert(infobox_data, {
'Major 2nd',
"Consistency limit",
approximation(suffix, size, equave, 9/8, 0, sharp + flat - octave)
consistency,
})
})
end
end
table.insert(infobox_data, {
local distinct_consistency = tonumber(frame.args["Distinct consistency"])
'Consistency limit',
if distinct_consistency == nil then
l.consistency_limit(size, equave)
distinct_consistency = limits.consistency_limit(et, consistency or true, max_limit)
})
end
table.insert(infobox_data, {
if distinct_consistency == nil then
'Distinct consistency limit',
distinct_consistency = "at least " .. max_limit
l.consistency_limit(size, equave, true)
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 ET.is_zeta(et) 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 EDOs|"
.. string.rep("#", string.len(et.size))
.. "]]"
end
end
if ET.is_zeta(et) and rat.eq(et.equave, 2) then
categories = categories
.. "[[Category:Zeta record EDOs|"
.. string.rep("#", string.len(et.size))
.. "]]"
if zeta_switch then
if #text > 0 then
text = text .. "<br>"
end
text = text .. ET.why_zeta(et)
end
end
if #text >0 then
table.insert(infobox_data, {
"Special properties",
"<div style=\"max-width: 300px;\">" .. text .. "</div>",
})
end
end


local s = '<div style="\n' ..
local result = infobox.build("[[" .. et.suffix .. "|" .. tuning .. "]]", infobox_data, prev_one, next_one)
'border: 1px solid #999;\n' ..
'margin: 0;\n' ..
if not value_provided(debug_mode) then
'margin-left: 1em;\n' ..
result = result .. categories
'margin-bottom: 0.5em;\n' ..
'padding: 0.5em;\n' ..
'background-color: #f0f0f0;\n' ..
'min-width: 15em;\n' ..
'float: right;\n' ..
'">\n' ..
'{| width="100%" style="border-collapse: collapse;"\n' ..
'|+ style="font-weight: bold" | ' .. frame.args['tuning'] .. '\n'
for i, entry in ipairs(infobox_data) do
local caption = entry[1]
local text = entry[2]
s = s .. '|-\n' ..
'| style="text-align:right; padding-right: 0.25em" | ' .. caption .. '\n' ..
'| style="background-color: white; padding-left: 0.25em; font-weight: bold" | ' .. text .. '\n'
end
end
s = s .. '|}</div>'
return s
return frame:preprocess(result)
end
end


return p
return p

Latest revision as of 18:27, 4 August 2025

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 ET = require("Module:ET")
local infobox = require("Module:Infobox")
local limits = require("Module:Limits")
local rat = require("Module:Rational")
local utils = require("Module:Utils")

-- 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)

	-- string for backslash notation
	-- "edo" is omitted
	local tuning = et.size
	if not rat.eq(et.equave, 2) then
		tuning = tuning .. et.suffix
	end

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

	-- convergence notice, suppressed for 1ed's
	local convergement_notice = ""
	local converges = rat.converges(ratio, math.log(interval) / math.log(rat.as_float(et.equave)))
	if et.size > 1 and 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 = string.format(" (&rarr;&nbsp;[[%s|%s%s]])",
			link,
			rat.as_ratio(ratio, "\\"),
			(rat.eq(et.equave, 2) == false and et.suffix or ""))
	else
		ratio = ""
	end

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

	return approx .. "\\" .. tuning .. " (" .. cents .. "{{c}})" .. 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(tuning .. "edo") or ET.parse("12edo")

	-- category of the main article
	categories = categories .. "{{#ifexist: Category:" .. tuning .. "|[[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))
			.. "]]"
	elseif rat.eq(et.equave, 3) then
		categories = categories .. "[[Category:Edts|" .. string.rep("#", string.len(et.size)) .. "]]"
	elseif rat.eq(et.equave, rat.new (3, 2)) then
		categories = categories .. "[[Category:Edfs|" .. string.rep("#", string.len(et.size)) .. "]]"
	else
		categories = categories .. "[[Category:" .. et.suffix .. "'s|" .. 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 EDOs|" .. 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 = zeta_override:match("^[Yy][Ee][Ss]$") and ET.is_zeta(et)
	else
		zeta_switch = false
	end

	-- navigation arrows
	local increment = 1
	if rat.eq(et.equave, rat.new(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 .. "|&larr;&nbsp;" .. (et.size - increment) .. et.suffix .. "]]"
	end
	local next_one = "[[" .. (et.size + increment) .. et.suffix .. "|" .. (et.size + increment) .. et.suffix .. "&nbsp;&rarr;]]"

	-- step size in cents
	local step_size = ET.cents(et, 1)
	if step_size > 100 then
		categories = categories .. "[[Category:Macrotonal|" .. string.rep("#", string.len(et.size)) .. "]]"
	end
	local note_12edo = ""
	if rat.eq(et.equave, 2) and et.size == 12 then
		note_12edo = " (by&nbsp;definition)"
	end

	-- octave, twelfth, and fifth in steps
	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 is_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) .. "{{c}}" .. note_12edo .. "&nbsp;",
	})

	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 .. "{{c}} : " .. m2_cents .. "{{c}})",
		})
		if is_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

	-- consistency and distinct consistency
	-- max_limit is used to prevent timeout
	local consistency = tonumber(frame.args["Consistency"])
	local max_limit = rat.eq(et.equave, 2) and 43 or 32
	if consistency == nil then
		consistency = limits.consistency_limit(et, false, max_limit)
	end
	if consistency == nil then
		consistency = "at least " .. max_limit
	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, max_limit)
	end
	if distinct_consistency == nil then
		distinct_consistency = "at least " .. max_limit
	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 ET.is_zeta(et) 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 EDOs|"
					.. string.rep("#", string.len(et.size))
					.. "]]"
			end
		end
		if ET.is_zeta(et) and rat.eq(et.equave, 2) then
			categories = categories
				.. "[[Category:Zeta record EDOs|" 
				.. string.rep("#", string.len(et.size)) 
				.. "]]"
			if zeta_switch then
				if #text > 0 then
					text = text .. "<br>"
				end
				text = text .. ET.why_zeta(et)
			end
		end	
		if #text >0 then
			table.insert(infobox_data, {
				"Special properties",
				"<div style=\"max-width: 300px;\">" .. text .. "</div>",
			})
		end
	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 frame:preprocess(result)
end

return p