#####
#
# This is GAP code
# 
# The file is ~steger/donald/SU21/C10p2/padic.gap
#
# See file ~steger/donald/SU21/C10p2/D.txt for the definition of the
# division algebra $\D$ and for all the notation.
#
#####

#####
#
# $k=\QQ[\sqrt{2}]$
#

Sk:=Sqrt(2);    # Generating element
kk:=Field(Sk);  # The field itself

kkBasis:=Basis(kk,[1,Sqrt(2)]); #Basis

SzFcn:=x->x^2-2*One(x); # Polynomial which S satisfies
IsZero(SzFcn(Sk));      # Check that it does

#Function to reduce elements of~$k$ modulo an integer
kkMod:=function(x,q)
  local c;
  c:=Coefficients(kkBasis,x);
  Apply(c,t->t mod q);
  return LinearCombination(kkBasis,c);
end;

#Function to reduce matrices over~$k$ modulo an integer
kkModM:=function(mtx,q)
  return List(mtx,row->List(row,x->kkMod(x,q)));
end;

#Calculate the ``discriminant'' of the basis
kkTraceMatrix:=List(kkBasis,x->List(kkBasis,y->Trace(kk,x*y)));
Factors(Determinant(kkTraceMatrix));
Set(Factors(Determinant(kkTraceMatrix))) = [2];

#####
#
# $\ell=\QQ[\sqrt{2},\sqrt{-5+2\sqrt{2}}]$
#

xk:=Indeterminate(kk,"x");
ll:=FieldExtension(kk,xk^2 - (1+Sk)*xk + 2); # The field itself

Ul:=RootOfDefiningPolynomial(ll); # Generating element, U
Vl:=2*Ul-(1+Sqrt(2))*One(ll);     # \sqrt{-5+2\sqrt{2}}
UPl:=2/Ul;                        # U', The conjugate of U
Sl:=Sk*One(ll);                   # \sqrt{2}

llBasis:=Basis(ll,[One(ll),Ul]); #Basis over $k$
llPBasis:=Basis(ll,[One(ll),UPl]); # Alternative Basis over $k$
#Function to calculate coefficients over $\QQ$
llCoeff:=x->List(Coefficients(llBasis,x),y->Coefficients(kkBasis,y));

VzFcn:=x->x^2-(-5+2*Sqrt(2))*One(x);  #Function satisfied by V
UzFcn:=x->x^2-(1+Sqrt(2))*x+2*One(x); #Function satisfied by U
#Check that S, V, U, U' are roots of their polynomials
IsZero(SzFcn(Sl)); IsZero(VzFcn(Vl));
IsZero(UzFcn(Ul)); IsZero(UzFcn(UPl));

#Function to conjugate an element
llConj:=x->LinearCombination(llPBasis,Coefficients(llBasis,x));
#Function to calculate the adjoint of a matrix
llAdj:=m->TransposedMat(List(m,r->List(r,x->llConj(x))));

#Check that conjugates are as expected
llConj(Ul) = UPl; llConj(UPl) = Ul; llConj(Vl) = -Vl; llConj(Sl) = Sl;

#Function to reduce elements of~$\ell$ modulo an integer
llMod:=function(x,q)
  return LinearCombination(llBasis,
    List(Coefficients(llBasis,x),c->kkMod(c,q))
  );
end;

#Function to reduce matrices over~$\ell$ modulo an integer
llModM:=function(mtx,q)
  return List(mtx,row->List(row,x->llMod(x,q)));
end;


#Calculate the ``discriminant'' of the basis
llTraceMatrix:=ListX(kkBasis,llBasis,
  function(x1,x2)
    return ListX(kkBasis,llBasis,
      function(y1,y2)
        return Trace(kk,Rationals,x1*y1*Trace(x2*y2));
      end);
  end);
Factors(Determinant(llTraceMatrix));
Set(Factors(Determinant(llTraceMatrix))) = [2,17];

#####
#
# $k[\zeta_9+\zeta_9^-1] = \QQ[\sqrt{2},\zeta_9+\zeta_9^-1$,
# which is the totally real part of
# $m=k[\sqrt{-5+2\sqrt{2}},\zeta_9+\zeta_9^{-1}$ 
#

SRe:=Sk; #Element generating $k$
WRe:=E(9)+E(9)^-1; #Element generating mmRe
mmRe:=Field(WRe,SRe); # The field mmRe

WzFcn:=x->x^3-3*x+One(x); # Polynomial of W
IsZero(WzFcn(WRe)); # Check that it works

#Basis over~$\QQ$
mmReBasis:=Basis(mmRe,[1,SRe,WRe,SRe*WRe,WRe^2,SRe*WRe^2]);
#Automorphisms taking~$W$ to its two conjugates
phiRe:=ANFAutomorphism(mmRe,31);
phi2Re:=phiRe^2;

#Polynomials which give $\phi$ and $\phi^2$, when applied to~$W$
phiOnW:= W -> -W^2-W+2*One(W);
phi2OnW:= W -> W^2 - 2*One(W);

#Check that the conjugates of W are conjugates
IsZero(WzFcn(WRe^phiRe)); IsZero(WzFcn(WRe^phi2Re));
#Check that they are the right conjugates
WRe^phiRe = phiOnW(WRe); WRe^phi2Re = phi2OnW(WRe);

#Function to reduce elements of _mmRe_ modulo an integer
mmReMod:=function(x,q)
  local c;
  c:=Coefficients(mmReBasis,x);
  Apply(c,t->t mod q);
  return LinearCombination(mmReBasis,c);
end;

#Calculate the ``discriminant'' of the basis
mmReTraceMatrix:=
  List(mmReBasis,x->List(mmReBasis,y->Trace(mmRe,x*y)));
Factors(Determinant(mmReTraceMatrix));
Set(Factors(Determinant(mmReTraceMatrix))) = [2,3];

#####
#
# $m=\QQ[\sqrt{2},\sqrt{-5+2\sqrt{2}},\zeta_9+\zeta_9^{-1}$
#

xRe:=Indeterminate(mmRe,"x");
mm:=FieldExtension(mmRe,xRe^2 - (1+SRe)*xRe + 2); #The field $m$

Um:=RootOfDefiningPolynomial(mm); #U, the generator of $m$
UPm:=2/Um;                        #U', the conjugate of U
Vm:=2*Um-(1+Sqrt(2))*One(mm);     #\sqrt{-5+2\sqrt{2}}
Sm:=SRe*One(mm);  # \sqrt{2}
Wm:=WRe*One(mm);  # \zeta_9+\zeta_9^{-1}

mmBasis:=Basis(mm,[One(mm),Um]);   #Basis over _mmRe_
mmPBasis:=Basis(mm,[One(mm),UPm]); #Alternative Basis over _mmRe_
#Function to calculate coefficients over~$\QQ$
mmCoeff:=x->List(Coefficients(mmBasis,x),y->Coefficients(mmReBasis,y));

#Check that S, U, UP, V, and W are roots of the right polynomials
IsZero(SzFcn(Sm)); IsZero(UzFcn(Um)); IsZero(UzFcn(UPm));
IsZero(VzFcn(Vm)); IsZero(WzFcn(Wm));

#Automorphisms taking $W$ to its two conjugates
phimm:=x->LinearCombination(mmBasis,
  List(Coefficients(mmBasis,x),y->y^phiRe));
phi2mm:=x->LinearCombination(mmBasis,
  List(Coefficients(mmBasis,x),y->y^phi2Re));

#Check that these fix S and V
phimm(Sm) = Sm; phimm(Vm) = Vm; phi2mm(Sm)=Sm; phi2mm(Vm)=Vm;
#Check that the conjugates are actually conjugates
IsZero(WzFcn(phimm(Wm))); IsZero(WzFcn(phi2mm(Wm))); 
#Check that they can be calculated from W using the right polynomials
phiOnW(Wm) = phimm(Wm); phi2OnW(Wm) = phi2mm(Wm); 

#Is an element of~$m$ actually in~$\ell$
Inl:=x->(phimm(x)=x);

#Convert an element of~$m$ to~$\ell$
mTol:=function(x)
  if(not Inl(x)) then
    return fail;
  else
    return LinearCombination(llBasis,Coefficients(mmBasis,x));
  fi;
end;

#Check that this works
mTol(Sm)=Sl; mTol(Um)=Ul; mTol(Vm)=Vl; mTol(Wm)=fail;

#Convert a matrix over~$m$ to a matrix over~$\ell$
mTolM:=mtx->List(mtx,row->List(row,x->mTol(x)));

#Complex conjugation
mmConj:=x->LinearCombination(mmPBasis,Coefficients(mmBasis,x));
#Adjoint of a matrix
mmAdj:=m->TransposedMat(List(m,r->List(r,x->mmConj(x))));

#Check that conjugation acts as it should
mmConj(Um) = UPm; mmConj(UPm) = Um; mmConj(Vm) = -Vm;
mmConj(Sm) = Sm; mmConj(Wm) = Wm;
#Norm from $m$ to $\ell$
NmTol:=x->mTol(x*phimm(x)*phi2mm(x));

#Is an element of~$m$ actually in _mmRe_
InmmRe:=x->IsZero(Coefficients(mmBasis,x)[2]);

#Convert an element of~$m$ to _mmRe_
mToRe:=function(x)
  local c;
  c:=Coefficients(mmBasis,x);
  if(IsZero(c[2]))then
    return c[1];
  else
    return fail;
  fi;
end;

#Check that this works
mToRe(Sm)=SRe; mToRe(Um)=fail; mToRe(Vm)=fail; mToRe(Wm)=WRe;

#Norm from $m$ to _mmRe_
NmTomRe:=x->mToRe(x*mmConj(x));

#Function to reduce elements of~$m$ modulo an integer
mmMod:=function(x,q)
  return LinearCombination(mmBasis,
    List(Coefficients(mmBasis,x),c->mmReMod(c,q))
  );
end;
#Function to reduce matrices over~$m$ modulo an integer
mmModM:=function(mtx,q)
  return List(mtx,row->List(row,x->mmMod(x,q)));
end;

#Calculate the ``discriminant'' of the basis
mmTraceMatrix:=ListX(mmReBasis,mmBasis,
  function(x1,x2)
    return ListX(mmReBasis,mmBasis,
      function(y1,y2)
        return Trace(mmRe,Rationals,x1*y1*Trace(x2*y2));
      end);
  end);
Factors(Determinant(mmTraceMatrix));
Set(Factors(Determinant(mmTraceMatrix))) = [2,3,17];

#####
#
# Utility functions for matrices
#

fOnM:=function(f,mtx)
  return List(mtx,row->List(row,x->f(x)));
end;

######
#
# The representation of~$\D$ as matrices over~$m$
#
######

WDM:=[[Wm,0,0],[0,phimm(Wm),0],[0,0,phi2mm(Wm)]]*One(mm);
SDM:=Sm*One(WDM);
UDM:=Um*One(WDM);
UPDM:=2/UDM;
VDM:=Vm*One(WDM);
sigmaDM:=[[0,1,0],[0,0,1],[Um/Sm,0,0]]*One(mm);
sigmaDMInv:=sigmaDM^-1;

