% UTP_PZMODEL_SIMPLIFY a set of UTPs for the pzmodel/simplify method
%
% M Hewitson 06-08-08
%
% $Id: utp_pzmodel_simplify.m,v 1.3 2009/07/23 18:56:38 ingo Exp $
%

% <MethodDescription>
%
% The simplify method of the pzmodel class simplifies pzmodels by
% cancelling like poles with like zeros.
%
% </MethodDescription>

function results = utp_pzmodel_simplify(varargin)

  % Check the inputs
  if nargin == 0

    % Some keywords
    class   = 'pzmodel';
    mthd    = 'simplify';

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

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

    % Run the tests
    results = [results utp_01];    % getInfo call
    results = [results utp_02];    % Vector input
    results = [results utp_03];    % Matrix input
    results = [results utp_04];    % List input
    results = [results utp_05];    % Test with mixed input
    results = [results utp_06];    % Test history is working
    results = [results utp_07];    % Test the modify call works
    results = [results utp_08];    % Test cancle poles at different positions

    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('tol'), atest = false; end
        % Check default value
        if ~isequal(io(3).plists.find('tol'), 1e-10), atest = false; end
        % Check options
        if ~isequal(io(3).plists.getOptionsForParam('tol'), {1e-10}), 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 simplify method works with a vector of PZMODELs as input.
  %
  % </TestDescription>
  function result = utp_02

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

    try
      % <SyntaxCode>
      pz1 = pzmodel(2, [pz(1) pz(1,2) pz(3)], [pz(3) pz(1,2) pz(3)]);
      pzv = [pz1, pz1, pz1];
      out = simplify(pzv);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the number of elements in 'out' is the same as in 'pzv'
    % 2) Check the poles of the output
    % 3) Check the zeros of the output
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      if ~isequal(size(out), size(pzv)), atest = false; end
      for kk = 1:numel(pzv)
        % Check 'poles'
        if ~eq(out(kk).poles, pz(1)), atest = false; end
        % Check 'zeros'
        if ~eq(out(kk).zeros, pz(3)), atest = false; end
      end
      % </AlgoCode>
    else
      atest = false;
    end

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

  end % END UTP_02

  %% UTP_03

  % <TestDescription>
  %
  % Tests that the simplify method works with a matrix of PZMODELs as input.
  %
  % </TestDescription>
  function result = utp_03

    % <SyntaxDescription>
    %
    % Tests that the simplify method works with a matrix of PZMODELs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      pz1 = pzmodel(2, [pz(1) pz(1,2) pz(3)], [pz(3) pz(1,2) pz(3)]);
      pzm = [pz1, pz1, pz1; pz1, pz1, pz1];
      out = simplify(pzm);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the number of elements in 'out' is the same as in 'pzm'
    % 2) Check the poles of the output
    % 3) Check the zeros of the output
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      if ~isequal(size(out), size(pzm)), atest = false; end
      for kk = 1:numel(pzm)
        % Check 'poles'
        if ~eq(out(kk).poles, pz(1)), atest = false; end
        % Check 'zeros'
        if ~eq(out(kk).zeros, pz(3)), atest = false; end
      end
      % </AlgoCode>
    else
      atest = false;
    end

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

  end % END UTP_03

  %% UTP_04

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

    % <SyntaxDescription>
    %
    % Tests that the simplify method works with a list of PZMODELs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      pz1 = pzmodel(2, [pz(1) pz(1,2) pz(3)], [pz(3) pz(1,2) pz(3)]);
      out = simplify(pz1, pz1, pz1);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the number of elements in 'out' is the same as in the input
    % 2) Check the poles of the output
    % 3) Check the zeros of the output
    %
    % </AlgoDescription>

    atest = true;
    pzin  = [pz1, pz1, pz1];
    if stest
      % <AlgoCode>
      if ~isequal(size(out), size(pzin)), atest = false; end
      for kk = 1:numel(pzin)
        % Check 'poles'
        if ~eq(out(kk).poles, pz(1)), atest = false; end
        % Check 'zeros'
        if ~eq(out(kk).zeros, pz(3)), atest = false; end
      end
      % </AlgoCode>
    else
      atest = false;
    end

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

  end % END UTP_04

  %% UTP_05

  % <TestDescription>
  %
  % Tests that the simplify method works with a mix of different shaped
  % PZMODELs as input.
  %
  % </TestDescription>
  function result = utp_05

    % <SyntaxDescription>
    %
    % Tests that the simplify method works with a mix of different shaped
    % PZMODELs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      pz1  = pzmodel(2, [pz(1) pz(1,2) pz(3)], [pz(3) pz(1,2) pz(3)]);
      out  = simplify(pz1, [pz1, pz1], pz1, [pz1, pz1;pz1, pz1]);
      mout = rebuild(out);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the number of elements in 'out' is the same as in the input
    % 2) Check the poles of the output
    % 3) Check the zeros of the output
    % 4) Check the re-built object
    %
    % </AlgoDescription>

    atest = true;
    pzin  = [pz1, pz1, pz1, pz1, pz1, pz1, pz1, pz1];
    if stest
      % <AlgoCode>
      if ~isequal(size(out), size(pzin)), atest = false; end
      for kk = 1:numel(pzin)
        % Check 'poles'
        if ~eq(out(kk).poles, pz(1)), atest = false; end
        % Check 'zeros'
        if ~eq(out(kk).zeros, pz(3)), atest = false; end
      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_05

  %% UTP_06

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

    % <SyntaxDescription>
    %
    % Test that the result of applying the simplify method can be processed
    % back.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      pz1  = pzmodel(2, [pz(1) pz(1,2) pz(3)], [pz(3) pz(1,2) pz(3)]);
      out  = simplify(pz1);
      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
    %    'simplify'.
    % 2) Check that re-built object is the same object as the input.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the last step in the history of 'out'
      if ~strcmp(out.hist.methodInfo.mname, 'simplify'), atest = false; end
      % The rebuilt object must be the same as 'out'
      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 simplify method can modify the input PZMODEL.
  %
  % </TestDescription>
  function result = utp_07

    % <SyntaxDescription>
    %
    % Test that the simplify method can modify the input PZMODEL by calling
    % with no output and that the method doesn't change the input of the
    % function notation (with a equal sign).
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      pz1  = pzmodel(2, [pz(1) pz(1,2) pz(3)], [pz(3) pz(1,2) pz(3)]);
      amodi = pzmodel(pz1);
      aeq   = pzmodel(pz1);
      out = aeq.simplify();
      amodi.simplify();
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that 'out' and 'aeq' are now different.
    % 2) Check that 'aeq' is not changed
    % 3) Check that the modified input is the simplified value of the copy
    % 4) Check that out and amodi are the same
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check that 'out' and 'aeq' are now different.
      if eq(out, aeq, ple2), atest = false; end
      % Check that 'aeq' is not changed
      if ~eq(aeq, pzmodel(pz1), ple1), atest = false; end
      % Check that the modified input is simplified
      if ~eq(amodi.poles, pz(1)), atest = false; end
      if ~eq(amodi.zeros, pz(3)), atest = false; end
      % Check that out and amodi are the same
      if ~eq(out, amodi, ple1), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

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

  %% UTP_08

  % <TestDescription>
  %
  % Check that the simplify method cancle poles/zeros at different
  % positions.
  %
  % </TestDescription>
  function result = utp_08

    % <SyntaxDescription>
    %
    % Check that the simplify method cancle poles/zeros at different
    % positions.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      % Cancle at POLE: End
      pz1 = pzmodel(1, [pz(1), pz(1,2)], [pz(3), pz(1,2), pz(4)]);
      pz1.simplify();
      % Cancle at POLE: End + Middle
      pz2 = pzmodel(1, [pz(1), pz(2), pz(7), pz(1,2)], [pz(1), pz(6,2), pz(4), pz(2)]);
      pz2.simplify();
      % Cancle at POLE: End + Begin
      pz3 = pzmodel(1, [pz(1), pz(2), pz(7), pz(1,2)], [pz(1), pz(1,2), pz(4)]);
      pz3.simplify();
      % Cancle at POLE: Begin
      pz4 = pzmodel(1, [pz(1), pz(1,2)], [pz(1), pz(3,2), pz(4)]);
      pz4.simplify();
      % Cancle at POLE: Begin + Middle
      pz5 = pzmodel(1, [pz(1), pz(1,2), pz(9)], [pz(1), pz(1,2)]);
      pz5.simplify();
      % Cancle at POLE: Begin + Middle + End
      pz6 = pzmodel(1, [pz(1), pz(3), pz(1,2), pz(9)], [pz(1), pz(9), pz(5), pz(1,2)]);
      pz6.simplify();
      % Cancle at ZERO: End
      pz7 = pzmodel(1, [pz(5), pz(1,2), pz(4)], [pz(1), pz(1,2)]);
      pz7.simplify();
      % Cancle at ZERO: End + Middle
      pz8 = pzmodel(1, [pz(1), pz(1,2), pz(4), pz(2)], [pz(1+2i), pz(2), pz(7), pz(1,2)]);
      pz8.simplify();
      % Cancle at ZERO: End + Begin
      pz9 = pzmodel(1, [pz(1), pz(1,2), pz(4)], [pz(1), pz(2), pz(7), pz(1,2)]);
      pz9.simplify();
      % Cancle at ZERO: Begin
      pz10 = pzmodel(1, [pz(1), pz(3,2), pz(4)], [pz(1), pz(1,2)]);
      pz10.simplify();
      % Cancle at ZERO: Begin + Middle
      pz11 = pzmodel(1, [pz(1), pz(1,2)], [pz(1), pz(1,2), pz(9)]);
      pz11.simplify();
      % Cancle at ZERO: Begin + Middle + End
      pz12 = pzmodel(1, [pz(1), pz(9), pz(5), pz(1,2)], [pz(1), pz(3), pz(1,2), pz(9)]);
      pz12.simplify();
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check the poles and peros of the output.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      if ~eq(pz1.poles, pz(1)), atest = false; end
      if ~eq(pz1.zeros, [pz(3), pz(4)]), atest = false; end
      if ~eq(pz2.poles, [pz(7), pz(1,2)]), atest = false; end
      if ~eq(pz2.zeros, [pz(6,2), pz(4)]), atest = false; end
      if ~eq(pz3.poles, [pz(2), pz(7)]), atest = false; end
      if ~eq(pz3.zeros, pz(4)), atest = false; end
      if ~eq(pz4.poles, pz(1,2)), atest = false; end
      if ~eq(pz4.zeros, [pz(3,2), pz(4)]), atest = false; end
      if ~eq(pz5.poles, pz(9)), atest = false; end
      if ~isempty(pz5.zeros), atest = false; end
      if ~eq(pz6.poles, pz(3)), atest = false; end
      if ~eq(pz6.zeros, pz(5)), atest = false; end
      if ~eq(pz7.poles, [pz(5), pz(4)]), atest = false; end
      if ~eq(pz7.zeros, pz(1)), atest = false; end
      if ~eq(pz8.poles, [pz(1), pz(4)]), atest = false; end
      if ~eq(pz8.zeros, [pz(1+2i), pz(7)]), atest = false; end
      if ~eq(pz9.poles, pz(4)), atest = false; end
      if ~eq(pz9.zeros, [pz(2), pz(7)]), atest = false; end
      if ~eq(pz10.poles, [pz(3,2), pz(4)]), atest = false; end
      if ~eq(pz10.zeros, pz(1,2)), atest = false; end
      if ~isempty(pz11.poles), atest = false; end
      if ~eq(pz11.zeros, pz(9)), atest = false; end
      if ~eq(pz12.poles, pz(5)), atest = false; end
      if ~eq(pz12.zeros, pz(3)), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

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

end
