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

#####
#
# $\ell=\QQ[\sqrt{-1}]$
#
ll:=Field(Sqrt(-1));
	
#####
#
# $m=\ell[Z]/(Z^3-3*Z^2-2)
#
xl:=Indeterminate(ll,"xl");
f:=xl^3-3*xl^2-2*One(ll); # Discriminant = -324 = -18^2
mm:=FieldExtension(ll,f);
Zm:=RootOfDefiningPolynomial(mm);

fFcn:=x->x^3-3*x^2-2*One(x);
fFcn(Zm) = Zero(mm);

#####
#
# $\phi:m\to m$
#

sum:=3-Zm; # Sum of the other two roots
diff:=Sqrt(Discriminant(f))/(Zm^2-sum*Zm+2/Zm);
  # Difference of the other two roots
phiZm:=(sum-diff)/2;
phi2Zm:=(sum+diff)/2;

phi:=x->
  (3/2+1/2*Sqrt(-1))*One(x)
        +(-1/2-2*Sqrt(-1))*x
        +(1/2*Sqrt(-1))*x^2;

phi2:=x->
  (3/2-1/2*Sqrt(-1))*One(x)
        +(-1/2+2*Sqrt(-1))*x
        +(-1/2*Sqrt(-1))*x^2;

phiZm^3-3*phiZm^2-2 = Zero(mm);    
phi2Zm^3-3*phi2Zm^2-2 = Zero(mm);    

phiZm =  (3/2+1/2*Sqrt(-1))*One(mm)
        +(-1/2-2*Sqrt(-1))*Zm
        +(1/2*Sqrt(-1))*Zm^2;
phi2Zm =  (3/2-1/2*Sqrt(-1))*One(mm)
        +(-1/2+2*Sqrt(-1))*Zm
        +(-1/2*Sqrt(-1))*Zm^2;

phi2Zm =  (3/2+1/2*Sqrt(-1))*One(mm)
        +(-1/2-2*Sqrt(-1))*phiZm
        +(1/2*Sqrt(-1))*phiZm^2;
Zm =     (3/2+1/2*Sqrt(-1))*One(mm)
        +(-1/2-2*Sqrt(-1))*phi2Zm
        +(1/2*Sqrt(-1))*phi2Zm^2;

phiZm =   (3/2-1/2*Sqrt(-1))*One(mm)
        +(-1/2+2*Sqrt(-1))*phi2Zm
        +(-1/2*Sqrt(-1))*phi2Zm^2;
Zm    =  (3/2-1/2*Sqrt(-1))*One(mm)
        +(-1/2+2*Sqrt(-1))*phiZm
        +(-1/2*Sqrt(-1))*phiZm^2;

#####
#
# Conjugation in~$m$ (taking $\bar Z=Z$)
#
mConj:=function(x)
  local c;
  c:=ExtRepOfObj(x);
  return ComplexConjugate(c[1])*One(mm)
        +ComplexConjugate(c[2])*Zm
        +ComplexConjugate(c[3])*Zm^2;
end;

mConj(Zm) = Zm;
mConj(phiZm) = phi2Zm;
mConj(phi2Zm) = phiZm;

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

I3:=IdentityMat(3); # 3 by 3 identity matrix
Z3:=Zero(I3); # 3 by 3 zero matrix

# Function to conjugate a matrix over~$m$
mConjM:=mtx->
  List(mtx,row->
    List(row,x->mConj(x))
  );

# Function to calculate the adjoint of a matrix over~$m$
mAdjM:=mtx->TransposedMat(mConjM(mtx));

# Function to conjugate a matrix over $\ell=\QQ[\sqrt{-1}]
lConjM:=mtx->
  List(mtx,row->
    List(row,x->ComplexConjugate(x))
  );

# Function to calculate the adjoint of a matrix over~$\ell$
lAdjM:=mtx->TransposedMat(lConjM(mtx));