#Polynomial of which~$\sigma$ is a root
sigmazmFcn:=sigma->sigma^3-(Um/Sm)*One(sigma);

#Check that W, S, U, U', V, and $\sigma$  are roots of the right
#polynomials.
IsZero(WzFcn(WDM)); IsZero(SzFcn(SDM));
IsZero(UzFcn(UDM)); IsZero(UzFcn(UPDM));
IsZero(VzFcn(VDM)); IsZero(sigmazmFcn(sigmaDM));

#Check that W, S, V, and U all commute
WDM*SDM=SDM*WDM; WDM*VDM=VDM*WDM; WDM*UDM=UDM*WDM;
SDM*VDM=VDM*SDM; SDM*UDM=UDM*SDM; VDM*UDM=UDM*VDM; 

#Check that $\sigma$ commutes with S, V, and U
sigmaDM*SDM=SDM*sigmaDM; sigmaDM*VDM=VDM*sigmaDM;
sigmaDM*UDM=UDM*sigmaDM;

#Check that~$\sigma$ conjugates~$W$ correctly
sigmaDM*WDM=phiOnW(WDM)*sigmaDM;
sigmaDM*phiOnW(WDM)=phi2OnW(WDM)*sigmaDM;
sigmaDM*phi2OnW(WDM)=WDM*sigmaDM;

#####
#
# In the matrix representation of~$\D$ over~$m$, the matrix~$F$ so
# that
#
#   \iota(A) = F^{-1} A^* F
#

FDM:=-Sm*One(WDM)+(1-Sm)*WDM+WDM^2;

# Check that FDM is self-adjoint
mmAdj(FDM) = FDM;
# Check that $\iota$ is acts correctly on S, U, V, W, $\sigma$,
# and $sigma^-1$ 
mmAdj(SDM)*FDM = FDM*SDM;  mmAdj(UDM)*FDM = FDM*UPDM;
mmAdj(VDM)*FDM = -FDM*VDM; mmAdj(WDM)*FDM = FDM*WDM;
mmAdj(sigmaDM)*FDM = 
  FDM*(-Sm*One(WDM)+(-3+Sm)*WDM+(1-Sm)*WDM^2)*sigmaDMInv;
mmAdj(sigmaDMInv)*FDM = 
  FDM*((1-2*Sm)*One(WDM)+(1+2*Sm)*WDM+(1+Sm)*WDM^2)*sigmaDM;

#####
#
# We work out the correct integrality conditions at the 3-adic
# place.
#
# See file ~steger/donald/SU21/C10p2/D.txt for the definition of the
# division algebra $\D$ and for all the notation.
#
#####

#####
#
# $\eta_3\in m$  so that
#
#   N_{m/\ell} \eta_3 = U/\sqrt{2} \mod 3^12
#  \eta_3 \bar\eta_3 = 1 \mod 3^12
#
# This is an approximation $\mod 3^12$.
#

#First we find $\eta_3$ such that $N_{m/\ell}\eta_3=U/\sqrt{2} \mod 3$
etaList1:=[];
for c00 in [-1..1] do
  for c01 in [-1..1] do
    for c10 in [-1..1] do
      for c11 in [-1..1] do
  trial:=(c00+c01*Sm)+(c10+c11*Sm)*Um;
  test:=NmTol(trial)-Ul/Sl;
  if IsZero(llMod(test,3)) then
    Add(etaList1,[[c00,c01,c10,c11],trial]);
  fi;
      od;
    od;
  od;
od;
Length(etaList1);

eta3Approx1:= 2*One(mm)+(2*One(mm)+2*Sm)*Um;

#Next, we find $\eta_3 such that $N_{m/\ell}\eta_3=U/\sqrt{2} \mod 9$ and
# $eta \bar\eta_3 = 1 \mod (W+1)^2$
etaList2:=[];
for c001 in [0..2] do
  for c011 in [0..2] do
    for c101 in [0..2] do
      for c111 in [0..2] do
  trial:=eta3Approx1+((c001+c011*Sm)+(c101+c111*Sm)*Um)*(Wm+1);
  lTest:=NmTol(trial)-Ul/Sl;
  ReTest:=NmTomRe(trial)-1;
  if IsIntegralCyclotomic(ReTest/(WRe+1)^2) and
     IsZero(llMod(lTest,9))
  then
    Add(etaList2,[[c001,c011,c101,c111],trial]);
  fi;
      od;
    od;
  od;
od;
Length(etaList2);

# Now we improve this value by successive approximations

eta3ApproxY:=(3+Sm)+(1+Sm)*Wm+(4+4*Sm)*Um+(2+2*Sm)*Wm*Um;

for j in [0..50] do
  pow3:=3^j;
  etaListX:=[];
  for c00a in [0..2] do
    for c01a in [0..2] do
      for c00b in [0..2] do
        for c01b in [0..2] do
          trial:=eta3ApproxY
            +(c00a+c01a*Sm)*pow3*(Wm+1)^2+(c00b+c01b*Sm)*pow3*3;
          lTest:=NmTol(trial)-Ul/Sl;
          ReTest:=NmTomRe(trial)-1;
          if IsIntegralCyclotomic(ReTest/(3*(WRe+1)*pow3)) and
             IsZero(llMod(lTest,9*pow3))
          then
            Add(etaListX,[[c00a,c01a,c00b,c01b],trial]);
          fi;
        od;
      od;
    od;
  od;
  #Print("pow3= 3^",j);
  #Print("  Length(etaListX)=  ",Length(etaListX));
  eta3ApproxX:=etaListX[1][2];
  etaListY:=[];
  for c00 in [0..2] do
    for c01 in [0..2] do
      for c10 in [0..2] do
        for c11 in [0..2] do
          trial:=eta3ApproxX
            +((c00+c01*Sm)+(c10+c11*Sm)*Um)*pow3*3*(Wm+1);
          lTest:=NmTol(trial)-Ul/Sl;
          ReTest:=NmTomRe(trial)-1;
          if IsIntegralCyclotomic(ReTest/(3*pow3*(WRe+1)^2)) and
             IsZero(llMod(lTest,27*pow3))
          then
            Add(etaListY,[[c00,c01,c10,c11],trial]);
          fi;
        od;
      od;
    od;
  od;
  #Print("  Length(etaListY)=  ",Length(etaListY),"\n");
  eta3ApproxY:=etaListY[1][2];
od;

eta3:=
  (205986+150195*Sm)+(110557+196619*Sm)*Wm+(399498+365303*Sm)*Wm^2+
  (481942+177067*Sm)*Um+(481940+177065*Sm)*Wm*Um;

#####
#
# Matrices to conjugate the representation of~$\D$ as matrices
# over~$m$ to the representation as matrices
# over~$\ell_3=\ell\otimes\QQ_3=\QQ_3[\sqrt{2},\sqrt{-5+2\sqrt{2}}]$ 
#
# These are approximations $\mod 3^12$
#
C1:=[[eta3*phimm(eta3),0,0],[0,phimm(eta3),0],[0,0,1]]*One(mm);
C1Inv:=mmModM(C1^-1,3^12);
C2:=[
  [1,1,1],
  [(Wm+1),phimm(Wm+1),phi2mm(Wm+1)],
  [(Wm+1)^2,phimm((Wm+1)^2),phi2mm((Wm+1)^2)]
]*One(mm);
C2Inv:=C2^-1;

#Check that the inverses work
IsZero(mmModM(C1*C1Inv-One(C1),3^12));
IsOne(C2*C2Inv);

#####
#
# Conjugated copies of SDM, WDM, UDM, VDM, and sigmaDM lying in
# $\ell_3$.
#
# These are approximations $\mod 3^10$.
#
SDM3:=mTolM(mmModM(C2*C1*SDM*C1Inv*C2Inv,3^10));
UDM3:=mTolM(mmModM(C2*C1*UDM*C1Inv*C2Inv,3^10));
UPDM3:=llModM(2/UDM3,3^10);
VDM3:=mTolM(mmModM(C2*C1*VDM*C1Inv*C2Inv,3^10));
WDM3:=mTolM(mmModM(C2*C1*WDM*C1Inv*C2Inv,3^10));
sigmaDM3:=mTolM(mmModM(C2*C1*sigmaDM*C1Inv*C2Inv,3^10));
sigmaDM3Inv:=llModM(sigmaDM3^-1,3^10);

#Check that sigmaDM3Inv is the inverse of sigmaDM3
IsZero(llModM(sigmaDM3Inv*sigmaDM3-One(sigmaDM3),3^10));

#Polynomial of which~$\sigma$ is a root
sigmazlFcn:=sigma->sigma^3-(Ul/Sl)*One(sigma);

#Check that W, S, U, U', V, and $\sigma$  are roots of the right
#polynomials.
IsZero(llModM(WzFcn(WDM3),3^10));
IsZero(llModM(SzFcn(SDM3),3^10));
IsZero(llModM(UzFcn(UDM3),3^10));
IsZero(llModM(UzFcn(UPDM3),3^10));
IsZero(llModM(VzFcn(VDM3),3^10));
IsZero(llModM(sigmazlFcn(sigmaDM3),3^10));

#Check that W, S, V, and U all commute
IsZero(llModM(WDM3*SDM3-SDM3*WDM3,3^10));
IsZero(llModM(WDM3*VDM3-VDM3*WDM3,3^10));
IsZero(llModM(WDM3*UDM3-UDM3*WDM3,3^10));
IsZero(llModM(SDM3*VDM3-VDM3*SDM3,3^10));
IsZero(llModM(SDM3*UDM3-UDM3*SDM3,3^10));
IsZero(llModM(VDM3*UDM3-UDM3*VDM3,3^10));

#Check that $\sigma$ commutes with S, V, and U
IsZero(llModM(sigmaDM3*SDM3-SDM3*sigmaDM3,3^10));
IsZero(llModM(sigmaDM3*VDM3-VDM3*sigmaDM3,3^10));
IsZero(llModM(sigmaDM3*UDM3-UDM3*sigmaDM3,3^10));

#Check that~$\sigma$ conjugates~$W$ correctly
IsZero(llModM(sigmaDM3*WDM3-phiOnW(WDM3)*sigmaDM3,3^10));
IsZero(llModM(sigmaDM3*phiOnW(WDM3)-phi2OnW(WDM3)*sigmaDM3,3^10));
IsZero(llModM(sigmaDM3*phi2OnW(WDM3)-WDM3*sigmaDM3,3^10));

#####
#
# In the 3-adic matrix representation, the element~$F$ such that
#
#   \iota(A) = F^{-1} A^* F
#
# Thus, $\iota$-unitary elements will preserve the sesquilinear form
# defined by $F$.
#
# This is an approximation $\mod 3^10$.
#

FDM3:=mTolM(mmModM(3*mmAdj(C2Inv)*mmAdj(C1Inv)*FDM*C1Inv*C2Inv,3^10));

