function x = nbininv(y,r,p)
%NBININV Inverse of the negative binomial cumulative distribution function, optimised for GPU.
%   X = NBININV(Y,R,P)
%
%   See also NBININV, GPUARRAY.

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

%   Copyright 2021 The MathWorks, Inc.

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

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

% Work out the expected output type
outProto = cast(1,"like",internal.stats.dominantType(y,r,p));

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


