function sys = dss(varargin)
%DSS  Create descriptor state-space (DSS) models
%          .
%        E x = A x + B u            E x[n+1] = A x[n] + B u[n]
%                             or 
%          y = C x + D u                y[n] = C x[n] + D u[n]  
%         
%   SYS = DSS(A,B,C,D,E) creates a continuous-time DSS model SYS
%   with matrices A,B,C,D,E.  The output SYS is an object of class @ss.
%   You can set D=0 to mean the zero matrix of appropriate dimensions.
%
%   SYS = DSS(A,B,C,D,E,Ts) creates a discrete-time DSS model with 
%   sample time Ts (set Ts=-1 if the sample time is undetermined).
%
%   In both syntax, the input list can be followed by pairs
%      'PropertyName1', PropertyValue1, ...
%   that set the various properties of SS models (see LTIPROPS for 
%   details).
%
%   You can create arrays of DSS models by specifying ND arrays for 
%   A,B,C,D,E.  See help for SS for more details.
%
%   DSS models may be improper when the E matrix is rank deficient.
%   When SYS is proper, you can eliminate the algebraic variables and
%   compute an explicit realization (E=I) using SYS = SS(SYS,'explicit')
%   or [A,B,C,D] = SSDATA(SYS).
%
%   See also SS, LTI/DSSDATA, LTI/SSDATA, LTI/ISPROPER.

%   Author(s): P. Gahinet, 4-1-96
%   Copyright 1986-2010 The MathWorks, Inc.

%#function ss.checkABCDE

% REVISIT: update help to mention about DSS(A,B,C,D,E) where all or parts of 
% A,B,C,D are StaticModel's
ni = nargin;

% Dissect input list
PVStart = min([ni+1 find(cellfun(@(x) (ischar(x) || isStringScalar(x)),varargin),1)]);
DataInputs = PVStart-1;
if DataInputs<5 || DataInputs>6
   ctrlMsgUtils.error('Control:general:InvalidSyntaxForCommand','dss','dss')
end

% Process numerical data
try
   % Validate data
   a = ss.checkABCDE(varargin{1},'A');
   b = ss.checkABCDE(varargin{2},'B');
   c = ss.checkABCDE(varargin{3},'C');
   d = ss.checkABCDE(varargin{4},'D');
   e = ss.checkABCDE(varargin{5},'E');
   
   % Sample time
   if DataInputs==6
      Ts = ltipack.utValidateTs(varargin{6});
   else
      Ts = 0;
   end
   
   % Harmonize types of A,B,C,D (either numeric or StaticModel at this point)
   [a,b,c,d,e] = ltipack.matchType('',a,b,c,d,e);

   % Treat empty E as [] for consistency with DOUBLE/DSS
   if all(iosize(e)==0)
      e = [];
   end
   
   % Construct GENSS or USS model
   sys = ssModel_(a,b,c,d,e,Ts);
   
   % Process additional settings and validate system
   if PVStart<=ni
      sys = set(sys,varargin{PVStart:ni});
   end
catch ME
   throw(ME)
end

