La documentación para este módulo puede ser creada en Módulo:Math/doc

local p = {};

local shared = require("Módulo:Shared");
local stuff = {"'", "°", "%%", "−", "&minus;", "÷", "x", "•", "&times;", "{", "}", "%[", "%]", "e", "gamma", "phi", "π", "pi", "%)%(", "<sup>", "</sup>", "E", "c#eil", "#e%*p"};
local replaceStuff = {"", "", "/100", "%-", "%-", "/", "%*", "%*", "%*", "%(", "%)", "%(", "%)", "#e", "#gamma", "#phi", "#pi", "#pi", "%)%*%(", "%^%(", "%)", "%*10%^", "ceil", "exp"};

local function replace(str)
    str = string.gsub(str, "%s+", "");
    
    for i, v in ipairs(stuff) do
        str = string.gsub(str, stuff[i], replaceStuff[i]);
    end
    
    str = string.gsub(str, "#e", math.exp(1));
    str = string.gsub(str, "#gamma", 0.57721566490153);
    str = string.gsub(str, "#phi", (1 + math.sqrt(5))/2);
    str = string.gsub(str, "#pi", math.pi);
    
    for i = 0, 9 do
        str = string.gsub(str, i.."%(", i.."%*%(");
        str = string.gsub(str, "%)"..i, "%)%*"..i);
    end
    
    patterns = {"\"", "\\", ",", "%?", "_", "`", "#"}
    for i,v in ipairs(patterns) do
        str = string.gsub(str, v, "");
    end
    
    return '('..str..')';
end

function p.replace(frame)
    local p = '';
    if type(tonumber(frame.args[2])) ~= 'number' then
        p = '100*';
    end
    return p..replace(frame.args[1]);
end


local function mod(num, by)
    by = math.abs(by);
    local val = num % by;
    return val;
end

function p.mod(frame)
    local val = mod(frame.args[1] + 0, frame.args[2] + 0);
    if type(tonumber(frame.args[3])) ~= 'number' then
        val = 100*val;
    end
    if math.log(math.abs(val))/math.log(10) <= -5 then
        return val;
    else
        return shared.formatnum(val);
    end
end


local function round(num, to)
    local total = num + to / 2;
    local val = total - (total % to);
    return val;
end

function p.round(frame)
    local val = round(frame.args[1] + 0, frame.args[2] + 0);
    if type(tonumber(frame.args[3])) ~= 'number' then
        val = 100*val;
    end
    if math.log(math.abs(val))/math.log(10) <= -5 then
        return val;
    else
        return shared.formatnum(val);
    end
end


local function trunc(num)
    local val = 0;
    if (num > 0) then
        val = num - math.floor(num);
    else
        val = num - math.ceil(num);
    end
    val = num - val;
    
    return val;
end

function p.trunc(frame)
    local val = trunc(frame.args[1] + 0);
    if type(tonumber(frame.args[2])) ~= 'number' then
        val = 100*val;
    end
    if math.log(math.abs(val))/math.log(10) <= -5 then
        return val;
    else
        return shared.formatnum(val);
    end
end


local function ceil(num)
    local val = math.ceil(num);
    return val;
end

function p.ceil(frame)
    local val = ceil(frame.args[1] + 0);
    if type(tonumber(frame.args[2])) ~= 'number' then
        val = 100*val;
    end
    if math.log(math.abs(val))/math.log(10) <= -5 then
        return val;
    else
        return shared.formatnum(val);
    end
end


local function floor(num)
    local val = math.floor(num);
    return val;
end

function p.floor(frame)
    local val = floor(frame.args[1] + 0);
    if type(tonumber(frame.args[2])) ~= 'number' then
        val = 100*val;
    end
    if math.log(math.abs(val))/math.log(10) <= -5 then
        return val;
    else
        return shared.formatnum(val);
    end
end


local function abs(num)
    local val = math.abs(num);
    return val;
end

