function B = mpower(A,p)
%POWER  Symbolic matrix power.
%   POWER(A,p) overloads symbolic A^p.
%
%   Example;
%      A = [x y; alpha 2]
%      A^2 returns [x^2+alpha*y  x*y+2*y; alpha*x+2*alpha  alpha*y+4].

%   Copyright (c) 1993-98 by The MathWorks, Inc.
%   $Revision: 1.12 $  $Date: 1997/11/29 01:05:53 $

A = sym(A);
p = sym(p); 

if all(size(A) == 1) & all(size(p) == 1)
   % Both scalars
   B = maple(A,'^',p);

elseif all(size(p) == 1)
   % Scalar exponent
   if ~isempty(findsym(p)) | p ~= fix(double(p))
      error('Exponent must be a numeric integer.')
   end
   if size(A,1) ~= size(A,2)
      error('Matrix must be square.')
   end
   B = sym(eye(size(A))); 
   p = double(p);
   if p > 0
      for k = 1:p
         B = A*B;
      end
   else
      A = inv(A);
      for k = 1:abs(p)
         B = A*B;
      end
   end
      
elseif all(size(A) == 1)
   % Scalar base
   if size(p,1) ~= size(p,2)
      error('Matrix must be square.')
   end
   B = expm(log(A)*p);

else
   error('Either base or exponent must be a scalar.')
end 