# Check that FDM3 is self-adjoint
llAdj(FDM3) = FDM3;
# Check that $\iota$ is acts correctly on S, U, V, W, $\sigma$,
# and $sigma^-1$ 
IsZero(llModM(llAdj(SDM3)*FDM3 - FDM3*SDM3,3^10));
IsZero(llModM(llAdj(UDM3)*FDM3 - FDM3*UPDM3,3^10));
IsZero(llModM(llAdj(VDM3)*FDM3 - -FDM3*VDM3,3^10));
IsZero(llModM(llAdj(WDM3)*FDM3 - FDM3*WDM3,3^10));
IsZero(llModM(llAdj(sigmaDM3)*FDM3 - 
  FDM3*(-Sl*One(WDM3)+(-3+Sl)*WDM3+(1-Sl)*WDM3^2)*sigmaDM3Inv,3^10));
IsZero(llModM(llAdj(sigmaDM3Inv)*FDM3 - 
  FDM3*((1-2*Sl)*One(WDM3)+(1+2*Sl)*WDM3+(1+Sl)*WDM3^2)*sigmaDM3,3^10));

#Check that FDM3 and its inverse are integral
IsZero(llModM(3*FDM3,3));
IsZero(llModM(3*FDM3^-1,3));
IsZero(llMod(3*Determinant(FDM3),3));
not IsZero(llMod(Determinant(FDM3),3));
			  
IsZero(llModM(
 FDM3 - One(FDM3)*
   [[3-3*Sqrt(2),-3*Sqrt(2),-1+2*Sqrt(2)],
    [-3*Sqrt(2),2-Sqrt(2),-1+Sqrt(2)],
    [-1+2*Sqrt(2),-1+Sqrt(2),1-Sqrt(2)]]
,3^10));

# The preceding checks show that the standard $\ell_3$ lattice
# $L_0=\O_{\ell_3}^3$, is self-dual with respect to the form given
# by~$F$.  Note that
# $\O_{\ell_3}=\ZZ_3[\sqrt{2},\sqrt{-5+2\sqrt{2}}}]$. 
 
# Consequently matrices which preserve that lattice, which is to say
# matrices in $GL(3,\ZZ_3[\sqrt{2},\sqrt{-5+2\sqrt{2}}}]$, fix a
# hyperspecial vertex in the 3-adic tree corresponding to the 3-adic
# version of $PU(\D,\iota)$.

# 36-element basis of $\D$ over $\QQ$
basisDM3:=ListX([0..1],[0..1],[0..2],[-1..1],
  function(i,j,k,l)
    return Sl^i*Ul^j*WDM3^k*sigmaDM3^l;
  end
);;

# For a $3\times 3$ matrix with coefficients in
# $\O_\ell=\ZZ[\sqrt{2},\sqrt{-5+2\sqrt{2}}]$, for each of the
# 9~entries this function calculate the four coefficients, each
# modulo~$q$, and puts them together as a 36~element vector.

CondCoeff3:=function(mtx,q)
  return Concatenation(ListX([1..3],[1..3],
    function(j,k)
      local c;
      c:=Concatenation(llCoeff(mtx[j][k]));
      Apply(c,x->x mod q);
      return c;
    end)
  );
end;

# The element
#
#   \sum c_{ijkl} \sqrt{2}^i U^j W^k \sigma^l 
#
# in~$\D$ will have (approximate) 3-adic matrix representation
#
#    Sum([1..36],ix->c[ix]*basis[ix])
#  
# and this 3-adic matrix representation will be 3-adically integral if
# and only if the vector:
#
#   CondMtxDM3Type1 * c
#
# consists of 3-adic integers.
CondMtxDM3Type1:=
  TransposedMat(List(basisDM3,mtx->CondCoeff3(mtx,3^3)));;


#CondMtxDM3Type1:=[
#[ 23, 1, 8, 13, 26, 22, 5, 1, 14, 4, 0, 23, 2, 0, 10, 4, 0, 11, 26, 0, 3, 16, 
#    0, 0, 14, 0, 0, 8, 0, 19, 4, 0, 20, 8, 0, 22 ], 
#[ 13, 0, 15, 8, 0, 0, 7, 0, 0, 4, 0, 23, 2, 0, 10, 4, 0, 11, 23, 1, 8, 13, 
#    26, 22, 5, 1, 14, 4, 0, 23, 2, 0, 10, 4, 0, 11 ], 
#[ 25, 0, 2, 26, 0, 22, 25, 0, 8, 17, 1, 14, 10, 26, 7, 26, 1, 11, 23, 0, 4, 
#    25, 0, 17, 23, 0, 16, 18, 0, 11, 12, 0, 7, 6, 0, 5 ], 
#[ 25, 0, 2, 26, 0, 22, 25, 0, 8, 9, 0, 19, 6, 0, 17, 3, 0, 16, 25, 0, 2, 26, 
#    0, 22, 25, 0, 8, 17, 1, 14, 10, 26, 7, 26, 1, 11 ], 
#[ 22, 0, 22, 4, 1, 4, 18, 25, 0, 7, 0, 14, 18, 0, 15, 11, 0, 4, 16, 0, 20, 
#    16, 0, 10, 15, 0, 20, 23, 0, 19, 0, 0, 12, 4, 0, 26 ], 
#[ 8, 0, 10, 8, 0, 5, 21, 0, 10, 25, 0, 23, 0, 0, 6, 2, 0, 13, 22, 0, 22, 4, 
#    1, 4, 18, 25, 0, 7, 0, 14, 18, 0, 15, 11, 0, 4 ], 
#[ 10, 0, 20, 18, 0, 6, 8, 0, 25, 7, 0, 19, 22, 1, 4, 24, 25, 12, 2, 0, 4, 0, 
#    0, 21, 25, 0, 14, 11, 0, 10, 25, 0, 16, 2, 0, 3 ], 
#[ 1, 0, 2, 0, 0, 24, 26, 0, 7, 19, 0, 5, 26, 0, 8, 1, 0, 15, 10, 0, 20, 18, 
#    0, 6, 8, 0, 25, 7, 0, 19, 22, 1, 4, 24, 25, 12 ], 
#[ 6, 0, 13, 17, 0, 2, 19, 1, 3, 0, 0, 20, 9, 0, 23, 7, 0, 17, 13, 0, 24, 25, 
#    0, 17, 14, 0, 17, 0, 0, 4, 0, 0, 19, 23, 0, 16 ], 
#[ 20, 0, 12, 26, 0, 22, 7, 0, 22, 0, 0, 2, 0, 0, 23, 25, 0, 8, 6, 0, 13, 17, 
#    0, 2, 19, 1, 3, 0, 0, 20, 9, 0, 23, 7, 0, 17 ], 
#[ 0, 0, 17, 9, 0, 2, 10, 0, 5, 6, 0, 1, 26, 0, 8, 4, 1, 0, 0, 0, 25, 0, 0, 4, 
#    2, 0, 19, 13, 0, 2, 16, 0, 25, 9, 0, 19 ], 
#[ 0, 0, 26, 0, 0, 2, 1, 0, 23, 20, 0, 1, 8, 0, 26, 18, 0, 23, 0, 0, 17, 9, 0, 
#    2, 10, 0, 5, 6, 0, 1, 26, 0, 8, 4, 1, 0 ], 
#[ 9, 0, 3, 18, 0, 9, 21, 24, 18, 6, 0, 6, 6, 0, 21, 6, 0, 18, 15, 0, 3, 3, 0, 
#    0, 9, 0, 24, 12, 0, 12, 12, 0, 15, 12, 0, 9 ], 
#[ 21, 0, 15, 15, 0, 0, 18, 0, 12, 6, 0, 6, 6, 0, 21, 6, 0, 18, 9, 0, 3, 18, 
#    0, 9, 21, 24, 18, 6, 0, 6, 6, 0, 21, 6, 0, 18 ], 
#[ 24, 0, 24, 24, 0, 3, 24, 0, 18, 0, 0, 21, 9, 0, 18, 12, 24, 18, 21, 0, 21, 
#    21, 0, 6, 21, 0, 9, 3, 0, 18, 18, 0, 12, 24, 0, 15 ], 
#[ 24, 0, 24, 24, 0, 3, 24, 0, 18, 15, 0, 9, 9, 0, 6, 12, 0, 21, 24, 0, 24, 
#    24, 0, 3, 24, 0, 18, 0, 0, 21, 9, 0, 18, 12, 24, 18 ], 
#[ 26, 1, 26, 22, 26, 4, 8, 1, 17, 25, 0, 2, 2, 0, 19, 4, 0, 8, 5, 0, 3, 4, 0, 
#    3, 20, 0, 3, 23, 0, 4, 4, 0, 11, 8, 0, 16 ], 
#[ 16, 0, 15, 2, 0, 15, 10, 0, 15, 25, 0, 2, 2, 0, 19, 4, 0, 8, 26, 1, 26, 22, 
#    26, 4, 8, 1, 17, 25, 0, 2, 2, 0, 19, 4, 0, 8 ], 
#[ 1, 0, 26, 26, 0, 4, 25, 0, 23, 2, 1, 23, 19, 26, 16, 2, 1, 5, 2, 0, 25, 25, 
#    0, 8, 23, 0, 19, 9, 0, 26, 0, 0, 19, 12, 0, 14 ], 
#[ 1, 0, 26, 26, 0, 4, 25, 0, 23, 18, 0, 13, 0, 0, 23, 6, 0, 7, 1, 0, 26, 26, 
#    0, 4, 25, 0, 23, 2, 1, 23, 19, 26, 16, 2, 1, 5 ], 
#[ 23, 0, 15, 9, 1, 5, 10, 1, 23, 9, 0, 16, 16, 0, 13, 7, 0, 12, 11, 0, 14, 
#    12, 0, 7, 22, 0, 17, 0, 0, 23, 23, 0, 8, 23, 0, 15 ], 
#[ 19, 0, 7, 6, 0, 17, 11, 0, 22, 0, 0, 25, 25, 0, 4, 25, 0, 21, 23, 0, 15, 9, 
#    1, 5, 10, 1, 23, 9, 0, 16, 16, 0, 13, 7, 0, 12 ], 
#[ 9, 0, 19, 19, 0, 7, 10, 0, 21, 5, 0, 9, 3, 1, 8, 22, 1, 23, 0, 0, 2, 2, 0, 
#    23, 2, 0, 6, 2, 0, 0, 25, 0, 17, 17, 0, 11 ], 
#[ 0, 0, 1, 1, 0, 25, 1, 0, 3, 1, 0, 0, 26, 0, 22, 22, 0, 19, 9, 0, 19, 19, 0, 
#    7, 10, 0, 21, 5, 0, 9, 3, 1, 8, 22, 1, 23 ], 
#[ 0, 0, 12, 12, 24, 0, 12, 24, 15, 12, 0, 0, 12, 0, 12, 18, 0, 21, 18, 0, 3, 
#    12, 0, 24, 3, 0, 21, 24, 0, 0, 24, 0, 24, 9, 0, 15 ], 
#[ 9, 0, 15, 6, 0, 12, 15, 0, 24, 12, 0, 0, 12, 0, 12, 18, 0, 21, 0, 0, 12, 
#    12, 24, 0, 12, 24, 15, 12, 0, 0, 12, 0, 12, 18, 0, 21 ], 
#[ 21, 0, 0, 21, 0, 21, 18, 0, 3, 9, 0, 12, 21, 24, 9, 12, 24, 24, 15, 0, 0, 
#    15, 0, 15, 9, 0, 6, 21, 0, 3, 15, 0, 0, 21, 0, 6 ], 
#[ 21, 0, 0, 21, 0, 21, 18, 0, 3, 24, 0, 15, 21, 0, 0, 24, 0, 3, 21, 0, 0, 21, 
#    0, 21, 18, 0, 3, 9, 0, 12, 21, 24, 9, 12, 24, 24 ], 
#[ 21, 0, 3, 3, 0, 21, 21, 24, 3, 0, 0, 21, 6, 0, 0, 12, 0, 9, 9, 0, 6, 24, 0, 
#    6, 0, 0, 9, 0, 0, 15, 12, 0, 0, 24, 0, 18 ], 
#[ 18, 0, 3, 12, 0, 3, 0, 0, 18, 0, 0, 21, 6, 0, 0, 12, 0, 9, 21, 0, 3, 3, 0, 
#    21, 21, 24, 3, 0, 0, 21, 6, 0, 0, 12, 0, 9 ], 
#[ 0, 0, 3, 24, 0, 0, 21, 0, 9, 21, 0, 12, 21, 0, 21, 3, 24, 3, 0, 0, 6, 21, 
#    0, 0, 15, 0, 18, 9, 0, 18, 12, 0, 6, 3, 0, 18 ], 
#[ 0, 0, 3, 24, 0, 0, 21, 0, 9, 18, 0, 9, 6, 0, 3, 15, 0, 9, 0, 0, 3, 24, 0, 
#    0, 21, 0, 9, 21, 0, 12, 21, 0, 21, 3, 24, 3 ], 
#[ 5, 1, 20, 19, 2, 1, 14, 4, 23, 25, 0, 2, 23, 0, 25, 19, 0, 8, 23, 0, 21, 7, 
#    0, 24, 20, 0, 24, 23, 0, 4, 19, 0, 23, 11, 0, 16 ], 
#[ 25, 0, 24, 17, 0, 12, 10, 0, 12, 25, 0, 2, 23, 0, 25, 19, 0, 8, 5, 1, 20, 
#    19, 2, 1, 14, 4, 23, 25, 0, 2, 23, 0, 25, 19, 0, 8 ], 
#[ 1, 0, 26, 2, 0, 1, 4, 0, 23, 8, 1, 17, 25, 2, 4, 26, 4, 11, 2, 0, 25, 4, 0, 
#    2, 8, 0, 19, 0, 0, 17, 15, 0, 1, 9, 0, 8 ], 
#[ 1, 0, 26, 2, 0, 1, 4, 0, 23, 0, 0, 22, 21, 0, 14, 18, 0, 4, 1, 0, 26, 2, 0, 
#    1, 4, 0, 23, 8, 1, 17, 25, 2, 4, 26, 4, 11 ]
#];


