function c = sym2poly(p)
%SYM2POLY Symbolic polynomial to polynomial coefficient vector.
%   SYM2POLY(P) returns a row vector containing the coefficients 
%   of the symbolic polynomial P.
%
%   Example:
%      sym2poly(x^3 - 2*x - 5) returns [1 0 -2 -5].
%
%   See also POLY2SYM.

%   Copyright (c) 1993-98 by The MathWorks, Inc.
%   $Revision: 1.14 $  $Date: 1997/11/29 01:06:12 $

p = expand(p);
x = symfind(p);

if isempty(x)
   % constant case
   c = double(p);

elseif ~isempty(find(x==','))
   error('Input has more than one symbolic variable.')

else
   maple(['_ans := ' p.s]);
   [c,stat] = maple(['[seq(coeff(_ans,' x ',k),k=0..degree(_ans))]']);
   [cl,statl] = maple(['[seq(coeff(_ans,' x ',k),k=ldegree(_ans)..-1)]']);
   if stat | statl | ~isequal(cl,'[]')
      error([char(p) ' is not a polynomial.'])
   else
       c = fliplr(eval(c));
   end
   maple('_ans := ''_ans'';');
end;


%-------------------------

function vars = symfind(f)
% SYMFIND Finds the Symbolic variables in a Symbolic Expression.
%  SYMFIND(f) returns a string, arranged in lexicographic order,
%  of all the symbolic variables in f EXCEPT pi.
%  Examples:
%     symfind(exp(a*t)*C1)  returns  C1, a, t
%     symfind(cos(pi*x*y))  returns  x, y

% Find the symbolic variables in f.
vars = [ '{' findsym(f) '}' ];
% Remove pi as a variable.
vars = maple([ vars ' minus ' '{pi}' ]);
% Remove blanks.
vars(findstr(vars,''));
% Remove '{' and '}' from vars.
vars = strrep(vars,'{',''); vars = strrep(vars,'}','');
% Insure that vars are in lexicographic order.
vars = maple(['sort( [' vars '], lexorder);']);
% Remove '[' and ']' from the list vars
vars = strrep(vars,'[',''); vars = strrep(vars,']','');
