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

%   Copyright 1986-2023 The MathWorks, Inc.
if isfinite(D)
   % Compute implicit form
   nx = size(D.a,1);
   [D,SingularFlag] = inv@ltipack.AbstractSS(D);
   dnx = size(D.a,1)-nx;
   if dnx>0
      if nargin>1
         % inv(sys,'min'): Try eliminating algebraic variables
         [D.a,br,D.c,dr,D.e] = elimAX(D.a,[D.b,D.dx0],D.c,[D.d,D.y0],D.e,nx+1:nx+dnx);
         D.b = br(:,1:end-1);  D.dx0 = br(:,end);
         D.d = dr(:,1:end-1);  D.y0 = dr(:,end);
         D.x0 = D.x0(1:nx,:);
         if ~isfinite(D)
            error(message('Control:transformation:inv3'))
         end
      else
         % Update state info to include algebraic states
         if ~isempty(D.StateName)
            D.StateName = [D.StateName ; strings(dnx,1)];
         end
         if ~isempty(D.StatePath)
            D.StatePath = [D.StatePath ; strings(dnx,1)];
         end
         if ~isempty(D.StateUnit)
            D.StateUnit = [D.StateUnit ; strings(dnx,1)];
         end
      end
   end
else
   SingularFlag = false;
end
