# g2-gen
# Maple code for calculating in the exceptional Lie algebra G2 
# via the embedding in SO(7)
# by
# Piotr Kobak and Andrew Swann
# 
# This code is generated from a noweb source file g2.nw
# See that for further description and comments.  
# RCS info from g2.nw:
# $Id: g2.nw,v 1.4 2000/01/05 14:10:18 swann Exp $
with(linalg):
AntiDiagonal := matrix(7,7,0):
for i to 7 do
    AntiDiagonal[i,8-i] := 1;
od:
`index/so7` := proc(inds,A,v)
    local ind,i,j,t,swap;
    ind := [op(inds)];
    i := ind[1];
    j := ind[2];
    t := i+j-8;
    if t>0 then
      swap := i;
      i:=8-j;
      j:=8-swap;
    fi;
    if (nargs = 2) then
      if t=0 then
         RETURN(0);
      elif t<0 then
         RETURN(A[i,j]);
      else
         RETURN(-A[i,j]);
      fi;
    elif (nargs = 3) then
      if t=0 then
          if op(v)=0 then
              RETURN(0);
          else
              ERROR(`Elements of SO(7) cannot have`,
              `non-zero entries on the antidiagonal`)
          fi;
      elif t<0 then
          A[i,j] := op(v);
      else
          A[i,j] := -op(v);
      fi;
    else ERROR(`Invalid arguments passed to`, procname);
    fi;           
end:
so7matrix := proc() array(so7,1..7,1..7) end:
so7sparse := proc() array(so7,sparse,1..7,1..7) end: 
so7transpose := proc(A)
    local a,i,j,out;
    a:=evalm(A);
    out:=matrix(7,7);
    for i from 1 to 7 do
        for j from 1 to 7 do
            out[i,j] := A[8-j,8-i];
        od;
    od;
    evalm(out);
end:
LieBracket := proc (X, Y, trans)
    local Z;
    if (nargs = 3) then 
        Z := evalm(X &* Y);
        RETURN(evalm(Z - trans(Z)));
    elif (nargs = 2) then 
        RETURN(evalm(X &* Y - Y &* X));
    else 
        ERROR(`Wrong number of arguments to`, procname);
    fi;
end:
so7Lb := proc(X,Y)
    LieBracket(X,Y,so7transpose);
end:
MetricForm := proc(X,Y,nn::integer)
    local boundsx,boundsy,i,j,n,total;
    global MetricNormalisation;
    if (nargs = 3) then 
        n := nn;
    else
        boundsx := [op(2,evalm(X))];
        boundsy := [op(2,evalm(Y))];
        n := op([1,2],boundsx);
        if not(n=op([2,2],boundsx))
            or not(n=op([1,2],boundsy))
            or not(n=op([2,2],boundsy))
        then
            ERROR(`Arguments of`, procname,
            `need to be square matrices of the same size`);
        fi;
    fi;
    total := 0;
    for i to n do
        for j to n do
            total := total + X[i,j]*Y[j,i];
        od;
    od;
    - MetricNormalisation * total;
end:
RConj := proc(X)
    evalm(- transpose(X));
end:
Conj := proc(X)
    map(conjugate,evalm(RConj(X)));
end:
sl3 := proc(A)
    local Am,i,j,out;
    Am := convert(eval(A),matrix);
    if (not([op(2,eval(Am))]=[1..3,1..3])) then
        ERROR(`Argument of`, procname,
            `must be convertible to a 3x3 matrix`);
    fi;
    if not(trace(Am)=0) then
        print(`Warning: matrix passed to sl3 is not trace-free`);
    fi;
    out := so7sparse();
    for i from 1 to 3 do
        for j from 1 to 3 do
            out[i,j]:= Am[i,j];
        od;
    od;
    evalm(out);
end:
V10 := proc(L::list)
    local i,out;
    if not(nops(L)=3) then
        ERROR(`Argument to`, procname, `must have 3 elements`);
    fi;
    out := so7sparse();
    out[6,3] :=  L[1]/sqrt(2);
    out[5,1] :=  L[2]/sqrt(2);
    out[7,2] :=  L[3]/sqrt(2);
    for i from 1 to 3 do
        out[i,4] := L[i];
    od;
    evalm(out);
end:
V01 := proc(L::list)
    local i,out;
    if not(nops(L)=3) then
        ERROR(`Argument to`, procname, `must have 3 elements`);
    fi;
    out := so7sparse();
    out[3,6] :=  L[1]/sqrt(2);
    out[1,5] :=  L[2]/sqrt(2);
    out[2,7] :=  L[3]/sqrt(2);
    for i from 1 to 3 do
        out[4,i] := L[i];
       od;
    evalm(out);
end:
