function P = pascal(n,k,classname)
%MATLAB Code Generation Library Function

%   Copyright 2002-2015 The MathWorks, Inc.
%#codegen

eml_invariant(nargin > 0,'MATLAB:minrhs');
ZERO = coder.internal.indexInt(0);
ONE = coder.internal.indexInt(1);
TWO = coder.internal.indexInt(2);
if nargin == 1
    k = int8(0);
    classname = 'double';
elseif nargin == 2
    if coder.internal.isCharOrScalarString(k)
        P = pascal(n,int8(0),k);
        return
    end
    classname = 'double';
else
    eml_invariant(coder.internal.isFloatClass(classname), ...
        'Coder:toolbox:pascal_2','IfNotConst','Fail');
end
coder.internal.prefer_const(n,k,classname);
eml_invariant(coder.internal.isBuiltInNumeric(n) && isscalar(n) ...
    && n == floor(n) && n >= 0, ...
    'Coder:toolbox:pascal_nMustBeNonNegativeInteger');
eml_invariant(coder.internal.isBuiltInNumeric(k) && isscalar(k) && ...
    (k == 0 || k == 1 || k == 2), ...
    'MATLAB:pascal:InvalidArg2');
nn = coder.internal.indexInt(n);
if nn < 2
    P = ones(n,classname);
    return
end
P = zeros(nn,classname);
P(:,1) = 1;
plusminus1 = cast(-1,classname);
for j = TWO:nn
    P(j,j) = plusminus1;
    plusminus1 = -plusminus1;
end
% Generate the Pascal Cholesky factor (up to signs).
if nn > TWO
    for j = TWO:(nn - 1)
        for i = (j + 1):nn
            P(i,j) = P(i - 1,j) - P(i - 1,j - 1);
        end
    end
end
if k == int8(0)
    P = P*P';
elseif k == int8(2)
    P = rot90(P,int8(3));
    if eml_bitand(nn,ONE) == ZERO
        P = -P;
    end
end
