#############################################################################
##
#A  genVkonvert          CHEVIE library          Meinolf Geck & Frank L"ubeck
##
#Y  Copyright 1992--1993,  Lehrstuhl D f"ur Mathematik,    RWTH Aachen,   and
#Y                         IWR   der   Universit"at    Heidelberg,   Germany.
##
####################################################################
##
##  Umwandlung des GEW-Formates in das qscew-Format:
## `remember` nicht gut, wenn Tafeln mit verschiedenen
## setCongruences verwendet werden!!!
##
#H 1994/08/22  FL
#H eliminated last call of maple's 'simplify'-routine in 'GEW2qscew'.
#H Now the new function 'simpCharVal' is used.
##
#H 1994/08/05  FL
#H GEW2qscewpot slightly changed. Now expressions like EE^(2*Pi*I*(q-1)/7)
#H can be converted (these can appear for example after some Spec..Param)
##
 
# Definition of the function 'simpCharVal' for a normal form of 
# character values:
simpCharValhilf:=proc()
  option remember;
  false;
end:
simpCharValpot:=proc(tt)
  local b,e;
  global simpCharValhilf;
  b:=tt;
  e:=1;
  while type(b,`^`) do
    e:=op(2,b)*e;
    b:=op(1,b);
  od;
  if simpCharValhilf(b)<>false then
    simpCharValhilf(b):=simpCharValhilf(b)+e;
  else
    simpCharValhilf(b):=e;
  fi;
  NULL;
end:
simpCharValmal:=proc(tt)
  global simpCharValhilf;
  local erg,ind,a;
  simpCharValhilf:=subsop(4=NULL,eval(simpCharValhilf));
  if type(tt,`*`) then
    map(simpCharValpot,convert(tt,list));
  else
    simpCharValpot(tt);
  fi;
  ind:=[indices(op(4,op(simpCharValhilf)))];
  erg:=1;
  for a in ind do 
    erg:=erg*op(a)^expand(simpCharValhilf(op(a)));
  od;
  erg;
end:
simpCharVal:=proc(tt)
  local t;
  t:=expand(eval(tt));
  if type(t,`+`) then
    map(simpCharValmal,t);
  else
    simpCharValmal(t);
  fi;
end:




# Hilfsfunktion:
nameisinkonvlist:=proc(n)
  local i;
  for i to nops(konvertGEWlist) do
    if n=konvertGEWlist[i][1] then
      RETURN(true);
    fi;
  od;
  false;
end:

GEW2qscewpot:=proc(tt)
  local i, b;
  b:=op(1,tt);
  for i to nops(konvertGEWlist) do
    if b=konvertGEWlist[i][1] then
      RETURN(nfcew([1,op(2,tt),konvertGEWlist[i][2]]));
    fi;
  od;
  # warning: in terms of the form EE^?? we cannot check if the expression
  # makes sense. 
  if b=EE then
    i:=normal(op(2,tt)/(2*Pi*I),expanded);
    RETURN(nfcew([1,numer(i),denom(i)]));
  fi;
  [tt,0,1];
end:

GEW2qscewmal:=proc(tt)
  local erg, i, t, j;
  erg:=[[1,0,1]];
  if type(tt,`*`) then
    for i to nops(tt) do
      t:=op(i,tt);
      if not type(t,`^`) then
        if nameisinkonvlist(t)=true then
          for j to nops(konvertGEWlist) do
            if t=konvertGEWlist[j][1] then
              erg:=malscew(erg, [[1,1,konvertGEWlist[j][2]]]);
            fi;
          od;
        else
          erg:=malscew(erg, [[op(i,tt),0,1]]);
        fi;
      else
        erg:=malscew(erg, [GEW2qscewpot(op(i,tt))]);
      fi;
    od;
  else
    if not type(tt,`^`) then
      if nameisinkonvlist(tt)=true then
        for j to nops(konvertGEWlist) do
          if tt=konvertGEWlist[j][1] then
            erg:=[[1,1,konvertGEWlist[j][2]]];
          fi;
        od;
      else
        erg:=[[tt,0,1]];
      fi;
    else
      erg:=[GEW2qscewpot(tt)];
    fi;
  fi;
  op(map(nfcew, erg));
end:

GEW2qscew:=proc(tt)
  local t, i, erg;
  if tt=0 then 
    RETURN([[],[[1,0,1]]]);
  fi;
  t:=simpCharVal(tt);
  erg:=[];
  if type(t,`+`) then
    for i to nops(t) do
      erg:=[op(erg),GEW2qscewmal(op(i,t))];
    od;
  else
    erg:=[GEW2qscewmal(t)];
  fi;
  [simpscew(erg),[[1,0,1]]];
end:


cew2GEW:=proc(cew1)
  local i, qu, cew;
  cew:=eval(cew1);
  if cew[2]=0 then
    RETURN(factor(cew[1]));
  fi;
  for i to nops(konvertGEWlist) do
    if divide(konvertGEWlist[i][2],cew[3],'qu') 
                              and type(qu,polynom(integer)) then
      RETURN(factor(cew[1])*
                       konvertGEWlist[i][1]^(expand(qu*cew[2])));
    fi;
  od;
# Falls Umwandlung nicht anders moeglich:
  cew[1]*EE^(2*Pi*I*cew[2]/cew[3]);
end:

scew2GEW:=proc(scew)
  local cew, erg;
  erg:=0;
  for cew in scew do
    erg:=erg+cew2GEW(cew);
  od;
  erg;
end:

qscew2GEW:=proc(qscew)
  normal(scew2GEW(qscew[1])/scew2GEW(qscew[2]));
end:

