function y = nbinpdf(x,r,p)
%NBINPDF Negative binomial probability density function, optimised for GPU.
%   Y = NBINPDF(X,R,P)
%
%   See also NBINPDF, GPUARRAY.

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

%   Copyright 2021 The MathWorks, Inc.

if nargin < 3
    error(message('stats:nbinpdf:TooFewInputs'));
end

errorcode = distchck(3,x,r,p);
if errorcode > 0
    error(message('stats:nbinpdf:InputSizeMismatch'));
end

if ~isfloat(x)
    x = double(x);
end
% Work out the output type
outProto = ones("like",internal.stats.dominantType(x,r,p));

% Process each element independently using arrayfun. This causes the
% function to be compiled into an optimised GPU kernel.
y = arrayfun(@nbinpdf_scalar, x, r, p, outProto);
end