# Real and imaginary parts of an element of~$\ell$
lRe:=x->(x+ComplexConjugate(x))/2;
lIm:=x->(x-ComplexConjugate(x))/(2*Sqrt(-1));

# Real and imaginary parts of a matrix over~$\ell$
lReM:=mtx->List(mtx,row->List(row,x->lRe(x)));
lImM:=mtx->List(mtx,row->List(row,x->lIm(x)));

# Element of~$l$ modulo q
lMod:=function(x,q)
  return (lRe(x) mod q)*One(ll) + (lIm(x) mod q)*Sqrt(-1);
end;

# Matrix over~$l$ modulo q
lModM:=function(mtx,q)
  return List(mtx,row->List(row,x->lMod(x,q)));
end;

# Real and imaginary parts of an element of~$m$
mRe:=x->(x+mConj(x))/2;
mIm:=x->(x-mConj(x))/(2*Sqrt(-1));

# Element of~$m$ modulo q
mMod:=function(x,q)
  return (mRe(x) mod q)*One(ll) + (mIm(x) mod q)*Sqrt(-1);
end;

# Matrix over~$m$ modulo q
mModM:=function(mtx,q)
  return List(mtx,row->List(row,x->mMod(x,q)));
end;

# Check if an element of~$m$ actually belongs to~$\ell$
Inl:=function(x)
  local c;
  c:=ExtRepOfObj(x);
  return c[2] = 0 and c[3] = 0;
end;

# Check if a matrix over~$m$ is actually a matrix over~$\ell$
InlM:=mtx->ForAll(mtx,row->ForAll(row,x->Inl(x)));

# Convert an element which belongs of~$m$ which actually belongs
# to~$\ell$ to the corresponding element of~$\ell$
mTol:=x->ExtRepOfObj(x)[1];

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

#####
#
# We work out first the correct integrality conditions at the 2-adic
# place.
#
# $Z$, $\phi(Z)$, and $\phi^2(Z)$ in the 2-adic approximation

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

# Approximation $\mod 2^10$ to the 2-adic solution of $Z^3-3Z^2-2=0$
Z2:=1301;
fFcn(Z2) mod 2^10 = 0;

#####
#
# The two other roots:
#
phiZ2:=  (3/2+1/2*Sqrt(-1))*One(ll)
        +(-1/2-2*Sqrt(-1))*Z2
        +(1/2*Sqrt(-1))*Z2^2;
phi2Z2:=  (3/2-1/2*Sqrt(-1))*One(ll)
        +(-1/2+2*Sqrt(-1))*Z2
        +(-1/2*Sqrt(-1))*Z2^2;

lMod((phiZ2^3-3*phiZ2^2-2),2^10) = 0;
lMod((phi2Z2^3-3*phi2Z2^2-2),2^10) = 0;

lMod(phi2Z2 -
  ((3/2+1/2*Sqrt(-1))*One(ll)
   +(-1/2-2*Sqrt(-1))*phiZ2
   +(1/2*Sqrt(-1))*phiZ2^2)
,2^10) = 0;
lMod(Z2 -
  ((3/2+1/2*Sqrt(-1))*One(ll)
   +(-1/2-2*Sqrt(-1))*phi2Z2
   +(1/2*Sqrt(-1))*phi2Z2^2)
,2^10) = 0;

lMod(phiZ2 -
  ((3/2-1/2*Sqrt(-1))*One(ll)
   +(-1/2+2*Sqrt(-1))*phi2Z2
   +(-1/2*Sqrt(-1))*phi2Z2^2)
,2^10) = 0;
lMod(Z2 -
  ((3/2-1/2*Sqrt(-1))*One(ll)
   +(-1/2+2*Sqrt(-1))*phiZ2
   +(-1/2*Sqrt(-1))*phiZ2^2)
,2^10) = 0;

