function Y = polyvalm(p,X)
%MATLAB Code Generation Library Function

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

eml_invariant(nargin == 2,'MATLAB:minrhs');
eml_invariant(isvector(p) || isempty(p), ...
    'MATLAB:polyvalm:InvalidP');
eml_invariant(ismatrix(X) && size(X,1) == size(X,2), ...
    'MATLAB:polyvalm:NonSquareMatrix');
m = size(X,1);
np = numel(p);
Y = zeros(size(X),'like',coder.internal.scalarEg(p,X));
if ~isempty(p)
    for k = 1:m
        Y(k,k) = p(1);
    end
    for i = 2:np
        Y = X * Y;
        for k = 1:m
            Y(k,k) = Y(k,k) + p(i);
        end
    end
end