function p.abs(frame)
    local val = abs(frame.args[1] + 0);
    if type(tonumber(frame.args[2])) ~= 'number' then
        val = 100*val;
    end
    if math.log(math.abs(val))/math.log(10) <= -5 then
        return val;
    else
        return shared.formatnum(val);
    end
end


local function pow(num, expo)
    local val = math.pow(num, expo);
    return val;
end

function p.pow(frame)
    local val = pow(frame.args[1] + 0, frame.args[2] + 0);
    if type(tonumber(frame.args[3])) ~= 'number' then
        val = 100*val;
    end
    if math.log(math.abs(val))/math.log(10) <= -5 then
        return val;
    else
        return shared.formatnum(val);
    end
end


local function exp(expo)
    local val = math.exp(expo);
    return val;
end

function p.exp(frame)
    local val = exp(frame.args[1] + 0);
    if type(tonumber(frame.args[2])) ~= 'number' then
        val = 100*val;
    end
    if math.log(math.abs(val))/math.log(10) <= -5 then
        return val;
    else
        return shared.formatnum(val);
    end
end


local function ln(num)
    local val = math.log(num);
    return val;
end

function p.ln(frame)
    local val = ln(frame.args[1] + 0);
    if type(tonumber(frame.args[2])) ~= 'number' then
        val = 100*val;
    end
    if math.log(math.abs(val))/math.log(10) <= -5 then
        return val;
    else
        return shared.formatnum(val);
    end
end


local function log(base, num)
    local val = math.log(num) / math.log(base);
    return val;
end

function p.log(frame)
    local val = log(frame.args[1] + 0, frame.args[2] + 0);
    if type(tonumber(frame.args[3])) ~= 'number' then
        val = 100*val;
    end
    if math.log(math.abs(val))/math.log(10) <= -5 then
        return val;
    else
        return shared.formatnum(val);
    end
end


local function sin(num)
    local val = math.sin(num);
    return val;
end

function p.sin(frame)
    local val = sin(frame.args[1] + 0);
    if type(tonumber(frame.args[2])) ~= 'number' then
        val = 100*val;
    end
    if math.log(math.abs(val))/math.log(10) <= -5 then
        return val;
    else
        return shared.formatnum(val);
    end
end


local function cos(num)
    local val = math.cos(num);
    return val;
end

function p.cos(frame)
    local val = cos(frame.args[1] + 0);
    if type(tonumber(frame.args[2])) ~= 'number' then
        val = 100*val;
    end
    if math.log(math.abs(val))/math.log(10) <= -5 then
        return val;
    else
        return shared.formatnum(val);
    end
end


local function tan(num)
    local val = math.tan(num);
    return val;
end

function p.tan(frame)
    local val = tan(frame.args[1] + 0);
    if type(tonumber(frame.args[2])) ~= 'number' then
        val = 100*val;
    end
    if math.log(math.abs(val))/math.log(10) <= -5 then
        return val;
    else
        return shared.formatnum(val);
    end
end


local function asin(num)
    local val = math.asin(num);
    return val;
end

function p.asin(frame)
    local val = asin(frame.args[1] + 0);
    if type(tonumber(frame.args[2])) ~= 'number' then
        val = 100*val;
    end
    if math.log(math.abs(val))/math.log(10) <= -5 then
        return val;
    else
        return shared.formatnum(val);
    end
end


local function acos(num)
    local val = math.acos(num);
    return val;
end

function p.acos(frame)
    local val = acos(frame.args[1] + 0);
    if type(tonumber(frame.args[2])) ~= 'number' then
        val = 100*val;
    end
    if math.log(math.abs(val))/math.log(10) <= -5 then
        return val;
    else
        return shared.formatnum(val);
    end
end


local function atan(num)
    local val = math.atan(num);
    return val;
end

function p.atan(frame)
    local val = atan(frame.args[1] + 0);
    if type(tonumber(frame.args[2])) ~= 'number' then
        val = 100*val;
    end
    if math.log(math.abs(val))/math.log(10) <= -5 then
        return val;
    else
        return shared.formatnum(val);
    end
