Module:Harmonics in ED: Difference between revisions

From Xenharmonic Reference
added support for odds
FloraC (talk | contribs)
Use HTML newline for proper line spacing
 
(18 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local p = {}
local p = {}
local primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89}
local primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113}
local p_cols = {"prime2", "prime3", "prime5", "prime7", "prime11", "prime13", "prime17", "prime19", "prime23", "prime29", "prime31", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime"}
local p_cols = {"prime2", "prime3", "prime5", "prime7", "prime11", "prime13", "prime17", "prime19", "prime23", "prime29", "prime31", "prime37", "prime41", "prime43", "prime47", "prime53", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime"}


local odds = {3, 5, 7, 9, 11, 13, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57}
local odds = {3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57}
local o_cols = {"prime3", "prime5", "prime7", "higherPrime", "prime11", "prime13", "higherPrime", "prime17", "prime19", "higherPrime", "prime23", "higherPrime", "higherPrime", "prime29", "prime31", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime"}
local o_cols = {"prime3", "prime5", "prime7", "higherPrime", "prime11", "prime13", "higherPrime", "prime17", "prime19", "higherPrime", "prime23", "higherPrime", "higherPrime", "prime29", "prime31", "higherPrime", "higherPrime", "prime37", "higherPrime", "prime41", "prime43", "higherPrime", "prime47", "higherPrime", "higherPrime", "prime53", "higherPrime", "higherPrime"}


local function steps(et, harm) -- steps of harmonic
local function steps(et, harm) -- steps of harmonic
Line 35: Line 35:


function p.table(frame) -- making the table itself
function p.table(frame) -- making the table itself
    local p_len = #primes
     local et = frame.args["et"]
     local et = math.floor(frame.args["et"])
     local nowiki = frame.args["nowiki"]
     local nowiki = frame.args["nowiki"]
     local p_lim = math.floor(frame.args["p_lim"])
   
     local type = frame.args["type"]
    if (type == "0") then
    type = "prime";
end
   
    local harms = primes;
    local cols = p_cols;
    local p_lim = frame.args["p_lim"]
        if (p_lim == "odd" or p_lim == "prime") then
        type = p_lim;
            p_lim = "default";
    end


     local tab = "{| class=\"wikitable\"\n"
        if (type == "prime") then
        if (p_lim == "default") then
        p_lim = 31;
        end
        elseif (type == "odd") then
        if (p_lim == "default") then
        p_lim = 25;
        end
        harms = odds;
        cols = o_cols;
        end
    local lim = math.floor(p_lim)
    local p_len = #harms
 
     local tab = "{| class=\"wikitable center-all\"\n"
     if (tonumber(nowiki) == 1) then
     if (tonumber(nowiki) == 1) then
     tab = "<nowiki>" .. tab end
     tab = "<nowiki>" .. tab end
     tab = tab .. "|+Approximation of prime harmonics in "
     tab = tab .. "|+Approximation of " .. type .. " harmonics in "
     tab = tab .. et .. "edo\n"
     tab = tab .. et .. "edo\n"
     tab = tab .. "! colspan=\"2\" |Harmonic\n"
     tab = tab .. "! colspan=\"2\" |Harmonic\n"
     for i = 1,p_len do
     for i = 1,p_len do
         tab = tab .. "! class=\""
         tab = tab .. "! class=\""
         tab = tab .. p_cols[i] .. "\" |" .. primes[i] .. "\n"
         tab = tab .. cols[i] .. "\" |" .. harms[i] .. "\n"
         if primes[i] >= p_lim then break end
         if harms[i] >= lim then break end
     end
     end
     tab = tab .. "|-\n! rowspan=\"2\" |Error\n!Absolute (¢)\n"
     tab = tab .. "|-\n! rowspan=\"2\" |Error\n!Absolute (¢)\n"
     for i = 1,p_len do
     for i = 1,p_len do
         tab = tab .. "| "
         tab = tab .. "| "
         if rel_err(et, primes[i]) > 0 then tab = tab .. "+" end
         if rel_err(et, harms[i]) > 0 then tab = tab .. "+" end
         tab = tab .. string.format("%.1f", abs_err(et, primes[i])) .. "\n"
         tab = tab .. string.format("%.1f", abs_err(et, harms[i])) .. "\n"
         if primes[i] >= p_lim then break end
         if harms[i] >= lim then break end
     end
     end
     tab = tab .. "|-\n!Relative (%)\n"
     tab = tab .. "|-\n!Relative (%)\n"
     for i = 1,p_len do
     for i = 1,p_len do
         local er = rel_err(et, primes[i])
         local er = rel_err(et, harms[i])
         tab = tab .. "| class=\"" .. rel_col(er) .. "\" | "
         tab = tab .. "| class=\"" .. rel_col(er) .. "\" | "
         if er > 0 then tab = tab .. "+" end
         if er > 0 then tab = tab .. "+" end
         tab = tab .. string.format("%.1f", er * 100) .. "\n"
         tab = tab .. string.format("%.1f", er * 100) .. "\n"
         if primes[i] >= p_lim then break end
         if harms[i] >= lim then break end
     end
     end
     tab = tab .. "|-\n! colspan=\"2\" |Steps\n(reduced)\n"
     tab = tab .. "|-\n! colspan=\"2\" |Steps<br>(reduced)\n"
     for i = 1,p_len do
     for i = 1,p_len do
         tab = tab .. "|" .. steps(et, primes[i]) .. "\n(" .. steps_re(et, primes[i]) .. ")\n"
         tab = tab .. "|" .. steps(et, harms[i]) .. "<br>(" .. steps_re(et, harms[i]) .. ")\n"
         if primes[i] >= p_lim then break end
         if harms[i] >= lim then break end
     end
     end
     tab = tab .. "|}"
     tab = tab .. "|}"

Latest revision as of 16:59, 16 July 2026

Documentation for this module may be created at Module:Harmonics in ED/doc

local p = {}
local primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113}
local p_cols = {"prime2", "prime3", "prime5", "prime7", "prime11", "prime13", "prime17", "prime19", "prime23", "prime29", "prime31", "prime37", "prime41", "prime43", "prime47", "prime53", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime", "higherPrime"}

local odds = {3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57}
local o_cols = {"prime3", "prime5", "prime7", "higherPrime", "prime11", "prime13", "higherPrime", "prime17", "prime19", "higherPrime", "prime23", "higherPrime", "higherPrime", "prime29", "prime31", "higherPrime", "higherPrime", "prime37", "higherPrime", "prime41", "prime43", "higherPrime", "prime47", "higherPrime", "higherPrime", "prime53", "higherPrime", "higherPrime"}

local function steps(et, harm) -- steps of harmonic
    return math.floor((math.log(harm) / math.log(2) * et) + 0.5)
end

local function steps_re(et, harm) -- steps of reduced harmonic
    return steps(et, harm) - (math.floor(math.log(harm) / math.log(2)) * et)
end

local function rel_err(et, harm) -- relative error of harmonic
    st = math.log(harm) / math.log(2) * et
    return math.floor(st + 0.5) - st
end

local function abs_err(et, harm) -- absolute error of harmonic
    return 1200 / et * rel_err(et, harm)
end

local function rel_col(err) -- color used for relative error
    abs_err = math.abs(err)
    if(abs_err == 0) then
        return "acc0"
    end
    if(abs_err < 0.03125) then
        return "acc1"
    end
    return "acc" .. math.floor(abs_err * 12) + 2
end

function p.table(frame) -- making the table itself
    local et = frame.args["et"]
    local nowiki = frame.args["nowiki"]
    
    local type = frame.args["type"]
    if (type == "0") then
    	type = "prime";
	end
    
    local harms = primes;
    local cols = p_cols;
    local p_lim = frame.args["p_lim"]
        if (p_lim == "odd" or p_lim == "prime") then
    	    type = p_lim;
            p_lim = "default";
	    end

        if (type == "prime") then
        	if (p_lim == "default") then
        		p_lim = 31;
        	end
        elseif (type == "odd") then
        	if (p_lim == "default") then
        		p_lim = 25;
        	end
        	harms = odds;
        	cols = o_cols;
        end
    local lim = math.floor(p_lim)
    local p_len = #harms

    local tab = "{| class=\"wikitable center-all\"\n"
    if (tonumber(nowiki) == 1) then
    tab = "<nowiki>" .. tab end
    tab = tab .. "|+Approximation of " .. type .. " harmonics in "
    tab = tab .. et .. "edo\n"
    tab = tab .. "! colspan=\"2\" |Harmonic\n"
    for i = 1,p_len do
        tab = tab .. "! class=\""
        tab = tab .. cols[i] .. "\" |" .. harms[i] .. "\n"
        if harms[i] >= lim then break end
    end
    tab = tab .. "|-\n! rowspan=\"2\" |Error\n!Absolute (¢)\n"
    for i = 1,p_len do
        tab = tab .. "| "
        if rel_err(et, harms[i]) > 0 then tab = tab .. "+" end
        tab = tab .. string.format("%.1f", abs_err(et, harms[i])) .. "\n"
        if harms[i] >= lim then break end
    end
    tab = tab .. "|-\n!Relative (%)\n"
    for i = 1,p_len do
        local er = rel_err(et, harms[i])
        tab = tab .. "| class=\"" .. rel_col(er) .. "\" | "
        if er > 0 then tab = tab .. "+" end
        tab = tab .. string.format("%.1f", er * 100) .. "\n"
        if harms[i] >= lim then break end
    end
    tab = tab .. "|-\n! colspan=\"2\" |Steps<br>(reduced)\n"
    for i = 1,p_len do
        tab = tab .. "|" .. steps(et, harms[i]) .. "<br>(" .. steps_re(et, harms[i]) .. ")\n"
        if harms[i] >= lim then break end
    end
    tab = tab .. "|}"
    if (tonumber(nowiki) == 1) then
    tab = tab .. "</nowiki>" end
    return tab
end

return p