Module:Temperament data: Difference between revisions

CompactStar (talk | contribs)
No edit summary
ArrowHead294 (talk | contribs)
mNo edit summary
 
(33 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local rat = require('Module:Rational')
local p = {}
local p = {}
local u = require('Module:Utils')


local function gcd(a,b)
local rat = require("Module:Rational")
  if type(a) == 'number' and type(b) == 'number' and  
local u = require("Module:Utils")
        a == math.floor(a) and b == math.floor(b) then
 
    if b == 0 then
local function gcd(a, b)
      return a
    if type(a) == "number" and type(b) == "number" and a == math.floor(a) and b == math.floor(b) then
        if b == 0 then
            return a
        else
            return gcd(b, a % b) -- tail recursion
        end
     else
     else
      return gcd(b, a % b) -- tail recursion
        error("Invalid argument to gcd (" .. tostring(a) .. ", " .. tostring(b) .. ")", 2)
     end
     end
  else
    error('Invalid argument to gcd (' .. tostring(a) .. ',' ..
          tostring(b) .. ')', 2)
  end
end
end


Line 68: Line 67:


local function matinv(a)
local function matinv(a)
local dbl_identity = {}
local xn = scalarmatmul(a, 1e-7)
for i = 1, #a do
 
dbl_identity[i] = {}
for i = 1, 75 do
for j = 1, #a do
xn = matsub(scalarmatmul(xn, 2), matmul(xn, matmul(a, xn)))
if i == j then
dbl_identity[i][j] = 2
else
dbl_identity[i][j] = 0
end
end
end
end
local xn = scalarmatmul(a, 0.000005)
for i = 1, 30 do
xn = matmul(xn, matsub(dbl_identity, matmul(a, xn)))
end
return xn
return xn
end
end
Line 140: Line 128:
end
end


local function get_te_tuning_map(subgroup, comma_basis)
local function get_reduced_mapping(comma_basis, preimage)
local v = unreduced_mapping_from_basis(comma_basis)
a =  pseudoinv(matmul(unreduced_mapping_from_basis(comma_basis), preimage))
for i = 1, #a do
for j = 1, #a[1] do
a[i][j] = math.floor(a[i][j] + 0.5) -- round each entry (they are not exact integers)
end
end
return a
end
 
local function get_te_generator(subgroup, comma_basis, preimage)
local v = get_reduced_mapping(comma_basis, preimage)
local w = {}
local w = {}
for i = 1, #subgroup do
for i = 1, #subgroup do
Line 147: Line 145:
for j = 1, #subgroup do
for j = 1, #subgroup do
if i == j then
if i == j then
w[i][j] = math.log(2)/math.log(subgroup[i])
w[i][j] = math.log(2) / math.log(subgroup[i])
else
else
w[i][j] = 0
w[i][j] = 0
Line 163: Line 161:
end
end


local function get_te_generator(subgroup, comma_basis, preimage)
return matmul(get_te_tuning_map(subgroup, comma_basis), preimage)
end


local function get_pote_generator(subgroup, comma_basis, preimage)
local function get_pote_generator(subgroup, comma_basis, preimage)
Line 174: Line 169:
local te = get_te_generator(subgroup, comma_basis, preimage)
local te = get_te_generator(subgroup, comma_basis, preimage)
local stretch_factor = te[1][1] * math.log(2) / math.log(period)
local stretch_factor = te[1][1] * math.log(2) / math.log(period)
return scalarmatmul(te, stretch_factor)
return scalarmatmul(te, 1 / stretch_factor)
end
end


-- Parsing/display functions
-- Parsing/display functions
Line 221: Line 218:
local function mysplit (inputstr, sep)
local function mysplit (inputstr, sep)
         if sep == nil then
         if sep == nil then
                sep = '%s'
    sep = "%s"
         end
         end
         local t={}
         local t = {}
         for str in string.gmatch(inputstr, '([^'..sep..']+)') do
         for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
                 table.insert(t, str)
                 table.insert(t, str)
         end
         end
Line 232: Line 229:
local function trim(x)
local function trim(x)
local str = x
local str = x
str = str:gsub('%s+', '')
str = str:gsub("%s+", "")
     str = string.gsub(str, '%s+', '')
     str = string.gsub(str, "%s+", "")
     return str
     return str
end
end


function p.temperament_data(frame)
function p.temperament_data(frame)
local subgroup = mysplit(frame.args['subgroup'], '.')
local subgroup = mysplit(frame.args["subgroup"], ".")
for i = 1, #subgroup do
for i = 1, #subgroup do
subgroup[i] = tonumber(subgroup[i])
subgroup[i] = tonumber(subgroup[i])
end
end
local comma_matrix = mysplit(frame.args['comma_list'], ',')
local comma_matrix = mysplit(frame.args["comma_list"], ",")
for i = 1, #comma_matrix do
for i = 1, #comma_matrix do
comma_matrix[i] = rat.parse(comma_matrix[i])
comma_matrix[i] = rat.parse(comma_matrix[i])
end
end
comma_matrix = rat_list_to_matrix(subgroup, comma_matrix)
comma_matrix = rat_list_to_matrix(subgroup, comma_matrix)
local unparsed_gens = mysplit(frame.args['generators'], ',')
local unparsed_gens = mysplit(frame.args["generators"], ",")
local generators =  mysplit(frame.args['generators'], ',')
local generators =  mysplit(frame.args["generators"], ",")
for i = 1, #generators do
for i = 1, #generators do
generators[i] = rat.parse(generators[i])
generators[i] = rat.parse(generators[i])
end
end
generators = rat_list_to_matrix(subgroup, generators)
generators = rat_list_to_matrix(subgroup, generators)
local pote_generator = get_te_generator(subgroup, comma_matrix, generators)
local mapping = get_reduced_mapping(comma_matrix, generators)
local result =  '[[Subgroup]]: ' .. frame.args['subgroup']
local cte_generator = frame.args["cte_generator"]
result = result .. '\n\n[[Comma list]]: ' .. frame.args['comma_list']
local pote_generator = get_pote_generator(subgroup, comma_matrix, generators)
result = result .. '\n\n: mapping generators: '
local result =  "[[Subgroup]]: " .. frame.args["subgroup"]
result = result .. "\n\n[[Comma list]]: " .. frame.args["comma_list"]
result = result .. "\n\n[[Mapping]]: [⟨"
for i = 1, #mapping do
for j = 1, #mapping[1] do
result = result .. mapping[i][j] .. " "
end
result = result:sub(0,-2) .. "], ⟨"
end
result = result:sub(0,-6) .. "]"
result = result .. "\n\n: mapping generators: "
for i = 1, #unparsed_gens do
for i = 1, #unparsed_gens do
result = result .. '~' .. trim(unparsed_gens[i]) .. ', '
result = result .. "~" .. trim(unparsed_gens[i]) .. ", "
end
end
result = result:sub(0,-3)
result = result:sub(0, -3)
if subgroup[1] ~= 2 then
if cte_generator ~= "" then
result = result .. '\n\n[[Optimal tuning]] ([[Lp tuning|POL2]]): '
cte_generator = mysplit(cte_generator, ",")
for i = 1, #cte_generator do
cte_generator[i] = tonumber(cte_generator[i])
end
result = result .. "\n\n[[Optimal tuning]]s:\n* [[CTE]]:"
for i = 1, #(cte_generator) do
result = result .. "~" .. trim(unparsed_gens[i]) .. " = "
if subgroup[1] == 2 and i == 1 then
result = result .. "1\\1"
elseif subgroup[1] == 3 and i == 1 then
result = result .. "1\\1edt"
else
result = result .. u._round(cte_generator[i], 7)
end
result = result .. ", "
end
result = result:sub(0,-3)
if subgroup[1] ~= 2 then
result = result .. "\n* [[Lp tuning|POL2]]:"
else
result = result .. "\n* [[POTE]]:"
end
else
else
result = result .. '\n\n[[Optimal tuning]] ([[POTE]]): '
if subgroup[1] ~= 2 then
result = result .. "\n\n[[Optimal tuning]] ([[Lp tuning|POL2]]): "
else
result = result .. "\n\n[[Optimal tuning]] ([[POTE]]): "
end
end
end
for i = 1, #(pote_generator[1]) do
for i = 1, #(pote_generator[1]) do
result = result .. "~" .. trim(unparsed_gens[i]) .. " = "
if subgroup[1] == 2 and i == 1 then
if subgroup[1] == 2 and i == 1 then
result = result .. '~' .. trim(unparsed_gens[i]) ..  ' = ' .. '1\\1' .. ', '
result = result .. "1\\1"
else
elseif subgroup[1] == 3 and i == 1 then
result = result .. '~' .. trim(unparsed_gens[i]) .. ' = ' .. u._round(pote_generator[1][i] * 1200, 7) .. ', '
result = result .. "1\\1edt"
else
result = result .. u._round(pote_generator[1][i] * 1200, 7)
end
end
result = result .. ", "
end
end
result = result:sub(0,-3)
result = result:sub(0,-3)
return result
return result
end
end


return p
return p