function [z,nrk] = tzero(a,b,c,d,e,varargin)
%TZERO  Invariant zeros of linear systems.
%
%   Z = TZERO(SYS) computes the invariant zeros of the dynamic system SYS.
%   For state-space models with matrices A,B,C,D,E, the invariant zeros
%   are the complex values s for which the rank of the matrix
%
%             [ A - s E   B]
%             [    C      D]
%
%   drops below its normal value. For minimal realizations, this coincides 
%   with the transmission zeros of SYS (values of s for which its transfer 
%   function drops rank).
%
%   Z = TZERO(SYS,TOL) specifies the relative tolerance TOL controlling  
%   rank decisions. Increasing TOL helps detect non-minimal modes and 
%   eliminate zeros near infinity but may artificially increase the number
%   of transmission zeros. The default tolerance is EPS^(3/4).
%
%   Z = TZERO(A,B,C,D,E,TOL) computes the invariant zeros directly from the
%   state-space matrices A,B,C,D,E. When omitted or set to [], E defaults
%   to the identity matrix. The default tolerance TOL is EPS^(3/4). No 
%   scaling of A,B,C,E is performed when using this syntax.
%
%   [Z,NRK] = TZERO(...) also returns the normal rank of the transfer 
%   function D + C * inv(s*E-A) * B. The matrix s*E-A should be invertible
%   for almost all s. 
%
%   Note: When C and D are empty or zero, TZERO computes the uncontrollable
%   modes of (A-s*E,B). Similarly, TZERO computes the unobservable modes of
%   (C,A-s*E) when B and D are empty or zero.
%
%   See also ZERO, PZMAP, POLE, LTI, DYNAMICSYSTEM.

%   Copyright 1984-2012 The MathWorks, Inc.

%  Based on SLICOT routines AB08ND and AG08BD.
%
%  Acknowledgment: Thanks to Prof. Ferdinand Svaricek for helpful 
%                  suggestions.
%
%  References: "Computation of Zeros of Linear Multivariable Systems",
%               A. Emami-Naeini and P. Van Dooren, Automatica Vol. 18,
%               No. 4, pp. 415-430, 1982.
%              "Computation of structural invariants of generalized
%               state-space systems", P. Misra, P. Van Dooren and A. Varga,
%               Automatica, 30, pp. 1921-1936, 1994.
narginchk(4,6)

% Defaults
ni = nargin;
if ni<5
   e = [];
end

% Convenience: Resize empty matrices to appropriate rectangular shape
if isempty(b)
   b = zeros(size(a,1),size(d,2));
end
if isempty(c)
   c = zeros(size(d,1),size(a,2));
end
if isempty(d)
   d = zeros(size(c,1),size(b,2));
end

% Validate data
error(abcdchk(a,b,c,d))
if ~(allfinite(a) && allfinite(b) && allfinite(c) && allfinite(d) && allfinite(e))
   error(message('Control:foundation:tzero2'))
end

% Compute zeros
try
   [z,nrk] = ltipack.tzero(a,b,c,d,e,varargin{:});
catch ME
   throw(ME)
end
