function [varargout] = unifcdf(x,varargin)
%UNIFCDF Uniform (continuous) cumulative distribution function optimized for GPU.
%   P = UNIFCDF(X,A,B)
%   P = UNIFCDF(X,A,B,'upper')
%
%   See also UNIFCDF, GPUARRAY.

%   Copyright 2019 The MathWorks, Inc.

% Keep in-sync with toolbox/stats/stats/unifcdf

if nargin > 1
    [varargin{:}] = convertStringsToChars(varargin{:});
end

if nargin < 1
    error(message('stats:unifcdf:TooFewInputs'));
end

if nargin>1 && strcmpi(varargin{end},'upper')
    %Compute upper tail and remove 'upper' flag
    uflag=true;
    varargin(end) = [];
elseif nargin>1 && ischar(varargin{end})&& ~strcmpi(varargin{end},'upper')
    error(message('stats:cdf:UpperTailProblem'));
else
    uflag=false;
end
[varargout{1:max(1,nargout)}] = localunifcdf(x,uflag,varargin{:});
end

function p = localunifcdf(x,uflag,a,b)
if nargin <3
    a = 0;
    b = 1;
end

[errorcode,x,a,b] = distchck(3,x,a,b);

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

% Process each element using arrayfun so that it gets compiled into a single GPU kernel
outProto = zeros(1,"like",internal.stats.dominantType(x,a,b));
p = arrayfun(@unifcdf_scalar, x, a, b, uflag, outProto);

end
