#(C) Graham Ellis, 2005-2006

#####################################################################
InstallGlobalFunction(PrimePartDerivedFunctor,
function(G,R,F,n)
local
	C,P, DCRS, L, AscChn,
	K, gensK, S, f,fx,
	HP, HK, HPK, HKhomHPK, HPKhomHP, HKhomHP,
	HKx,HPKx, 
	HKxhomHPKx, HPKxhomHP, HKxhomHP, HKhomHKx,  HKhomHP2,
	HPrels, x, y, i,prime,  
	HPpres,G1;

C:=F(R);

P:=R!.group;
prime:=Factors(Order(P))[1];
AscChn:=AscendingChain(G,P);
DCRS:=[];
for i in [2..Length(AscChn)] do
Append(DCRS,
#GeneratorsOfGroup(AscChn[i]));
List(Elements(RightTransversal(AscChn[i],AscChn[i-1])),x->x^-1));
#Append(DCRS,
#List(DoubleCosetRepsAndSizes(AscChn[i],AscChn[i-1],AscChn[i-1]), 
#x->x[1]));
od;

#DCRS:=List(DoubleCosetRepsAndSizes(G,P,P),x->x[1]);
Print(Length(DCRS),"\n");

HP:=GroupHomomorphismByFunction(P,P,x->x);
HP:=EquivariantChainMap(R,R,HP);
HP:=F(HP);


HP:=Homology(HP,n);

HP:=Source(HP);
HPrels:=[Identity(HP)];

if Length(AbelianInvariants(HP))=0 then return []; fi;

for L in DCRS do
K:=Intersection(P,P^L);
if Order(K)>1 then


gensK:=ReduceGenerators(GeneratorsOfGroup(K),K);

G1:=Group(Concatenation(gensK,[Identity(P)]));
if Order(G1)<=32 then
S:=ResolutionFiniteGroup(gensK,n+1);
else
S:=ResolutionNormalSeries(LowerCentralSeries(G1),n+1);
fi;
#S:=ResolutionFiniteSubgroup(R,K);
if not (Homology(F(S),n)=[]) then

f:=GroupHomomorphismByFunction(K,P,x->x);
HKhomHPK:=Homology(F(EquivariantChainMap(S,R,f)),n);
HK:=Source(HKhomHPK);

HPK:=Range(HKhomHPK);
HPKhomHP:=GroupHomomorphismByImagesNC(HPK,HP,GeneratorsOfGroup(HPK),
                                                  GeneratorsOfGroup(HP));
HKhomHP:=GroupHomomorphismByFunction(HK,HP,x->
Image(HPKhomHP, Image(HKhomHPK,x) ) );

fx:=GroupHomomorphismByFunction(K,P,g->Image(f,g)^(L^-1));
HKxhomHPKx:=Homology(F(EquivariantChainMap(S,R,fx)),n);
HKx:=Source(HKxhomHPKx);
HPKx:=Parent(Image(HKxhomHPKx));
HPKxhomHP:=GroupHomomorphismByImagesNC(HPKx,HP,GeneratorsOfGroup(HPKx),
                                                  GeneratorsOfGroup(HP));
HKxhomHP:=GroupHomomorphismByFunction(HKx,HP,x->
Image(HPKxhomHP, Image(HKxhomHPKx,x) ) );
HKhomHKx:=GroupHomomorphismByImagesNC(HK,HKx,GeneratorsOfGroup(HK),GeneratorsOfGroup(HKx));
HKhomHP2:=GroupHomomorphismByFunction(HK,HP,a->
Image(HKxhomHP, Image(HKhomHKx,a)));

for x in GeneratorsOfGroup(HK) do
Append(HPrels, [Image(HKhomHP,x)*Image(HKhomHP2,x)^-1]);
od;

fi;
fi;
od;

return AbelianInvariants(HP/NormalClosure(HP,Group(HPrels)));
end);
#####################################################################

