function [D1,SingularFlag,ElimFlag] = mldivide(D1,D2)
% Computes D = D1\D2

%   Copyright 1986-2010 The MathWorks, Inc.

% Square system, no support for scalar division
[ny1,~] = iosize(D1);
if ny1>0
   if hasdelay(D1)
      ctrlMsgUtils.error('Control:combination:divide1','SYS1\SYS2','SYS1')
   elseif ny1==1
      % D1 is SISO
      [D1inv,SingularFlag] = inv(D1);
      [D1,ElimFlag] = mtimes(D1inv,D2,false(1,2));
   else
      % Use state-space representation
      % Note: Better that inv(D1)*D2 because avoids converting inv(ss(D1)) back to TF
      [D1inv,SingularFlag] = inv(ss(D1));
      Dss = mtimes(D1inv,ss(D2),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
