#
# Use the "qtensor" (quick and dirty) algorithm for decomposing the
#  tensor product of irreps with highest weights u and v.
# Note the secondary uses of X here as a global variable
# Reference: <http://www.math.lsa.umich.edu/~jrs/papers/carswc.ps.gz>,
#  MSJ Memoirs Vol. 11 pp. 1--38, Math. Soc. Japan, Tokyo, 2001.
#
`weyl/qtensor`:=proc(u,v,R)
  local coS,r,pr,copr,cow,r0,wts,i,c,vars,ht,wl,h0,wc,nm;
  coS:=coxeter['co_base'](R);
  copr:=coxeter['pos_roots'](coS);
  pr:=[seq(2*r/coxeter['iprod'](r,r),r=copr)];
  cow:=weyl['weights'](coS); r0:=convert(pr,`+`)/2;
  r:=coxeter['vec2fc'](u-coxeter['vec2fc'](-v,coS),coS);
  wts:=weyl['weight_sys'](u+v,R,'wc',r,cow);
  vars:=[seq(c[i],i=1..nops(wts))];
  wts:=map((x,y)->x+y,[u,v,op(wts)],r0);
  nm:=[seq(convert(map((x,y)->X[coxeter['iprod'](x,y)],copr,r),`*`),r=wts)];
  ht:=map(coxeter['iprod'],cow,r0);
  h0:=min(op(ht)); wl:=[[nops(coS),0,0]];
  for i while nops(indets(vars))>0 do
    if i>nops(wl) then wl:=`weyl/qtensor/morecow`(wl,cow,ht,h0) fi;
    userinfo(2,'qtensor',nops(indets(vars)),`using catalyst`,wl[i][3]);
    vars:=`weyl/qtensor/spec`(wts,wl[i][3],u+v+r0,nm,pr,cow,coS,vars);
  od;
  convert(zip((x,y)->x*X[op(y)],vars,wc),`+`);
end:
#
# Given a (structured) list of all co-weights up to some height ht,
# build a larger list of all co-weights up to height ht+h0.
#
`weyl/qtensor/morecow`:=proc(wl,cow,ht,h0) local new,m,u,h,i;
  new:=NULL; m:=wl[nops(wl)][2];
  for u in wl do
    for i to u[1] do h:=ht[i]+u[2];
      if h<=m+h0 and h>m then new:=new,[i,h,u[3]+cow[i]] fi
    od
  od;
  new:=sort([new],(x,y)->evalb(x[2]<=y[2]));
  [op(wl),op(new)];
end:
#
# Notice that specializations need not be symmetric with respect to
# replacing q->1/q, unless u0 is self-dual. However, if we only
# build equations up to the middle degree, the upper half will get
# built when we use the dual of u0.
#
`weyl/qtensor/spec`:=proc(wts,u0,v1,nm,pr,cow,coS)
  local u1,ev,i,j,q,N,k,dn,vars;
  N:=nops(wts); u1:=u0+convert(cow,`+`); vars:=args[8];
  dn:=convert([seq(X[coxeter['iprod'](j,u1)],j=pr)],`*`);
  dn:=map((x,y)->x/y,nm,dn);
  ev:=`weyl/qtensor/eval`(wts,u0,cow,coS,q);
  ev:=`weyl/qtensor/cyclosimp`(ev,dn,q);
  ev:=[[0,ev[1]*ev[2]],seq([coxeter['iprod'](v1-wts[i],u1),ev[i]],i=3..N)];
  k:=1+iquo(degree(ev[1][2],q),2);
  ev:=map((x,y,z)->[x[1],taylor(x[2],y,z-x[1])],ev,q,k);
  ev:=map((x,y,z)->taylor(y^x[1]*x[2],y,z),ev,q,k);
  for j from 0 to k-1 while nops(indets(vars))>0 do
    u1:=convert([seq(coeff(ev[i+1],q,j)*vars[i],i=1..N-2)],`+`)
      -coeff(ev[1],q,j);
    if u1=0 then next else i:=max(op(map(op,indets(u1))),0) fi;
    if i=0 then ERROR(`this cannot happen`) fi;
    u1:=-coeff(u1,vars[i],0)/coeff(u1,vars[i],1);
    vars:=subs(vars[i]=u1,vars);
  od; vars
end:
#
# Given a list of weights wts, and a dominant co-weight u0,
# return a list of evaluations chi(u0,-v), for v in wts,
# normalized so that the lowest order term is 1.
# We are assuming that the orbits are small, so traversal is overkill.
#
`weyl/qtensor/eval`:=proc(wts,u0,cow,coS,q) local wm,res,a,a0,m,orb,i;
  if u0=0 then RETURN([1$nops(wts)]) fi; # the most common case
  res:=[0$nops(wts)]; wm:=weyl['weight_mults'](u0,coS);
  if type(wm,`+`) then wm:=[op(wm)] else wm:=[wm] fi;
  for a in wm do
    a0:=op(indets(a,'indexed')); m:=coeff(a,a0);
    a0:=convert(zip((x,y)->x*y,[op(a0)],cow),`+`);
    orb:=map((x,y)->y-x,coxeter['orbit'](a0,coS),u0);
    res:=[seq(res[i]+m*convert(map((x,y,z)->
      z^coxeter['iprod'](x,y),orb,wts[i],q),`+`),i=1..nops(wts))];
  od; res
end:
#
# Note that we assume that numtheory[cyclotomic](m,q) always returns a
# polynomial with constant term 1. This is not true for m=1, but this
# will never occur in any of our expressions.
#
`weyl/qtensor/cyclosimp`:=proc(ev,dn,q) local vars,sa,i,j;
  vars:=map(op,indets(dn));
  sa:=subs({seq(X[i]=convert([seq(X[j],
    j=numtheory['divisors'](i))],`*`),i=vars)},dn);
  sa:=subs(X[1]=1-q,sa); vars:=map(op,indets(sa));
  sa:=zip((x,y)->[x/denom(y),numer(y)],ev,sa);
  sa:=subs({seq(X[i]=numtheory['cyclotomic'](i,q),i=vars)},sa);
  map(x->normal(x[1])*x[2],sa);
end:
#
save `weyl/qtensor`, `weyl/qtensor/morecow`, `weyl/qtensor/cyclosimp`,
  `weyl/qtensor/spec`, `weyl/qtensor/eval`, cat(HomeLib,`/weyl/qtensor.m`);
quit;

