#
# Given a polynomial expression f in the variables M[..] (orbit sums)
#  and X[..] (Weyl characters), where [..]=fund.wt. coordinates, convert
#  f into a linear combination of orbit sums M[..] using the Brauer-Klimyk
#  Rule (and related techniques).
# The output is "collected" with respect to the variables M[..].
#
`weyl/toM`:=proc(f,R) local S,coS,wt,r,N,g,v,pr,prwc,ct;
  if args[nargs]<>'flat' then
    if assigned(M) or assigned(X) then
      ERROR(`M and X cannot have assigned values`) fi;
    S:=coxeter['base'](R); r:=ilcm(op(map(denom,S)));
    S:=map((x,y)->y*x,S,r); wt:=weyl['weights'](S);
    coS:=coxeter['co_base'](S);
    N:=`weyl/neighbor`(S,{},0);
    g:=`weyl/flatten`(f,S,coS,wt,N);
    `weyl/toM`(g,S,coS,wt,'flat')
  else
    v:=`weyl/flatten/vars`(f,args[4],'X');
    v:=map(proc(x) if x[3]=0 then NULL else x fi end,v);
    g:=subs(X[0$nops(R)]=M[0$nops(R)],f);
    if nops(v)>0 then
      pr:=coxeter['pos_roots'](R);
      prwc:=[seq(map(coxeter['iprod'],args[3],r),r=pr)];
      g:=subs({seq(r[1]=weyl['weight_mults'](r[3],R,pr,prwc),r=v)},g)
    fi;
    v:=indets(g,'indexed');
    v:=map(proc(x) if op(0,x)='M' then x fi end,v);
    ct:=subs({seq(r=0,r=v)},g); g:=g-ct+ct*M[0$nops(R)];
    if ct<>0 then v:={op(v),M[0$nops(R)]} fi;
    collect(g,v,'distributed')
  fi
end:
#
save `weyl/toM`, cat(HomeLib,`/weyl/toM.m`);
read cat(HomeLib,`/help/weyl/toM`);
quit;

