#
# Compute tensor product multiplicities and branching multiplicities via
#  minuscule chain algorithm. The smaller of the two irreps should be u.
# Calling sequence:  `weyl/mchain`(u,v,J,R), where
#  u can either be a dominant weight or a chain
#  J is the set of nodes to keep ( = {1,...,n} for tensor products),
#  v is the second dominant weight ( = 0 for branching).
#
`weyl/mchain`:=proc(u,v,J,R) local S,coS,rep,chld,nc,c,i,rs,res,ct;
  S:=coxeter['base'](R); coS:=coxeter['co_base'](S);
  if type(u,'list') then rep:=u else rep:=`weyl/findrep`(u,S) fi;
  chld:=map(`weyl/mchain/downset`,rep,S,coS);
  nc:=map(nops,chld);
  rs[0]:=[map(coxeter['iprod'],coS,v),[0$nops(S)]];
  res:=0; c[1]:=0; i:=1; ct:=0;
  while i>0 do
    if i>nops(rep) then i:=i-1; res:=res+X[op(rs[i][1])]
    elif c[i]=nc[i] then i:=i-1
    else c[i]:=c[i]+1;
      rs[i]:=`weyl/mchain/insert`(c[i],chld[i],rs[i-1],J);
      if rs[i]=NULL then ct:=ct+1 else i:=i+1; c[i]:=0 fi;
    fi
  od;
  userinfo(2,'mchain',cat(`number of dead ends=`,ct)); res
end:
#
# Given a rise-sequence rs, generate the rise-sequence obtained
#  by inserting the j-th term of chld. Reject if it does not terminate
#  at the top element of chld, or if the total weight is not dominant.
# Possible inefficiency: this does not use seq/map to build the answer.
#
`weyl/mchain/insert`:=proc(j,chld,rs,J) local wc,u,new,r,i,eps;
  u:=chld[j]; wc:=zip((x,y)->x+y,u[1],rs[1]);
  if min(1,seq(wc[i],i=J))<0 then RETURN() fi;
  i:=nops(rs); new:=NULL;
  while i>1 do
    eps:=zip((x,y)->max(0,x+y),rs[i],u[1]);
    if u[3]>0 then eps:=subsop(u[3]=eps[u[3]]+1,eps) fi;
    new:=eps,new;
    for r in u[2] do
      if rs[i][r[2]]+u[1][r[2]]<0 then
        if r[1]=0 then RETURN() fi;
        u:=chld[r[1]]; i:=i+1; break
      fi
    od;
    i:=i-1;
  od;
  if u[3]<0 then if chld[j][3]>0 then wc:=rs[1] fi; [wc,new] fi;
end:
#
# Given a **nonzero** (quasi-)minuscule v0, generate a list of objects
# below v0 in the corresponding thin system. The list has the format
#   [weight-coords, edge-list, flag], where
# flag=-1 indicates that this is the top object, of weight v0<>0.
# flag=j>0 indicates that this is the object 0_j.
#   In this case, "weight-coords" = [0...-1...0].
# flag=0 for all other objects.
# Edge-lists are pairs [i,j], where i is the list position of the object
#  obtained by applying E_j. Set i=0 if applying E_j would yield an
#  object that cannot reach v0.
#
`weyl/mchain/downset`:=proc(v0,S,coS)
  local new,J,u,v,wt,i,j,k,old,mu,eps,nu,p,q;
  J:=[$1..nops(S)]; new:=[v0]; q:=0; eps[1]:=NULL;
  while nops(new)>0 do
    old:=new; new:=[]; p:=q; q:=q+nops(old);
    if old=[0] then k:=nops([eps[p+1]]);
      for u in [eps[p+1]] do
        j:=u[2]; nu:=-S[j]; new:=[op(new),nu];
        mu[q]:=[subsop(j=-1,[0$nops(S)]),[u],j];
        eps[q+k]:=[q,j]; q:=q+1;
      od; q:=q-1;
    else
      for i from p+1 to q do
        v:=old[i-p]; wt:=map(coxeter['iprod'],coS,v);
        nu:=subsop(seq(j[2]=0,j=[eps[i]]),wt); 
        nu:=map(proc(x,y) if y[x]<0 then [0,x] fi end,J,nu);
        mu[i]:=[wt,[op(nu),eps[i]],0];
        for j to nops(S) do
          if wt[j]>0 then u:=v-S[j];
            if not member(u,new,'k') then
              new:=[op(new),u]; k:=nops(new); eps[q+k]:=NULL fi;
            eps[q+k]:=eps[q+k],[i,j];
          fi
        od
      od
    fi;
  od;
  [subsop(3=-1,mu[1]),seq(mu[i],i=2..q)];
end:
#
save `weyl/mchain`, `weyl/mchain/insert`, `weyl/mchain/downset`,
  cat(HomeLib,`/weyl/mchain.m`);
quit;