end


local function func(x, z)
    local power = (z-1)*math.log(x/(1-x))/math.log(10) - (x/(1-x))/math.log(10) - 2*math.log(1-x)/math.log(10);
    return power;
end

local function log_sum(old_pows)
    local n = table.getn(old_pows);
    if n == 1 then
        return old_pows[1];
    end
        
    local pows = {};
    local j = 1;
    for i = 1, math.floor(n/2) do
        pows[i] = old_pows[j] + log(10, 1 + 10^(old_pows[j+1] - old_pows[j]));
        j = j + 2;
    end
    if n % 2 == 1 then
        pows[math.floor(n/2)+1] = old_pows[n];
    end

    return log_sum(pows);
end

local function gamma_(z)
    if z <= 1 then
        return gamma_(z+1) - log(10, z);
    end
        
    local n = 100000;
    local d = 1/n;

    local pows_ = {};
    local pows = {};
    local j = 1;
    local k = n-1;
    pows_[1] = func(d, z) - math.log(n)/math.log(10);
    for i = 2, n-1 do
        pows_[i] = func(i*d, z) - math.log(n)/math.log(10);
        if (pows_[i] >= -323) and (pows_[i-1] < -323) then
            j = i;
        end
        if (pows_[i] < -323) and (pows_[i-1] >= -323) then
            k = i-1;
            break;
        end
    end
    for i = 1, k-j+1 do
        pows[i] = pows_[i+j-1];
    end

    local power = log_sum(pows);
    return power;
end

local function gamma(z, p)
    local power = gamma_(z);
    if type(tonumber(p)) ~= 'number' then
        p = 2;
    else
        p = 0;
    end
    if math.abs(power) > 7 then
        return round(10^(power-math.floor(power)), 10^-7), 'E', floor(power + p);
    else
        return round(10^(power + p), 10^-7);
    end
end

function p.gamma(frame)
    return gamma(frame.args[1] + 0, frame.args[2]);
end


local function ncr(n, r)
    -- Error Checks --
    if (n < 0) then
        n = 0;
    end
    if (r > n) then
        r = n;
    end
    
    local val = 1;
    
    -- Special Cases --
    if (r <= 0 or r >= n) then
        return val;
    elseif (r == 1 or r == n - 1) then
        val = n;
        return val;
    end
    
    -- n! / r!(n-r)! --
    local total = n - r;
    local minimum = r + 1;
    local j = total;
    if (total > r) then
        minimum = total + 1;
        j = r;
    end
    
    for i = n, minimum, -1 do
        val = val * i / j;
        if (j >= 2) then
            j = j - 1;
        end
    end
    
    return val;
end

function p.ncr(frame)
    local val = ncr(frame.args[1] + 0, frame.args[2] + 0);
    if type(tonumber(frame.args[3])) ~= 'number' then
        val = 100*val;
    end
    if math.log(math.abs(val))/math.log(10) <= -5 then
        return val;
    else
        return shared.formatnum(val);
    end
end


local function npr(n, r)
    -- Error Checks --
    if (n < 0) then
        n = 0;
    end
    if (r > n) then
        r = n;
    end
    
    local val = 1;
    
    -- Special Cases --
    if (r <= 0) then
        return val;
    elseif (r == 1) then
        val = n;
        return val;
    end
    
    -- n! / (n-r)! --
    local total = n - r;
    local minimum = total + 1;
    
    for i = n, minimum, -1 do
        val = val * i;
    end
    
    return val;
end

function p.npr(frame)
    local val = npr(frame.args[1] + 0, frame.args[2] + 0);
    if type(tonumber(frame.args[3])) ~= 'number' then
        val = 100*val;
    end
    if math.log(math.abs(val))/math.log(10) <= -5 then
        return val;
    else
        return shared.formatnum(val);
    end
end


local function binomial(p, n, r)
    if p < 0 then
        p = 0;
    elseif p > 1 then
        p = 1;
    end
    
    local c = ncr(n, r);
    local val = c*(p^r)*(1-p)^(n-r);
    return shared.formatnum(val);
