function [D1,SingularFlag,ElimFlag] = mrdivide(D1,D2)
% Computes D = D1/D2

%   Copyright 1986-2010 The MathWorks, Inc.

% Square system, no support for scalar division
[ny2,~] = iosize(D2);
if ny2>0
   if hasdelay(D2)
      ctrlMsgUtils.error('Control:combination:divide1','SYS1/SYS2','SYS2')
   elseif ny2==1
      % D2 is SISO
      [D2inv,SingularFlag] = inv(D2);
      [D1,ElimFlag] = mtimes(D1,D2inv,false(1,2));
   else
      % Use state-space representation
      % Note: Better that D1*inv(D2) because avoids converting inv(ss(D2)) back to TF
      [D2inv,SingularFlag] = inv(ss(D2));
      Dss = mtimes(ss(D1),D2inv,false(1,2));
      ElimFlag = (Dss.Ts~=0 && hasInternalDelay(Dss));
      try
         D1 = tf(Dss);
      catch %#ok<CTCH>
         ctrlMsgUtils.error('Control:combination:InternalDelaysConvert2SS')
      end
   end
else
   SingularFlag = false;  ElimFlag = false;
end
