#
# `weyl/orbit_sum`(u0,v0+rho,S,coS,Nghbr,'skew')
#  computes the irreducible character expansion of the product of the
#  orbit sum for u0 and the Weyl character indexed by v0 using the
#  Brauer-Klimyk Rule. The output is a linear combo of variables X[..],
#  where [..] are fund.wt.coords.
# Here, S,coS = simple roots and co-roots, Nghbr = the neighbor table.
# Similarly, `weyl/orbit_sum`(u0,v0,S,coS,Nghbr,'symm') computes the
#  expansion of the product of the orbit sums corresponding to u0 and v0
#  as a linear combination of orbit sums. The output is a linear combo
#  of variables M[..], where [..] are fund.wt.coords.
# Note that the orbit of u0 is efficiently traversed.
#
`weyl/orbit_sum`:=proc(u0,v0,S,coS,Nghbr)
  local u,res,i,j,f,g,r,rnk,Cstack,cc;
  rnk:=0; Cstack:=table();
  g:=cat(`weyl/orbit_sum/`,args[6]);  # args[6]='skew' or 'symm'
  i:=nops(S)+1; u:=u0; res:=0;
  do
    res:=res+g(u+v0,S,coS,Nghbr);
    cc:=[seq(`weyl/orbit_sum/ref`(u,j,S,coS),j=1..i-1)];
    for j in Nghbr[i] do f:=1;
      for r in Nghbr[i,j] do
        if coxeter['iprod'](r,u)<0 then f:=0; break fi
      od;
      if f=1 then cc:=[op(cc),`weyl/orbit_sum/ref`(u,j,S,coS)] fi;
    od;
    if nops(cc)>0 then rnk:=rnk+1 else
      do cc:=Cstack[rnk];
        if nops(cc)=0 then rnk:=rnk-1 else break fi;
      od;
      if rnk=0 then break fi
    fi;
    Cstack[rnk]:=subsop(1=NULL,cc);
    i:=cc[1][1]; u:=cc[1][2];
  od;
  if args[6]='symm' then f:=min(coeffs(res)); res/f else res fi
end:
#
# Given v0, return sgn(w)*X[..] or 0, where [..] are the fund.wt.coords
# of the unique dominant v such that w.(v+rho) = v0 (if v exists).
# This is faster than 'vec2fc', since we exploit neighborhood data.
#
`weyl/orbit_sum/skew`:=proc(v0,S,coS,N) local v,l,i,j,c,ndom;
  v:=v0; i:=0; ndom:=true;
  for l while ndom do
    ndom:=false;
    for j in N[-i] do
      c:=coxeter['iprod'](coS[j],v);
      if c<0 then v:=v-c*S[j]; ndom:=true; break
        elif c=0 then RETURN(0) fi;
    od;
    i:=j
  od;
  (-1)^l*X[seq(coxeter['iprod'](j,v)-1,j=coS)]
end:
#
# Given v0, return the expression c*M[..], where [..]=fund.wt.coords
# of the unique dominant weight in the W-orbit of v0, and c is the order
# of the W-stabilizer of v0.
#
`weyl/orbit_sum/symm`:=proc(v0,S,coS,N) local v,i,j,c,ndom,S0;
  v:=v0; i:=0; ndom:=true;
  while ndom do
    ndom:=false;
    for j in N[-i] do
      c:=coxeter['iprod'](coS[j],v);
      if c<0 then v:=v-c*S[j]; ndom:=true; break fi
    od;
    i:=j
  od;
  v:=[seq(coxeter['iprod'](j,v),j=coS)];
  S0:=map(proc(x,y,z) if y[x]=0 then z[x] fi end,[$1..nops(S)],v,S);
  coxeter['size'](S0)*M[op(v)]
end:
#
`weyl/orbit_sum/ref`:=proc(v,i,S,coS) local c;
  c:=coxeter['iprod'](coS[i],v);
  if c>0 then [i,v-c*S[i]] fi
end:
#
save `weyl/orbit_sum`, `weyl/orbit_sum/skew`, `weyl/orbit_sum/symm`,
  `weyl/orbit_sum/ref`, cat(HomeLib,`/weyl/orbit_sum.m`);
quit;

