Module:Harmonics in equal: Difference between revisions
Tristanbay (talk | contribs) Doubled the number of primes (copied from OEIS A000040) and the number of odd numbers Tags: Mobile edit Mobile web edit |
ArrowHead294 (talk | contribs) mNo edit summary |
||
| Line 5: | Line 5: | ||
local function map(p, steps, num, denom) | local function map(p, steps, num, denom) | ||
local s = math.log(p) / math.log(num/denom) | local s = math.log(p) / math.log(num/denom) | ||
return math.floor(s*steps + .5) | return math.floor(s * steps + 0.5) | ||
end | end | ||
-- check consistency for 9, 15 and 21 | -- check consistency for 9, 15 and 21 | ||
| Line 16: | Line 16: | ||
local p21 = map(21, steps, num, denom) | local p21 = map(21, steps, num, denom) | ||
return (p9 == 2*p3) and (p15 == p3+p5) and (p21 == p3+p7) | return (p9 == 2 * p3) and (p15 == p3 + p5) and (p21 == p3 + p7) | ||
end | end | ||
| Line 22: | Line 22: | ||
local thead | local thead | ||
if collapsed then | if collapsed then | ||
thead = | thead = "{| class=\"wikitable center-all mw-collapsible mw-collapsed\"\n" | ||
else | else | ||
thead = | thead = "{| class=\"wikitable center-all\"\n" | ||
end | end | ||
local tpri = { | local tpri = {"! colspan=\"2\" | Harmonic "} | ||
local tabs = { | local tabs = {"! rowspan=\"2\" | Error \n! Absolute ([[cent|¢]]) "} | ||
local trel = { | local trel = {"! [[Relative interval error|Relative]] ([[relative cent|%]]) "} | ||
local tdeg | local tdeg | ||
if reduction then | if reduction then | ||
tdeg = { | tdeg = {"! colspan=\"2\" | Steps<br />([[octave reduction|reduced]])"} | ||
else | else | ||
tdeg = { | tdeg = {"! colspan=\"2\" | Step"} | ||
end | end | ||
local fmt_abs = string.format(' %%+.%df', prec) | local fmt_abs = string.format(' %%+.%df', prec) | ||
local fmt_rel = | local fmt_rel = " %+.1f" | ||
local equave = math.log(num/denom) / math.log(2) | local equave = math.log(num/denom) / math.log(2) | ||
for _, p in pairs(intervals) do | for _, p in pairs(intervals) do | ||
s = math.log(p) / math.log(num/denom) | s = math.log(p) / math.log(num/denom) | ||
v = s*steps | v = s * steps | ||
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 * equave * (ev - v ) / steps), "%-", "-")) | table.insert(tabs, "" .. string.gsub(string.format(fmt_abs, 1200 * equave * (ev - v ) / steps), "%-", "-")) | ||
table.insert(trel, "" .. string.gsub(string.format(fmt_rel, 100 * (ev - v)), "%-", "-")) | table.insert(trel, "" .. string.gsub(string.format(fmt_rel, 100 * (ev - v)), "%-", "-")) | ||
if reduction then | if reduction then | ||
table.insert(tdeg, | table.insert(tdeg, "" .. ev .. "<br />(".. ev % steps .. ")") | ||
else | else | ||
table.insert(tdeg, | table.insert(tdeg, "" .. ev) | ||
end | end | ||
end | end | ||
local titleMarkup = | local titleMarkup = "" | ||
if title then | if title then | ||
titleMarkup = | titleMarkup = "|-\n|+ style=\"white-space: nowrap;\" | " .. title .. "\n" | ||
end | end | ||
return thead .. | return thead .. | ||
titleMarkup .. | titleMarkup .. | ||
"|-\n! " .. | |||
table.concat(tpri, | table.concat(tpri, " !! ") .. "\n" .. | ||
"|-\n| " .. | |||
table.concat(tabs, | table.concat(tabs, " || ") .. "\n" .. | ||
"|-\n| ".. | |||
table.concat(trel, | table.concat(trel, " || ") .. "\n" .. | ||
"\n| " .. | |||
table.concat(tdeg, | table.concat(tdeg, " || ") .. "\n" .. | ||
"|}" | |||
end | end | ||
| Line 78: | Line 78: | ||
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) ~= | if type(input) ~= "number" then | ||
result = def_value | result = def_value | ||
if type(input) == | 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 92: | Line 92: | ||
-- calculate default precision | -- calculate default precision | ||
local function prec_by_equal(steps, num, denom) | local function prec_by_equal(steps, num, denom) | ||
return math.floor(10*math.log(steps*1.9*math.log(2)/math.log(num/denom))/math.log(10))/10 | return math.floor(10 * math.log(steps * 1.9 * math.log(2) / math.log(num / denom)) / math.log(10)) / 10 | ||
end | end | ||
function p.harmonics_in_equal (frame) | function p.harmonics_in_equal (frame) | ||
-- optional number of steps, default: 12 | -- optional number of steps, default: 12 | ||
local steps = eval_num_arg(frame.args[ | local steps = eval_num_arg(frame.args["steps"], 12) | ||
-- numerator, default: 2 | -- numerator, default: 2 | ||
local num = eval_num_arg(frame.args[ | local num = eval_num_arg(frame.args["num"], 2) | ||
-- denominator, default: 1 | -- denominator, default: 1 | ||
local denom = eval_num_arg(frame.args[ | local denom = eval_num_arg(frame.args["denom"], 1) | ||
-- optional number of columns, default: 11 | -- optional number of columns, default: 11 | ||
local columns = eval_num_arg(frame.args[ | local columns = eval_num_arg(frame.args["columns"], 11) | ||
-- optional start column, default: start with prime 2 | -- optional start column, default: start with prime 2 | ||
local start = eval_num_arg(frame.args[ | local start = eval_num_arg(frame.args["start"], 1) | ||
-- option intervals | -- option intervals | ||
local select_intervals = "integer" | local select_intervals = "integer" | ||
local name = steps .. | local name = steps .. "ed" .. num .. "/" .. denom | ||
if denom == 1 then | if denom == 1 then | ||
if num == 2 then | if num == 2 then | ||
name = steps .. | name = steps .. "edo" | ||
select_intervals = "odd" | select_intervals = "odd" | ||
if check_consistency(steps, num, denom) then | if check_consistency(steps, num, denom) then | ||
| Line 120: | Line 120: | ||
end | end | ||
elseif num == 3 then | elseif num == 3 then | ||
name = steps .. | name = steps .. "edt" | ||
else | else | ||
name = steps .. | name = steps .. "ed" .. num | ||
end | end | ||
end | end | ||
if num == 3 and denom == 2 then | if num == 3 and denom == 2 then | ||
name = steps .. | name = steps .. "edf" | ||
end | end | ||
-- override default intervals | -- override default intervals | ||
if frame.args[ | if frame.args["intervals"] and string.len(frame.args["intervals"]) > 0 then | ||
select_intervals = frame.args[ | select_intervals = frame.args["intervals"] | ||
end | end | ||
| Line 141: | Line 141: | ||
end | end | ||
local title = frame.args[ | 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[ | local prec = eval_num_arg(frame.args["prec"], prec_by_equal(steps, num, denom)) | ||
local reduction = true | local reduction = true | ||
if steps == 1 then | if steps == 1 then | ||
reduction = false | reduction = false | ||
end | end | ||
local collapsed = utils.value_provided(frame.args[ | local collapsed = utils.value_provided(frame.args["collapsed"]) | ||
return approx( steps, num, denom, {unpack(intervals[select_intervals], start, start+columns-1)}, title, prec, reduction, collapsed) | return approx(steps, num, denom, {unpack(intervals[select_intervals], start, start + columns - 1)}, title, prec, reduction, collapsed) | ||
end | end | ||
return p | return p | ||