function [Dinv,SingularFlag] = inv(D,~)
% Computes inv(D)

%   Copyright 1986-2010 The MathWorks, Inc.
% RE: no input or output delays, square system
[ny,nu] = size(D.num);
SingularFlag = false;
if ny==0 || nu==0
   Dinv = transpose(D);
elseif hasdelay(D)
   error(message('Control:transformation:inv1'))
elseif ny==1
   % SISO system w/o delays
   Dinv = D;
   if all(D.num{1}==0)
      % INV(0) is NaN
      Dinv.num = {NaN};
      Dinv.den = {1};
      SingularFlag = true;
   else
      Dinv.num = D.den;
      Dinv.den = D.num;
   end
else
   % Convert to state-space
   try
      Dinv = tf(inv(ss(D)));
      SingularFlag = ~isfinite(Dinv);
   catch E
      throw(E)
   end
end
