Module:Primes in edo fs: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
Xenwolf (talk | contribs)
force sign for non-zero fifthspans
ArrowHead294 (talk | contribs)
mNo edit summary
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}
local yesno = require("Module:Yesno")


-- greatest common divisor of a and b
-- greatest common divisor of a and b
Line 37: Line 39:


local function fmt_sign(s)
local function fmt_sign(s)
   if type(s) == 'number' then
   if type(s) == "number" then
     s = tostring(s)
     s = tostring(s)
   end
   end
   return s:gsub('-', '')
   return s:gsub("-", "")
end
end


Line 50: Line 52:
local function edoprox(edo, primes, title, prec, fse_arg)
local function edoprox(edo, primes, title, prec, fse_arg)
   local f = 1/edo
   local f = 1/edo
   local tpri = {'! colspan="2" | Prime number'}
   local tpri = {"! colspan=\"2\" | Prime number"}
   local tabs = {'! rowspan="2" | Error\n! absolute ([[cent|¢]])'}
   local tabs = {"! rowspan=\"2\" | Error\n! absolute ([[cent|¢]])"}
   local trel = {'! [[Relative error|relative]] (%)'}
   local trel = {"! [[Relative error|relative]] (%)"}
   local tdeg = {'! rowspan="2" | Mapping\n! [[patent val]] \'\'v\'\''}
   local tdeg = {"! rowspan=\"2\" | Mapping\n! [[patent val]] ''v''"}
   local tdegor = {"! ''v'' ([[octave-reduced|mod "..edo..']])'}
   local tdegor = {"! ''v'' ([[octave-reduced|mod "..edo.."]])"}
   local fspans = {'! colspan="2" | [[Fifthspan]]'}
   local fspans = {"! colspan=\"2\" | [[Fifthspan]]"}
   local fmt_abs = string.format(' %%+.%df', prec)
   local fmt_abs = string.format(" %%+.%df", prec)
   local fmt_rel = ' %+.0f'
   local fmt_rel = " %+.0f"
   local fs_enable = fse_arg or false
   local fs_enable = fse_arg or false
   local function cat_row(tab, row)
   local function cat_row(tab, row)
Line 67: Line 69:
     ev = round(v)
     ev = round(v)
     evo = ev % edo
     evo = ev % edo
     table.insert(tpri, ' ' .. p)
     table.insert(tpri, " " .. p)
     table.insert(tabs, ''..fmt_sign(string.format(fmt_abs, 1200 * (ev - v ) / edo)))
     table.insert(tabs, ""..fmt_sign(string.format(fmt_abs, 1200 * (ev - v ) / edo)))
     table.insert(trel, ''..fmt_sign(string.format(fmt_rel, 100 * (ev - v))))
     table.insert(trel, ""..fmt_sign(string.format(fmt_rel, 100 * (ev - v))))
     table.insert(tdeg, ' ' .. ev)
     table.insert(tdeg, " " .. ev)
     table.insert(tdegor, ' ' .. evo)
     table.insert(tdegor, " " .. evo)
   end
   end
    
    
Line 88: Line 90:
         end
         end
         if rsp ~= 0 then
         if rsp ~= 0 then
           rsp = string.format('%+.0f', rsp)
           rsp = string.format("%+.0f", rsp)
         end
         end
         table.insert(fspans, ' '.. fmt_sign(rsp))
         table.insert(fspans, " ".. fmt_sign(rsp))
       end
       end
     else
     else
       local reason = ''
       local reason = ""
       if (multi > 1) then
       if (multi > 1) then
         reason = reason .. ' ('.. multi ..'x [[' .. (edo / multi) .. '-EDO]])'
         reason = reason .. " (".. multi .."x [[" .. (edo / multi) .. "-EDO]])"
       end
       end
       table.insert(fspans, ' N/A' .. reason)
       table.insert(fspans, " N/A" .. reason)
     end
     end
   end
   end


   local border_style = ''
   local border_style = ""
   if fs_enable then
   if fs_enable then
     border_style = ' class="thick-border" style="border-top: 2px solid #aaa"'
     border_style = " class=\"thick-border\" style=\"border-top: 2px solid #aaa;\""
   end
   end
    
    
   local titleMarkup = ''
   local titleMarkup = ""
   if title then
   if title then
  titleMarkup = '|-\n|+ ' .. title .. '\n'
  titleMarkup = "|+ style=\"font-size: 105%;\" | " .. title .. "\n"
   end
   end
   local data_rows = ''
   local data_rows = "|-"..border_style.."\n" .. table.concat(tabs, "\n|") .. "\n"
  data_rows = data_rows .. '|-'..border_style..'\n' .. table.concat(tabs, '\n|') .. '\n'
