Module:Harmonics in ED: Difference between revisions

From Xenharmonic Reference
No edit summary
Cleaned up code
Line 22: Line 22:


local function rel_col(err) -- color used for relative error
local function rel_col(err) -- color used for relative error
-- TODO: switch out these colors for proper classes
    abs_err = math.abs(err)
abs_err = math.abs(err)
    if(abs_err == 0) then
-- red = math.pow(abs_err * 2, 1 / 3.0) * 150
        return "acc0"
-- green = math.pow(1 - (abs_err * 2), 0.5) * 150
    end
-- blue = (1 - (abs_err * 10)) * 150
    if(abs_err < 0.03) then
-- if blue < 10 then blue = 10 end
        return "acc1"
-- return string.format("%02x%02x%02x", math.floor(red), math.floor(green), math.floor(blue))
    end
 
    if(abs_err < 0.07) then
if(abs_err == 0) then
        return "acc2"
return "acc0"
    end
end
    if(abs_err < 0.11) then
 
        return "acc3"
if(abs_err < 0.03) then
    end
return "acc1"
    if(abs_err < 0.16) then
end
        return "acc4"
 
    end
if(abs_err < 0.07) then
    if(abs_err < 0.25) then
return "acc2"
        return "acc5"
end  
    end
 
    if(abs_err < 0.36) then
if(abs_err < 0.11) then
        return "acc6"
return "acc3"
    end
end  
    return "acc7"
 
if(abs_err < 0.16) then
return "acc4"
end  
 
if(abs_err < 0.25) then
return "acc5"
end  
 
if(abs_err < 0.36) then
return "acc6"
end  
 
return "acc7"
 
 
end
end


Line 91: Line 75:
     for i = 1,p_len do
     for i = 1,p_len do
         local er = rel_err(et, primes[i])
         local er = rel_err(et, primes[i])
         tab = tab .. "| class=\"" .. rel_col(er)
         tab = tab .. "| class=\"" .. rel_col(er) .. "\" | "
        tab = tab .. "\" | "
         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"

Revision as of 02:59, 17 December 2025

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}
-- TODO: switch out these colors for proper classes
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 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.03) then
        return "acc1"
    end
    if(abs_err < 0.07) then
        return "acc2"
    end
    if(abs_err < 0.11) then
        return "acc3"
    end
    if(abs_err < 0.16) then
        return "acc4"
    end
    if(abs_err < 0.25) then
        return "acc5"
    end
    if(abs_err < 0.36) then
        return "acc6"
    end
    return "acc7"
end

function p.table(frame) -- making the table itself
-- TODO: switch out these colors for proper classes
	local p_len = #primes
    local et = math.floor(frame.args["et"])
    local nowiki = frame.args["nowiki"]
    local p_lim = math.floor(frame.args["p_lim"])

    local tab = "{| class=\"wikitable\"\n"
    if (tonumber(nowiki) == 1) then
    tab = "<nowiki>" .. tab end
    tab = tab .. "|+Approximation of prime harmonics in "
    tab = tab .. et .. "edo\n"
    tab = tab .. "! colspan=\"2\" |Harmonic\n"
    for i = 1,p_len do
        tab = tab .. "! class=\""
        tab = tab .. p_cols[i] .. "\" |" .. primes[i] .. "\n"
        if primes[i] >= p_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, primes[i]) > 0 then tab = tab .. "+" end
        tab = tab .. string.format("%.1f", abs_err(et, primes[i])) .. "\n"
        if primes[i] >= p_lim then break end
    end
    tab = tab .. "|-\n!Relative (%)\n"
    for i = 1,p_len do
        local er = rel_err(et, primes[i])
        tab = tab .. "| class=\"" .. rel_col(er) .. "\" | "
        if er > 0 then tab = tab .. "+" end
        tab = tab .. string.format("%.1f", er * 100) .. "\n"
        if primes[i] >= p_lim then break end
    end
    tab = tab .. "|-\n! colspan=\"2\" |Steps\n(reduced)\n"
    for i = 1,p_len do
        tab = tab .. "|" .. steps(et, primes[i]) .. "\n(" .. steps_re(et, primes[i]) .. ")\n"
        if primes[i] >= p_lim then break end
    end
    tab = tab .. "|}"
    if (tonumber(nowiki) == 1) then
    tab = tab .. "</nowiki>" end
    return tab
end

return p