function W = wilkinson(n,classname)
% MATLAB Code Generation Library Function

%LIMITATIONS:
%  1. n must be a fixed-size scalar

%   Copyright 1984-2014 The MathWorks, Inc.
%#codegen

eml_invariant(nargin >= 1, ...
    'MATLAB:minrhs');
coder.internal.prefer_const(n);
eml_invariant(eml_is_const(n) || eml_option('VariableSizing'), ...
    'Coder:toolbox:wilkinson_2', ...
    'IfNotConst','Fail');
coder.internal.assertValidSizeArg(n);
eml_invariant(coder.internal.isBuiltInNumeric(n) && isscalar(n) && isreal(n), ...
    'Coder:toolbox:wilkinson_3', ...
    'IfNotConst','Fail');
eml_invariant(coder.internal.scalar.floor(n) == n, ...
    'Coder:toolbox:wilkinson_argMustBeRealIntScalar');
if nargin == 1
    classname = 'double';
else
    eml_invariant(coder.internal.isFloatClass(classname), ...
        'Coder:toolbox:wilkinson_5', ...
        'IfNotConst','Fail');
end
W = zeros(n,classname);
if n > 1
    nd = cast(n,classname);
    m = eml_rdivide(nd-1,2);
    % W = diag(abs(-m:m)) + diag(e,1) + diag(e,-1);
    W(1,1) = m;
    W(2,1) = 1;
    for k = 2:n-1
        W(coder.internal.indexMinus(k,1),k) = 1;
        W(k,k) = abs(cast(k,classname)-m-1);
        W(coder.internal.indexPlus(k,1),k) = 1;
    end
    W(n-1,n) = 1;
    W(n,n) = m;
end