.. "|-\n" .. table.concat(trel, "\n|") .. "\n"
  data_rows = data_rows .. '|-\n' .. table.concat(trel, '\n|') .. '\n'
.. "|-"..border_style.."\n" .. table.concat(tdeg, "\n|") .. "\n"
  data_rows = data_rows .. '|-'..border_style..'\n' .. table.concat(tdeg, '\n|') .. '\n'
.. "|-\n" .. table.concat(tdegor, "\n|") .. "\n"
  data_rows = data_rows .. '|-\n' .. table.concat(tdegor, '\n|') .. '\n'
    
    
   if fs_enable then
   if fs_enable then
     data_rows = data_rows .. '|-'..border_style..'\n' .. table.concat(fspans, '\n|') .. '\n'
     data_rows = data_rows .. "|-"..border_style.."\n" .. table.concat(fspans, "\n|") .. "\n"
   end
   end
    
    
   return '{| class="wikitable center-all"\n' .. titleMarkup ..
   return "{| class=\"wikitable center-all\"\n" .. titleMarkup ..
  '|-\n' ..
  "|-\n" ..
table.concat(tpri, '\n!') .. '\n' .. data_rows  .. '|}'
table.concat(tpri, "\n!") .. "\n" .. data_rows  .. "|}"
end
end


Line 128: Line 129:
   2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,  
   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,
   43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
  101, 103, 107, 109, 113, 127
}
}


Line 133: Line 135:
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 152: Line 154:
function p.primes_in_edo (frame)
function p.primes_in_edo (frame)
   -- optional EDO number, default: 12
   -- optional EDO number, default: 12
   local edo = eval_num_arg(frame.args['edo'], 12)
   local edo = eval_num_arg(frame.args["edo"], 12)
 
   -- optional number of columns, default: 8
   -- optional number of columns, default: 8
   local columns = eval_num_arg(frame.args['columns'], 8)
   local columns = eval_num_arg(frame.args["columns"], 8)
 
   -- 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)
   local title = frame.args['title'] or 'Approximation of prime intervals in ' .. edo .. ' EDO'
   local title = frame.args["title"] or "Approximation of prime intervals in " .. edo .. " EDO"
 
   -- 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_edo(edo))
   local prec = eval_num_arg(frame.args["prec"], prec_by_edo(edo))
 
   -- option to show fifthspan information, default: false
   -- option to show fifthspan information, default: false
   local fs = (frame.args['fs'] == 1) or (frame.args['fs'] == '1') or false
   local fs = (frame.args["fs"] == 1) or (frame.args["fs"] == "1") or false
   return edoprox( edo, {unpack(primes, start, start+columns-1)}, title, prec, fs)
    
  local result = edoprox( edo, {unpack(primes, start, start+columns-1)}, title, prec, fs)
 
  -- debugger option
  local debugg = yesno(frame.args["debug"])
  if debugg == true then
    result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
  end
 
  return frame:preprocess(result)
end
end


return p;
return p

Latest revision as of 13:12, 1 June 2025

Module documentation[view] [edit] [history] [purge]
This module should not be invoked directly; use its corresponding template instead: Template:Primes in edo fs.

This module generates a table with the approximations of prime intervals of a given EDO.

Introspection summary for Module:Primes in edo fs 
Functions provided (1)
Line Function Params
154 primes_in_edo (invokable) (frame)
Lua modules required (1)
Variable Module Functions used
yesno Module:Yesno yesno

No function descriptions were provided. The Lua code may have further information.


local p = {}

local yesno = require("Module:Yesno")

-- greatest common divisor of a and b
-- source: https://rosettacode.org/wiki/Greatest_common_divisor#Lua
local function gcd(a, b)
    while b ~= 0 do 
        a, b = b, a % b
    end
    return math.abs(a)
end

local function calc_step_span(edo, val)
    local multiplicity = gcd(edo, val)
    if multiplicity == 1 then
        test = 0
        for i=0, edo-1 do
            if test == 1 then
                return i, 1
            end
            test = test + val
            test = test % edo
        end
    end
    return nil, multiplicity
end

-- round number to decimals. halfway up for positive numbers
local function round(number, decimals)
  local scale = 10^(decimals or 0)
  local nt = math.floor(number * scale + 0.5)
  return nt / scale
end

local function log2(v)
  return math.log(v) / math.log(2)
end

local function fmt_sign(s)
  if type(s) == "number" then
    s = tostring(s)
  end
  return s:gsub("-", "−")
end