ComplexConjugate(Z2) = Z2;
lMod(ComplexConjugate(phiZ2) - phi2Z2,2^10) = 0;
lMod(ComplexConjugate(phi2Z2) - phiZ2,2^10) = 0;

#####
#
# In the 2-adic matrix representation of~$\D$ (as matrices over
# $\ell\otimes\QQ_2) the matrices for $Z$, $\phi(Z)$, $\phi^2(Z)$,
# and $\sigma$
#
# These are approximations $\mod 2^10$
#
ZDM2:=[[Z2,0,0],[0,phiZ2,0],[0,0,phi2Z2]]*One(ll);
phiZDM2:=lModM(
  ((3/2+1/2*Sqrt(-1))*I3
   +(-1/2-2*Sqrt(-1))*ZDM2
   +(+1/2*Sqrt(-1))*ZDM2^2)
,2^10);
phi2ZDM2:=lModM(
  ((3/2-1/2*Sqrt(-1))*I3
   +(-1/2+2*Sqrt(-1))*ZDM2
   +(-1/2*Sqrt(-1))*ZDM2^2),
2^10);
sigmaDM2:=[[0,1,0],[0,0,1],[5,0,0]]*One(ll);

lModM(ZDM2^3-3*ZDM2^2-2*One(ZDM2),2^10) = Zero(ZDM2);
lModM(phiZDM2^3-3*phiZDM2^2-2*One(ZDM2),2^10) = Zero(ZDM2);
lModM(phi2ZDM2^3-3*phi2ZDM2^2-2*One(ZDM2),2^10) = Zero(ZDM2) ;

lModM(sigmaDM2^3 - 5*One(ZDM2),2^10) = Zero(sigmaDM2);
lModM(sigmaDM2*ZDM2 - phiZDM2*sigmaDM2,2^10) = Zero(sigmaDM2);
lModM(sigmaDM2*phiZDM2 - phi2ZDM2*sigmaDM2,2^10) = Zero(sigmaDM2);
lModM(sigmaDM2*phi2ZDM2 - ZDM2*sigmaDM2,2^10) = Zero(sigmaDM2);

#####
#
# In the 2-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$.
#
FDM2:=[[5,0,0],[0,0,1],[0,1,0]]*One(ll);

lModM(lAdjM(FDM2) - FDM2,2^10) = Zero(FDM2);
lModM(FDM2^-1*lAdjM(sigmaDM2)*FDM2 - sigmaDM2,2^10) = Zero(FDM2);
lModM(FDM2^-1*lAdjM(ZDM2)*FDM2 - ZDM2,2^10) = Zero(FDM2);
lModM(FDM2^-1*lAdjM(phiZDM2)*FDM2 - phi2ZDM2,2^10) = Zero(FDM2);
lModM(FDM2^-1*lAdjM(phi2ZDM2)*FDM2 - phiZDM2,2^10) = Zero(FDM2);
lModM(FDM2^-1*lAdjM(I3*Sqrt(-1)*One(ll))*FDM2 + I3*Sqrt(-1)*One(ll)
  ,2^10) = Zero(FDM2);

#Check that FDM2 and its inverse are integral $\mod 2$.
lModM(2*FDM2,2) = Zero(FDM2);
lModM(2*FDM2^-1,2) = Zero(FDM2);
Determinant(FDM2) mod 2 <> 0;

# The previous check shows that the standard $\QQ_2$ lattice
# $L_0=\ZZ_2^3$, is self-dual with respect to the form given by~$F$.

# Consequently unitary matrices which preserve that lattice, which is
# to say matrices in $PU \cap PGL(3,\ZZ_2)$, fix a vertex of Type~1 in
# the 2-adic tree corresponding to the 2-adic version of
# $PU(\D,\iota)$.

# 18-element basis of $\D$ over $\QQ$
basisDM2:=ListX([0..1],[0..2],[0..2],
  function(j,k,l)
    return Sqrt(-1)^j*ZDM2^k*sigmaDM2^l;
  end
);;

