function [M,SingularFlag] = mldivide(M1,M2)
%MLDIVIDE  Left division for static models.
%
%   M = MLDIVIDE(M1,M2) is invoked by M=M1\M2 and is equivalent to 
%   M = INV(M1)*M2.
%
%   See also STATICMODEL/MRDIVIDE, STATICMODEL/INV, STATICMODEL/MTIMES.

%   Author(s): P. Gahinet
%   Copyright 1986-2010 The MathWorks, Inc.
try
   if ltipack.hasMatchingType('mtimes',M1,M2)
      % All operands are of the same type
      % Size checking
      ios1 = M1.IOSize_;
      ios2 = M2.IOSize_;
      ScalarFlags = [all(ios1==1) all(ios2==1)];
      if ios1(1)~=ios1(2)
         error(message('Control:combination:divide2','M1\M2','M1'))
      elseif ios1(2)~=ios2(1) && ~any(ScalarFlags)
         error(message('Control:combination:IncompatibleIODims'))
      end
      [iM1,SingularFlag] = inv(M1);
      M = iM1*M2;
   else
      % Harmonize types and try again
      if isnumeric(M1) || ~hasSimpleInverse_(M1)
         [M1,M2] = ltipack.matchType('mtimes',M1,M2);
         [M,SingularFlag] = mldivide(M1,M2);
      else
         % Invert before matching types for SISO TF, ZPK, PID
         [iM1,SingularFlag] = inv(M1);
         [iM1,M2] = ltipack.matchType('mtimes',iM1,M2);
         M = mtimes(iM1,M2);
      end
   end
   if nargout<2 && SingularFlag
      warning(message('Control:combination:SingularLeftDivision'))
   end
catch E
   throw(E)
end