#####
#
# We work out the correct integrality conditions at the  first
# $17$-adic place of~$\ell$, denoted $17+$, and characterized
# by the choice:
#
#   \sqrt{2} = 6 \mod 17+
#
# See file ~steger/donald/SU21/C10p2/D.txt for the definition of the
# division algebra $\D$ and for all the notation.
#
#####

#####
#
# S17 so that
#
#   S17^2 = 2 \mod 17^{10},  S17 = 6 \mod 17
#
# This is an approximation $\mod 17^{10}$

S17Approx:=6;

for j in [1..10] do
  S17Approx:=(S17Approx - (S17Approx^2-2)/(2*S17Approx)) mod 17^10;
od;

S17:=292094863096;
S17^2 mod 17^10 = 2;


#####
#
# $k_{17+}=\QQ_{17+}$, version for place $17+$
#
# Everything is approximated $\mod 17^{10}$

Sk17p:=S17;
kk17p:=Field(Sk17p);  # The field itself

kk17pBasis:=Basis(kk17p,[1]); #Basis

#Function to reduce elements of~$k$ modulo an integer
kk17pMod:=function(x,q)
  return x mod q;
end;

#Check that Sk17p satisfies the correct polynomial
IsZero(kk17pMod(SzFcn(Sk17p),17^10));   

#Function to reduce matrices over~$k_{17+}$ modulo an integer
kk17pModM:=function(mtx,q)
  return List(mtx,row->List(row,x->kk17pMod(x,q)));
end;

#Calculate the ``discriminant'' of the basis
kk17pTraceMatrix:=
  List(kk17pBasis,x->List(kk17pBasis,y->Trace(kk17p,x*y)));
Factors(Determinant(kk17pTraceMatrix));
Set(Factors(Determinant(kk17pTraceMatrix))) = [1];

#####
#
# $\ell_{17+}=\QQ_{17}[\sqrt{-5+2\cdot 6}]=\QQ_{17}[\sqrt{7}]
#
# Everything is approximated $\mod 17^{10}$.

ll17p:=Field(Sqrt(7));
Sl17p:=Sk17p*One(ll17p);  # \sqrt{2}

ll17pBasis0:=Basis(ll17p,[1,Sqrt(7)]); # Basis

#Function to reduce elements of $l_{17+}$_ modulo an integer
ll17pMod:=function(x,q)
  local c;
  c:=Coefficients(ll17pBasis0,x);
  Apply(c,t->t mod q);
  return LinearCombination(ll17pBasis0,c);
end;

# Find an approximation $\mod 17^{10}$ to $\sqrt{-5+2\sqrt{2}}$
Vl17pApprox:=Sqrt(7);
for j in [1..10] do
  Vl17pApprox:=ll17pMod(Vl17pApprox
    - (Vl17pApprox^2-(-5+2*Sl17p))/(2*Vl17pApprox),17^10);
od;

Vl17p:=1692262524954*Sqrt(7);   # \sqrt{-5+2\sqrt{2}}
Ul17p:=ll17pMod((1+Sl17p+Vl17p)/2,17^10); # Generating element, U
UPl17p:=ll17pMod(2/Ul17p,17^10);          # U', The conjugate of U

ll17pBasis:=Basis(ll17p,[One(ll17p),Ul17p]); #Basis over $k_{17+}$ 
# Alternative Basis over $k_{17+}$
llP17pBasis:=Basis(ll17p,[One(ll17p),UPl17p]); 
#Function to calculate coefficients
ll17pCoeff:=x->Coefficients(ll17pBasis,x);

Vz17pFcn:=x->x^2-(-5+2*Sl17p)*One(x);  #Function satisfied by V
Uz17pFcn:=x->x^2-(1+Sl17p)*x+2*One(x); #Function satisfied by U

#Check that S, V, U, U' are roots of their polynomials
IsZero(ll17pMod(SzFcn(Sl17p),17^10));
IsZero(ll17pMod(Vz17pFcn(Vl17p),17^10));
IsZero(ll17pMod(Uz17pFcn(Ul17p),17^10));
IsZero(ll17pMod(Uz17pFcn(UPl17p),17^10));

#Function to conjugate an element
ll17pConj:=x->x^ANFAutomorphism(ll17p,17);
#Function to calculate the adjoint of a matrix
ll17pAdj:=m->TransposedMat(List(m,r->List(r,x->ll17pConj(x))));

#Check that conjugates are as expected
IsZero(ll17pMod(ll17pConj(Ul17p) - UPl17p,17^10));
IsZero(ll17pMod(ll17pConj(UPl17p) - Ul17p,17^10));
ll17pConj(Vl17p) = -Vl17p;
ll17pConj(Sl17p) = Sl17p;

#Function to reduce matrices over~$\ell_{17p}$ modulo an integer
ll17pModM:=function(mtx,q)
  return List(mtx,row->List(row,x->ll17pMod(x,q)));
end;

#####
#
# The polynomial $W^3-3W+1$ splits in $\ell_{17+}$.  Indeed it splits
# in $k_{17+}=\QQ_{17}$.
#
# Consequently $\mm_{17+}=\ell_{17+}[W]/(W^3-3W+1)$ is the direct sum
# of 3~copies of $\ell_{17+}$.  In these three copies we have,
# respectively, $W=7 \mod 17$, $W=14 \mod 17$, and $W=13 \mod 17$. 

# Find $W$, $\mod 17^{10}$
Wl17pApprox:=7;
for j in [1..10] do
  Wl17pApprox:=ll17pMod(Wl17pApprox
      - (WzFcn(Wl17pApprox)/(3*Wl17pApprox^2-3*One(Wl17pApprox)))
    ,17^10);
od;

Wl17p:=1108420179313;
phiWl17p:=phiOnW(Wl17p) mod 17^10;
phi2Wl17p:=phi2OnW(Wl17p) mod 17^10;

# Check that $W$ and its conjugates are roots of the right polynomial
# ($\mod 17^{10}$) 
IsZero(ll17pMod(WzFcn(Wl17p),17^10));
IsZero(ll17pMod(WzFcn(phiWl17p),17^10));
IsZero(ll17pMod(WzFcn(phi2Wl17p),17^10));

#Check that they are the right conjugates
IsZero(ll17pMod(phiOnW(phiWl17p)-phi2Wl17p,17^10));
IsZero(ll17pMod(phiOnW(phi2Wl17p)-Wl17p,17^10));
	
######
#
# The representation of~$\D$ as matrices over~$\ell_{17+}$.
#
# We find matrices for $S$, $U$, $U'$, $V$, $W, and~$\sigma$, all of
# which are approximations $\mod 17^{10}$. 

WDM17p:=[[Wl17p,0,0],[0,phiWl17p,0],[0,0,phi2Wl17p]]*One(ll17p);
SDM17p:=Sl17p*One(WDM17p);
UDM17p:=Ul17p*One(WDM17p);
UPDM17p:=ll17pModM(2/UDM17p,17^10);
VDM17p:=Vl17p*One(WDM17p);
sigmaDM17p:=
  ll17pModM([[0,1,0],[0,0,1],[Ul17p/Sl17p,0,0]]*One(ll17p),17^10);
sigmaDM17pInv:=ll17pModM(sigmaDM17p^-1,17^10);

#Polynomial of which~$\sigma$ is a root
sigmazl17pFcn:=sigma->sigma^3-(Ul17p/Sl17p)*One(sigma);

#Check that W, S, U, U', V, and $\sigma$  are roots of the right
#polynomials.
IsZero(ll17pModM(WzFcn(WDM17p),17^10));
IsZero(ll17pModM(SzFcn(SDM17p),17^10));
IsZero(ll17pModM(Uz17pFcn(UDM17p),17^10));
IsZero(ll17pModM(Uz17pFcn(UPDM17p),17^10));
IsZero(ll17pModM(Vz17pFcn(VDM17p),17^10));
IsZero(ll17pModM(sigmazl17pFcn(sigmaDM17p),17^10));

#Check that W, S, V, and U all commute
IsZero(ll17pModM(WDM17p*SDM17p-SDM17p*WDM17p,17^10));
IsZero(ll17pModM(WDM17p*VDM17p-VDM17p*WDM17p,17^10));
IsZero(ll17pModM(WDM17p*UDM17p-UDM17p*WDM17p,17^10));
IsZero(ll17pModM(SDM17p*VDM17p-VDM17p*SDM17p,17^10));
IsZero(ll17pModM(SDM17p*UDM17p-UDM17p*SDM17p,17^10));
IsZero(ll17pModM(VDM17p*UDM17p-UDM17p*VDM17p,17^10)); 

