function [D1,ElimFlag] = plus(D1,D2)
% Adds two transfer functions.

%   Copyright 1986-2012 The MathWorks, Inc.

% Equalize input, output, and I/O delays (may fail in continuous-time)
if hasdelay(D1) || hasdelay(D2)
   ZeroIO1 = cellfun(@localIsZero,D1.num);
   ZeroIO2 = cellfun(@localIsZero,D2.num);
   [D1,D2,ElimFlag] = plusDelay(D1,D2,...
      struct('Input',all(ZeroIO1,1),'Output',all(ZeroIO1,2),'IO',ZeroIO1),...
      struct('Input',all(ZeroIO2,1),'Output',all(ZeroIO2,2),'IO',ZeroIO2));
else
   ElimFlag = false;
end

% Compute resulting model
[ny,nu] = size(D1.num);
num = cell(ny,nu);
den = cell(ny,nu);
for ct=1:ny*nu
   [num{ct},den{ct}] = utAddSISO(D1.num{ct},D1.den{ct},D2.num{ct},D2.den{ct});
end
% Eliminate extra leading zeros generated, e.g., in s^2+s (assumption that 
% NUM(1) or DEN(1) is nonzero is pervasive in @tfdata code)
[D1.num,D1.den] = ltipack.tfdata.utTrimNumDen(num,den,false);

%--------------
function boo = localIsZero(x)
boo = all(x==0);
