#
# toq(f) converts a symmetric function f in the omega algebra into a
#  polynomial function of q1,q2,q3,...
# toq(f,'b'), where 'b' is one of the names p, P[] or Q[], will
#   assume that f is expressed in base b.
#
# N.B. the representation of f in this form is not unique, but it
# has the advantage that it runs faster than 'toddq'.
#
`QF/toq`:=proc() local poly,sp,mu,i,f,hack,z,j,d,bases;
  poly:=args[1];
  if nargs=1 then bases:={'P[]','Q[]','p'}
    elif args[2]='P' then bases:={'P[]'}
    elif args[2]='Q' then bases:={'Q[]'}
    else bases:={args[2]}
  fi;
  if member('P[]',bases) then 
    sp:=SF['varset'](poly,'P[]');
    poly:=subs({seq(P[op(mu)]=Q[op(mu)]/2^nops(mu),mu=sp)},poly);
    bases:=bases minus {'P[]','P'} union {'Q[]'};
  fi;
  sp:=SF['varset'](poly,bases);
  if member('p',bases) and sp['p']>0 then d:=sp['p'];
    if modp(d,2)=0 then ERROR(`polynomial not in omega algebra`) fi;
    f:=convert([1,seq(cat('q',i)*z^i,i=1..d)],`+`);
    hack:=taylor((z/2)*diff(f,z)*subs(z=-z,f),z,d+1);
    poly:=subs({seq(cat('p',2*i+1)=coeff(hack,z,2*i+1),i=0..(d-1)/2)},poly);
  fi;
  if member('Q[]',bases) then
    sp:=map(proc(x) if nops(x)>2 then x fi end,sp['Q']);
    poly:=subs({seq(Q[op(mu)]=QF['pfaff'](mu),mu=sp)},poly);
    sp:=SF['varset'](poly,'Q[]');
    for mu in sp do
      if mu=[] then f:=1
      elif nops(mu)=1 then f:=cat('q',op(mu))
      else f:=cat('q',mu[1])*cat('q',mu[2])+2*convert(
        [seq((-1)^j*cat('q',mu[1]+j)*cat('q',mu[2]-j),j=1..mu[2])],`+`)
      fi;
      poly:=subs(Q[op(mu)]=f,poly);
    od;
    poly:=subs(q0=1,poly);
  fi;
  d:=SF['varset'](poly,'q');
  collect(poly,[seq(cat('q',i),i=1..d)],'distributed',normal);
end:
#
save `QF/toq`, cat(HomeLib,`/QF/toq.m`);
read cat(HomeLib,`/help/QF/toq`);
quit;

