Module:Harmonics in cet: Difference between revisions

Godtone (talk | contribs)
m i find this style subtly very irritating sorry, i was surprised to find it so bothersome; if the motivation is to make - the same width as + then this solution is worse than using an ASCII - because it makes - longer than +. and one should prefer ASCII to avoid confusing people copying the data which they expect to be the ASCII minus
ArrowHead294 (talk | contribs)
No edit summary
Line 2: Line 2:


local function approx(step, intervals, title, prec)
local function approx(step, intervals, title, prec)
local tpri = {'! colspan="2" | Harmonic '}
local tpri = {"! colspan=\"2\" | Harmonic "}
local tabs = {'! rowspan="2" | Error \n! Absolute ([[cent|¢]]) '}
local tabs = {"! rowspan=\"2\" | Error \n! Absolute ([[cent|¢]]) "}
local trel = {'! [[Relative interval error|Relative]] (%) '}
local trel = {"! [[Relative interval error|Relative]] (%) "}
local tdeg = {'! colspan="2" | Steps'}
local tdeg = {"! colspan=\"2\" | Steps"}
local fmt_abs = string.format(' %%+.%df', prec)
local fmt_abs = string.format(" %%+.%df", prec)
local fmt_rel = ' %+.0f'
local fmt_rel = " %+.0f"
local size = step/1200
local size = step / 1200
for _, p in pairs(intervals) do
for _, p in pairs(intervals) do
s = math.log(p) / math.log(2)
s = math.log(p) / math.log(2)
v = s/size
v = s/size
ev = math.floor(v + .5)
ev = math.floor(v + 0.5)
table.insert(tpri, "" .. p)
table.insert(tpri, "" .. p)
table.insert(tabs, "" .. string.gsub(string.format(fmt_abs, 1200 * (ev - v)*size), "%-", "-"))
table.insert(tabs, "" .. string.gsub(string.format(fmt_abs, 1200 * (ev - v) * size), "%-", "-"))
table.insert(trel, "" .. string.gsub(string.format(fmt_rel, 100 * (ev - v)), "%-", "-"))
table.insert(trel, "" .. string.gsub(string.format(fmt_rel, 100 * (ev - v)), "%-", "-"))
table.insert(tdeg, "" .. ev)
table.insert(tdeg, "" .. ev)
end
local titleMarkup = ''
if title then
titleMarkup = '|-\n|+ ' .. title .. '\n'
end
end
return '{| class="wikitable center-all"\n' ..
return "{| class=\"wikitable center-all\"\n"
titleMarkup ..
.. "|+ style=\"font-size: 105%;\" | " .. title .. "\n"
'|-\n' ..
.. "|-\n"
table.concat(tpri, '\n!') .. '\n' ..
.. table.concat(tpri, "\n!") .. "\n"
'|-\n' ..
.. "|-\n"
table.concat(tabs, '\n|') .. '\n' ..
.. table.concat(tabs, "\n|") .. "\n"
'|-\n' ..
.. "|-\n"
table.concat(trel, '\n|') .. '\n' ..
.. table.concat(trel, "\n|") .. "\n"
'|-\n' ..
.. "|-\n"
table.concat(tdeg, '\n|') .. '\n' ..
.. table.concat(tdeg, "\n|") .. "\n"
'|}'
.. "|}"
end
end


Line 48: Line 44:
local function eval_num_arg(input, def_value)
local function eval_num_arg(input, def_value)
local result = input
local result = input
if type(input) ~= 'number' then
if type(input) ~= "number" then
result = def_value
result = def_value
if type(input) == 'string' then
if type(input) == "string" then
input = input:match("^%s*(.-)%s*$")
input = input:match("^%s*(.-)%s*$")
if string.len(input) > 0 then
if string.len(input) > 0 then
Line 62: Line 58:
-- calculate default precision
-- calculate default precision
local function prec_by_cet(step)
local function prec_by_cet(step)
return math.floor(math.log(1.9*1200/step)/math.log(10))
return math.floor(math.log(1.9 * 1200 / step) / math.log(10))
end
end


function p.harmonics_in_cet (frame)
function p.harmonics_in_cet (frame)
-- step size in cents, default: 65
-- step size in cents, default: 65
local step = eval_num_arg(frame.args['step'], 63.1578947368)
local step = eval_num_arg(frame.args["step"], 63.1578947368)
-- optional number of columns, default: 10
-- optional number of columns, default: 10
local columns = eval_num_arg(frame.args['columns'], 10)
local columns = eval_num_arg(frame.args["columns"], 10)
-- optional start column, default: start with prime 2
-- optional start column, default: start with prime 2
local start = eval_num_arg(frame.args['start'], 1)
local start = eval_num_arg(frame.args["start"], 1)
-- option intervals
-- option intervals
local select_intervals = "integer"
local select_intervals = "integer"
if frame.args['intervals'] and string.len(frame.args['intervals']) > 0 then
if frame.args["intervals"] and string.len(frame.args["intervals"]) > 0 then
select_intervals = frame.args['intervals']
select_intervals = frame.args["intervals"]
end
end
Line 87: Line 83:
end
end
local title = frame.args['title']
local title = frame.args["title"]
if title == nil or string.len(title) == 0 then
if title == nil or string.len(title) == 0 then
title = "Approximation of ".. title_intervals .. " in " .. name
title = "Approximation of ".. title_intervals .. " in " .. name
end
end
-- optional precision for abs error, default about 3 digits
-- optional precision for abs error, default about 3 digits
local prec = eval_num_arg(frame.args['prec'], prec_by_cet(step))
local prec = eval_num_arg(frame.args["prec"], prec_by_cet(step))
return approx( step, {unpack(intervals[select_intervals], start, start+columns-1)}, title, prec)
return approx(step, {unpack(intervals[select_intervals], start, start + columns - 1)}, title, prec)
end
end


return p;
return p