end

function p.binomial(frame)
    local val = binomial(frame.args[1] + 0, frame.args[2] + 0, frame.args[3] + 0);
    if type(tonumber(frame.args[4])) ~= 'number' then
        val = 100*val;
    end
    if math.log(math.abs(val))/math.log(10) <= -5 then
        return val;
    else
        return shared.formatnum(val);
    end
end


local function sum(array)
    local sum = 0;
    for i, v in ipairs(array) do
        sum = sum + v;
    end

    return sum;
end

local function getProbs(array)
    local probs = {};
    
    local j = 1;
    for i, v in ipairs(array) do
        if (v + 0) > 0 then
            probs[j] = v + 0;
            j = j + 1;
        end
    end
    
    local sumP = sum(probs);
    
    if sumP > 1 then
        for i, v in ipairs(probs) do
            probs[i] = v/sumP;
        end
    end
    
    return probs;
end

local function expected(array)
    local probs = getProbs(array);
    
    local sumP = sum(probs);
    
    if sumP <= 0 then
        return 0;
    end
    
    local runs = 1;
    local probArray = {};
    
    for i, v in ipairs(probs) do
        probArray = getProbs(array);
        probArray[i] = 0;
        run = expected(probArray);
        runs = runs + v*run;
    end
    
    runs = runs/sumP;
    return runs;
end

function p.expected(frame)
    local runs = expected(frame.args);
    
    if math.floor(runs) == math.ceil(runs) then
        return shared.formatnum(runs);
    else
        return floor(runs)..'&nbsp;-&nbsp;'..ceil(runs);
    end
end


local function func2(probs, dp, x)
    local ngFunction = 0;
    
    for i, v in ipairs(probs) do
        ngFunction = ngFunction + (1 - v)^x;
    end
    
    return 1 - dp - ngFunction;
end

local function IQI(probs, dp, x_guess_one, x_guess_two, x_guess_three, error_desired, max_iterations)
local x_old_old = x_guess_one;
local x_old = x_guess_two;
local x_current = x_guess_three;
local error_approx = error_desired + 1;
local num_iterations = 0;

if func2(probs, dp, x_old_old) == 0 then
    x_root = x_old_old;
elseif func2(probs, dp, x_old) == 0 then
    x_root = x_old;
elseif func2(probs, dp, x_current) == 0 then
    x_root = x_current;
else
    while (error_approx > error_desired) and (num_iterations < max_iterations) do
        num_iterations = num_iterations + 1;
        y_old_old = func2(probs, dp, x_old_old);
        y_old = func2(probs, dp, x_old)
        y_current = func2(probs, dp, x_current)
        x_new = (y_old*y_old_old/((y_current - y_old)*(y_current - y_old_old)))*x_current + (y_current*y_old_old/((y_old - y_current)*(y_old - y_old_old)))*x_old + (y_current*y_old/((y_old_old - y_current)*(y_old_old - y_old)))*x_old_old;
        
        if x_new ~= 0 then
            error_approx = math.abs((x_new - x_current)/x_new) * 100;
        end
        
        x_old_old = x_old;
        x_old = x_current;
        x_current = x_new;
    end
    x_root = x_new;
end

return x_root;
end

local function nearlyGuaranteed(array)
    local probs = getProbs(array);
    
    local nine9 = IQI(probs, 0.99, 0, 1, 2, 0.000001, 500);
    local nine99 = IQI(probs, 0.999, 0, 1, 2, 0.000001, 500);
    local nine999 = IQI(probs, 0.9999, 0, 1, 2, 0.000001, 500);
    
    local range = round(nine99, 1)..'&nbsp;&#177;&nbsp;'..round((nine999 - nine9)/2, 1);
    return range;
end

function p.nearlyGuaranteed(frame)
    return nearlyGuaranteed(frame.args);
end


return p;
El contenido de la comunidad está disponible bajo CC-BY-SA a menos que se indique lo contrario.