function [Dm,SingularFlag] = mpower(D,m)
% Integer powers of state-space models.
% Note: m can be positive or negative.

%   Copyright 1986-2020 The MathWorks, Inc.
SingularFlag = false;
if m==0
   [~,nio] = iosize(D);
   Dm = createGain(D,eye(nio));
else
   % General case: perform M-1 products
   Dm = D;
   for j=2:abs(m)
      Dm = mtimes(Dm,D);
   end
   % Invert result if m<0
   if m<0
      [Dm,SingularFlag] = inv(Dm);
   end
end
