/*
Test some G-module stuff.
AKS 2/2/94.
*/


AG := ActionGenerator;

action_eq := func<M, N |
    forall{i: i in [1 .. Nagens(M)] | AG(M, i) eq AG(N, i)}
>;

mod_slash := func<M, N |
    quo<M | N>
>;

procedure test(M)
    print "test:", M;

    dim := Dimension(M);
    cs, cf, ct := CompositionSeries(M);

    assert action_eq(cs[1], cf[1]);

    if dim lt 30 then
	for i := 2 to #cs - 1 do
	    assert action_eq(mod_slash(cs[i], cs[i - 1]), cf[i]);
	end for;
    end if;

    cons := Constituents(M);
//cons;

    for i := 1 to #cons do
	for j := i + 1 to #cons do
	    assert not IsIsomorphic(cons[i], cons[j]);
	end for;
    end for;

    if dim le 400 then
    K := BaseRing(M);
    repeat
	T := Random(MatrixRing(K, dim));
    until IsUnit(T);

    M2 := M^T;
//"iso"; time
    l, T := IsIsomorphic(M, M2);
    assert l;
    for i := 1 to Nagens(M) do
	assert ActionGenerator(M, i)*T eq T*ActionGenerator(M2, i);
    end for;

//"InternalIsomorphismTestFF";
    //time l := InternalIsomorphismTestFF(M, M2, AHom(M, M2)); assert l;
    end if;

end procedure;
    

test(GModule(PSL(3, 4), GF(2)));
test(GModule(PSL(3, 4), GF(3)));
test(GModule(PSU(3, 5), GF(3)));

m23 := sub<Sym(23) |  
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 
        22, 23),
    (2, 16, 9, 6, 8)(3, 12, 13, 18, 4)(7, 17, 10, 11, 22)(14, 19, 21, 20, 15)
/**/>;

for p in [2, 3, 5, 7] do
    M := GModule(m23, GF(p));
    test(M);
    test(ExteriorSquare(M));
    test(TensorProduct(M, M));
end for;

G := sub<Sym(152) |
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 
        22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 
        40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 
        58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 
        76)(77, 116, 79, 118, 81, 120, 83, 122, 85, 124, 87, 126, 89, 128, 91, 
        130, 93, 132, 95, 134, 97, 136, 99, 138, 101, 140, 103, 142, 105, 144, 
        107, 146, 109, 148, 111, 150, 113, 152, 115, 78, 117, 80, 119, 82, 121, 
        84, 123, 86, 125, 88, 127, 90, 129, 92, 131, 94, 133, 96, 135, 98, 137, 
        100, 139, 102, 141, 104, 143, 106, 145, 108, 147, 110, 149, 112, 151, 
        114),
    (1, 77, 11, 87, 21, 97, 31, 107, 41, 117, 51, 127, 61, 137, 71, 147, 5, 81, 
        15, 91, 25, 101, 35, 111, 45, 121, 55, 131, 65, 141, 75, 151, 9, 85, 19,
        95, 29, 105, 39, 115, 49, 125, 59, 135, 69, 145, 3, 79, 13, 89, 23, 99, 
        33, 109, 43, 119, 53, 129, 63, 139, 73, 149, 7, 83, 17, 93, 27, 103, 37,
        113, 47, 123, 57, 133, 67, 143)(2, 78, 12, 88, 22, 98, 32, 108, 42, 118,
        52, 128, 62, 138, 72, 148, 6, 82, 16, 92, 26, 102, 36, 112, 46, 122, 56,
        132, 66, 142, 76, 152, 10, 86, 20, 96, 30, 106, 40, 116, 50, 126, 60, 
        136, 70, 146, 4, 80, 14, 90, 24, 100, 34, 110, 44, 120, 54, 130, 64, 
        140, 74, 150, 8, 84, 18, 94, 28, 104, 38, 114, 48, 124, 58, 134, 68, 
        144)
/**/>;

/*
F<a, b, c, d> := FreeGroup(4);
pc := quo< GrpPC: F | a^2 = d, b^19, c^2 = d, d^2, c^a = c*d >;
//, (a, b), (c, b), (d, a), (d, b), (d, c) >;

> g := DirectProduct(CyclicGroup(19), DihedralGroup(4));

*/

M := GModule(G, GF(5));
test(M);
c := Constituents(M);
assert
    [Dimension(EndomorphismRing(x)): x in c] eq
    [ 1, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 ];


// Test hard Endo case of Meataxe
K := GF(17);
for i := 1 to 100 do
    M:=RModule([ MatrixAlgebra(K, 4) |
    Matrix(4, 4, \[ 2, 3, 6, 0, 15, 9, 0, 11, 3, 0, 9, 3, 0, 14, 15, 2 ]),
    Matrix(4, 4, \[ 15, 4, 1, 0, 16, 7, 0, 16, 16, 0, 7, 4, 0, 1, 16, 15])]);
    assert Dimension(AHom(Constituents(M)[1], M)) eq 2;
end for;

// IsProjective (JFC 2016)
G := PermutationGroup("M11", 1);
M := Induction(TrivialModule(Sylow(G,2), GF(2)),G);
assert not IsProjective(M);
M := Induction(TrivialModule(Sylow(G,3), GF(2)),G);
assert IsProjective(M);
M := Induction(TrivialModule(Sylow(G,3), GF(3)),G);
assert not IsProjective(M);
M := Induction(TrivialModule(Sylow(G,2), GF(3)),G);
assert IsProjective(M);


// Nov 2017
K:=GF(3);
G:=MatrixGroup<3,K|[1,1,0, 0,1,0, 0,0,1], [1,0,1, 0,1,0, 0,0,1]>;
M:=GModule(G);
X:=TensorProduct(M,M);
// X is a temporary name //
MD:=IndecomposableSummands(X)[1];
// MD is M dual //
N6:=IndecomposableSummands(X)[2];
N6D:=Dual(N6);
N9:=TensorProduct(M,MD);
X:=TensorProduct(N6,N6);
P:=IndecomposableSummands(X)[1];
N12:=IndecomposableSummands(X)[2];
N15:=IndecomposableSummands(X)[3];
N18:=TensorProduct(M,N6);
X:=TensorProduct(M,N12);
N15A:=IndecomposableSummands(X)[1];
N21:=IndecomposableSummands(X)[2];
N15AD:=Dual(N15A);
A := N15A;
B := N15AD;

assert not IsIsomorphic(A, B);
test(A);
test(B);
test(ExteriorSquare(A));
test(ExteriorSquare(B));
test(DirectSum(A, B));


//quit;
