function [M,SingularFlag] = inv(M)
%INV  Computes the inverse of an input/output model.
%
%   MI = INV(M) computes the inverse model MI such that
%
%       y = M * u   <---->   u = MI * y 
%
%   The model M must have as many inputs as outputs.
%   
%   See also INPUTOUTPUTMODEL/MLDIVIDE, INPUTOUTPUTMODEL/MRDIVIDE, 
%   INPUTOUTPUTMODEL.

%   Author(s): P. Gahinet
%   Copyright 1986-2011 The MathWorks, Inc.
sizes = size(M);
if any(sizes==0),
   M = M.';  SingularFlag = false;  return
elseif sizes(1)~=sizes(2),
   ctrlMsgUtils.error('Control:transformation:inv5')
end
try
   % Convert to combinable type for *
   M = ltipack.matchType('mtimes',M);
   % Invert 
   [M,SingularFlag] = inv_(M);
   if nargout<2 && SingularFlag
      ctrlMsgUtils.warning('Control:transformation:inv2')
   end
   M = invMetaData(M);
catch E
   ltipack.throw(E,'command','inv',class(M))
end