# For a $3\times 3$ matrix with coefficients in $\ZZ[i]$, this
# function calculate the real and imaginary parts of the
# 9~entries, each modulo~$4$, and puts them together as an 18~element
# vector.
CondCoeff2:=function(mtx)
  return Concatenation(
    ListX([1..3],[1..3],function(j,k)
      return lRe(mtx[j][k]) mod 2^2;
    end),
    ListX([1..3],[1..3],function(j,k)
      return lIm(mtx[j][k]) mod 2^2;
    end)
  );
end;

# The element
#
#   \sum d_{jkl} i^j Z^k \sigma^l
#
# in~$\D$ will have (approximate) 2-adic matrix representation
#
#    Sum([1..18],ix->d[ix]*basis[ix])
#  
# and this 2-adic matrix representation will be 2-adically integral if
# and only if the vector:
#
#   CondMtxDM2Type1 * d
#
# consists of 2-adic integers.
CondMtxDM2Type1:=TransposedMat(List(basisDM2,mtx->CondCoeff2(mtx)));

#CondMtxDM2Type1:=[
#[ 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 0, 0, 1, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2 ], 
#[ 1, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0 ], 
#[ 0, 1, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0 ], 
#[ 0, 1, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 2, 0 ], 
#[ 0, 0, 1, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 2 ], 
#[ 1, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 2, 0, 0 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0 ], 
#[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1 ], 
#[ 0, 0, 0, 0, 0, 3, 0, 0, 2, 0, 0, 1, 0, 0, 3, 0, 0, 0 ], 
#[ 0, 0, 0, 3, 0, 0, 2, 0, 0, 1, 0, 0, 3, 0, 0, 0, 0, 0 ], 
#[ 0, 0, 0, 0, 3, 0, 0, 2, 0, 0, 1, 0, 0, 3, 0, 0, 0, 0 ], 
#[ 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 1, 0, 0, 3, 0, 0, 0, 0 ], 
#[ 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 1, 0, 0, 3, 0, 0, 0 ], 
#[ 0, 0, 0, 1, 0, 0, 2, 0, 0, 1, 0, 0, 3, 0, 0, 0, 0, 0 ]
#];

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

ConjMtx2A:=[[1,0,0],[0,1,0],[0,0,(1+Sqrt(-1))/2]];
ConjMtx2Ainv:=ConjMtx2A^-1;

# $Z$, $\phi(Z)$, and $\phi^2(Z)$ in the ALTERNATIVE 2-adic matrix
# representation. These are approximations $\mod 2^10$

ZDM2A:=ConjMtx2A*ZDM2*ConjMtx2Ainv;
phiZDM2A:=ConjMtx2A*phiZDM2*ConjMtx2Ainv;
phi2ZDM2A:=ConjMtx2A*phi2ZDM2*ConjMtx2Ainv;

# $\sigma$ in the ALTERNATIVE 2-adic matrix represenation.
#
sigmaDM2A:=ConjMtx2A*sigmaDM2*ConjMtx2Ainv;

# In the ALTERNATIVE 2-adic matrix representation, the element~$F$ so
# that \iota(A) = F^{-1} A^* F # Thus, $\iota$-unitary elements will
# preserve the sesquilinear form defined by $F$.

FDM2A:=lAdjM(ConjMtx2Ainv)*FDM2*ConjMtx2Ainv;

# Repeat the various checks.  In each case, we only check
# modulo~$2^10$.  Our matrices are rational approximations to matrices
# with coefficients in $\QQ_5[i]$, and the approximations are
# supposed to be valid modulo~$2^10$ (or sometimes~$2^10$).

# Check that $Z^3-3*Z^2-2=0$ and similar:
lModM(ZDM2A^3-3*ZDM2A^2-2*One(ZDM2A),2^10) = Zero(ZDM2A);
lModM(phiZDM2A^3-3*phiZDM2A^2-2*One(ZDM2A),2^10) = Zero(ZDM2A);
lModM(phi2ZDM2A^3-3*phi2ZDM2A^2-2*One(ZDM2A),2^10) = Zero(ZDM2A) ;

