% TIMES implements element multiplication for cdata objects.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: TIMES implements element multiplication for two cdata objects.
%
% CALL:
%              a = d1.*d2
%              a = times(d1,d2);
%
%
% <a href="matlab:utils.helper.displayMethodInfo('cdata', 'times')">Parameters Description</a>
%
% VERSION:     $Id: times.m,v 1.2 2011/04/08 08:56:34 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = times(varargin)
  
  % get two data objects
  d1 = copy(varargin{1}, nargout);
  d2 = copy(varargin{2}, nargout);
  
  % check units
  d1.yunits.simplify();
  d2.yunits.simplify();
    
  % add the data
  dout = applyoperator(d1,d2,'times');
  
  % handle units: multiply the units
  dout.yunits = d1.yunits.*d2.yunits;
  
  % handle errors
  err = @(err1, err2, val1, val2) sqrt( (err1./val1).^2 + (err2./val2).^2 ) .* abs(val1.*val2);
  if ~isempty(d1.dy) || ~isempty(d2.dy)    
    if isempty(d1.dy)
      d1.dy = zeros(size(d2.dy));
    end
    if isempty(d2.dy)
      d2.dy = zeros(size(d1.dy));
    end    
    dout.dy = err(d1.dy, d2.dy, d1.y, d2.y);
  else
    dout.dy = [];
  end

  % Single output
  varargout{1} = dout;
  
end

