% UTP_AO_LINCOM a set of UTPs for the ao/lincom method
%
% M Hewitson 06-08-08
%
% $Id: utp_ao_lincom.m,v 1.17 2011/04/17 15:46:21 ingo Exp $
%

% <MethodDescription>
%
% The lincom method of the ao class computes the combination of the input y
% and/or x data.
%
% </MethodDescription>

function results = utp_ao_lincom(varargin)

  % Check the inputs
  if nargin == 0

    % Some keywords
    class   = 'ao';
    mthd    = 'lincom';

    results = [];
    disp('******************************************************');
    disp(['****  Running UTPs for ' class '/' mthd]);
    disp('******************************************************');

    % Test AOs
    [at1,at2,at3,at4,at5,at6,atvec,atmat] = eval(['get_test_objects_' class]);

    % Exception list for the UTPs:
    [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples();

    unit_list = unit.supportedUnits;
    
    % Run the tests
    results = [results utp_01];    % getInfo call
    results = [results utp_02];    % Vector input
    results = [results utp_04];    % List input
    results = [results utp_06];    % Test history is working
    results = [results utp_07];    % Test the modify call works
    results = [results utp_08];    % Test input data shape == output data shape
    results = [results utp_11(mthd, [at1 at1], ple1, plist('COEFFS', [1 2]))];    % Test plotinfo doesn't disappear
    results = [results utp_12];    % Vector + pest input
    results = [results utp_14];    % List + pest input
    
    disp('Done.');
    disp('******************************************************');

  elseif nargin == 1 % Check for UTP functions
    if strcmp(varargin{1}, 'isutp')
      results = 1;
    else
      results = 0;
    end
  else
    error('### Incorrect inputs')
  end

  %% UTP_01

  % <TestDescription>
  %
  % Tests that the getInfo call works for this method.
  %
  % </TestDescription>
  function result = utp_01


    % <SyntaxDescription>
    %
    % Test that the getInfo call works for no sets, all sets, and each set
    % individually.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      % Call for no sets
      io(1) = eval([class '.getInfo(''' mthd ''', ''None'')']);
      % Call for all sets
      io(2) = eval([class '.getInfo(''' mthd ''')']);
      % Call for each set
      for kk=1:numel(io(2).sets)
        io(kk+2) = eval([class '.getInfo(''' mthd ''', ''' io(2).sets{kk} ''')']);
      end
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that getInfo call returned an minfo object in all cases.
    % 2) Check that all plists have the correct parameters.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % check we have minfo objects
      if isa(io, 'minfo')
        %%% SET 'None'
        if ~isempty(io(1).sets), atest = false; end
        if ~isempty(io(1).plists), atest = false; end
        %%% Check all Sets
        if ~any(strcmpi(io(2).sets, 'Default')), atest = false; end
        if numel(io(2).plists) ~= numel(io(2).sets), atest = false; end
        %%%%%%%%%%   SET 'Default'
        if io(3).plists.nparams ~= 1, atest = false; end
        % Check key
        if ~io(3).plists.isparam('coeffs'), atest = false; end
        % Check default value
        if ~isEmptyDouble(io(3).plists.find('coeffs')), atest = false; end
        % Check options
        if ~isequal(io(3).plists.getOptionsForParam('coeffs'), {[]}), atest = false; end
      end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_01

  %% UTP_02

  % <TestDescription>
  %
  % Tests that the lincom method works with a vector of AOs as input.
  %
  % </TestDescription>
  function result = utp_02

    % <SyntaxDescription>
    %
    % Test that the lincom method works for a vector of AOs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      % Coefficients for combination
      u_d = unit(cell2mat(utils.math.randelement(unit_list,1)));
      u_n = unit(cell2mat(utils.math.randelement(unit_list,1)));
      coeffs = ao([0.34 -0.4], plist('yunits', u_n / u_d)); 
      % we need a vector of AOs that are all the same length
      avec = [at5.setYunits(u_d) at6.setYunits(u_d) coeffs];
      out = lincom(avec);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the number of elements in 'out' is 1
    % 2) Check that the output AO contains the correct units
    % 3) Check that the output AO contains the correct data.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out) ~= 1, atest = false; end
      % Check we have the correct units
      if ~isequal(out.yunits, u_n), atest = false; end            
      % Check each output against the linear combination of the inputs      
      com = zeros(size(avec(1).y));
      for kk = 1:(numel(avec)-1)
        com = com + avec(numel(avec)).y(kk).*avec(kk).y;
      end
      if ~isequal(com, out.data.getY), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_02

 
  %% UTP_04

  % <TestDescription>
  %
  % Tests that the lincom method works with a list of AOs as input.
  %
  % </TestDescription>
  function result = utp_04

    % <SyntaxDescription>
    %
    % Test that the lincom method works for a list of AOs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      % Coefficients for combination      
      u_d = unit(cell2mat(utils.math.randelement(unit_list,1)));
      u_n = unit(cell2mat(utils.math.randelement(unit_list,1)));
      coeffs = ao([0.34 -0.4 0.1], plist('yunits', u_n / u_d)); 
      out = lincom(at5.setYunits(u_d), at6.setYunits(u_d), at5.setYunits(u_d), coeffs);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the number of elements in 'out' is 1.
    % 2) Check that the output AO contains the correct units
    % 3) Check that the output AO contains the correct data.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out) ~= 1, atest = false; end      
      % Check we have the correct units
      if ~isequal(out.yunits, u_n), atest = false; end      
      % Check each output against the linear combination of the inputs
      com = coeffs.y(1).*at5.y + coeffs.y(2).*at6.y + coeffs.y(3).*at5.y;
      if ~isequal(com, out.y), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_04

 
  %% UTP_06

  % <TestDescription>
  %
  % Tests that the lincom method properly applies history.
  %
  % </TestDescription>
  function result = utp_06

    % <SyntaxDescription>
    %
    % Test that the result of applying the lincom method can be processed back
    % to an m-file.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      a = at1.setYunits('Hz');
      out  = lincom(a, a, ao([1 -1]));
      mout = rebuild(out);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the last entry in the history of 'out' corresponds to
    %    'lincom'.
    % 2) Check that the re-built object is the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the last step in the history of 'out'
      if ~strcmp(out.hist.methodInfo.mname, 'lincom'), atest = false; end
      % Check the re-built object
      if ~eq(mout, out, ple2), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_06

  %% UTP_07

  % <TestDescription>
  %
  % Tests that the lincom method can not modify the input AO.
  %
  % </TestDescription>
  function result = utp_07

    % <SyntaxDescription>
    %
    % Test that the lincom method can not modify the input AO.
    % The method must throw an error for the modifier call.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      % copy at1 to work with
      ain = ao(at5);
      % modify ain
      ain.lincom(at6, plist('Coeffs', [1 -1]));
      % </SyntaxCode>
      stest = false;
    catch err
      stest = true;
    end

    % <AlgoDescription>
    %
    % 1) Nothing to check.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_07

  %% UTP_08

  % <TestDescription>
  %
  % Test the shape of the output.
  %
  % </TestDescription>
  function result = utp_08

    % <SyntaxDescription>
    %
    % Test that the plus method keeps the data shape of the input object. The
    % input AO must be an AO with row data and an AO with column data.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      a = at5.setYunits('Hz');
      b = at6.setYunits('Hz');
      out1 = lincom(a, b, ao([1 -1]));
      out2 = lincom(b, a, ao([1 -1]));
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the shpe of the output data doesn't change.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the shape of the output data
      if size(out1.data.y, 2) ~= 1, atest = false; end
      if size(out2.data.y, 1) ~= 1, atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_08
  
    %% UTP_12

  % <TestDescription>
  %
  % Tests that the lincom method works with a vector of AOs as input + a pest object.
  %
  % </TestDescription>
  function result = utp_12

    % <SyntaxDescription>
    %
    % Test that the lincom method works for a vector of AOs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      % Coefficients for combination
      u_1 = unit(cell2mat(utils.math.randelement(unit_list,1)));
      u_2 = unit(cell2mat(utils.math.randelement(unit_list,1)));
      u_c1 = unit(cell2mat(utils.math.randelement(unit_list,1)));            
      coeffs = pest(plist('y', [0.34 -0.4], 'paramnames', {'i','ii'}, 'yunits', {u_c1, u_c1 * u_1 / u_2}));
      % we need a vector of AOs that are all the same length
      avec = [at5.setYunits(u_1) at6.setYunits(u_2)];
      out = lincom(avec, coeffs);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the number of elements in 'out' is 1
    % 2) Check that the output AO contains the correct units
    % 3) Check that the output AO contains the correct data.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out) ~= 1, atest = false; end
      % Check we have the correct units
      if ~eq(out.yunits, u_c1 * u_1), atest = false; end         
      % Check each output against the linear combination of the inputs      
      com = zeros(size(avec(1).y));
      for kk = 1:numel(avec)
        com = com + avec(kk).y.*coeffs.y(kk);
      end
      if ~isequal(com, out.data.getY), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end  % END UTP_12

 
  %% UTP_14

  % <TestDescription>
  %
  % Tests that the lincom method works with a list of AOs as input + a pest object.
  %
  % </TestDescription>
  function result = utp_14

    % <SyntaxDescription>
    %
    % Test that the lincom method works for a list of AOs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      % Coefficients for combination      
      u_d = unit(cell2mat(utils.math.randelement(unit_list,1)));
      u_n = unit(cell2mat(utils.math.randelement(unit_list,1)));
      coeffs = ao([0.34 -0.4 0.1], plist('yunits', u_n / u_d));  % TODO: Use a pest with different units!!
      out = lincom(at5.setYunits(u_d), at6.setYunits(u_d), at5.setYunits(u_d), coeffs);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the number of elements in 'out' is 1.
    % 2) Check that the output AO contains the correct units
    % 3) Check that the output AO contains the correct data.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out) ~= 1, atest = false; end      
      % Check we have the correct units
      if ~isequal(out.yunits, u_n), atest = false; end      
      % Check each output against the linear combination of the inputs
      com = coeffs.y(1).*at5.y + coeffs.y(2).*at6.y + coeffs.y(3).*at5.y;
      if ~isequal(com, out.y), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end  % END UTP_14

end
