% MPOWER implements mpower operator for analysis objects.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: MPOWER implements mpower operator for analysis objects.
%
% CALL:        a = a1^scalar
%              a = a1^a2
%
% <a href="matlab:utils.helper.displayMethodInfo('ao', 'mpower')">Parameters Description</a>
%
% VERSION:     $Id: mpower.m,v 1.36 2011/04/08 08:56:15 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = mpower(varargin)

  import utils.const.*
  
  callerIsMethod = utils.helper.callerIsMethod;
  
  operatorName   = 'mpower';
  operatorSymbol = '^';
  
  % Check if this is a call for parameters
  if utils.helper.isinfocall(varargin{:})
    varargout{1} = getInfo(varargin{3});
    return
  end

  utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);

  % Collect input aos, plists and ao variable names
  in_names = cell(size(varargin));
  for ii = 1:nargin,in_names{ii} = inputname(ii);end

  % Check for numbers in the other arguments and convert to AOs
  args = utils.helper.collect_values(varargin);
  [as, ao_invars] = utils.helper.collect_objects(args, 'ao', in_names);
  pl              = utils.helper.collect_objects(args, 'plist');

  % Combine with default plist
  pl = parse(pl, getDefaultPlist);

  % Decide on a deep copy or a modify
  bs = copy(as, nargout);
  
  % Settings
  if callerIsMethod
    infoObj = [];
  else
    infoObj = getInfo();
  end
  
  if ~isempty(pl.find('exponent'))
    bs = [reshape(bs, [], 1), ao(pl.find('exponent'))];
    ao_invars{end+1} = mat2str(pl.find('exponent'));
  end
  
  % Apply method and set output
  varargout{1} = bs.applyoperator(callerIsMethod, ao_invars, operatorName, operatorSymbol, pl, infoObj);
end

%--------------------------------------------------------------------------
% Get Info Object
%--------------------------------------------------------------------------
function ii = getInfo(varargin)
  if nargin == 1 && strcmpi(varargin{1}, 'None')
    sets = {};
    pl   = [];
  else
    sets = {'Default'};
    pl   = getDefaultPlist;
  end
  % Build info object
  ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.aop, '$Id: mpower.m,v 1.36 2011/04/08 08:56:15 hewitson Exp $', sets, pl);
  ii.setArgsmin(2);
end

%--------------------------------------------------------------------------
% Get Default Plist
%--------------------------------------------------------------------------
function plout = getDefaultPlist()
  persistent pl;  
  if ~exist('pl', 'var') || isempty(pl)
    pl = buildplist();
  end
  plout = pl;  
end

function pl = buildplist()
  pl = plist({'exponent', 'The exponent.'}, paramValue.EMPTY_DOUBLE);
end


