Module:Matrix

local p = {} local getArgs = require('Module:Arguments').getArgs local split = require('Module:Split')

local i18n = { error = { missing_input = 'Missing required arguments', }, level_title = 'Level %s:', }

local function round(value, decimals) return math.floor(value * (10^decimals) + 0.5) / (10^decimals) end

function p.main(frame) local args = getArgs(frame, {   wrappers = {      'Template:Matrix'    }  }) return p._main(args) end

function p._main(args) assert(args[1] and args['reps'] and args['multiplier'], i18n.error.missing_input)

-- Turn the base value into an array. args[1] = split(args[1], '%s*/%s*')

-- Convert the multiplier if it's a percentage value. if string.sub(args['multiplier'], -1) == '%' then args['multiplier'] = 1+(string.sub(args['multiplier'], 1, -2)/100) end

-- Create a matrix. local matrix = {} for n=1, #args[1] do   -- Create a table holding the current level's base value. matrix[n] = { tonumber(args[1][n]) } for o=2, args['reps'] do     -- Insert the other values into the current level's table. matrix[n][o] = round( matrix[n][o-1] * args['multiplier'], args['round'] or 1 ) end

-- Convert the table holding the current level's values into a string. matrix[n] = string.format("%s " .. i18n.level_title .. " %s", string.rep(':', args['indent'] or 1), n, table.concat(matrix[n], '/')) end return table.concat(matrix, '\n') end

return p