# Check that $\sigma^3 = 5:
lModM(sigmaDM2A^3 - 5*One(ZDM2A),2^10) = Zero(sigmaDM2A);

# Check that $\sigma Z \sigma^-1 = \phi(Z)$, and similar:
lModM(sigmaDM2A*ZDM2A - phiZDM2A*sigmaDM2A,2^9) = Zero(sigmaDM2A);
lModM(sigmaDM2A*phiZDM2A - phi2ZDM2A*sigmaDM2A,2^9) = Zero(sigmaDM2A);
lModM(sigmaDM2A*phi2ZDM2A - ZDM2A*sigmaDM2A,2^9) = Zero(sigmaDM2A);

# Check that $F$ is self-adjoint:
lModM(lAdjM(FDM2A) - FDM2A,2^10) = Zero(FDM2);

# Check that $\iota$ fixes both $\sigma$ and $Z$ and changes the sign
# of $\sqrt{-1}:
lModM(FDM2A^-1*lAdjM(sigmaDM2A)*FDM2A - sigmaDM2A,2^10) = Zero(FDM2A);
lModM(FDM2A^-1*lAdjM(ZDM2A)*FDM2A - ZDM2A,2^10) = Zero(FDM2A);
lModM(FDM2A^-1*lAdjM(phiZDM2A)*FDM2A - phi2ZDM2A,2^10) = Zero(FDM2A);
lModM(FDM2A^-1*lAdjM(phi2ZDM2A)*FDM2A - phiZDM2A,2^10) = Zero(FDM2A);
lModM(FDM2A^-1*lAdjM(I3*Sqrt(-1)*One(ll))*FDM2A + I3*Sqrt(-1)*One(ll)
  ,2^10) = Zero(FDM2A);

# Check the degree of integrality of FDM2A, the matrix of the
# new sesquilinear form, and of its inverse.

#Check that FDM2A and is integral $\mod 2$.
lModM(2*FDM2A,2) = Zero(FDM2A);
#Check that (1+Sqrt(-1))*FDM2A^-1 is integral $\mod 2$
lModM(2*(1+Sqrt(-1))*FDM2A^-1,2) = Zero(FDM2A);
#Check that Determinant(FDM2A) has the valuation of $1+i$
lMod(Determinant(FDM2A),2) = 0;
lMod(Determinant(FDM2A)*(1+Sqrt(-1))/2,2) <> 0;

# Since FDM2A is integral, $L_0 \subseteq L_0^*$
# Since FDM2A^-1*(1+Sqrt(-1)) is integral,
#   $(1+i)L_0^* \subseteq L_0$.
# Since Determinant(FDM2A) = -10, $[L_0^*:L_0] = 4$

# The preceding checks show that the standard $\QQ_2$ lattice
# $L_0=\ZZ_2^3$ and its dual lattice relative to the form~$F$ are
# neighbors in the $\tilde A_2$~building of $PGL_3(\QQ_2)$.
 
# Consequently matrices which preserve that lattice, which is to say
# matrices in $GL(3,\ZZ_2)$, fix a vertex of Type~2 in the
# 2-adic tree corresponding to the 2-adic version of $PU(\D,\iota)$.

# 18-element basis of $\D$ over $\QQ$
basisDM2A:=ListX([0..1],[0..2],[0..2],
  function(j,k,l)
    return Sqrt(-1)^j*ZDM2A^k*sigmaDM2A^l;
  end
);;

