Module:Infobox interval: Difference between revisions

Plumtree (talk | contribs)
mNo edit summary
Plumtree (talk | contribs)
Improved parsing
Line 8: Line 8:
local debug_mode = frame.args['debug'] ~= nil
local debug_mode = frame.args['debug'] ~= nil
-- TODO: better input parsing + large intervals + irrational intervals
local rational = false
local ratio_arg = frame.args['Ratio'] or '2/1'
local small = false
local ratio = rat.parse(ratio_arg) or rat.new(2)
local regular = false
local ratio_str = rat.as_ratio(ratio)
local ratio = nil
local ratio_ket = rat.as_ket(ratio, frame)
local cents = nil
local ratio_approx = rat.as_float(ratio)
local ket = nil
local ratio_cents = rat.cents(ratio)
local ratio_string = nil
local regular = not ratio.nan and not ratio.zero and not ratio.inf
-- intervals with relatively small powers
if frame.args['Ratio'] and #(frame.args['Ratio']) > 0 then
ratio = rat.parse(frame.args['Ratio'])
if ratio ~= nil then
rational = true
small = true
regular = not ratio.nan and not ratio.inf and not ratio.zero
cents = rat.cents(ratio)
ket = rat.as_ket(ratio, frame)
ratio_string = rat.as_ratio(ratio)
end
end
-- intervals with large powers
if ratio == nil and frame.args['Ket'] and #(frame.args['Ket']) > 0 then
ratio = rat.from_ket(frame.args['Ket'])
if ratio ~= nil then
rational = true
small = false
regular = true
cents = rat.cents(ratio)
ket = rat.as_ket(ratio, frame)
end
end
-- irrational intervals
if ratio == nil and frame.args['Cents'] and #(frame.args['Cents']) > 0 then
cents = tonumber(frame.args['Cents'])
if cents ~= nil then
rational = false
small = true
regular = true
ratio_string = frame.args['Ratio'] or ''
end
end
local infobox_data = {}
local infobox_data = {}
local cats = '[[Category:Rational intervals]]'
local cats = ''
if rational then
cats = cats .. '[[Category:Rational intervals]]'
else
cats = cats .. '[[Category:Irrational intervals]]'
end
table.insert(infobox_data, {
if small then
'Ratio',
table.insert(infobox_data, {
ratio_str
'Ratio',
})
ratio_string
table.insert(infobox_data, {
})
'Factorization',
end
rat.factorisation(ratio)
if rational then
})
table.insert(infobox_data, {
if regular then
'Factorization',
rat.factorisation(ratio)
})
end
if regular and rational then
table.insert(infobox_data, {
table.insert(infobox_data, {
'[[Just intonation subgroup|Subgroup]]',
'[[Just intonation subgroup|Subgroup]]',
Line 36: Line 80:
})
})
end
end
if regular then
if regular and rational then
table.insert(infobox_data, {
table.insert(infobox_data, {
'[[Monzo]]',
'[[Monzo]]',
ratio_ket
ket
})
})
end
if regular then
table.insert(infobox_data, {
table.insert(infobox_data, {
'Size in [[cent]]s',
'Size in [[cent]]s',
i._to_cents(ratio_approx, 8) .. '¢'
u._round(cents, 8) .. '¢'
})
})
end
end
Line 59: Line 105:
table.insert(infobox_data, {
table.insert(infobox_data, {
'Name(s)',
'Name(s)',
'<abbr title="missing value for parameter \'Name\'">\'\'missing\'\'</abbr></span><sup>[[Template:Infobox Interval| ?&nbsp;]]</sup>'
'<abbr title="missing value for parameter \'Name\'">\'\'missing\'\'</abbr><sup>[[Template:Infobox Interval| ?&nbsp;]]</sup>'
})
})
end
end
Line 73: Line 119:
local FJS_name = frame.args['FJS name']
local FJS_name = frame.args['FJS name']
if not FJS_name or #FJS_name == 0 then
if (not FJS_name or #FJS_name == 0) and rational then
FJS_name = rat.as_FJS(ratio)
FJS_name = rat.as_FJS(ratio)
end
end
Line 83: Line 129:
end
end
if regular then
if regular and small then
table.insert(infobox_data, {
table.insert(infobox_data, {
'Norms',
'Norms',
Line 95: Line 141:
table.insert(infobox_data, {
table.insert(infobox_data, {
'[[Harmonic entropy]]',
'[[Harmonic entropy]]',
'~' .. u._round(i.harmonic_entropy(ratio_cents), 6)
'~' .. u._round(i.harmonic_entropy(cents), 6)
})
})
end
end
Line 106: Line 152:
})
})
end
end
 
table.insert(infobox_data, {
if small then
'<small>[https://www.yacavone.net/xen-calc/?q=' .. ratio_str .. ' open this interval in \'\'xen-calc\'\']</small>'
table.insert(infobox_data, {
})
'<small>[https://www.yacavone.net/xen-calc/?q=' .. ratio_string .. ' open this interval in \'\'xen-calc\'\']</small>'
})
end
local s = infobox.build(
local s = infobox.build(