function [zepo,Kg]=zp(th,ku,ky,thresh)
%ZP	Computes zeros, poles and static gains associated with a model
%
%	[ZEPO,K] = zp(TH)
%
%	For a model defined by TH (in the format described by HELP THETA)
%	ZEPO is returned as the zeros and poles and K as the static gains
%	of the model. The first rows in these matrices contain integers with
%	information about the column in question. For the zeros and the gains
%	this integer is simply the input number, while for the poles it is
%	20 + the input number. The noise source e is in this context counted
%	as input number 0. For multi-output models, 1000*(output # - 1) is
%	added to these numbers.
%
%	With  [ZEPO,K] = zp(TH,KU,KY) the zeros and poles associated with
%	the input and output numbers given by the entries of the row vectors
%	KU and KY, respectively, are computed.
%	Default values: KU=[1:number of inputs], KY=[1:number of outputs].
%	Noise input is counted as input # 0.
%	The zeros and poles can be plotted by ZPPLOT. zpplot(zp(TH)) is a
%	possible construction.
%	See also th2zp.

%	L. Ljung 10-1-86, revised 7-3-87,10-2-90.
%	Copyright (c) 1986-90 by the MathWorks, Inc.
%	All Rights Reserved.

% *** Set up default values ***
if nargin<4, thresh=[];end
if nargin<3, ky=[];end
if nargin<2, ku=[];end
if isempty(thresh),thresh=100000;end
if isthss(th),eval('[zepo,Kg]=zpss(th,ku,ky,thresh);'),return,end
nu=th(1,3);
if nu==0,kudef=0;else kudef=1:nu;end
if isempty(ku), ku=kudef;end
if any(ku==-1), ku(find(ku==-1))=0;end
if max(ku)>nu | min(ku)<0, error('Input indices outside # of inputs in theta')
return,end


   [a,b,c,d,f]=polyform(th);


  if length(find(ku==0))>0
       b(nu+1,1:length(c))=c;
       f(nu+1,1:length(d))=d;
  end

  [n1,nb]=size(b); [n1,nf]=size(f);

  nzp=max(nb-1,nf+length(a)-2);

 zepo=zeros(nzp+1,2*length(ku))+inf;

      cc=1;
    for k=ku
      if k<=0,k1=501;k=nu+1;else k1=k;end
      zepo(1,cc:cc+1)=[k1 20+k1];Kg(1,(cc+1)/2)=k1;
      dg=conv(a,f(k,:));
      zb=roots(b(k,:)); nzb=length(zb);
            kind=find(abs(zb)>thresh);
	    if ~isempty(kind),zb(kind)=inf*ones(length(kind),1);end
      if nzb>0, zepo(2:nzb+1,cc)=zb;end
      zg=roots(dg); nzg=length(zg);
      if nzg>0, zepo(2:nzg+1,cc+1)=zg;end
      [tmp1,tmp2]=size(b(k,:));
      [md,nd] = size(dg);
      Kg(2,(cc+1)/2)=(b(k,:)*ones(tmp1,tmp2)')/(dg*ones(md,nd)');
      cc=cc+2;
    end