# For a $3\times 3$ matrix with coefficients in $\ZZ[i]$, this
# function calculate the real and imaginary parts of the 9~entries,
# each modulo~$8$, and puts them together as an 18~element vector.
CondCoeff2A:=function(mtx)
  return Concatenation(
    ListX([1..3],[1..3],function(j,k)
      return (2*lRe(mtx[j][k]) mod 2^4)/2;
    end),
    ListX([1..3],[1..3],function(j,k)
      return (2*lIm(mtx[j][k]) mod 2^4)/2;
    end)
  );
end;

# The element
#
#   \sum d_{jkl} \sqrt{-1}^j Z^k \sigma^l
#
# in~$\D$ will have (approximate) 2-adic matrix representation
#
#    Sum([1..18],ix->d[ix]*basis[ix])
#  
# and this 2-adic matrix representation will be 2-adically integral if
# and only if the vector:
#
#   CondMtxDM2Type2 * d
#
# consists of 2-adic integers.
CondMtxDM2Type2:=TransposedMat(List(basisDM2A,mtx->CondCoeff2A(mtx)));

#CondMtxDM2Type2:=[
#[ 1, 0, 0, 5, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 
#[ 0, 1, 0, 0, 5, 0, 0, 1, 0, 0,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, 0, 5, 0, 0, 2, 0, 0, 10 ], 
#[ 0, 0, 0, 10, 0, 0, 12, 0, 0, 2, 0, 0, 14, 0, 0, 0, 0, 0 ]
#];
 
#####
#
# Now we work out the correct integrality condition at the 3-adic
# place.
#
# See file ~steger/donald/SU21/a1p5/D.txt for the definition of the
# division algebra $\D$ and for all the notation.
#
#####

######
#
# $Z$, $\phi(Z)$, and $\phi^2(Z)$ in the matrix representation
# of~$\D$ over~$m$
#
ZDM:=[[Zm,0,0],[0,phiZm,0],[0,0,phi2Zm]]*One(mm);
phiZDM:=  (3/2+1/2*Sqrt(-1))*I3
         +(-1/2-2*Sqrt(-1))*ZDM
         +(1/2*Sqrt(-1))*ZDM^2;
phi2ZDM:= (3/2-1/2*Sqrt(-1))*I3
         +(-1/2+2*Sqrt(-1))*ZDM
         +(-1/2*Sqrt(-1))*ZDM^2;

ZDM^3-3*ZDM^2-2*One(ZDM) = Zero(ZDM);
phiZDM^3-3*phiZDM^2-2*One(ZDM) = Zero(ZDM);
phi2ZDM^3-3*phi2ZDM^2-2*One(ZDM) = Zero(ZDM) ;

#####
#
# $\sigma$ in the matrix representation of~$\D$ over~$m$
#
sigmaDM:=[[0,1,0],[0,0,1],[5,0,0]]*One(ZDM);

sigmaDM^3 = 5*One(ZDM);
sigmaDM*ZDM = phiZDM*sigmaDM;
sigmaDM*phiZDM = phi2ZDM*sigmaDM;
sigmaDM*phi2ZDM = ZDM*sigmaDM;

#####
#
# In the matrix representation of~$\D$ over~$m$, the matrix~$F$ so
# that
#
#   \iota(A) = F^{-1} A^* F
#
FDM:= [[5,0,0],[0,0,1],[0,1,0]]*One(mm);

mAdjM(FDM) = FDM;
FDM^-1*mAdjM(sigmaDM)*FDM = sigmaDM;
FDM^-1*mAdjM(ZDM)*FDM = ZDM;
FDM^-1*mAdjM(phiZDM)*FDM = phi2ZDM;
FDM^-1*mAdjM(phi2ZDM)*FDM = phiZDM;
FDM^-1*mAdjM(I3*Sqrt(-1)*One(mm))*FDM = -I3*Sqrt(-1)*One(mm);

#####
#
# $\eta\in m$ so that $N_{m/\ell} \eta = 5$.
#
# This is an approximation $\mod 3^10$.

