% RATIONAL rational representation of a transfer function.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: RATIONAL rational representation of a transfer function.
%
%           a(1)s^m + a(2)s^{m-1} + ... + a(m+1)
%   H(s) = --------------------------------------
%           b(1)s^n + b(2)s^{n-1} + ... + b(n+1)
%
% CONSTRUCTOR:
%
%       r = rational()                 - creates an empty rational object
%       r = rational(nun, den)         - construct from numerator and
%                                        denominator coefficients
%       r = rational(num, den, 'name') - construct including name
%       r = rational(num, den,         - construct from num, den, and io-units
%                    iunits, ounits)
%       r = rational(pl)               - create a rational object from the
%                                        description given in the parameter list.
%       r = rational(pzm)              - convert the TF described by the
%                                        pzmodel into a rational TF.
%
% Example constructor plists:
%
% Example: plist('filename', 'rational1.xml')
% Example: plist('filename', 'rational1.mat')
% Example: pzm = pzmodel(1, {1 2 3}, {4 5})
%          plist('pzmodel', pzm)
%
% Example: pzm = pzmodel(1, {1 2 3}, {4 5})
%          pl  = plist('pzmodel', pzm)
%          plist('PLIST', pl)
%
% <a href="matlab:utils.helper.displayMethodInfo('rational', 'rational')">Parameters Description</a>
%
% VERSION:     $Id: rational.m,v 1.45 2011/08/15 12:58:59 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

