#
# diagram(X) prints a Coxeter or Dynkin diagram for X, with
# the nodes numbered in the order used by base(X).
#
`coxeter/diagram`:=proc() local sys,R,L,i,r,j,pi,c,lpr;
  if [op(I)]=[1] then lpr:=proc() printf(`%0.75s\n`,args) end
    else lpr:=proc() lprint(args) end fi;
  lpr(` `); j:=1;
  R:=coxeter['name_of'](args[1],'pi');
  sys:=sort([op(indets(R))],`coxeter/cox_order`);
  sys:=[seq(r$degree(R,r),r=sys)];
  for R in sys do;
    r:=coxeter['rank'](R); 
    if type(R,'indexed') then L:=op(0,R) else L:=substring(R,1..1)  fi;
    if L='A' or R='I2[3]' or R='B1' or R='C1' then 
      lpr(cat(`   `,pi[j],seq(cat(`---`,pi[i]),i=j+1..j+r-1)))
    elif L='B' then
      lpr(cat(`   `,pi[j],`=<=`,pi[j+1],seq(cat(`---`,pi[i]),i=j+2..j+r-1)))
    elif L='C' then
      lpr(cat(`   `,pi[j],`=>=`,pi[j+1],seq(cat(`---`,pi[i]),i=j+2..j+r-1)))
    elif L='D' and r>1 then 
      c:=`      `,` `$length(pi[j]); lpr(cat(c,pi[j+1]));
      if r>2 then lpr(cat(c,`|`)) else lpr(` `) fi;
      lpr(cat(`   `,pi[j],seq(cat(`---`,pi[i]),i=j+2..j+r-1)))
    elif L='E' and r>2 then
      c:=`         `,` `$(length(pi[j])+length(pi[j+2]));
      lpr(cat(c,pi[j+1]));
      if r>3 then lpr(cat(c,`|`)) else lpr(` `) fi;
      lpr(cat(`   `,pi[j],seq(cat(`---`,pi[i]),i=j+2..j+r-1)))
    elif L='F' and r>2 then
      lpr(cat(`   `,pi[j],`---`,pi[j+1],`=<=`,pi[j+2],
        seq(cat(`---`,pi[i]),i=j+3..j+r-1)))
    elif R='G2' then 
      lpr(cat(`   `,pi[j],`=<<=`,pi[j+1]))
    elif L='H' and r>1 then
      lpr(cat(`   `,pi[j],`--(5)--`,pi[j+1],
        seq(cat(`---`,pi[i]),i=j+2..j+r-1)))
    elif L='I2' and type(op(R)-1,'posint') then 
      if op(R)=2 then c:=`   ` else c:=`--(`,op(R),`)--` fi;
      lpr(cat(`   `,pi[j],c,pi[j+1]))
    else
      ERROR(cat(R,` is not a recognized root system name`))
    fi;
    j:=j+r; lpr(` `);
  od;
end:
#
save `coxeter/diagram`, cat(HomeLib,`/coxeter/diagram.m`);
read cat(HomeLib,`/help/coxeter/diagram`);
quit;