#Check that $\sigma$ commutes with S, V, and U
IsZero(ll17pModM(sigmaDM17p*SDM17p-SDM17p*sigmaDM17p,17^10));
IsZero(ll17pModM(sigmaDM17p*VDM17p-VDM17p*sigmaDM17p,17^10));
IsZero(ll17pModM(sigmaDM17p*UDM17p-UDM17p*sigmaDM17p,17^10));

#Check that~$\sigma$ conjugates~$W$ correctly
IsZero(ll17pModM(sigmaDM17p*WDM17p -
  phiOnW(WDM17p)*sigmaDM17p,17^10));
IsZero(ll17pModM(sigmaDM17p*phiOnW(WDM17p) - 
  phi2OnW(WDM17p)*sigmaDM17p,17^10));
IsZero(ll17pModM(sigmaDM17p*phi2OnW(WDM17p) - 
  WDM17p*sigmaDM17p,17^10));


#####
#
# In the matrix representation of~$\D$ over~$\ell_{17+}$, the
# matrix~$F$ so that
#
#   \iota(A) = F^{-1} A^* F
#

FDM17p:= ll17pModM(-Sl17p*One(WDM17p)+(1-Sl17p)*WDM17p+WDM17p^2,17^10);

# Check that FDM17p is self-adjoint
ll17pAdj(FDM17p) = FDM17p;
# Check that $\iota$ is acts correctly on S, U, V, W, $\sigma$,
# and $sigma^-1$ 
ll17pAdj(SDM17p)*FDM17p = FDM17p*SDM17p;
IsZero(ll17pModM(ll17pAdj(UDM17p)*FDM17p - FDM17p*UPDM17p,17^10));
IsZero(ll17pModM(ll17pAdj(VDM17p)*FDM17p - -FDM17p*VDM17p,17^10));
IsZero(ll17pModM(ll17pAdj(WDM17p)*FDM17p - FDM17p*WDM17p,17^10));
IsZero(ll17pModM(ll17pAdj(sigmaDM17p)*FDM17p -  FDM17p*
     (-Sl17p*One(WDM17p)+(-3+Sl17p)*WDM17p+(1-Sl17p)*WDM17p^2)
      *sigmaDM17pInv
  ,17^10));
IsZero(ll17pModM(ll17pAdj(sigmaDM17pInv)*FDM17p - FDM17p*
     ((1-2*Sl17p)*One(WDM17p)+(1+2*Sl17p)*WDM17p+(1+Sl17p)*WDM17p^2)
     *sigmaDM17p
  ,17^10));

#Check that FDM17p and its inverse are integral
IsZero(ll17pModM(17*FDM17p,17));
IsZero(ll17pModM(17*FDM17p^-1,17));
IsZero(ll17pMod(17*Determinant(FDM17p),17));
not IsZero(ll17pMod(Determinant(FDM17p),17));

# The preceding checks show that the standard $\ell_{17+}$-lattice 
# $L_0=\O_{\ell_{17+}}^3$, is self-dual with respect to the form given
# by~$F$.  Note that $\O_{\ell_{17+}=\ZZ_{17}[U]/(U^2-(1+\sqrt{2})U+2)$
 
# Consequently matrices which preserve that lattice, which is to say
# matrices in $GL(3,\ZZ_{17}[U])$, fix a hyperspecial vertex in the
# $17+$-adic tree corresponding to the $17+$-adic version of 
# $PU(\D,\iota)$. 

# 36-element basis of $\D$ over $\QQ$
basisDM17p:=ListX([0..1],[0..1],[0..2],[-1..1],
  function(i,j,k,l)
    return Sl17p^i*Ul17p^j*WDM17p^k*sigmaDM17p^l;
  end
);;

# For a $3\times 3$ matrix with coefficients in
# $\O_{\ell_{17+}}=\ZZ_{17}[\sqrt{7}]$, for each of the 9~entries this
# function calculate the four coefficients, each modulo~$q$, and puts
# them together as a 18~element vector.

CondCoeff17p:=function(mtx,q)
  return Concatenation(ListX([1..3],[1..3],
    function(j,k)
      local c;
      c:=ll17pCoeff(mtx[j][k]);
      Apply(c,x->x mod q);
      return c;
    end)
  );
end;

# The element
#
#   \sum c_{ijkl} \sqrt{2}^i U^j W^k \sigma^l 
#
# in~$\D$, when reduced $\mod 17+$,  will have (approximate) 17-adic
# matrix representation 
#
#    Sum([1..36],ix->c[ix]*basis[ix])
#  
# and this $17$-adic matrix representation will be $17$-adically
# integral if and only if the vector:
#
#   CondMtxDM17pType1 * c
#
# consists of 17-adic integers.
CondMtxDM17pType1:=
  TransposedMat(List(basisDM17p,mtx->CondCoeff17p(mtx,17^2)));;

#CondMtxDM17pType1:=[
#[ 0, 1, 0, 0, 75, 0, 0, 134, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 244, 0, 0, 93, 
#    0, 0, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 75, 0, 0, 134, 0, 0, 0, 0, 0, 0, 0, 
#    0, 0, 0, 0, 244, 0, 0, 93, 0, 0, 39, 0 ], 
#[ 0, 0, 1, 0, 0, 75, 0, 0, 134, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 244, 0, 0, 
#    93, 0, 0, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 75, 0, 0, 134, 0, 0, 0, 0, 0, 0, 
#    0, 0, 0, 0, 0, 244, 0, 0, 93, 0, 0, 39 ], 
#[ 123, 0, 0, 266, 0, 0, 9, 0, 0, 244, 0, 0, 93, 0, 0, 39, 0, 0, 245, 0, 0, 
#    168, 0, 0, 173, 0, 0, 2, 0, 0, 150, 0, 0, 268, 0, 0 ], 
#[ 167, 0, 0, 98, 0, 0, 125, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 288, 0, 0, 214, 
#    0, 0, 155, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 1, 0, 0, 82, 0, 0, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 244, 0, 0, 67, 0, 0, 
#    3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 82, 0, 0, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
#    0, 0, 244, 0, 0, 67, 0, 0, 3, 0, 0 ], 
#[ 0, 1, 0, 0, 82, 0, 0, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 244, 0, 0, 67, 0, 
#    0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 82, 0, 0, 77, 0, 0, 0, 0, 0, 0, 0, 0, 
#    0, 0, 0, 244, 0, 0, 67, 0, 0, 3, 0 ], 
#[ 0, 0, 1, 0, 0, 82, 0, 0, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 244, 0, 0, 67, 
#    0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 82, 0, 0, 77, 0, 0, 0, 0, 0, 0, 0, 
#    0, 0, 0, 0, 244, 0, 0, 67, 0, 0, 3 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 160, 0, 0, 23, 0, 0, 0, 0, 0, 0, 
#    0, 0, 0, 0, 0, 287, 0, 0, 25, 0, 0, 121 ], 
#[ 0, 0, 122, 0, 0, 209, 0, 0, 133, 0, 0, 123, 0, 0, 52, 0, 0, 217, 0, 0, 1, 0, 
#    0, 132, 0, 0, 84, 0, 0, 245, 0, 0, 261, 0, 0, 61 ], 
#[ 1, 0, 0, 132, 0, 0, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 244, 0, 0, 129, 0, 
#    0, 266, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 132, 0, 0, 84, 0, 0, 0, 0, 0, 0, 0, 0, 
#    0, 0, 0, 244, 0, 0, 129, 0, 0, 266, 0, 0 ], 
#[ 0, 1, 0, 0, 132, 0, 0, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 244, 0, 0, 129, 
#    0, 0, 266, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 132, 0, 0, 84, 0, 0, 0, 0, 0, 0, 0, 
#    0, 0, 0, 0, 244, 0, 0, 129, 0, 0, 266, 0 ]
#];

#####
#
# We work out the correct integrality conditions at the second
# $17$-adic place of~$\ell$, denoted $17-$, and characterized
# by the choice:
#
#   \sqrt{2} = -6 \mod 17+
#
# See file ~steger/donald/SU21/C10p2/D.txt for the definition of the
# division algebra $\D$ and for all the notation.
#
#####


#####
#
# $k_{17-}=\QQ_{17-}$, version for place $17-$
#
# Everything is approximated $\mod 17^{10}$

Sk17m:=-S17;
kk17m:=Field(Sk17m);  # The field itself

kk17mBasis:=Basis(kk17m,[1]); #Basis

#Function to reduce elements of~$k$ modulo an integer
kk17mMod:=function(x,q)
  return x mod q;
end;

#Check that Sk17m satisfies the correct polynomial
IsZero(kk17mMod(SzFcn(Sk17m),17^10));   

#Function to reduce matrices over~$k_{17-}$ modulo an integer
kk17mModM:=function(mtx,q)
  return List(mtx,row->List(row,x->kk17mMod(x,q)));
end;

#Calculate the ``discriminant'' of the basis
kk17mTraceMatrix:=
  List(kk17mBasis,x->List(kk17mBasis,y->Trace(kk17m,x*y)));
Factors(Determinant(kk17mTraceMatrix));
Set(Factors(Determinant(kk17mTraceMatrix))) = [1];

#####
#
# $\ell_{17-}=\QQ_{17}[\sqrt{-5+2\cdot 6}]=\QQ_{17}[\sqrt{7}]
#
# Everything is approximated $\mod 17^{10}$.

ll17m:=Field(Sqrt(85));
Sl17m:=Sk17m*One(ll17m);  # \sqrt{2}

ll17mBasis0:=Basis(ll17m,[1,Sqrt(85)]); # Basis

#Function to reduce elements of $l_{17-}$_ modulo an integer
ll17mMod:=function(x,q)
  local c;
  c:=Coefficients(ll17mBasis0,x);
  Apply(c,t->t mod q);
  return LinearCombination(ll17mBasis0,c);
end;

# Find an approximation $\mod 17^{10}$ to $\sqrt{-5+2\sqrt{2}}$
Vl17mApprox:=Sqrt(85);
for j in [1..10] do
  Vl17mApprox:=ll17mMod(Vl17mApprox
    - (Vl17mApprox^2-(-5+2*Sl17m))/(2*Vl17mApprox),17^10);
od;

Vl17m:=1861007208251*Sqrt(85);   # \sqrt{-5+2\sqrt{2}}
Ul17m:=ll17mMod((1+Sl17m+Vl17m)/2,17^10); # Generating element, U
UPl17m:=ll17mMod(2/Ul17m,17^10);          # U', The conjugate of U

ll17mBasis:=Basis(ll17m,[One(ll17m),Ul17m]); #Basis over $k_{17-}$ 
# Alternative Basis over $k_{17-}$
llP17mBasis:=Basis(ll17m,[One(ll17m),UPl17m]); 
#Function to calculate coefficients
ll17mCoeff:=x->Coefficients(ll17mBasis,x);

