function y = betainc(x,a,b,tail)
%MATLAB Code Generation Library Function

%   Copyright 1984-2014 The MathWorks, Inc.
%#codegen

eml_invariant(nargin >= 3,'MATLAB:minrhs');
eml_invariant(isfloat(x) && isfloat(a) && isfloat(b), ...
    'Coder:toolbox:betainc_2');
eml_invariant(isreal(x) && isreal(a) && isreal(b), ...
    'Coder:toolbox:betainc_3');
if nargin < 4
    upper = false;
else
    coder.internal.prefer_const(tail);
    upper = strcmp(tail, 'upper');
    eml_invariant(upper || strcmp(tail,'lower'), ...
        'Coder:toolbox:betainc_4');
end
y = coder.internal.scalexpAlloc( ...
    complex(coder.internal.scalarEg(x,a,b)),x,a,b);
check_betainc_inputs(x,a,b);
% Process scalar inputs.
SCALARX = coder.const(eml_is_const(isscalar(x)) && isscalar(x));
SCALARA = coder.const(eml_is_const(isscalar(a)) && isscalar(a));
SCALARB = coder.const(eml_is_const(isscalar(b)) && isscalar(b));
SCALARAB = coder.const(SCALARA && SCALARB);
if SCALARX
    x1 = double(x(1));
end
if SCALARA
    a1 = double(a(1));
    glna1 = gammaln(a1);
end
if SCALARB
    b1 = double(b(1));
    glnb1 = gammaln(b1);
end
if SCALARAB
    lbetascalar = glna1 + glnb1 - gammaln(a1 + b1);
end
% Determine whether to use PARFOR.
n = coder.internal.indexInt(numel(y));
if coder.internal.useParforConst(mfilename,n)
    parfor k = 1:n
        if SCALARX
            xk = x1;
        else
            xk = double(x(k));
        end
        if SCALARA
            ak = a1;
            glna = glna1;
        else
            ak = double(a(k));
            glna = gammaln(ak);
        end
        if SCALARB
            bk = b1;
            glnb = glnb1;
        else
            bk = double(b(k));
            glnb = gammaln(bk);
        end
        if SCALARAB
            lbeta = lbetascalar;
        else
            lbeta = glna + glnb - gammaln(ak + bk);
        end
        y(k) = eml_betainc(xk,ak,bk,lbeta,upper);
    end
else
    for k = 1:n
        if SCALARX
            xk = x1;
        else
            xk = double(x(k));
        end
        if SCALARA
            ak = a1;
            glna = glna1;
        else
            ak = double(a(k));
            glna = gammaln(ak);
        end
        if SCALARB
            bk = b1;
            glnb = glnb1;
        else
            bk = double(b(k));
            glnb = gammaln(bk);
        end
        if SCALARAB
            lbeta = lbetascalar;
        else
            lbeta = glna + glnb - gammaln(ak + bk);
        end
        y(k) = eml_betainc(xk,ak,bk,lbeta,upper);
    end
end

%--------------------------------------------------------------------------

function check_betainc_inputs(x,a,b)
for k = 1:numel(a)
    if ~(a(k) >= 0) % including a == NaN
        coder.internal.error('Coder:MATLAB:betainc_PositiveZ');
    end
end
for k = 1:numel(b)
    if ~(b(k) >= 0) % including b == NaN
        coder.internal.error('Coder:MATLAB:betainc_PositiveW');
    end
end
for k = 1:numel(x)
    if ~((0 <= x(k)) && (x(k) <= 1)) % including x == NaN
        coder.internal.error('Coder:MATLAB:betainc_XOutOfRange');
    end
end

%--------------------------------------------------------------------------
