function p = hygecdf(x,m,k,n,uflag)
%HYGECDF Hypergeometric cumulative distribution function optimized for GPU.
%   P = HYGECDF(X,M,K,N)
%   P = HYGECDF(X,M,K,N,'upper')
%
%   See also HYGEINV, HYGEPDF, HYGERND, HYGESTAT, CDF.

%   See toolbox/stats/stats/hygecdf for implementation details.

%   Copyright 2019 The MathWorks, Inc.


if nargin > 4
    uflag = convertStringsToChars(uflag);
end

if nargin < 4
    error(message('stats:hygecdf:TooFewInputs'));
end
if nargin > 4
    if ~strcmpi(uflag,'upper')
        error(message('stats:cdf:UpperTailProblem'));
    end
    x = n - floor(x) - 1;
    k = m - k;
end
[errorcode, x, m, k, n] = distchck(4,x,m,k,n);

if errorcode > 0
    error(message('stats:hygecdf:InputSizeMismatch'));
end

% Make sure all parameters are floating point
if ~isfloat(x)
    x = double(x);
end
if ~isfloat(m)
    m = double(m);
end
if ~isfloat(k)
    k = double(k);
end
if ~isfloat(n)
    n = double(n);
end

% Process each element independently using arrayfun. This causes the
% function to be compiled into an optimised GPU kernel.
outProto = zeros(1,"like",internal.stats.dominantType(x,m,k,n));
p = arrayfun( @hygecdf_scalar, x, m, k, n, outProto );