Vz17mFcn:=x->x^2-(-5+2*Sl17m)*One(x);  #Function satisfied by V
Uz17mFcn:=x->x^2-(1+Sl17m)*x+2*One(x); #Function satisfied by U

#Check that S, V, U, U' are roots of their polynomials
IsZero(ll17mMod(SzFcn(Sl17m),17^10));
IsZero(ll17mMod(Vz17mFcn(Vl17m),17^10));
IsZero(ll17mMod(Uz17mFcn(Ul17m),17^10));
IsZero(ll17mMod(Uz17mFcn(UPl17m),17^10));

#Function to conjugate an element
ll17mConj:=x->x^ANFAutomorphism(ll17m,6);
#Function to calculate the adjoint of a matrix
ll17mAdj:=m->TransposedMat(List(m,r->List(r,x->ll17mConj(x))));

#Check that conjugates are as expected
IsZero(ll17mMod(ll17mConj(Ul17m) - UPl17m,17^10));
IsZero(ll17mMod(ll17mConj(UPl17m) - Ul17m,17^10));
ll17mConj(Vl17m) = -Vl17m;
ll17mConj(Sl17m) = Sl17m;

#Function to reduce matrices over~$\ell_{17m}$ modulo an integer
ll17mModM:=function(mtx,q)
  return List(mtx,row->List(row,x->ll17mMod(x,q)));
end;

#####
#
# The polynomial $W^3-3W+1$ splits in $\ell_{17-}$.  Indeed it splits
# in $k_{17-}=\QQ_{17}$.
#
# Consequently $\mm_{17-}=\ell_{17-}[W]/(W^3-3W+1)$ is the direct sum
# of 3~copies of $\ell_{17-}$.  In these three copies we have,
# respectively, $W=7 \mod 17$, $W=14 \mod 17$, and $W=13 \mod 17$. 

Wl17m:=Wl17p;
phiWl17m:=phiWl17p;
phi2Wl17m:=phi2Wl17p;

# Check that $W$ and its conjugates are roots of the right polynomial
# ($\mod 17^{10}$) 
IsZero(ll17mMod(WzFcn(Wl17m),17^10));
IsZero(ll17mMod(WzFcn(phiWl17m),17^10));
IsZero(ll17mMod(WzFcn(phi2Wl17m),17^10));

#Check that they are the right conjugates
IsZero(ll17mMod(phiOnW(phiWl17m)-phi2Wl17m,17^10));
IsZero(ll17mMod(phiOnW(phi2Wl17m)-Wl17m,17^10));

######
#
# The representation of~$\D$ as matrices over~$\ell_{17-}$.
#
# We find matrices for $S$, $U$, $U'$, $V$, $W, and~$\sigma$, all of
# which are approximations $\mod 17^{10}$. 

WDM17m:=[[Wl17m,0,0],[0,phiWl17m,0],[0,0,phi2Wl17m]]*One(ll17m);
SDM17m:=Sl17m*One(WDM17m);
UDM17m:=Ul17m*One(WDM17m);
UPDM17m:=ll17mModM(2/UDM17m,17^10);
VDM17m:=Vl17m*One(WDM17m);
sigmaDM17m:=
  ll17mModM([[0,1,0],[0,0,1],[Ul17m/Sl17m,0,0]]*One(ll17m),17^10);
sigmaDM17mInv:=ll17mModM(sigmaDM17m^-1,17^10);

#Polynomial of which~$\sigma$ is a root
sigmazl17mFcn:=sigma->sigma^3-(Ul17m/Sl17m)*One(sigma);

#Check that W, S, U, U', V, and $\sigma$  are roots of the right
#polynomials.
IsZero(ll17mModM(WzFcn(WDM17m),17^10));
IsZero(ll17mModM(SzFcn(SDM17m),17^10));
IsZero(ll17mModM(Uz17mFcn(UDM17m),17^10));
IsZero(ll17mModM(Uz17mFcn(UPDM17m),17^10));
IsZero(ll17mModM(Vz17mFcn(VDM17m),17^10));
IsZero(ll17mModM(sigmazl17mFcn(sigmaDM17m),17^10));

#Check that W, S, V, and U all commute
IsZero(ll17mModM(WDM17m*SDM17m-SDM17m*WDM17m,17^10));
IsZero(ll17mModM(WDM17m*VDM17m-VDM17m*WDM17m,17^10));
IsZero(ll17mModM(WDM17m*UDM17m-UDM17m*WDM17m,17^10));
IsZero(ll17mModM(SDM17m*VDM17m-VDM17m*SDM17m,17^10));
IsZero(ll17mModM(SDM17m*UDM17m-UDM17m*SDM17m,17^10));
IsZero(ll17mModM(VDM17m*UDM17m-UDM17m*VDM17m,17^10)); 

#Check that $\sigma$ commutes with S, V, and U
IsZero(ll17mModM(sigmaDM17m*SDM17m-SDM17m*sigmaDM17m,17^10));
IsZero(ll17mModM(sigmaDM17m*VDM17m-VDM17m*sigmaDM17m,17^10));
IsZero(ll17mModM(sigmaDM17m*UDM17m-UDM17m*sigmaDM17m,17^10));

#Check that~$\sigma$ conjugates~$W$ correctly
IsZero(ll17mModM(sigmaDM17m*WDM17m -
  phiOnW(WDM17m)*sigmaDM17m,17^10));
IsZero(ll17mModM(sigmaDM17m*phiOnW(WDM17m) - 
  phi2OnW(WDM17m)*sigmaDM17m,17^10));
IsZero(ll17mModM(sigmaDM17m*phi2OnW(WDM17m) - 
  WDM17m*sigmaDM17m,17^10));


#####
#
# In the matrix representation of~$\D$ over~$\ell_{17-}$, the
# matrix~$F$ so that
#
#   \iota(A) = F^{-1} A^* F
#
# This is an approximation $\mod 17^{10}$.

FDM17m:= ll17mModM(-Sl17m*One(WDM17m)+(1-Sl17m)*WDM17m+WDM17m^2,17^10);

# Check that FDM17m is self-adjoint
ll17mAdj(FDM17m) = FDM17m;
# Check that $\iota$  acts correctly on S, U, V, W, $\sigma$, and
# $sigma^-1$  
ll17mAdj(SDM17m)*FDM17m = FDM17m*SDM17m;
IsZero(ll17mModM(ll17mAdj(UDM17m)*FDM17m - FDM17m*UPDM17m,17^10));
IsZero(ll17mModM(ll17mAdj(VDM17m)*FDM17m - -FDM17m*VDM17m,17^10));
IsZero(ll17mModM(ll17mAdj(WDM17m)*FDM17m - FDM17m*WDM17m,17^10));
IsZero(ll17mModM(ll17mAdj(sigmaDM17m)*FDM17m -  FDM17m*
     (-Sl17m*One(WDM17m)+(-3+Sl17m)*WDM17m+(1-Sl17m)*WDM17m^2)
      *sigmaDM17mInv
  ,17^10));
IsZero(ll17mModM(ll17mAdj(sigmaDM17mInv)*FDM17m - FDM17m*
     ((1-2*Sl17m)*One(WDM17m)+(1+2*Sl17m)*WDM17m+(1+Sl17m)*WDM17m^2)
     *sigmaDM17m
  ,17^10));

#Check that FDM17m and its inverse are integral
IsZero(ll17mModM(17*FDM17m,17));
IsZero(ll17mModM(17*FDM17m^-1,17));
IsZero(ll17mMod(17*Determinant(FDM17m),17));
not IsZero(ll17mMod(Determinant(FDM17m),17));

# The preceding checks show that the standard $\ell_{17-}$-lattice 
# $L_0=\O_{\ell_{17-}}^3$, is self-dual with respect to the form given
# by~$F$.  Note that $\O_{\ell_{17-}=\ZZ_{17}[U]/(U^2-(1+\sqrt{2})U+2)$
 
# Consequently matrices which preserve that lattice, which is to say
# matrices in $GL(3,\ZZ_{17}[U])$, fix a vertex of Type~1 in the
# $17-$-adic tree corresponding to the $17-$-adic version of 
# $PU(\D,\iota)$.

# 36-element basis of $\D$ over $\QQ$
basisDM17m:=ListX([0..1],[0..1],[0..2],[-1..1],
  function(i,j,k,l)
    return Sl17m^i*Ul17m^j*WDM17m^k*sigmaDM17m^l;
  end
);;

# For a $3\times 3$ matrix with coefficients in
# $\O_{\ell_{17-}}=\ZZ_{17}[U]$, for each of the 9~entries
# this function calculate the four coefficients, each modulo~$q$, and
# puts them together as a 18~element vector.

CondCoeff17m:=function(mtx,q)
  return Concatenation(ListX([1..3],[1..3],
    function(j,k)
      local c;
      c:=ll17mCoeff(mtx[j][k]);
      Apply(c,x->x mod q);
      return c;
    end)
  );
end;

# The element
#
#   \sum c_{ijkl} \sqrt{2}^i U^j W^k \sigma^l 
#
# in~$\D$, when reduced $\mod 17-$,  will have (approximate) 17-adic
# matrix representation 
#
#    Sum([1..36],ix->c[ix]*basis[ix])
#  
# and this $17$-adic matrix representation will be $17$-adically
# integral if and only if the vector:
#
#   CondMtxDM17mType1 * c
#
# consists of 17-adic integers.
CondMtxDM17mType1:=
  TransposedMat(List(basisDM17m,mtx->CondCoeff17m(mtx,17^2)));;

# Here we check that putting together the Type~1 conditions at~$17\pm$
# we obtain the simplest possible condition --- each of the $c_{ijkl}$
# must be a $17$-adic integer.
CondMtxDM17pmType1:=
  Concatenation(CondMtxDM17pType1,CondMtxDM17mType1);
ForAll(CondMtxDM17pmType1,row->ForAll(row,x->IsInt(x)));
Determinant(CondMtxDM17pmType1) mod 17 <> 0;