eta:=2*Zm+1+2*3+2*3^3+2*3^7+3^8+2*3^9+3^10;
phieta:=2*phiZm+1+2*3+2*3^3+2*3^7+3^8+2*3^9+3^10;
phi2eta:=2*phi2Zm+1+2*3+2*3^3+2*3^7+3^8+2*3^9+3^10;

eta = 109411+2*Zm;
phieta = 109411+2*phiZm;
phi2eta = 109411+2*phi2Zm;
Norm(mm,ll,eta) mod 3^10 = 5;
Norm(mm,ll,phieta) mod 3^10 = 5;
Norm(mm,ll,phi2eta) mod 3^10 = 5;

#####
#
# Matrices to conjugate the representation of~$\D$ as matrices
# over~$m$ to the representation as matrices
# over~$\ell\otimes\QQ_3=\QQ_3[i]$ 
#
C1:=[[eta*phieta,0,0],[0,phieta,0],[0,0,1]]*One(mm);
C1inv:=C1^-1;
C2:=[[1,1,1],
     [(Zm+1)/3,(phiZm+1)/3,(phi2Zm+1)/3],
     [(Zm+1)^2/3,(phiZm+1)^2/3,(phi2Zm+1)^2/3]]*One(mm);
C2inv:=C2^-1;

#####
#
# Conjugated copies of ZDM, phiZDM, phi2ZDM, sigmaDM
#
ZDMConj3:=C2*C1*ZDM*C1inv*C2inv;
phiZDMConj3:=C2*C1*phiZDM*C1inv*C2inv;
phi2ZDMConj3:=C2*C1*phi2ZDM*C1inv*C2inv;
sigmaDMConj3:=C2*C1*sigmaDM*C1inv*C2inv;

InlM(ZDMConj3);
InlM(phiZDMConj3);
InlM(phi2ZDMConj3);
InlM(mModM(sigmaDMConj3,3^10));

phiZDMConj3 =
  (3/2+1/2*Sqrt(-1))*I3
  +(-1/2-2*Sqrt(-1))*ZDMConj3
  +(1/2*Sqrt(-1))*ZDMConj3^2;
phi2ZDMConj3 =
  (3/2-1/2*Sqrt(-1))*I3
  +(-1/2+2*Sqrt(-1))*ZDMConj3
  +(-1/2*Sqrt(-1))*ZDMConj3^2;

ZDMConj3^3-3*ZDMConj3^2-2*One(ZDMConj3) = Zero(ZDMConj3);
phiZDMConj3^3-3*phiZDMConj3^2-2*One(ZDMConj3) = Zero(ZDMConj3);
phi2ZDMConj3^3-3*phi2ZDMConj3^2-2*One(ZDMConj3) = Zero(ZDMConj3) ;

sigmaDMConj3^3 = 5*One(ZDMConj3);
sigmaDMConj3*ZDMConj3 = phiZDMConj3*sigmaDMConj3;
sigmaDMConj3*phiZDMConj3 = phi2ZDMConj3*sigmaDMConj3;
sigmaDMConj3*phi2ZDMConj3 = ZDMConj3*sigmaDMConj3;

#####
#
# In the conjugated matrix representation of~$\D$ over~$m$, the
# matrix~$F$ so that
#
#   \iota(A) = F^{-1} A^* F
FDMConj3:=mAdjM(C2inv)*mAdjM(C1inv)*FDM*C1inv*C2inv;

InlM(mModM(3^2*FDMConj3,3^12));

mAdjM(FDMConj3) = FDMConj3;
FDMConj3^-1*mAdjM(sigmaDMConj3)*FDMConj3 = sigmaDMConj3;
FDMConj3^-1*mAdjM(ZDMConj3)*FDMConj3 = ZDMConj3;
FDMConj3^-1*mAdjM(phiZDMConj3)*FDMConj3 = phi2ZDMConj3;
FDMConj3^-1*mAdjM(phi2ZDMConj3)*FDMConj3 = phiZDMConj3;
FDMConj3^-1*mAdjM(I3*Sqrt(-1)*One(mm))*FDMConj3 = -I3*Sqrt(-1)*One(mm);