--[[ 
primes: list of primes
prec: number of decimals (for abs errors)
fse_arg: should fifthspans be included as well
]]
local function edoprox(edo, primes, title, prec, fse_arg)
  local f = 1/edo
  local tpri = {"! colspan=\"2\" | Prime number"}
  local tabs = {"! rowspan=\"2\" | Error\n! absolute ([[cent|¢]])"}
  local trel = {"! [[Relative error|relative]] (%)"}
  local tdeg = {"! rowspan=\"2\" | Mapping\n! [[patent val]] ''v''"}
  local tdegor = {"! ''v'' ([[octave-reduced|mod "..edo.."]])"}
  local fspans = {"! colspan=\"2\" | [[Fifthspan]]"}
  local fmt_abs = string.format(" %%+.%df", prec)
  local fmt_rel = " %+.0f"
  local fs_enable = fse_arg or false
  local function cat_row(tab, row)
    tab = tab + row
  end
  for _, p in pairs(primes) do
    s = log2(p)
    v = s*edo
    ev = round(v)
    evo = ev % edo
    table.insert(tpri, " " .. p)
    table.insert(tabs, ""..fmt_sign(string.format(fmt_abs, 1200 * (ev - v ) / edo)))
    table.insert(trel, ""..fmt_sign(string.format(fmt_rel, 100 * (ev - v))))
    table.insert(tdeg, " " .. ev)
    table.insert(tdegor, " " .. evo)
  end
  
  if fs_enable then
    local vf = round(log2(3)*edo) % edo
    local sss, multi = calc_step_span(edo, vf)
    if sss ~= nil then
      for _, p in pairs(primes) do
        s = log2(p)
        v = s*edo
        v = round(v) % edo
        local sp = tonumber(v)*sss % edo
        local rsp = sp
        if (rsp > edo/2) then
           rsp = rsp - edo
        end
        if rsp ~= 0 then
          rsp = string.format("%+.0f", rsp)
        end
        table.insert(fspans, " ".. fmt_sign(rsp))
      end
    else
      local reason = ""
      if (multi > 1) then
        reason = reason .. " (".. multi .."x [[" .. (edo / multi) .. "-EDO]])"
      end
      table.insert(fspans, " N/A" .. reason)
    end
  end

  local border_style = ""
  if fs_enable then
    border_style = " class=\"thick-border\" style=\"border-top: 2px solid #aaa;\""
  end
  
  local titleMarkup = ""
  if title then
	  titleMarkup = "|+ style=\"font-size: 105%;\" | " .. title .. "\n"
  end
  local data_rows = "|-"..border_style.."\n" .. table.concat(tabs, "\n|") .. "\n"
	.. "|-\n" .. table.concat(trel, "\n|") .. "\n"
	.. "|-"..border_style.."\n" .. table.concat(tdeg, "\n|") .. "\n"
	.. "|-\n" .. table.concat(tdegor, "\n|") .. "\n"
  
  if fs_enable then
    data_rows = data_rows .. "|-"..border_style.."\n" .. table.concat(fspans, "\n|") .. "\n"
  end
  
  return "{| class=\"wikitable center-all\"\n" .. titleMarkup ..
	  "|-\n" ..
	table.concat(tpri, "\n!") .. "\n" .. data_rows  .. "|}"
end

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, 127
}

-- evaluate input on error use default
local function eval_num_arg(input, def_value)
  local result = input
  if type(input) ~= "number" then
    result = def_value
    if type(input) == "string" then
      input = input:match("^%s*(.-)%s*$")
      if string.len(input) > 0 then
        result = tonumber(input)
      end
    end
  end
  return result
end

-- calculate default precicion by EDO magnitude
local function prec_by_edo(edo)
  return math.floor(math.log(edo*1.9)/math.log(10))
end

function p.primes_in_edo (frame)
  -- optional EDO number, default: 12
  local edo = eval_num_arg(frame.args["edo"], 12)
  
  -- optional number of columns, default: 8
  local columns = eval_num_arg(frame.args["columns"], 8)
  
  -- optional start column, default: start with prime 2
  local start = eval_num_arg(frame.args["start"], 1)
  local title = frame.args["title"] or "Approximation of prime intervals in " .. edo .. " EDO"
  
  -- optional precision for abs error, default about 3 digits
  local prec = eval_num_arg(frame.args["prec"], prec_by_edo(edo))
  
  -- option to show fifthspan information, default: false
  local fs = (frame.args["fs"] == 1) or (frame.args["fs"] == "1") or false
  
  local result = edoprox( edo, {unpack(primes, start, start+columns-1)}, title, prec, fs)
  
  -- debugger option
  local debugg = yesno(frame.args["debug"])
  if debugg == true then
    result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
  end

  return frame:preprocess(result)
end

return p