classdef rational < ltpda_tf
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %                            Property definition                            %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  %---------- Public (read/write) Properties  ----------
  properties
  end
  
  %---------- Protected read-only Properties ----------
  properties (SetAccess = protected)
    num = []; % numerator coefficients [a]
    den = []; % denominator coefficients [b]
  end
  
  %---------- Private Properties ----------
  properties (GetAccess = protected, SetAccess = protected)
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %                          Check property setting                           %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  methods
    function set.num(obj, val)
      if ~isnumeric(val) && ~isempty(val)
        error('### The value for the property ''num'' must be a numeric array.');
      end
      obj.num = val;
    end
    function set.den(obj, val)
      if ~isnumeric(val) && ~isempty(val)
        error('### The value for the property ''den'' must be a numeric array.');
      end
      obj.den = val;
    end
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %                                Constructor                                %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  methods
    function obj = rational(varargin)
      
      import utils.const.*
      utils.helper.msg(msg.OMNAME, 'running %s/%s', mfilename('class'), mfilename);
      
      % Collect all pzmodel objects
      [rationals, invars, rest] = utils.helper.collect_objects(varargin(:), 'rational');
      
      if isempty(rest) && ~isempty(rationals)
        % Do copy constructor and return
        utils.helper.msg(msg.OPROC1, 'copy constructor');
        obj = copy(rationals, 1);
        for kk=1:numel(obj)
          obj(kk).addHistory(rational.getInfo('rational', 'None'), [], [], obj(kk).hist);
        end
        return
      end
      
      switch nargin
        case 0
          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
          %%%%%%%%%%%% Zero inputs %%%%%%%%%%%%%%%
          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
          utils.helper.msg(msg.OPROC1, 'empty constructor');
          obj.addHistory(rational.getInfo('rational', 'None'), plist(), [], []);
          
        case 1
          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
          %%%%%%%%%%%% One inputs %%%%%%%%%%%%%%%
          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
          
          if ischar(varargin{1})
            %%%%%%%%%%   pzm = pzmodel('foo.mat')   %%%%%%%%%%
            %%%%%%%%%%   pzm = pzmodel('foo.xml')   %%%%%%%%%%
            utils.helper.msg(msg.OPROC1, 'constructing from file %s', varargin{1});
            obj = fromFile(obj, varargin{1});
            
          elseif isstruct(varargin{1})
            %%%%%%%%%%   r = rational(struct)   %%%%%%%%%%
            utils.helper.msg(msg.OPROC1, 'constructing from struct');
            obj = fromStruct(obj, varargin{1});
            
          elseif isa(varargin{1}, 'pzmodel')
            %%%%%%%%%%   r = rational(pzm)   %%%%%%%%%%
            utils.helper.msg(msg.OPROC1, 'constructing from pzmodel');
            pl = plist('pzmodel', varargin{1});
            obj = fromPzmodel(obj, pl);
            
          elseif isa(varargin{1}, 'parfrac')
            %%%%%%%%%%   r = rational(pf)   %%%%%%%%%%
            utils.helper.msg(msg.OPROC1, 'constructing from parfrac');
            pl = plist('parfrac', varargin{1});
            obj = fromParfrac(obj, pl);
            
          elseif isa(varargin{1}, 'plist')
            %%%%%%%%%%   r = rational(plist)   %%%%%%%%%%
            pl = varargin{1};
            
            % Selection of construction method
            if pl.isparam('filename')
              utils.helper.msg(msg.OPROC1, 'constructing from file %s', pl.find('filename'));
              obj = fromFile(obj, pl);
              
            elseif pl.isparam('hostname') || pl.isparam('conn')
              utils.helper.msg(msg.OPROC1, 'constructing from repository %s', pl.find('hostname'));
              obj = obj.fromRepository(pl);
              
            elseif pl.isparam('num') || pl.isparam('den')
              utils.helper.msg(msg.OPROC1, 'constructing from coefficients');
              obj = fromCoefficients(obj, pl);
              
            elseif pl.isparam('pzmodel')
              utils.helper.msg(msg.OPROC1, 'constructing from pole/zero model');
              obj = fromPzmodel(obj, pl);
              
            elseif pl.isparam('parfrac')
              utils.helper.msg(msg.OPROC1, 'constructing from parfrac object');
              obj = fromParfrac(obj, pl);
              
            elseif pl.isparam('built-in')
              utils.helper.msg(msg.OPROC1, 'constructing from built-in model');
              obj = fromModel(obj, pl);
              
            elseif pl.isparam('Plist')
              ipl = find(pl, 'Plist');
              obj = rational(ipl);
              
            else
              obj.setObjectProperties(pl);
              obj.addHistory(rational.getInfo('rational', 'None'), pl, [], []);
            end
          else
            error('### Unknown single argument constructor.');
          end
          
        case 2
          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
          %%%%%%%%%%%%% Two inputs %%%%%%%%%%%%%%%
          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
          if (isa(varargin{1}, 'database') || isa(varargin{1}, 'java.sql.Connection')) && isnumeric(varargin{2})
            %%%%%%%%%%  f = rational(<database-object>, [IDs])   %%%%%%%%%%
            utils.helper.msg(msg.OPROC1, 'retrieve from repository');
            obj = obj.fromRepository(plist('conn', varargin{1}, 'id', varargin{2}));
            
          elseif isnumeric(varargin{1}) && isnumeric(varargin{2})
            %%%%%%%%%  f = rational(num,den) %%%%%%%%%
            obj = fromCoefficients(obj, plist('num', varargin{1}, 'den', varargin{2}));
            
          elseif isa(varargin{1}, 'rational') && isa(varargin{2}, 'plist') && isempty(varargin{2}.params)
            % pass to copy constructor
            obj = rational(varargin{1});
            
          elseif isa(varargin{1}, 'org.apache.xerces.dom.DeferredElementImpl') && ...
                 isa(varargin{2}, 'history')
            %%%%%%%%%%   obj = rational(DOM node, history-objects)   %%%%%%%%%%
            obj = fromDom(obj, varargin{1}, varargin{2});
            
          elseif isa(varargin{1}, 'ltpda_uoh') && isa(varargin{2}, 'plist')
            %%%%%%%%%%%   rational(<ltpda_uoh>-object, plist-object)   %%%%%%%%%%
            % always recreate from plist
            
            % If we are trying to load from file, and the file exists, do
            % that. Otherwise, copy the input object.
            if varargin{2}.isparam('filename')
              if exist(fullfile('.', find(varargin{2}, 'filename')), 'file')==2
                obj = rational(varargin{2});
              else
                obj = rational(varargin{1});
              end
            else
              obj = rational(varargin{2});
            end
          else
            error('### Unknown 2 argument constructor.');
          end
          
        case 3
          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
          %%%%%%%%%%%% Three inputs %%%%%%%%%%%%%%
          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
          
          %%%%%%%%%%   r = rational(num, den, name)   %%%%%%%%%%
          utils.helper.msg(msg.OPROC1, 'constructing from coefficients');
          pl = plist('num', varargin{1}, 'den', varargin{2}, 'name', varargin{3});
          obj = fromCoefficients(obj, pl);

        case 4
          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
          %%%%%%%%%%%% Four inputs %%%%%%%%%%%%%%%
          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
          %%%%%%%%%%   pzm = rational(num, den, iunits, ounits)   %%%%%%%%%%
          utils.helper.msg(msg.OPROC1, 'constructing from coefficients');
          pl = plist('num', varargin{1}, 'den', varargin{2}, 'iunits', varargin{3}, 'ounits', varargin{4});
          obj = fromCoefficients(obj, pl);
          
        case 5
          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
          %%%%%%%%%%%% five inputs %%%%%%%%%%%%%%%
          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
          %%%%%%%%%%   pzm = rational(num, den, name, iunits, ounits)   %%%%%%%%%%
          utils.helper.msg(msg.OPROC1, 'constructing from coefficients');
          pl = plist('num', varargin{1}, 'den', varargin{2}, 'name', varargin{3}, 'iunits', varargin{4}, 'ounits', varargin{5});
          obj = fromCoefficients(obj, pl);
          
        otherwise
          [rationals, invars, rest] = utils.helper.collect_objects(varargin, 'rational');
          
          %%% Do we have a list of RATIONAL objects as input
          if ~isempty(rationals) && isempty(rest)
            obj = rational(rationals);
          else
            error('### Unknown number of arguments.');
          end
      end
      
    end % End constructor
    
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %                            Methods (static)                               %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  methods (Static)
    
    function mdls = getBuiltInModels(varargin)
      mdls = ltpda_uo.getBuiltInModels('rational');
    end
    
    function out = VEROUT()
      out = '$Id: rational.m,v 1.45 2011/08/15 12:58:59 hewitson Exp $';
    end
    
    function ii = getInfo(varargin)
      ii = utils.helper.generic_getInfo(varargin{:}, 'rational');
    end
    
    function out = SETS()
      out = [SETS@ltpda_uoh,   ...
        {'From Pzmodel'},      ...
        {'From Coefficients'}, ...
        {'From Parfrac'}];
    end
    
    function plout = getDefaultPlist(set)
      persistent pl;
      persistent lastset;
      if exist('pl', 'var')==0 || isempty(pl) || ~strcmp(lastset, set)
        pl = rational.buildplist(set);
        lastset = set;
      end
      plout = pl;
    end
    
    function out = buildplist(set)
      
      if ~utils.helper.ismember(lower(rational.SETS), lower(set))
        error('### Unknown set [%s]', set);
      end
      
      out = plist();
      out = rational.addGlobalKeys(out);
      out = buildplist@ltpda_uoh(out, set);
      
      switch lower(set)
        case 'from pzmodel'
          % pzmodel
          p = param({'pzmodel','Construct from a pole/zero model.'}, {1, {pzmodel}, paramValue.OPTIONAL});
          out.append(p);
          % Iunits
          p = param({'iunits','The input units of the model.'}, paramValue.EMPTY_STRING);
          out.append(p);
          
          % Ounits
          p = param({'ounits','The output units of the model.'}, paramValue.EMPTY_STRING);
          out.append(p);
          
        case 'from parfrac'
          % parfrac
          p = param({'parfrac','Construct from a partial fraction model.'}, {1, {parfrac}, paramValue.OPTIONAL});
          out.append(p);
          % Iunits
          p = param({'iunits','The input units of the model.'}, paramValue.EMPTY_STRING);
          out.append(p);
          
          % Ounits
          p = param({'ounits','The output units of the model.'}, paramValue.EMPTY_STRING);
          out.append(p);
          
        case 'from coefficients'
          % Num
          p = param({'num','Vector of coefficients.'}, paramValue.EMPTY_DOUBLE);
          out.append(p);
          
          % Den
          p = param({'den','Vector of coefficients.'}, paramValue.EMPTY_DOUBLE);
          out.append(p);
          % Iunits
          p = param({'iunits','The input units of the model.'}, paramValue.EMPTY_STRING);
          out.append(p);
          
          % Ounits
          p = param({'ounits','The output units of the model.'}, paramValue.EMPTY_STRING);
          out.append(p);
      end
    end % function out = getDefaultPlist(varargin)
    
    function obj = initObjectWithSize(n,m)
      obj = rational.newarray([n m]);
    end
    
  end % End static methods
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %                         Methods (static, private)                         %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  methods (Static, Access=private)
  end % End static, private methods
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %                         Methods (static, hidden)                          % 
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  methods (Static = true, Hidden = true)
    varargout = loadobj(varargin)
    varargout = update_struct(varargin);
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %                             Methods (public)                              %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  methods
    varargout = char(varargin)
    varargout = display(varargin)
    varargout = copy(varargin)
  end
  
  methods (Hidden = true)
    varargout = attachToDom(varargin)
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %                            Methods (protected)                            %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  methods (Access = protected)
    varargout = respCore(varargin)
    varargout = fromStruct(varargin)
    varargout = fromDom(varargin)
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %                           Methods (private)                               %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  methods (Access = private)
    % Constructors
    varargout = fromCoefficients(varargin)
    varargout = fromPzmodel(varargin)
    varargout = fromParfrac(varargin)
  end
  
end % End classdef

