function [num,den] = zp2tf(z,p,k)
%MATLAB Code Generation Library Function

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

eml_invariant(nargin == 3, ...
    'MATLAB:minrhs');
den = real(poly(p(:)));
[~,nd] = size(den);
mk = numel(k);
if isempty(z)
    num = [zeros(mk,nd-1),k(:)];
    return
end
[m,n] = size(z);
eml_invariant(mk == n || m ~= 1, ...
    'Controllib:general:ZNotColumn');
eml_invariant(mk == n || m == 1, ...
    'Controllib:general:KMismatchZ');
numcols = eml_max(nd,m+1);
num = coder.nullcopy(eml_expand(coder.internal.scalarEg(z,k),[n,numcols]));
i0 = coder.internal.indexMinus(numcols,m);
for j = 1:n
    pj = real(poly(z(:,j))*k(j)); 
    % Since EML's POLY does not remove non-finite values, 
    % length(pj) = m + 1 in MATLAB Code Generation.
    for i = 1:coder.internal.indexMinus(i0,1)
        num(j,i) = 0;
    end
    for i = i0:numcols
        num(j,i) = pj(coder.internal.indexPlus(coder.internal.indexMinus(i,i0),1));
    end
end