#CondMtxDM17mType1:=[
#[ 0, 1, 0, 0, 75, 0, 0, 134, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 196, 
#    0, 0, 250, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 75, 0, 0, 134, 0, 0, 0, 0, 0, 0, 0, 
#    0, 0, 0, 0, 45, 0, 0, 196, 0, 0, 250, 0 ], 
#[ 0, 0, 1, 0, 0, 75, 0, 0, 134, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 
#    196, 0, 0, 250, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 75, 0, 0, 134, 0, 0, 0, 0, 0, 0, 
#    0, 0, 0, 0, 0, 45, 0, 0, 196, 0, 0, 250 ], 
#[ 168, 0, 0, 173, 0, 0, 259, 0, 0, 45, 0, 0, 196, 0, 0, 250, 0, 0, 46, 0, 0, 
#    271, 0, 0, 95, 0, 0, 2, 0, 0, 150, 0, 0, 268, 0, 0 ], 
#[ 122, 0, 0, 191, 0, 0, 164, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 288, 0, 0, 214, 
#    0, 0, 155, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 1, 0, 0, 82, 0, 0, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 222, 0, 0, 
#    286, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 82, 0, 0, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
#    0, 0, 45, 0, 0, 222, 0, 0, 286, 0, 0 ], 
#[ 0, 1, 0, 0, 82, 0, 0, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 222, 0, 
#    0, 286, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 82, 0, 0, 77, 0, 0, 0, 0, 0, 0, 0, 0, 
#    0, 0, 0, 45, 0, 0, 222, 0, 0, 286, 0 ], 
#[ 0, 0, 1, 0, 0, 82, 0, 0, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 222, 
#    0, 0, 286, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 82, 0, 0, 77, 0, 0, 0, 0, 0, 0, 0, 
#    0, 0, 0, 0, 45, 0, 0, 222, 0, 0, 286 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 244, 0, 0, 129, 0, 0, 266, 0, 0, 0, 0, 0, 
#    0, 0, 0, 0, 0, 0, 287, 0, 0, 25, 0, 0, 121 ], 
#[ 0, 0, 167, 0, 0, 80, 0, 0, 156, 0, 0, 168, 0, 0, 212, 0, 0, 240, 0, 0, 1, 0, 
#    0, 132, 0, 0, 84, 0, 0, 46, 0, 0, 3, 0, 0, 107 ], 
#[ 1, 0, 0, 132, 0, 0, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 160, 0, 
#    0, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 132, 0, 0, 84, 0, 0, 0, 0, 0, 0, 0, 0, 
#    0, 0, 0, 45, 0, 0, 160, 0, 0, 23, 0, 0 ], 
#[ 0, 1, 0, 0, 132, 0, 0, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 160, 
#    0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 132, 0, 0, 84, 0, 0, 0, 0, 0, 0, 0, 
#    0, 0, 0, 0, 45, 0, 0, 160, 0, 0, 23, 0 ]
#];

######
#
# Now we conjugate these matrices to change to a vertex of second
# type.
#
#####

ConjMtx17m:=ll17mModM(
  17*[[1,0,0,],[0,1/Vl17m,4/Vl17m],[0,1,1]]*One(ll17m)
,17^11)/17;
ConjMtx17mInv:=ll17mModM(17*ConjMtx17m^-1,17^11)/17;

#Check that the inverse of ConjMtx17m is correct:
IsZero(ll17mModM(ConjMtx17m*ConjMtx17mInv-One(ConjMtx17m),17^9));

######
#
# The ALTERNATIVE representation of~$\D$ as matrices
# over~$\ell_{17-}$. 
#
# We find the ALTERNATIVE matrices for $S$, $U$, $U'$, $V$, $W,
# and~$\sigma$, all of which are approximations $\mod 17^{10}$. 

WDM17mA:=ll17mModM(17*ConjMtx17m*WDM17m*ConjMtx17mInv,17^11)/17;
SDM17mA:=ll17mModM(ConjMtx17m*SDM17m*ConjMtx17mInv,17^10);
UDM17mA:=ll17mModM(ConjMtx17m*UDM17m*ConjMtx17mInv,17^10);
UPDM17mA:=ll17mModM(2/UDM17mA,17^10);
VDM17mA:=ll17mModM(ConjMtx17m*VDM17m*ConjMtx17mInv,17^10);
sigmaDM17mA:=ll17mModM(17*ConjMtx17m*sigmaDM17m*ConjMtx17mInv,17^11)/17;
sigmaDM17mAInv:=ll17mModM(17*sigmaDM17mA^2/(Ul17m/Sl17m),17^11)/17;

#Check that sigmaDM17mA and sigmaDM17mAInv are inverses
IsZero(ll17mModM(sigmaDM17mA*sigmaDM17mAInv-One(sigmaDM17mA),17^9));

#Check that the ALTERNATIVE W, S, U, U', V, and $\sigma$  are roots of
#the right polynomials.
IsZero(ll17mModM(WzFcn(WDM17mA),17^9));
IsZero(ll17mModM(SzFcn(SDM17mA),17^9));
IsZero(ll17mModM(Uz17mFcn(UDM17mA),17^10));
IsZero(ll17mModM(Uz17mFcn(UPDM17mA),17^10));
IsZero(ll17mModM(Vz17mFcn(VDM17mA),17^10));
IsZero(ll17mModM(sigmazl17mFcn(sigmaDM17mA),17^9));

#Check that the ALTERNATIVE W, S, V, and U all commute
IsZero(ll17mModM(WDM17mA*SDM17mA-SDM17mA*WDM17mA,17^9));
IsZero(ll17mModM(WDM17mA*VDM17mA-VDM17mA*WDM17mA,17^10));
IsZero(ll17mModM(WDM17mA*UDM17mA-UDM17mA*WDM17mA,17^9));
IsZero(ll17mModM(SDM17mA*VDM17mA-VDM17mA*SDM17mA,17^10));
IsZero(ll17mModM(SDM17mA*UDM17mA-UDM17mA*SDM17mA,17^10));
IsZero(ll17mModM(VDM17mA*UDM17mA-UDM17mA*VDM17mA,17^10)); 

#Check that the ALTERNATIVE $\sigma$ commutes with the ALTERNATIVE S,
#V, and U 
IsZero(ll17mModM(sigmaDM17mA*SDM17mA-SDM17mA*sigmaDM17mA,17^9));
IsZero(ll17mModM(sigmaDM17mA*VDM17mA-VDM17mA*sigmaDM17mA,17^10));
IsZero(ll17mModM(sigmaDM17mA*UDM17mA-UDM17mA*sigmaDM17mA,17^9));

#Check that the ALTERNATIVE~$\sigma$ conjugates the ALTERNATIVE~$W$
#correctly 
IsZero(ll17mModM(sigmaDM17mA*WDM17mA -
  phiOnW(WDM17mA)*sigmaDM17mA,17^9));
IsZero(ll17mModM(sigmaDM17mA*phiOnW(WDM17mA) - 
  phi2OnW(WDM17mA)*sigmaDM17mA,17^9));
IsZero(ll17mModM(sigmaDM17mA*phi2OnW(WDM17mA) - 
  WDM17mA*sigmaDM17mA,17^9));

#####
#
# In the ALTERNATIVE matrix representation of~$\D$ over~$\ell_{17-}$,
# the matrix~$F$ so that
#
#   \iota(A) = F^{-1} A^* F
#
# This is an approximation $\mod 17^{10}$.

FDM17mA:=ll17mModM(
  17*ll17mAdj(ConjMtx17mInv)*FDM17m*ConjMtx17mInv
,17^11)/17;
FDM17mAInv:=ll17mModM(17*FDM17mA^-1,17^11)/17;

#Check that FDM17mA and FDM17mAInv are inverses
IsZero(ll17mModM(FDM17mA*FDM17mAInv-One(FDM17mA),17^10));

# Check that FDM17mA is self-adjoint
IsZero(ll17mModM(ll17mAdj(FDM17mA) - FDM17mA,17^10));
# Check that $\iota$ acts correctly on the ALTERNATIVE S, U, V, W,
# $\sigma$, and $sigma^-1$ 
IsZero(ll17mModM(ll17mAdj(SDM17mA)*FDM17mA - FDM17mA*SDM17mA,17^10));
IsZero(ll17mModM(ll17mAdj(UDM17mA)*FDM17mA - FDM17mA*UPDM17mA,17^10));
IsZero(ll17mModM(ll17mAdj(VDM17mA)*FDM17mA - -FDM17mA*VDM17mA,17^10));
IsZero(ll17mModM(ll17mAdj(WDM17mA)*FDM17mA - FDM17mA*WDM17mA,17^9));
IsZero(ll17mModM(ll17mAdj(sigmaDM17mA)*FDM17mA -  FDM17mA*
     (-Sl17m*One(WDM17mA)+(-3+Sl17m)*WDM17mA+(1-Sl17m)*WDM17mA^2)
      *sigmaDM17mAInv
  ,17^9));
IsZero(ll17mModM(ll17mAdj(sigmaDM17mAInv)*FDM17mA - FDM17mA*
     ((1-2*Sl17m)*One(WDM17mA)+(1+2*Sl17m)*WDM17mA+(1+Sl17m)*WDM17mA^2)
     *sigmaDM17mA
  ,17^9));

# Check the degree of integrality of FDM17mA, the matrix of the
# new sesquilinear form, and of its inverse.  Note that _Vl17m_
# $=\sqrt{-5+2\sqrt{2}}$ is a	uniformizer for $\ell_{17-}$.

#Check that FDM17mA is integral $\mod 17-$.
IsZero(ll17mModM(17*FDM17mA,17));
#Check that Vl17m*FDM17mA^-1 is integral $\mod 17-$
IsZero(ll17mModM(17*Vl17m*FDM17mA^-1,17));
#Check that Determinant(FDM17mA) has the same valuation as~$17$
IsIntegralCyclotomic(Determinant(FDM17mA)/17);
not IsIntegralCyclotomic(Determinant(FDM17mA)/(17*Vl17m));

# Since FDM17mA is integral, $L_0 \subseteq L_0^*$
# Since FDM17mA^-1*Vl17m is integral, $_Vl17m_ L_0^* \subseteq L_0$.
# Since Determinant(FDM17mA) has the same $17-$-adic valuation
# as~$17$, $[L_0^*:L_0] = 289$ 

# The preceding checks show that the standard $\ell_{17-}$ lattice
# $L_0=\O_{\ell_{17-}}^3$ and its dual lattice relative to the
# form~$F$ are neighbors in the $\tilde A_2$~building of
# $PGL_3(\ell_{17-})$.  Note that
# $\O_{\ell_{17-}=\ZZ_{17}[U]/(U^2-(1+\sqrt{2})U+2)$.
 
# Consequently matrices which preserve that lattice, which is to say
# matrices in $GL(3,\ZZ_{17}[U]$, fix a vertex of Type~2 in the
# $17-$-adic tree corresponding to the $17-$-adic reduction of
# $PU(\D,\iota)$.

# 36-element basis of $\D$ over $\QQ$
basisDM17mA:=ListX([0..1],[0..1],[0..2],
    [sigmaDM17mAInv,sigmaDM17mA^0,sigmaDM17mA^1],
  function(i,j,k,sigmaDM17mATol)
    return Sl17m^i*Ul17m^j*WDM17mA^k*sigmaDM17mATol;
  end
);;

# For a $3\times 3$ matrix with coefficients in
# $\O_{\ell_{17-}}=\ZZ_{17}[U]$, for each of the 9~entries this
# function calculate the four coefficients, each modulo~$q$, and puts
# them together as a 18~element vector.

CondCoeff17m:=function(mtx,q)
  return Concatenation(ListX([1..3],[1..3],
    function(j,k)
      local c;
      c:=ll17mCoeff(mtx[j][k]);
      Apply(c,x->x mod q);
      return c;
    end)
  );
end;

# The element
#
#   \sum c_{ijkl} \sqrt{2}^i U^j W^k \sigma^l 
#
# in~$\D$, when reduced $\mod 17-$,  will have (approximate) 17-adic
# matrix representation 
#
#    Sum([1..36],ix->c[ix]*basis[ix])
#  
# and this $17$-adic matrix representation will be $17$-adically
# integral if and only if the vector:
#
#   CondMtxDM17mType2 * c
#
# consists of 17-adic integers.
CondMtxDM17mType2:=TransposedMat(
  List(basisDM17mA,mtx->CondCoeff17m(17*mtx,17*17^2)/17));;
  