#####
#
# In the 3-adic matrix representation of~$\D$ (as matrices over
# $\ell\otimes\QQ_3$) the matrices for $Z$, $\phi(Z)$, $\phi^2(Z)$,
# and $\sigma$
#
# These are approximations $\mod 3^10$
#
ZDM3:=lModM(mTolM(ZDMConj3),3^10);
phiZDM3:=lModM(mTolM(phiZDMConj3),3^10);
phi2ZDM3:=lModM(mTolM(phi2ZDMConj3),3^10);
sigmaDM3:=lModM(mTolM(sigmaDMConj3),3^10);

lModM(
  phiZDM3 -
    ((3/2+1/2*Sqrt(-1))*I3
     +(-1/2-2*Sqrt(-1))*ZDM3
     +(1/2*Sqrt(-1))*ZDM3^2)
  ,3^10) = Zero(ZDM3);
lModM(
  phi2ZDM3 -
    ((3/2-1/2*Sqrt(-1))*I3
     +(-1/2+2*Sqrt(-1))*ZDM3
     +(-1/2*Sqrt(-1))*ZDM3^2)
  ,3^10) = Zero(ZDM3);

lModM(ZDM3^3-3*ZDM3^2-2*One(ZDM3),3^10) = Zero(ZDM3);
lModM(phiZDM3^3-3*phiZDM3^2-2*One(ZDM3),3^10) = Zero(ZDM3);
lModM(phi2ZDM3^3-3*phi2ZDM3^2-2*One(ZDM3),3^10) = Zero(ZDM3) ;

lModM(sigmaDM3^3 - 5*One(ZDM3),3^10) = Zero(sigmaDM3);
lModM(sigmaDM3*ZDM3 - phiZDM3*sigmaDM3,3^10) = Zero(sigmaDM3);
lModM(sigmaDM3*phiZDM3 - phi2ZDM3*sigmaDM3,3^10) = Zero(sigmaDM3);
lModM(sigmaDM3*phi2ZDM3 - ZDM3*sigmaDM3,3^10) = Zero(sigmaDM3);

#####
#
# 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:=lModM(mTolM(FDMConj3),3^10);

lModM(lAdjM(FDM3) - FDM3,3^10) = Zero(FDM3);
lModM(FDM3^-1*lAdjM(sigmaDM3)*FDM3 - sigmaDM3,3^10) = Zero(FDM3);
lModM(FDM3^-1*lAdjM(ZDM3)*FDM3 - ZDM3,3^10) = Zero(FDM3);
lModM(FDM3^-1*lAdjM(phiZDM3)*FDM3 - phi2ZDM3,3^10) = Zero(FDM3);
lModM(FDM3^-1*lAdjM(phi2ZDM3)*FDM3 - phiZDM3,3^10) = Zero(FDM3);
lModM(FDM3^-1*lAdjM(I3*Sqrt(-1)*One(ll))*FDM3 + I3*Sqrt(-1)*One(ll)
  ,3^10) = Zero(FDM3);

#Check that FDM3 and its inverse are integral $\mod 3$.
lModM(3*FDM3,3) = Zero(FDM3);
lModM(3*FDM3^-1,3) = Zero(FDM3);
Determinant(FDM3) mod 3 <> 0;

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

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

# For a $3\times 3$ matrix with coefficients in $\ZZ[i]$, this
# function calculate the real and imaginary parts of the
# 9~entries, each modulo~$27$, and puts them together as an 18~element
# vector.
CondCoeff3:=function(mtx)
  return Concatenation(
    ListX([1..3],[1..3],function(j,k)
      return lRe(mtx[j][k]) mod 3^3;
    end),
    ListX([1..3],[1..3],function(j,k)
      return lIm(mtx[j][k]) mod 3^3;
    end)
  );
end;

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

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