function sys = times(sys1,sys2)
%TIMES  Multiplies two dynamic systems I/O-pairwise.
%
%   SYS = TIMES(SYS1,SYS2) performs SYS = SYS1 .* SYS2. This operation
%   amounts to an element-by-element multiplication of the transfer
%   functions of the dynamic systems SYS1 and SYS2.
%
%   If SYS1 and SYS2 are arrays of dynamic systems, their .* product is a
%   system array SYS with the same number of models where the k-th system
%   is obtained by
%      SYS(:,:,k) = SYS1(:,:,k) .* SYS2(:,:,k)
%
%   See also DYNAMICSYSTEM/MTIMES, SERIES, DYNAMICSYSTEM.

%   Author(s): P. Gahinet
%   Copyright 1986-2012 The MathWorks, Inc.
try
   if ~ltipack.hasMatchingType('times',sys1,sys2)
      % Harmonize types
      [sys1,sys2] = ltipack.matchType('times',sys1,sys2);
   end
   
   % Check I/O dimensions and handle scalar multiplication
   sizes1 = sys1.IOSize_;
   sizes2 = sys2.IOSize_;
   ScalarFlags = false(1,2);
   if all(sizes1(1:2)==1) && any(sizes2(1:2)~=1)
      % SYS1 is scalar
      if any(sizes2==0)
         % Scalar * Empty = Empty
         sys = sys2;   return
      else
         ScalarFlags(1) = true;
      end
   elseif all(sizes2(1:2)==1) && any(sizes1(1:2)~=1)
      % SYS2 is scalar
      if any(sizes1==0)
         % Scalar * Empty = Empty
         sys = sys1;   return
      else
         ScalarFlags(2) = true;
      end
   elseif ~any(ScalarFlags) && (sizes1(1)~=sizes2(1) || sizes1(2)~=sizes2(2))
      error(message('Control:combination:IncompatibleIODims'))
   end
   
   % Combine data
   sys = times_(sys1,sys2,ScalarFlags);% overloadable since sys1,sys2 are of the same class
   
   % Combine metadata
   if ScalarFlags(1)
      % Scalar multiplication: keep SYS2's metadata if SYS is scalar
      sys = copyMetaData(sys2,sys);
   elseif ScalarFlags(2)
      sys = copyMetaData(sys1,sys);
   else
      sys = plusInput(sys,sys1,sys2);
      sys = plusOutput(sys,sys1,sys2);
      sys.Name_ = '';  sys.Notes_ = strings(0,1);  sys.UserData = [];
   end
catch E
   ltipack.throw(E,'operator','.*',class(sys1))
end