#CondMtxDM17mType2:=[
#[ 0, 1, 0, 0, 75, 0, 0, 134, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 196, 
#    0, 0, 250, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 75, 0, 0, 134, 0, 0, 0, 0, 0, 0, 0, 
#    0, 0, 0, 0, 45, 0, 0, 196, 0, 0, 250, 0 ], 
#[ 55, 0, 208, 79, 0, 283, 145, 0, 128, 177, 0, 194, 270, 0, 100, 20, 0, 275, 
#    163, 0, 112, 87, 0, 19, 167, 0, 269, 162, 0, 60, 12, 0, 165, 33, 0, 237 ], 
#[ 56, 0, 192, 154, 0, 239, 279, 0, 7, 30, 0, 81, 227, 0, 6, 263, 0, 161, 208, 
#    0, 259, 283, 0, 62, 128, 0, 26, 194, 0, 177, 100, 0, 270, 275, 0, 20 ], 
#[ 233, 0, 194, 135, 0, 100, 10, 0, 275, 274, 0, 0, 31, 0, 0, 13, 0, 0, 81, 0, 
#    60, 6, 0, 165, 161, 0, 237, 192, 0, 0, 239, 0, 0, 7, 0, 0 ], 
#[ 152, 0, 0, 129, 0, 0, 138, 0, 0, 0, 0, 194, 0, 0, 100, 0, 0, 275, 193, 0, 0, 
#    25, 0, 0, 141, 0, 0, 0, 0, 60, 0, 0, 165, 0, 0, 237 ], 
#[ 4370/17, 0, 387/17, 269/17, 0, 3399/17, 672/17, 0, 718/17, 91/17, 0, 
#    2832/17, 1104/17, 0, 4193/17, 1516/17, 0, 2931/17, 708/17, 0, 364/17, 
#    545/17, 0, 74/17, 1051/17, 0, 231/17, 2650/17, 0, 569/17, 4885/17, 0, 
#    3436/17, 1172/17, 0, 3290/17 ], 
#[ 2411/17, 0, 3497/17, 4361/17, 0, 360/17, 4155/17, 0, 991/17, 543/17, 0, 
#    1143/17, 4644/17, 0, 2041/17, 4241/17, 0, 64/17, 3588/17, 0, 2172/17, 
#    14/17, 0, 3195/17, 4327/17, 0, 3268/17, 4205/17, 0, 4617/17, 4368/17, 0, 
#    3989/17, 3862/17, 0, 568/17 ], 
#[ 95, 1, 193, 113, 245, 220, 177, 279, 122, 0, 0, 0, 0, 0, 0, 0, 0, 0, 229, 
#    45, 15, 172, 43, 74, 162, 128, 288, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 1, 193, 113, 245, 220, 177, 279, 122, 0, 0, 
#    0, 0, 0, 0, 0, 0, 0, 229, 45, 15, 172, 43, 74, 162, 128, 288 ], 
#[ 2017/17, 0, 181/17, 653/17, 4699/17, 1548/17, 3831/17, 3623/17, 4689/17, 
#    2123/17, 0, 3245/17, 3374/17, 3085/17, 4545/17, 308/17, 3582/17, 2770/17, 
#    3776/17, 0, 4677/17, 3953/17, 4242/17, 1456/17, 3908/17, 2062/17, 2925/17, 
#    1032/17, 0, 2392/17, 4151/17, 972/17, 1647/17, 277/17, 1084/17, 1247/17 ], 
#[ 1395/17, 0, 834/17, 3226/17, 914/17, 184/17, 4759/17, 3122/17, 3528/17, 
#    2896/17, 0, 4732/17, 4260/17, 214/17, 3365/17, 1082/17, 1290/17, 224/17, 
#    4397/17, 0, 3717/17, 381/17, 4427/17, 1633/17, 2318/17, 4371/17, 1833/17, 
#    1137/17, 0, 236/17, 960/17, 671/17, 3457/17, 1005/17, 2851/17, 1988/17 ], 
#[ 1, 0, 0, 82, 0, 0, 77, 0, 0, 0, 0, 244, 0, 0, 129, 0, 0, 266, 45, 0, 0, 222, 
#    0, 0, 286, 0, 0, 0, 0, 287, 0, 0, 25, 0, 0, 121 ], 
#[ 0, 0, 167, 0, 0, 80, 0, 0, 156, 1, 0, 168, 82, 0, 212, 77, 0, 240, 0, 0, 1, 
#    0, 0, 132, 0, 0, 84, 45, 0, 46, 222, 0, 3, 286, 0, 107 ], 
#[ 208, 0, 81, 1, 4, 284, 132, 278, 168, 194, 0, 95, 176, 126, 276, 112, 87, 
#    90, 112, 0, 177, 45, 180, 64, 160, 83, 46, 60, 0, 229, 117, 179, 282, 127, 
#    158, 4 ], 
#[ 192, 0, 97, 201, 226, 151, 233, 101, 244, 81, 0, 208, 288, 285, 5, 157, 11, 
#    121, 259, 0, 30, 86, 55, 148, 81, 210, 287, 177, 0, 112, 244, 109, 225, 
#    129, 206, 243 ], 
#[ 194, 1, 96, 176, 258, 69, 112, 171, 167, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60, 45, 
#    274, 117, 50, 215, 127, 181, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 194, 1, 96, 176, 258, 69, 112, 171, 167, 0, 0, 0, 
#    0, 0, 0, 0, 0, 0, 60, 45, 274, 117, 50, 215, 127, 181, 1 ]
#];

#17*CondMtxDM17mType2:=[
#[ 0, 17, 0, 0, 1275, 0, 0, 2278, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 765, 0, 0, 
#    3332, 0, 0, 4250, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 0, 0, 1275, 0, 0, 2278, 0, 0, 0, 0, 0, 0, 
#    0, 0, 0, 0, 0, 765, 0, 0, 3332, 0, 0, 4250, 0 ], 
#[ 935, 0, 3536, 1343, 0, 4811, 2465, 0, 2176, 3009, 0, 3298, 4590, 0, 1700, 
#    340, 0, 4675, 2771, 0, 1904, 1479, 0, 323, 2839, 0, 4573, 2754, 0, 1020, 
#    204, 0, 2805, 561, 0, 4029 ], 
#[ 952, 0, 3264, 2618, 0, 4063, 4743, 0, 119, 510, 0, 1377, 3859, 0, 102, 4471, 
#    0, 2737, 3536, 0, 4403, 4811, 0, 1054, 2176, 0, 442, 3298, 0, 3009, 1700, 
#    0, 4590, 4675, 0, 340 ], 
#[ 3961, 0, 3298, 2295, 0, 1700, 170, 0, 4675, 4658, 0, 0, 527, 0, 0, 221, 0, 
#    0, 1377, 0, 1020, 102, 0, 2805, 2737, 0, 4029, 3264, 0, 0, 4063, 0, 0, 
#    119, 0, 0 ], 
#[ 2584, 0, 0, 2193, 0, 0, 2346, 0, 0, 0, 0, 3298, 0, 0, 1700, 0, 0, 4675, 
#    3281, 0, 0, 425, 0, 0, 2397, 0, 0, 0, 0, 1020, 0, 0, 2805, 0, 0, 4029 ], 
#[ 4370, 0, 387, 269, 0, 3399, 672, 0, 718, 91, 0, 2832, 1104, 0, 4193, 1516, 
#    0, 2931, 708, 0, 364, 545, 0, 74, 1051, 0, 231, 2650, 0, 569, 4885, 0, 
#    3436, 1172, 0, 3290 ], 
#[ 2411, 0, 3497, 4361, 0, 360, 4155, 0, 991, 543, 0, 1143, 4644, 0, 2041, 
#    4241, 0, 64, 3588, 0, 2172, 14, 0, 3195, 4327, 0, 3268, 4205, 0, 4617, 
#    4368, 0, 3989, 3862, 0, 568 ], 
#[ 1615, 17, 3281, 1921, 4165, 3740, 3009, 4743, 2074, 0, 0, 0, 0, 0, 0, 0, 0, 
#    0, 3893, 765, 255, 2924, 731, 1258, 2754, 2176, 4896, 0, 0, 0, 0, 0, 0, 0, 
#    0, 0 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1615, 17, 3281, 1921, 4165, 3740, 3009, 4743, 
#    2074, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3893, 765, 255, 2924, 731, 1258, 2754, 
#    2176, 4896 ], 
#[ 2017, 0, 181, 653, 4699, 1548, 3831, 3623, 4689, 2123, 0, 3245, 3374, 3085, 
#    4545, 308, 3582, 2770, 3776, 0, 4677, 3953, 4242, 1456, 3908, 2062, 2925, 
#    1032, 0, 2392, 4151, 972, 1647, 277, 1084, 1247 ], 
#[ 1395, 0, 834, 3226, 914, 184, 4759, 3122, 3528, 2896, 0, 4732, 4260, 214, 
#    3365, 1082, 1290, 224, 4397, 0, 3717, 381, 4427, 1633, 2318, 4371, 1833, 
#    1137, 0, 236, 960, 671, 3457, 1005, 2851, 1988 ], 
#[ 17, 0, 0, 1394, 0, 0, 1309, 0, 0, 0, 0, 4148, 0, 0, 2193, 0, 0, 4522, 765, 
#    0, 0, 3774, 0, 0, 4862, 0, 0, 0, 0, 4879, 0, 0, 425, 0, 0, 2057 ], 
#[ 0, 0, 2839, 0, 0, 1360, 0, 0, 2652, 17, 0, 2856, 1394, 0, 3604, 1309, 0, 
#    4080, 0, 0, 17, 0, 0, 2244, 0, 0, 1428, 765, 0, 782, 3774, 0, 51, 4862, 0, 
#    1819 ], [ 3536, 0, 1377, 17, 68, 4828, 2244, 4726, 2856, 3298, 0, 1615, 
#    2992, 2142, 4692, 1904, 1479, 1530, 1904, 0, 3009, 765, 3060, 1088, 2720, 
#    1411, 782, 1020, 0, 3893, 1989, 3043, 4794, 2159, 2686, 68 ], 
#[ 3264, 0, 1649, 3417, 3842, 2567, 3961, 1717, 4148, 1377, 0, 3536, 4896, 
#    4845, 85, 2669, 187, 2057, 4403, 0, 510, 1462, 935, 2516, 1377, 3570, 
#    4879, 3009, 0, 1904, 4148, 1853, 3825, 2193, 3502, 4131 ], 
#[ 3298, 17, 1632, 2992, 4386, 1173, 1904, 2907, 2839, 0, 0, 0, 0, 0, 0, 0, 0, 
#    0, 1020, 765, 4658, 1989, 850, 3655, 2159, 3077, 17, 0, 0, 0, 0, 0, 0, 0, 
#    0, 0 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 3298, 17, 1632, 2992, 4386, 1173, 1904, 2907, 
#    2839, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1020, 765, 4658, 1989, 850, 3655, 2159, 
#    3077, 17 ]
#];
