#
# cox_matrix(R) returns the Coxeter matrix of R.
# The empty matrix is represented as [].
#
`coxeter/cox_matrix`:=proc(M) local S,n,i,j,EPS,L;
  if type(M,'matrix') and M[1,1]=1 then eval(M)
    elif type(M,'matrix') then
      n:=linalg['rowdim'](M);
      array('symmetric',1..n,1..n,[seq([seq(
        `coxeter/cox_matrix/chk`({M[i,j],M[j,i]}),j=1..i-1),1],i=1..n)])
    else
      S:=coxeter['base'](M); n:=nops(S);
      if S=[] then RETURN(S) else EPS:=`coxeter/default`['epsilon'] fi;
      L:=[seq(coxeter['iprod'](i,i),i=S)];
      S:=[seq(evalf(S[i]/sqrt(L[i])),i=1..n)];
      array('symmetric',1..n,1..n,[seq([seq(`coxeter/cox_matrix/entry`
        (S[i],S[j],L[i],L[j],EPS),j=1..i-1),1],i=1..n)])
  fi
end:
#
`coxeter/cox_matrix/chk`:=proc(x) 
  if x={0} then 2 elif x={-1} then 3
    elif x={-1,-2} then 4 elif x={-1,-3} then 6
    elif type(x,'set'('negint')) then infinity
    else ERROR(`not a valid Cartan matrix`)
  fi
end:
#
`coxeter/cox_matrix/entry`:=proc(r,s,lr,ls,EPS) local m,rm;
  m:=evalf(Pi/arccos(-coxeter['iprod'](r,s))); rm:=round(m);
  if abs(rm-m)>EPS then ERROR(`not a valid base`)
    elif modp(rm,2)=1 and abs(lr-ls)>EPS then ERROR(`not a valid base`)
    else rm
  fi
end:
#
save `coxeter/cox_matrix`, `coxeter/cox_matrix/chk`,
  `coxeter/cox_matrix/entry`, cat(HomeLib,`/coxeter/cox_matrix.m`);
read cat(HomeLib,`/help/coxeter/cox_matrix`);
quit;

