
###########################################################
###########################################################
InstallGlobalFunction(HeckeOperator,
function(H,pp,k)
local  N, p, g, gg, HH, R, RH, TH, map1, map11, map12, THH, map3, 
ag, A, facts, T,E, Sfacts,Hecke ,fn, ConjugateIntersection,HHhomH;


N:=1;
if IsInt(pp) then
   facts:=Factors(pp);
   if Length(facts)=1 then 
   p:=facts[1];
   g:=[[1,0],[0,p]];
   else g:=fail;
   fi;
   if Gcd(pp,H!.level)>1 then 
   Print("The second argument is not coprime to the level.\n");
   return fail;
   fi;
fi;
if IsMatrix(pp) then
   g:=pp; p:=0;
fi;

if not g=fail then
#####################################################
#####################################################

ag:=[[ g[2][2]  , -g[1][2] ],
     [ -g[2][1] , g[1][1]  ]];;   

#######################################
ConjugateIntersection:=function(H,g)
local Hg, HH;

#THIS FUNCTION NEEDS TO BE PROPERLY IMPLEMENTED USING THE OBVIOUS
#COSET REPS FOR GAMMA0. THIS SHOULD THEN WORK MUST FASTER

if g=[[p,0],[0,1]] and IsBound(H!.name) then
  if H!.name="CongruenceSubgroupGamma0" then return
  HAP_CongruenceSubgroupGamma0(p*H!.level);
  fi;
fi;
  
Hg:=H^g;
HH:=Intersection(H,Hg);;
return HH;
end;
#######################################

HH:=ConjugateIntersection(H,g);
gg:=g^-1;;



R:=ResolutionSL2Z_alt(N+1);;
R:=TietzeReducedResolution(R);;  #This just invokes a recording of homotopies.
RH:=ResolutionFiniteSubgroup(R,H);;
TH:=TietzeReducedResolution(RH);;

HHhomH:=GroupHomomorphismByFunction(HH,H,x->x^gg);

A:=HomogeneousPolynomials(H,k-2);

#Print(Runtime(),"\n");
map1:=TransferCochainMap(TH,HH,A,true);;      
map11:=map1[1];
map12:=map1[2];
THH:=map1[3];   #resolution for HH
map11:=HomToIntegersModP(map11,-1/2);
map11:=HomologyVectorSpace(map11,N);
map11:=List(Basis(Source(map11)),x->Image(map11,x));
map11:=TransposedMat(map11);
#Print("map11 ",Runtime(),"\n");
map12:=HomToIntegersModP(map12,-1/2);
map12:=HomologyVectorSpace(map12,N);
map12:=List(Basis(Source(map12)),x->Image(map12,x));
map12:=TransposedMat(map12);
map12:=map12^-1;
#Print("map1 ",Runtime(),"\n");;
map1:=map12*map11;

#Print("map1^-1 ",Runtime(),"\n");;


map3:=EquivariantChainMap(THH,TH,HHhomH);
map3:=HomToIntegralModule(map3,A,ag);   
map3:=HomToIntegersModP(map3,-1/2);
map3:=HomologyVectorSpace(map3,N);
map3:=List(Basis(Source(map3)),x->Image(map3,x));
map3:=TransposedMat(map3);
#Print("map3 ",Runtime(),"\n");;



if map1=[] or map3=[] then return []; fi;
return Product([map3,map1]);
#####################################################
#####################################################
fi;

if not H!.name="CongruenceSubgroupGamma0" then
Print("Second argument is not a prime.\n");
return fail;
fi;

########################
fn:=function(A,n);
if n=0 then return p^(k-2)*IdentityMat(Length(A)); fi;
if n=1 then return A; fi;
if n=2 then return A^2 - p^(k-1)*IdentityMat(Length(A)); fi;

return A*fn(A,n-1) - p^(k-1)*fn(A,n-2);

end;
########################

T:=[];
E:=[];
Hecke:=[];
Sfacts:=SSortedList(facts);
for p in Sfacts do
T[p]:=HeckeOperator(H,p,k);
E[p]:=Length(Filtered(facts,x->x=p));
if Gcd(p,H!.level)>1 then
Add(Hecke,  T[p]^E[p]);
else
Add(Hecke,  fn(T[p],E[p]));
fi;
od;

return Product(Hecke);
end);
###########################################################
###########################################################

###########################################################
###########################################################
InstallGlobalFunction(HeckeOperatorWeight2,
function(H,p,N)
local g, gg, Hg, HH, gHH, R, RH, TH, RgHH, TgHH,
HHhomgHH, gHHhomH, map1, THH, eqmap2, map2, eqmap3, map3,
map32, map321,t;

if not IsPrimeInt(p) then
Print("Second argument is not prime.\n");
return fail;
fi;
g:=[[1,0],[0,p]];;   #This one seems to agree with Stein's book!!

Hg:=H^g;;
HH:=Intersection(H,Hg);;
gg:=g^-1;;
gHH:=HH^gg;;

R:=ResolutionSL2Z_alt(N+1);;
RH:=ResolutionFiniteSubgroup(R,H);;
TH:=TietzeReducedResolution(RH);;
RgHH:=ResolutionFiniteSubgroup(TH,gHH);;
TgHH:=TietzeReducedResolution(RgHH);;

HHhomgHH:=GroupHomomorphismByFunction(HH,gHH,x->x^gg);
gHHhomH:=GroupHomomorphismByFunction(gHH,H,x->x);

map1:=TransferChainMap(TH,HH);;
THH:=map1!.resH;


eqmap2:=EquivariantChainMap(THH,TgHH,HHhomgHH);
map2:=TensorWithIntegers(eqmap2);
eqmap3:=EquivariantChainMap(TgHH,TH,gHHhomH);
map3:=TensorWithIntegers(eqmap3);

map32:=Compose(map3,map2);
map321:=Compose(map32,map1);
map321:=HomToIntegers(map321);

t:=Cohomology(map321,N);
return t;
end);
###########################################################
###########################################################

###########################################################
###########################################################
InstallGlobalFunction(HomomorphismAsMatrix,
function(t);
return
List(GeneratorsOfGroup(Source(t)),x->Exponents(Image(t,x)));
end);
###########################################################
###########################################################

