#
# name_of(X) produces the name of the Coxeter group with simple roots X,
#  or Coxeter matrix X, or Cartan matrix X. Note that Coxeter matrices
#  cannot distinguish between B/C, or G2/I2[6] (etc).
# name_of(X,'pi') does the same but also assigns to 'pi' a permutation of
#  [1,...,n] (n=rank) indicating an ordering of the roots of X that would
#  agree with the canonical ordering used for the root system of that name.
# Note that the L vector can be bogus in the case of cartan data, but we
#  only compare adjacent nodes, so it doesn't matter.
#
`coxeter/name_of`:=proc(S) local M,L,n,r,i,j;
  if type(S,'list') and nops(S)>0 then
    M:=coxeter['cox_matrix'](S); n:=nops(S);
    L:=[seq(coxeter['iprod'](r,r),r=S)];
  elif type(S,'matrix') and S[1,1]=2 then
    n:=linalg['rowdim'](S);
    L:=[seq(max(seq(-S[i,j],j=1..n)),i=1..n)];
    M:=coxeter['cox_matrix'](S);
  elif type(S,'matrix') then
    M:=S; n:=linalg['rowdim'](M); L:=[1$n];
  else
    if nargs>1 then assign(args[2],[$1..coxeter['rank'](S)]) fi;
    if S=[] then RETURN(1) else RETURN(S) fi
  fi;
  `coxeter/name_of/main`(M,L,n,args[2..nargs]);
end:
#
# separate the connected components and toss out cases that are not
# trees or have an infinite edge or multiple edges of weight>3.
#
`coxeter/name_of/main`:=proc(M,L,n) local X,i,j,edge,sp,res,k,l,N,EPS;
  X:=[$1..n]; EPS:=`coxeter/default`['epsilon'];
  for i to n do
    edge[i]:=NULL; sp[i]:=NULL;
    for j to i-1 do
      if M[i,j]=2 then next fi;
      if M[i,j]=infinity or X[i]=X[j] then
        ERROR(`not a finite Coxeter group`) fi;
      k:=min(X[i],X[j]); l:=max(X[i],X[j]);
      edge[k]:=edge[k],edge[l],{i,j}; sp[k]:=sp[k],sp[l];
      if M[i,j]>3 then
        if L[i]+EPS<L[j] then sp[k]:=sp[k],M[i,j],L[j]/L[i],[i,j]
        else sp[k]:=sp[k],M[i,j],L[i]/L[j],[j,i] fi
      fi;
      X:=subs(l=k,X);
    od;
    if nops([sp[X[i]]])>3 then ERROR(`not a finite Coxeter group`) fi;
  od;
  X:=sort([op({op(X)})]);
  for i in X do if sp[i]=NULL then sp[i]:=3,1,[i] fi od;
  if nargs>3 then
    res:=[seq([`coxeter/name_of/cc`({edge[i]},sp[i],true)],i=X)];
    res:=sort(res,(x,y)->`coxeter/cox_order`(x[1],y[1]));
    assign(args[4],map(x->op(op(2,x)),res));
    N:=map(x->op(1,x),res)
  else
    N:=[seq(`coxeter/name_of/cc`({edge[i]},sp[i],false),i=X)]
  fi;
  convert(N,`*`)
end:
#
# identify the connected component (and preferred ordering, if needed).
#
`coxeter/name_of/cc`:=proc(G,m,r,sp,flag) local pi,X,N,i,leafs,fork,J,n,k;
  if nops(G)<2 then
    if flag then pi:=sp else pi:=NULL fi;
    if nops(G)=0 then RETURN('A1',pi)
      elif m=4 and type(r,'rational') then RETURN('B2',pi)
      elif m=6 and type(r,'rational') then RETURN('G2',pi)
      elif m>3 then RETURN(I2[m],pi)
    fi
  fi;
  X:=sort([op(map(op,G))]); N:=table([seq(i=NULL,i=X)]);
  for k in G do N[k[1]]:=N[k[1]],k[2]; N[k[2]]:=N[k[2]],k[1] od;
  leafs:=NULL; n:=nops(X); pi:=NULL;
  for i in X do k:=nops([N[i]]);
    if k=1 then leafs:=leafs,i elif k=3 then fork:=i fi
  od;
  if nops([leafs])=2 then
    if flag then pi:=`coxeter/name_of/path`(leafs,N) fi;
    if m=3 then RETURN(cat('A',n),pi) fi;
    J:={leafs} intersect {sp[1],sp[2]};
    if nops(J)=1 then
      if flag and not pi[1]=J[1] then pi:=[seq(pi[-i],i=-n..-1)] fi;
      if m=4 and r=2 and sp[2]=J[1] then RETURN(cat('C',n),pi)
        elif m=4 then RETURN(cat('B',n),pi)
        elif m=5 and n<5 then RETURN(cat('H',n),pi)
      fi
    elif n=4 and m=4 then
      if flag and r=2 and pi[2]=sp[2] then pi:=[seq(pi[-i],i=-4..-1)] fi;
      RETURN('F4',pi)
    fi
  elif nops([leafs])=3 and m=3 then
    J:={leafs} intersect {N[fork]};
    if nops(J)>1 then
      if flag then pi:=op({leafs} minus {J[1],J[2]});
        pi:=`coxeter/name_of/path`(fork,pi,N); pi:=[J[1],J[2],op(pi)]
      fi;
      RETURN(cat('D',n),pi)
    elif nops(J)=1 and n<9 then
      X:={N[fork]} intersect {seq(N[i],i=[leafs])};
      if nops(X)>0 then
        if flag then k:=op({leafs} intersect {N[X[1]]});
          pi:=`coxeter/name_of/path`(fork,op({leafs} minus {k,J[1]}),N);
          pi:=[k,J[1],X[1],op(pi)]
        fi;
        RETURN(cat('E',n),pi)
      fi
    fi
  fi;
  ERROR(`not a finite Coxeter group`);
end:
#
# Find the unique path from vertex i to leaf j
# 
`coxeter/name_of/path`:=proc(i,j,N) local path,k,old,L;
  path:=j; k:=j; old:={};
  while k<>i do
    L:=op({N[k]} minus old); old:={k}; k:=L; path:=k,path
  od; [path]
end: 
#
save `coxeter/name_of`, `coxeter/name_of/main`, `coxeter/name_of/cc`,
  `coxeter/name_of/path`, cat(HomeLib,`/coxeter/name_of.m`);
read cat(HomeLib,`/help/coxeter/name_of`);
quit;

