function y = binopdf(x,n,p)
% BINOPDF Binomial probability density function, optimised for GPU.
%   Y = BINOPDF(X,N,P)
%
%   See also BINOPDF, GPUARRAY.

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

%   Copyright 2019 The MathWorks, Inc.

if nargin < 3
    error(message("stats:binopdf:TooFewInputs"));
end

[errorcode, x, n, p] = distchck(3,x,n,p);

if errorcode > 0
    error(message("stats:binopdf:InputSizeMismatch"));
end

% Binomial distribution is defined on positive integers less than N.
if ~isfloat(x)
    x = double(x);
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,n,p));
y = arrayfun( @binopdf_scalar, x, n, p, outProto );
