Module:Primes in edo fs: Difference between revisions

Xenwolf (talk | contribs)
force sign for non-zero fifthspans
ArrowHead294 (talk | contribs)
mNo edit summary
Line 37: Line 37:


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 50:
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 67:
     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 88:
         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 = "|-\n|+ style=\"font-size: 105%;\" | " .. title .. "\n"
   end
   end
   local data_rows = ''
   local data_rows = ""
   data_rows = data_rows .. '|-'..border_style..'\n' .. table.concat(tabs, '\n|') .. '\n'
   data_rows = data_rows .. "|-"..border_style.."\n" .. table.concat(tabs, "\n|") .. "\n"
   data_rows = data_rows .. '|-\n' .. table.concat(trel, '\n|') .. '\n'
   data_rows = data_rows .. "|-\n" .. table.concat(trel, "\n|") .. "\n"
   data_rows = data_rows .. '|-'..border_style..'\n' .. table.concat(tdeg, '\n|') .. '\n'
   data_rows = data_rows .. "|-"..border_style.."\n" .. table.concat(tdeg, "\n|") .. "\n"
   data_rows = data_rows .. '|-\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 128:
   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 134:
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 153:
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)
   return edoprox( edo, {unpack(primes, start, start+columns-1)}, title, prec, fs)
end
end


return p;
return p;