#
# Given a polynomial expression f in the variables M[..] and X[..],
# flatten it into a linear combo of M[..]'s and X[..]'s expeditiously.
# Heuristics: (1) Typical product expressions will have the smaller factors
# listed first, so parse from right to left. (2) Use the monomial*monomial
# algorithm only when the entire expression uses only M[..]-variables.
#
`weyl/flatten`:=proc(f,S,coS,wt,Nghbr) local v,g,res,i;
  v:=indets(f,'indexed');
  v:=map(proc(x) if op(0,x)='M' or op(0,x)='X' then x fi end,v);
  if not type(f,'polynom'('anything',v)) then
    ERROR(`invalid expression`) fi;
  if degree(f,v)<2 then res:=collect(f,v,'distributed')
    elif type(f,`+`) then
      g:=map(procname,args); v:=indets(g,'indexed');
      v:=map(proc(x) if op(0,x)='M' or op(0,x)='X' then x fi end,v);
      res:=collect(g,v,'distributed')
    elif type(f,`*`) then
      g:=map(procname,[op(f)],args[2..5]);
      g:=[seq(g[-i],i=-nops(g)..-1)];
      if map(x->op(0,x),v)={'M'} then res:=g[1];
        for i from 2 to nops(g) do
          res:=`weyl/flatten/multM`(g[i],res,S,coS,wt,Nghbr)
        od
      else
        for i to nops(g) do
          v:=map(x->op(0,x),indets(g[i],'indexed'));
          if member('X',v) and not member('M',v) then
            g:=subsop(1=g[i],i=g[1],g); break fi;
        od;
        if i>nops(g) then g:=[X[0$nops(S)],op(g)] fi;
        res:=g[1];
        for i from 2 to nops(g) do
          res:=`weyl/flatten/multX`(g[i],res,S,coS,wt,Nghbr)
        od
      fi
    elif type(f,`^`) and type(op(2,f),'posint') then
      g:=procname(op(1,f),args[2..5]); res:=g;
      if map(x->op(0,x),v)={'M'} then
        for i from 2 to op(2,f) do
          res:=`weyl/flatten/multM`(g,res,S,coS,wt,Nghbr)
        od
      else
        g:=weyl['toM'](g,S,coS,wt,'flat');
        if member('M',map(x->op(0,x),v)) then
          res:=`weyl/flatten/multX`(res,X[0$nops(S)],S,coS,wt,Nghbr)
        fi;
        for i from 2 to op(2,f) do
          res:=`weyl/flatten/multX`(g,res,S,coS,wt,Nghbr)
        od
      fi
    else ERROR(`invalid expression`)
  fi; res
end:
#
# The monomial*monomial algorithm.
# Given expressions f,g that are linear combos of M[..]'s (orbit sums), 
# Convert f*g to a linear combo of the M[..]-basis. For max efficiency,
# the "smaller" of the two factors (measured by orbit sizes) should be f.
#
`weyl/flatten/multM`:=proc(f,g,S,coS,wt,Nghbr) local res,v,vf,vg,u,w;
  vf:=`weyl/flatten/vars`(f,wt,'M');
  vg:=`weyl/flatten/vars`(g,wt,'M');
  res:=0;
  for u in vf do
    for w in vg do
      res:=res+u[2]*w[2]*`weyl/orbit_sum`(u[3],w[3],S,coS,Nghbr,'symm');
      v:=indets(res,'indexed'); # collect at each step--avoid swell
      v:=map(proc(x) if op(0,x)='M' then x fi end,v);
      res:=collect(res,v,'distributed')
    od
  od; res
end:
#
# The monomial*character algorithm.
# Given an expression f0 that is a linear combo of M[..]'s and X[..]'s,
# and a g that is a linear combo of X[..]'s, convert f0 to the M[..]
# basis, and use Brauer-Klimyk to expand f0*g in the X[..] basis.
#
`weyl/flatten/multX`:=proc(f0,g,S,coS,wt,Nghbr) local res,f,r0,vf,vg,v,u,w;
  f:=weyl['toM'](f0,S,coS,wt,'flat');
  vf:=`weyl/flatten/vars`(f,wt,'M');
  vg:=`weyl/flatten/vars`(g,wt,'X');
  res:=0; r0:=convert(wt,`+`);
  for u in vf do
    for w in vg do
      res:=res+u[2]*w[2]*`weyl/orbit_sum`(u[3],w[3]+r0,S,coS,Nghbr,'skew');
      v:=indets(res,'indexed'); # collect at each step--avoid swell
      v:=map(proc(x) if op(0,x)='X' then x fi end,v);
      res:=collect(res,v,'distributed')
    od
  od; res
end:
#
# Assuming f0 is linear and collected with respect to a set of variables
# of the form L[..], where [..] represents fundamental weight coordinates,
# pull apart f0 into a list, with each operand having the form [V,c,v],
# where V=L[..] is the variable name, c is the coefficient of V, and v is
# the vector form of the dominant weight corresponding to V.
# Special attention is needed for the constant term.
#
`weyl/flatten/vars`:=proc(f0,wt,L) local v,f,u,ct;
  v:=indets(f0,'indexed');
  v:=map(proc(x,y) if op(0,x)=y then x fi end,v,L);
  ct:=subs({seq(u=0,u=v)},f0); f:=f0-ct+ct*L[0$nops(wt)];
  if ct<>0 then v:={op(v),L[0$nops(wt)]} fi;
  [seq([u,coeff(f,u),convert(zip((x,y)->x*y,[op(u)],wt),`+`)],u=v)]
end:
#
save `weyl/flatten`, `weyl/flatten/multM`, `weyl/flatten/multX`,
  `weyl/flatten/vars`, cat(HomeLib,`/weyl/flatten.m`);
quit;

