function p = tcdf(x,v,uflag)
%TCDF Student's T cumulative distribution function (cdf) optimized for GPU.
%   P = TCDF(X,V)
%   P = TCDF(X,V,'upper')
%
%   See also TCDF, GPUARRAY.

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

%   Copyright 2019 The MathWorks, Inc.

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

if nargin < 2
    error(message('stats:tcdf:TooFewInputs'));
end

[errorcode,x,v] = distchck(2,x,v);

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

if nargin>2
    % Compute upper tail
    if ~strcmpi(uflag,'upper')
        error(message('stats:cdf:UpperTailProblem'));
    end
    x = -x;
end

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

end
