#
# class_size(w,R) returns the size of the conjugacy class of w.
# class_size(R) returns the sizes of all conjugacy classes.
#
`coxeter/class_size`:=proc() local n,R,N,res,new,sys,i,j,w,L,w0,pi;
  R:=coxeter['name_of'](args[nargs],'pi');
  sys:=sort([op(indets(R))],`coxeter/cox_order`);
  sys:=[seq(i$degree(R,i),i=sys)];
  if nargs=1 then res:=[1];
    for R in sys do
      n:=coxeter['rank'](R);
      if type(R,'indexed') then L:=op(0,R) else L:=substring(R,1..1) fi;
      new:=`coxeter/class_size/list`(L,n,R);
      res:=[seq(seq(i*j,i=res),j=new)];
    od;
  else
    N:=0; res:=1;
    w0:=subs({seq(pi[i]=i,i=1..nops(pi))},args[1]);
    for R in sys do
      n:=coxeter['rank'](R);
      w:=map(proc(x,y,z) if x>y and x<=z then x-y fi end,w0,N,N+n);
      if type(R,'indexed') then L:=op(0,R) else L:=substring(R,1..1) fi;
      new:=`coxeter/mytype`(w,L,n,R);
      if L='A' then new:=`coxeter/par2A`(new,0) 
        elif L='B' or L='C' then new:=`coxeter/par2B`(new,0) 
        elif L='D' then new:=`coxeter/par2D`(new[1..2],0)
        elif L='E' and n<6 then
          new:=`coxeter/class_size`(w,coxeter['base'](R))
        elif member(L,['E','F','H']) then 
          new:=`coxeter/read`(R,'class_size','sizes')[new]
        else new:=`coxeter/class_size/list`(L,n,R)[new]
      fi;
      res:=res*new; N:=N+n;
    od
  fi; res
end:
#
`coxeter/class_size/list`:=proc(L,n,R) local i,mu,nu,m,sa;
  if L='A' then
    [seq(`coxeter/par2A`(mu,0),mu=combinat['partition'](n+1))]
  elif L='B' or L='C' then
    [seq(seq(seq(`coxeter/par2B`(mu,nu,0),mu=combinat['partition'](n-i)),
      nu=combinat['partition'](i)),i=0..n)]
  elif L='D' and n>1 then
    sa:=seq(seq(seq(`coxeter/par2D`(mu,nu,0),mu=combinat['partition'](n-i)), 
      nu=combinat['partition'](i)),i=0..n);
    if modp(n,2)=0 then [sa,seq(`coxeter/par2D`(map(x->2*x,mu),[],0),
      mu=combinat['partition'](n/2))] else [sa] fi
  elif L='E' and n<6 then
    `coxeter/class_size`(coxeter['base'](R))
  elif member(L,['E','F','H']) then
    `coxeter/read`(R,'class_size','sizes')
  elif R='G2' then [1,3,3,2,2,1]
  elif L='I2' and type(op(R)-1,'posint') then m:=op(R);
    if modp(m,2)=0 then [1,m/2,m/2,2$(m/2-1),1]
      else [1,m,2$((m-1)/2)] fi;
  else
    ERROR(cat(R,` is not a recognized root system name`))
  fi;
end: 
#
save `coxeter/class_size`, `coxeter/class_size/list`,
  cat(HomeLib,`/coxeter/class_size.m`);
read cat(HomeLib,`/help/coxeter/class_size`);
quit;

