# Copyright (c) 2018-2021, Carnegie Mellon University
# See LICENSE for details

# Don't forget to Import(platforms.sse)

#NOTE:
# Bluestein breaks for SSE_2x64f -- disabled for small for now

# doParSimdDft seems broken
benchSSE := function()
    if LocalConfig.cpuinfo.SIMD().hasSSE2() then
        return rec(
            2x32f := rec(
                wht := rec(
                    small := _defaultSizes(s->doSimdWht(s, SSE_2x32f, rec(propagateNth := true, useDeref := true, verify := true, oddSizes := true, svct := true, stdTTensor := true, tsplPFA := false)), [4]),
                    medium := _defaultSizes(s->doSimdWht(s, SSE_2x32f, rec(propagateNth := true, useDeref := true, oddSizes := false, svct := true, stdTTensor := true, tsplPFA := false)), List([2..10], i->2^i))
                ),
                1d := rec(
                    dft_sc := rec(
                        small := _defaultSizes(s->doSimdDft(s, SSE_2x32f, rec(propagateNth := true, useDeref := true, verify:=true, tsplBluestein:=false, interleavedComplex := true, PRDFT:=true, URDFT:= true, cplxVect := true, stdTTensor := false, globalUnrolling:=10000)), [ 2..32 ]),

                    ),
                    dft_ic := rec(
                        small := _defaultSizes(s->doSimdDft(s, SSE_2x32f, rec(propagateNth := true, useDeref := true, verify:=true, tsplBluestein:=false, interleavedComplex := true, PRDFT:=true, URDFT:= true, cplxVect := true, stdTTensor := false, globalUnrolling:=10000)), [2..32])
                    )
                )
            ),
            2x64f := rec(
                wht := rec(
                    small := _defaultSizes(s->doSimdWht(s, SSE_2x64f, rec(verify := true, oddSizes := true, svct := true, stdTTensor := true, tsplPFA := false)), [4]),
                    medium := _defaultSizes(s->doSimdWht(s, SSE_2x64f, rec(oddSizes := false, svct := true, stdTTensor := true, tsplPFA := false)), List([2..10], i->2^i))
                ),
                1d := rec(
                    dft_sc := rec(
                        small := _defaultSizes(s->doSimdDft(s, SSE_2x64f, rec(verify:=true, tsplBluestein:=false, interleavedComplex := false, PRDFT:=true, URDFT:= true, cplxVect := true, stdTTensor := false, globalUnrolling:=10000)),
                                [ 2..32 ]),
                        medium := _defaultSizes(s->doSimdDft(s, SSE_2x64f, rec(tsplRader:=false, tsplBluestein:=false, tsplPFA:=false, oddSizes:=false, interleavedComplex := false)),
                                _svctSizes(1024, 16, 2)),
    #                    medium := spiral.libgen.doParSimdDft(SSE_2x64f, 1, _svctSizes(1024, 16, 4), false, true, false, false),
    #                    large := spiral.libgen.doParSimdDft(SSE_2x64f, 1, List([4..20], i->2^i), false, true, false, false)

                    ),
                    dft_ic := rec(
                        small := _defaultSizes(s->doSimdDft(s, SSE_2x64f, rec(verify:=true, tsplBluestein:=false, interleavedComplex := true, PRDFT:=true, URDFT:= true, cplxVect := true, stdTTensor := false, globalUnrolling:=10000)),
                                [2..32]),
                        medium := _defaultSizes(s->doSimdDft(s, SSE_2x64f, rec(tsplRader:=false, tsplBluestein:=false, tsplPFA:=false, oddSizes:=false, interleavedComplex := true)),
                                _svctSizes(1024, 16, 2)),
                        medium_cx := _defaultSizes(s->doSimdDft(s, SSE_2x64f, rec(tsplRader:=false, tsplBluestein:=false, tsplPFA:=false, oddSizes:=false, interleavedComplex := true, cplxVect := true, realVect := false)),
                                _svctSizes(1024, 16, 2)),
    #                    medium := spiral.libgen.doParSimdDft(SSE_2x64f, 1, _svctSizes(1024, 16, 4), false, true, false, true),
    #                    large := spiral.libgen.doParSimdDft(SSE_2x64f, 1, List([4..20], i->2^i), false, true, false, true)
                    ),
                    trdft := _defaultSizes(s->doSimdSymDFT(TRDFT, s, SSE_2x64f, rec( verify:=true,
                                interleavedComplex := true, PRDFT:=true, URDFT:= true, tsplBluestein := false, cplxVect := true,
                                realVect := true, propagateNth := true, useDeref := true,
                                globalUnrolling:=10000)), 4*[1..32]),

                    dht := _defaultSizes(s->doSimdSymDFT(TDHT, s, SSE_2x64f, rec(verify :=true)), 8*[1..12]),
                    dct2 := _defaultSizes(s->doSimdSymDFT(TDCT2, s, SSE_2x64f, rec(verify :=true)), 8*[1..12]),
                    dct3 := _defaultSizes(s->doSimdSymDFT(TDCT3, s, SSE_2x64f, rec(verify :=true)), 8*[1..12]),
                    dct4 := _defaultSizes(s->doSimdSymDFT(TDCT4, s, SSE_2x64f, rec(verify :=true)), 8*[1..12]),
                    dst2 := _defaultSizes(s->doSimdSymDFT(TDST2, s, SSE_2x64f, rec(verify :=true)), 8*[1..12]),
                    dst3 := _defaultSizes(s->doSimdSymDFT(TDST3, s, SSE_2x64f, rec(verify :=true)), 8*[1..12]),
                    dst4 := _defaultSizes(s->doSimdSymDFT(TDST4, s, SSE_2x64f, rec(verify :=true)), 8*[1..12]),
                    mdct := _defaultSizes(s->doSimdSymDFT(TMDCT, s, SSE_2x64f, rec(verify :=true)), 8*[1..12]),
                    imdct := _defaultSizes(s->doSimdSymDFT(TIMDCT, s, SSE_2x64f, rec(verify :=true)), 8*[1..12])
                ),
                2d := rec(
                    dft_ic := rec(
                        medium := _defaultSizes(s->doSimdMddft(s, SSE_2x64f, rec(interleavedComplex := true,
                                    oddSizes := false, svct := true, splitL := false, pushTag := true, flipIxA := false, stdTTensor := true, tsplPFA := false)),
                                    4*List([1..16], i->[i,i])),
                        small := _defaultSizes(s->doSimdMddft(s, SSE_2x64f, rec(verify:=true, interleavedComplex := true, globalUnrolling:=10000,
                                    tsplPFA := false, pushTag:= false, oddSizes := true, svct := true, splitL := false)),
                                    List([2..16], i->[i,i]))
                    ),
                    dft_sc := rec(
                        medium := _defaultSizes(s->doSimdMddft(s, SSE_2x64f, rec(interleavedComplex := false,
                                    oddSizes := false, svct := true, splitL := false, pushTag := true, flipIxA := false, stdTTensor := true, tsplPFA := false)),
                                    4*List([1..16], i->[i,i])),
                        small := _defaultSizes(s->doSimdMddft(s, SSE_2x64f, rec(verify:=true, interleavedComplex := false, globalUnrolling:=10000,
                                    tsplPFA := false, pushTag:= false, oddSizes := true, svct := true, splitL := false)),
                                    List([2..16], i->[i,i]))
                    ),
                    dct2 := _defaultSizes(s->doSimdSymMDDFT(DCT2, s, SSE_2x64f, rec(verify := true)), [4, 8, 12, 16]),
                    dct3 := _defaultSizes(s->doSimdSymMDDFT(DCT3, s, SSE_2x64f, rec(verify := true)), [4, 8, 12, 16]),
                    dct4 := _defaultSizes(s->doSimdSymMDDFT(DCT4, s, SSE_2x64f, rec(verify := true)), [4, 8, 12, 16]),
                    dst2 := _defaultSizes(s->doSimdSymMDDFT(DST2, s, SSE_2x64f, rec(verify := true)), [4, 8, 12, 16]),
                    dst3 := _defaultSizes(s->doSimdSymMDDFT(DST3, s, SSE_2x64f, rec(verify := true)), [4, 8, 12, 16]),
                    dst4 := _defaultSizes(s->doSimdSymMDDFT(DST4, s, SSE_2x64f, rec(verify := true)), [4, 8, 12, 16])
                )
            ),
            4x32i := rec(
                wht := rec(
                    small := _defaultSizes(s->doSimdWht(s, SSE_4x32i, rec(verify := true, oddSizes := true, svct := true, stdTTensor := true, tsplPFA := false)), List([], i->2^i)),
                    medium := _defaultSizes(s->doSimdWht(s, SSE_4x32i, rec(oddSizes := false, svct := true, stdTTensor := true, tsplPFA := false)), List([4..10], i->2^i))
                )
            ),
            4x32f := rec(
                wht := rec(
                    small := _defaultSizes(s->doSimdWht(s, SSE_4x32f, rec(verify := true, oddSizes := true, svct := true, stdTTensor := true, tsplPFA := false)), List([1..3], i->2^i)),
                    medium := _defaultSizes(s->doSimdWht(s, SSE_4x32f, rec(oddSizes := false, svct := true, stdTTensor := true, tsplPFA := false)), List([4..10], i->2^i))
                ),
                1d := rec(
                    dft_sc := rec(
                        small := _defaultSizes(s->doSimdDft(s, SSE_4x32f, rec(verify:=true, interleavedComplex := false, stdTTensor := false, globalUnrolling:=10000)),
                                [ 2..64 ]),
                        medium := _defaultSizes(s->doSimdDft(s, SSE_4x32f, rec(tsplRader:=false, tsplBluestein:=false, tsplPFA:=false, oddSizes:=false, interleavedComplex := false)),
                                _svctSizes(1024, 16, 4)),
    #                    medium := spiral.libgen.doParSimdDft(SSE_4x32f, 1, _svctSizes(1024, 16, 4), false, true, false, false),
    #                    large := spiral.libgen.doParSimdDft(SSE_4x32f, 1, List([4..20], i->2^i), false, true, false, false)

                    ),
                    dft_ic := rec(
                        small := _defaultSizes(s->doSimdDft(s, SSE_4x32f, rec(verify:=true, interleavedComplex := true, PRDFT:=true, URDFT:= true, cplxVect := true, stdTTensor := false, globalUnrolling:=10000)),
                                [ 2..64 ]),
                        medium := _defaultSizes(s->doSimdDft(s, SSE_4x32f, rec(tsplRader:=false, tsplBluestein:=false, tsplPFA:=false, oddSizes:=false, interleavedComplex := true)),
                                _svctSizes(1024, 16, 4)),
                        medium_cx := _defaultSizes(s->doSimdDft(s, SSE_4x32f, rec(tsplRader:=false, tsplBluestein:=false, tsplPFA:=false, oddSizes:=false, interleavedComplex := true,
                                cplxVect := true, realVect := false, PRDFT := false, URDFT := true)),
                                _svctSizes(1024, 16, 4))
    #                    medium := spiral.libgen.doParSimdDft(SSE_4x32f, 1, _svctSizes(1024, 16, 4), false, true, false, true),
    #                    large := spiral.libgen.doParSimdDft(SSE_4x32f, 1, List([4..20], i->2^i), false, true, false, true)
                    ),
                    rdft := _defaultSizes(s->doSimdSymDFT(TRDFT, s, SSE_4x32f, rec(verify :=true)), 32*[1..12]),
                    trdft := _defaultSizes(s->doSimdSymDFT(TRDFT, s, SSE_4x32f, rec( verify:=true,
                                interleavedComplex := true, PRDFT:=true, URDFT:= true, tsplBluestein := false, cplxVect := true,
                                stdTTensor := false, realVect := true, propagateNth := true, useDeref := true,
                                globalUnrolling:=10000)), 8*[1..32]),
                    dht := _defaultSizes(s->doSimdSymDFT(TDHT, s, SSE_4x32f, rec(verify :=true)), 32*[1..12]),
                    dct2 := _defaultSizes(s->doSimdSymDFT(TDCT2, s, SSE_4x32f, rec(verify :=true)), 32*[1..12]),
                    dct3 := _defaultSizes(s->doSimdSymDFT(TDCT3, s, SSE_4x32f, rec(verify :=true)), 32*[1..12]),
                    dct4 := _defaultSizes(s->doSimdSymDFT(TDCT4, s, SSE_4x32f, rec(verify :=true)), 32*[1..12]),
                    dst2 := _defaultSizes(s->doSimdSymDFT(TDST2, s, SSE_4x32f, rec(verify :=true)), 32*[1..12]),
                    dst3 := _defaultSizes(s->doSimdSymDFT(TDST3, s, SSE_4x32f, rec(verify :=true)), 32*[1..12]),
                    dst4 := _defaultSizes(s->doSimdSymDFT(TDST4, s, SSE_4x32f, rec(verify :=true)), 32*[1..12]),
                    mdct := _defaultSizes(s->doSimdSymDFT(TMDCT, s, SSE_4x32f, rec(verify :=true)), 32*[1..12]),
                    imdct := _defaultSizes(s->doSimdSymDFT(TIMDCT, s, SSE_4x32f, rec(verify :=true)), 32*[1..12])
                ),
                2d := rec(
                    dft_ic := rec(
                        medium := _defaultSizes(s->doSimdMddft(s, SSE_4x32f, rec(interleavedComplex := true,
                                    oddSizes := false, svct := true, splitL := false, pushTag := true, flipIxA := false, stdTTensor := true, tsplPFA := false)),
                                    16*List([1..8], i->[i,i])),
                        small := _defaultSizes(s->doSimdMddft(s, SSE_4x32f, rec(verify:=true, interleavedComplex := true, globalUnrolling:=10000,
                                    tsplPFA := false, pushTag:= false, oddSizes := true, svct := true, splitL := false)),
                                    List([2..16], i->[i,i]))
                    ),
                    dft_sc := rec(
                        medium := _defaultSizes(s->doSimdMddft(s, SSE_4x32f, rec(interleavedComplex := false,
                                    oddSizes := false, svct := true, splitL := false, pushTag := true, flipIxA := false, stdTTensor := true, tsplPFA := false)),
                                    16*List([1..8], i->[i,i])),
                        small := _defaultSizes(s->doSimdMddft(s, SSE_4x32f, rec(verify:=true, interleavedComplex := false, globalUnrolling:=10000,
                                    tsplPFA := false, pushTag:= false, oddSizes := true, svct := true, splitL := false)),
                                    List([2..16], i->[i,i]))
                    ),
                    dct2 := _defaultSizes(s->doSimdSymMDDFT(DCT2, s, SSE_4x32f, rec(verify := true)), [4, 8, 12, 16]),
                    dct3 := _defaultSizes(s->doSimdSymMDDFT(DCT3, s, SSE_4x32f, rec(verify := true)), [4, 8, 12, 16]),
                    dct4 := _defaultSizes(s->doSimdSymMDDFT(DCT4, s, SSE_4x32f, rec(verify := true)), [4, 8, 12, 16]),
                    dst2 := _defaultSizes(s->doSimdSymMDDFT(DST2, s, SSE_4x32f, rec(verify := true)), [4, 8, 12, 16]),
                    dst3 := _defaultSizes(s->doSimdSymMDDFT(DST3, s, SSE_4x32f, rec(verify := true)), [4, 8, 12, 16]),
                    dst4 := _defaultSizes(s->doSimdSymMDDFT(DST4, s, SSE_4x32f, rec(verify := true)), [4, 8, 12, 16]),
				# DO NOT USE BECAUSE IT GENERATES A NON_COMPATIBLE INIT() FUNCTION FOR USE WITH STANDARD TIMER
                #    conv := _defaultSizes(s->doSimdMdConv(s, SSE_4x32f, rec(svct := true, oddSizes := false,
				#					splitComplexTPrm := true, TRCDiag_VRCLR := true, globalUnrolling := 150, measureFinal := false)),
				#					16*[1..32])
                )
            ),
            8x16i := rec(
                wht := rec(
                    small := _defaultSizes(s->doSimdWht(s, SSE_8x16i, rec(verify := true, oddSizes := true, svct := true, stdTTensor := true, tsplPFA := false)), List([1..5], i->2^i)),
                    medium := _defaultSizes(s->doSimdWht(s, SSE_8x16i, rec(oddSizes := false, svct := true, stdTTensor := true, tsplPFA := false)), List([6..10], i->2^i))
                ),
                1d := rec(
                    dft_ic := rec(
                        medium := _defaultSizes(s->doSimdDft(s, SSE_8x16i, rec(tsplRader:=false, tsplBluestein:=false, tsplPFA:=false, oddSizes:=false, interleavedComplex := true, verify := true)), 64*[1..4]),
                        small := _defaultSizes(s->doSimdDft(s, SSE_8x16i, rec(verify:=true, interleavedComplex := true, stdTTensor := false, globalUnrolling:=10000)), [2..16])
                    ),
                    dft_sc := rec(
                        medium := _defaultSizes(s->doSimdDft(s, SSE_8x16i, rec(tsplRader:=false, tsplBluestein:=false, tsplPFA:=false, oddSizes:=false, interleavedComplex := false, verify := true)), 64*[1..4]),
                        small := _defaultSizes(s->doSimdDft(s, SSE_8x16i, rec(verify:=true, interleavedComplex := false, stdTTensor := false, globalUnrolling:=10000)), [2..16])
                    ),
                    rdft := _defaultSizes(s->doSimdSymDFT(TRDFT, s, SSE_8x16i, rec(verify := true)), 128*[1..4]),
                    dht := _defaultSizes(s->doSimdSymDFT(TDHT, s, SSE_8x16i, rec(verify :=true)), 128*[1..4]),
                    dct2 := _defaultSizes(s->doSimdSymDFT(TDCT2, s, SSE_8x16i, rec(verify := true, fracbits := 8)), [128]),
                    dct3 := _defaultSizes(s->doSimdSymDFT(TDCT3, s, SSE_8x16i, rec(verify := true, fracbits := 8)), [128]),
                    dct4 := _defaultSizes(s->doSimdSymDFT(TDCT4, s, SSE_8x16i, rec(verify := true)), 128*[1..4]),
                    dst2 := _defaultSizes(s->doSimdSymDFT(TDST2, s, SSE_8x16i, rec(verify := true)), 128*[1..4]),
                    dst3 := _defaultSizes(s->doSimdSymDFT(TDST3, s, SSE_8x16i, rec(verify := true)), 128*[1..4]),
                    dst4 := _defaultSizes(s->doSimdSymDFT(TDST4, s, SSE_8x16i, rec(verify := true)), 128*[1..4]),
                    mdct := _defaultSizes(s->doSimdSymDFT(TMDCT, s, SSE_8x16i, rec(verify :=true)), 128*[1..4]),
                    imdct := _defaultSizes(s->doSimdSymDFT(TMDCT, s, SSE_8x16i, rec(verify :=true)), 128*[1..4])
                ),
                2d := rec(
                    dft_ic := rec(
                        medium := _defaultSizes(s->doSimdMddft(s, SSE_8x16i, rec(interleavedComplex := true,
                                    oddSizes := false, svct := true, splitL := false, pushTag := true, flipIxA := false, stdTTensor := true, tsplPFA := false)),
                                    64*List([1..2], i->[i,i])),
                        small := _defaultSizes(s->doSimdMddft(s, SSE_8x16i, rec(verify:=true, interleavedComplex := true, globalUnrolling:=10000,
                                    tsplPFA := false, pushTag:= false, oddSizes := true, svct := true, splitL := false)),
                                    List([2..16], i->[i,i]))
                    ),
                    dft_sc := rec(
                        medium := _defaultSizes(s->doSimdMddft(s, SSE_8x16i, rec(interleavedComplex := false,
                                    oddSizes := false, svct := true, splitL := false, pushTag := true, flipIxA := false, stdTTensor := true, tsplPFA := false)),
                                    64*List([1..2], i->[i,i])),
                        small := _defaultSizes(s->doSimdMddft(s, SSE_8x16i, rec(verify:=true, interleavedComplex := false, globalUnrolling:=10000,
                                    tsplPFA := false, pushTag:= false, oddSizes := true, svct := true, splitL := false)),
                                    List([2..16], i->[i,i]))
                    ),
                    dct2 := _defaultSizes(s->doSimdSymMDDFT(DCT2, s, SSE_8x16i, rec(verify := true, fracbits := 10)), [8, 16]),
                    dct3 := _defaultSizes(s->doSimdSymMDDFT(DCT3, s, SSE_8x16i, rec(verify := true, fracbits := 10)), [8, 16]),
                    dct4 := _defaultSizes(s->doSimdSymMDDFT(DCT4, s, SSE_8x16i, rec(verify := true, fracbits := 10)), [8, 16]),
                    dst2 := _defaultSizes(s->doSimdSymMDDFT(DST2, s, SSE_8x16i, rec(verify := true, fracbits := 10)), [8, 16]),
                    dst3 := _defaultSizes(s->doSimdSymMDDFT(DST3, s, SSE_8x16i, rec(verify := true, fracbits := 10)), [8, 16]),
                    dst4 := _defaultSizes(s->doSimdSymMDDFT(DST4, s, SSE_8x16i, rec(verify := true, fracbits := 10)), [8, 16])
                )
            )
        );
    else
        return false;
    fi;
end;


#
# ---------- Constructors ----------
#

InstallMethod(Unipotent,
              "Unipotent element in simple adjoint type",
              [IsChevalleyAdj,IsList,IsList,IsInt],
              function(sys,coeffs,ordering,order)
              local object;

              if Filtered(coeffs,i->not i[2] in Integers)=[] then coeffs:=List(coeffs,i->[i[1],One(ring(sys))*i[2]]);
              elif Filtered(coeffs,i->not i[2] in ring(sys))<>[] then Error("The coefficients ar not in indicated ring."); fi;

              object:=Objectify(NewType(NewFamily("UnipotentFamily"),
                                        IsAttributeStoringRep and
                                        IsUnipotent and
                                        IsMultiplicativeElementWithInverse and
                                        IsMultiplicativeElementWithOne),
                           rec());

              SetchevalleyAdj(object,sys);
              SetOrdering(object,ordering);

              Setcoefficients(object,Canonic(sys,coeffs,ordering));
#             SetInverse(object,InverseOp(object)); This is set at the first call Inverse(obj);

              if order = -1 and Characteristic(sys)>0 then
                  SetOrder(object,OrderOp(object));
              elif order > -1 then
                  SetOrder(object,order);
              fi;

              SetName(object,Concatenation("<unipotent element for ",type(sys),String(rank(sys)),
                                      " in characteristic ",String(Characteristic(sys)),">"));
              return object;
end);

InstallMethod(Unipotent,
              "Unipotent element in simple adjoint type",
              [IsChevalleyAdj,IsList,IsList],
              function(sys,coeffs,ordering)

              return Unipotent(sys,coeffs,ordering,-2);
end);

InstallMethod(Unipotent,
              "Unipotent element in simple adjoint type",
              [IsChevalleyAdj,IsList,IsInt],
              function(sys,coeffs,order)
              local ordering,tmp,i,slot;

              tmp:=ShallowCopy(coeffs);
              Sort(tmp,function(a,b) return a[1]<b[1]; end);

              ordering:=[1..Length(positiveRoots(sys))];
              tmp:=List(tmp,i->ordering[i[1]]);

              for i in [1..Length(coeffs)] do
                  ordering[coeffs[i][1]]:=tmp[i];
              od;

              return Unipotent(sys,Filtered(coeffs,i->i[2]<>Zero(ring(sys))),ordering,order);
end);

InstallMethod(Unipotent,
              "Unipotent element in simple adjoint type",
              [IsChevalleyAdj,IsList],
              function(sys,coeffs)

              return Unipotent(sys,coeffs,-2);#-1);
end);

InstallMethod(Unipotent,
              "Unipotent element in simple adjoint type",
              [IsNilpotentChv],
              function(e)

              return Unipotent(chevalleyAdj(e),coefficients(e));
end);

#
# ---------- Canonic form of element ----------
#

InstallMethod(Canonic,
              "Canonic form of coefficients of unipotent element in given ordering",
              [IsChevalleyAdj,IsList,IsList],
              function(sys,coeffs,ordering)
              local lista,
              pr,Cijrs,
              flag,i,temp,suma,param,cc,k;

              lista:=List(coeffs,i->ShallowCopy(i));
              pr:=positiveRoots(sys);

              Cijrs:=C(sys);
              flag:=true;
              while flag do
                  flag:=false;
                  i:=Length(lista);
                  while 0 < i do
                            if lista[i][2] = Zero(ring(sys)) then
                                Remove(lista,i);
                                flag:=true;
                            elif 1 < i and lista[i][1] = lista[i-1][1] then
                                lista[i-1][2]:=lista[i-1][2]+lista[i][2];
                                Remove(lista,i);
                                flag:=true;
                            elif 1 < i and ordering[lista[i][1]] < ordering[lista[i-1][1]] then
                                temp:=lista[i]; lista[i]:=lista[i-1]; lista[i-1]:=temp;

                                #aici r si s sunt pe pozitia i-1 si repsectiv i
                                k:=1;
                                for cc in Cijrs[lista[i-1][1]][lista[i][1]] do
                                    suma:=cc[1]*pr[lista[i-1][1]]+cc[2]*pr[lista[i][1]];
                                    param:=cc[3]*((-1)^cc[1])*(lista[i-1][2]^cc[1])*(lista[i][2]^cc[2]);
                                    Add(lista,[Position(pr,suma),param],i+k);
                                    k:=k+1;
                                od;
                                flag:=true;
                            fi;
                            i:=i-1;
                  od;
              od;

              return Immutable(lista);
end);

#
# ---------- Arithmetic Operations ----------
#

InstallMethod(InverseMutable,
              "Inverse of unipotent element",
              [IsUnipotent],
              function(u)
              local i,lista,len,reverse_order;

              lista:=List(coefficients(u),i->StructuralCopy(i));
              len:=Length(lista);
              for i in [1..len] do
                  Add(lista,[lista[len-i+1][1],-lista[len-i+1][2]]);
                  Remove(lista,len-i+1);
              od;

              reverse_order:=function(o)
                  local maxim;
                  maxim:=Maximum(o);
                  return List(o,i->maxim-i+1);
              end;

              return Unipotent(chevalleyAdj(u),lista,reverse_order(Ordering(u)),-2);#0);
end);

InstallMethod(OneMutable,
              "Inverse of unipotent element",
              [IsUnipotent],
              function(u)

              return One(chevalleyAdj(u),[],0);
end);

InstallMethod(\*,
              "Multiplication for unipotent elements a*b with the ordering of b",
              [IsUnipotent,IsUnipotent],
              function(u1,u2)
              local sys1,sys2,
              generic,result,coeffs1,coeffs2,
              APR,avars,pr_len,
              i;

              sys1:=chevalleyAdj(u1);
              sys2:=chevalleyAdj(u2);
              if type(sys1)<>type(sys2) or
                 rank(sys1)<>rank(sys2) or
                 ring(sys1)<>ring(sys2) then Error("Not in the same family."); fi;

              return Unipotent(sys2,Concatenation(coefficients(u1),coefficients(u2)),Ordering(u2));
end);

InstallMethod(\*,
              "Action of unipotent element u on nilpotent e: u*e",
              [IsUnipotent,IsNilpotentChv],
              function(u,e)
              local result,
              sys1,sys2,Mrsi,pr,
              uas,r,s,i,pos;

              sys1:=chevalleyAdj(u);
              sys2:=chevalleyAdj(e);
              if type(sys1)<>type(sys2) or
                 rank(sys1)<>rank(sys2) or
                 ring(sys1)<>ring(sys2) then Error("Not in the same family.");
              fi;

              pr:=positiveRoots(sys1);
              Mrsi:=M(sys1);

              uas:=Reversed(coefficients(u));
              result:=List(coefficients(e),i->ShallowCopy(i));

              # for this see Carter pg.64
              for r in uas do
                  for s in result do
                      for i in [1..Length(Mrsi[r[1]][s[1]])] do
                          pos:=Position(pr,i*pr[r[1]]+pr[s[1]]);
                          result:=Concatenation(result,[[pos,Mrsi[r[1]][s[1]][i]*r[2]^i*s[2]]]);
                      od;
                  od;
              od;

              return NilpotentChv(sys2,result);
end);

InstallMethod(OrderOp,
              "Order of unipotent element",
              [IsUnipotent],
              function(u)
              local result,
              p,u_to_p,putere,i;

              if coefficients(u)=[] then return 1; fi;

              p:=Characteristic(chevalleyAdj(u));

              u_to_p:=u;
              for i in [1..p-1] do u_to_p:=u_to_p*u; od;

              result:=1;
              putere:=u_to_p;
              while coefficients(putere)<>[] do
                  putere:=putere*u_to_p;
                  result:=result+1;
              od;

              return p*result;
end);

InstallMethod(Comm,
              "Commutator for unipotent elements a^-1b^-1ab with the ordering of b",
              [IsUnipotent,IsUnipotent],
              function(u1,u2)
              local sys1,sys2;

              sys1:=chevalleyAdj(u1);
              sys2:=chevalleyAdj(u2);
              if type(sys1)<>type(sys2) or
                 rank(sys1)<>rank(sys2) or
                 ring(sys1)<>ring(sys2) then Error("Not in the same family."); fi;
              return Unipotent(sys2,
                                  Concatenation(coefficients(u1^-1),coefficients(u2^-1),
                                                     coefficients(u1),coefficients(u2)),
                                  Ordering(u2));
end);

InstallMethod(Conj,
              "Conjugation for unipotent elements b^-1ab with the ordering of b",
              [IsUnipotent,IsUnipotent],
              function(u1,u2)
              local sys1,sys2;

              sys1:=chevalleyAdj(u1);
              sys2:=chevalleyAdj(u2);
              if type(sys1)<>type(sys2) or
                 rank(sys1)<>rank(sys2) or
                 ring(sys1)<>ring(sys2) then Error("Not in the same family."); fi;
              return Unipotent(sys2,
                                  Concatenation(coefficients(u2^-1),coefficients(u1),coefficients(u2)),
                                  Ordering(u2));
end);

InstallMethod(Subword,
              "Subword of a unipotent element by list of roots",
              [IsUnipotent,IsList],
              function(u,l)
              local coeffs;
              coeffs:=Filtered(coefficients(u),i->i[1] in l);

              return Unipotent(chevalleyAdj(u),coeffs,Ordering(u));
end);

InstallMethod(InsertWord,
              "Shuffles a word into a unipotent element u according to the ordering of u",
              [IsUnipotent,IsList],
              function(u,word)
              local coeffs,
              sys,check,ord;

              sys:=chevalleyAdj(u);
              check:=ForAll(word,i-> i[2] in ring(sys));
              if not check then
                  Error("The word cannot be shuffled into u: not over the same ring!\n");
              fi;

              coeffs:=coefficients(u);
              coeffs:=Concatenation(coeffs,word);
              ord:=Ordering(u);
              Sort(coeffs,function(a,b) return ord[a[1]]<ord[b[1]]; end);
              coeffs:=Canonic(sys,coeffs,ord);

#              Setcoefficients(u,coeffs); Doesn't work this way

              return Unipotent(sys,coeffs,Ordering(u));
end);

#
# ---------- Lie Algebra Action ----------
#

InstallMethod(AduAlphat,
              "Adu_{alpha}(t) on the whole Lie algebra",
              [IsChevalleyAdj,IsPosInt,IsRingElement],
              function(sys,root,param)
              local result,
              pr,pr_len,roots,
              A_rs,M_rsi,
              L,CB,Rank,B,cochars,h,
              tmp,zero,unu,s,i,poz;

              pr:=positiveRoots(sys);
              pr_len:=Length(pr);
              roots:=Concatenation(pr,-pr);

              L:=lieAlgebra(sys);
              CB:=ChevalleyBasis(L);
              Rank:=rank(sys);
              B:=Basis(L);
              cochars:=Cocharacters(L);
              cochars:=List(cochars,i->Coefficients(B,i));

              zero:=Zero(ring(sys));
              unu:=One(ring(sys));

              A_rs:=A(sys);
              M_rsi:=M(sys);

              result:=[];
              # s in positive roots
              for s in [1..pr_len] do
                  tmp:=List([1..Dimension(L)],i->zero);

                  if s = root then
                      tmp[s]:=unu;
                  else
                      tmp[s]:=unu;
                      for i in [1..Length(M_rsi[root][s])] do
                          poz:=Position(roots,i*roots[root]+roots[s]);
                          tmp[poz]:=M_rsi[root][s][i]*param^i;
                      od;
                  fi;
                  Add(result,tmp);
              od;

              # s in negative roots
              for s in [1..pr_len]+pr_len do
                  tmp:=List([1..Dimension(L)],i->zero);

                  if s = root+pr_len then
                      # e_{-r}
                      tmp[s]:=unu;
                      # -t^2*e_{r}
                      tmp[root]:=-param^2;
                      # t*h_{r}
                      tmp:=tmp+param*cochars[root];
                  else
                      tmp[s]:=unu;
                      for i in [1..Length(M_rsi[root][s])] do
                          poz:=Position(roots,i*roots[root]+roots[s]);
                          tmp[poz]:=M_rsi[root][s][i]*param^i;
                      od;
                  fi;
                  Add(result,tmp);
              od;

              # h_s
              for s in [1..Rank] do
                  tmp:=List([1..Dimension(L)],i->zero);
                  # h_s
                  tmp:=tmp+cochars[s];
                  # -A_{sr}*t*e_r
                  tmp[root]:=-A_rs[s][root]*param;
                  Add(result,tmp);
              od;

              return result;#TransposedMat(result);
end);

InstallMethod(Adu,
              "Adu on the whole Lie algebra",
              [IsUnipotent],
              function(u)
              return Product(coefficients(u),i->AduAlphat(chevalleyAdj(u),i[1],i[2]));
end);

InstallMethod(AduJordanBlocksOp,
              "Computes Jordan Blocks of Adu using SageMath",
              [IsUnipotent],
              function(u)
              local sage_script,current_dir,tmp_file,result,
              ad,dim,i,j;

              sage_script:=Concatenation("~/sage/sage-4.7.2-linux-32bit-ubuntu_10.04_lts-i686-Linux/sage ",
                                      "~/workspace/ChevalleyAdj/sage/tmp/jordan.sage");
              current_dir:=Directory("~/workspace/ChevalleyAdj/sage/tmp/");
              tmp_file:=Filename(current_dir,"matrix.sage");
              result:=Filename(current_dir,"blocks.gap");

              #while IsPolynomialRing(ring(chevalleyAdj(u))) do u:=Descend(u,CoefficientsRing(ring(chevalleyAdj(u)))); od;
              if IsAlgebraicU(chevalleyAdj(u)) then u:=Descend(u,chevalleyAdj(chevalleyAdj(u))); fi;
              ad:=Adu(u);
              ad:=List(ad,i->List(i,j->Int(j)));
              PrintTo(tmp_file,"p=",String(Characteristic(chevalleyAdj(u))),"\nmat=");
              AppendTo(tmp_file,"[");
              dim:=Length(ad);
              for i in [1..dim] do
                  AppendTo(tmp_file,"[");
                  for j in [1..dim] do
                      AppendTo(tmp_file,String(ad[i][j]));
                          if j<>dim then AppendTo(tmp_file,","); fi;
                  od;
                  AppendTo(tmp_file,"]");
                  if i<>dim then AppendTo(tmp_file,",\n"); fi;
              od;
              AppendTo(tmp_file,"]");

              Exec(sage_script);

              result:=ReadAsFunction(result);
              result:=result();

              return result;
end);

InstallMethod(PositiveAdu,
              "Ad(u) restricted to Lie(U) where U is spanned by positive root groups",
              [IsUnipotent],
              function(u)
              local result,
              sys,
              baza,b,pr,pr_len;

              sys:=chevalleyAdj(u);
              pr:=positiveRoots(sys);
              pr_len:=Length(pr);
              baza:=List([1..pr_len],i->NilpotentChv(sys,[[i,1]]));

              result:=[];
              for b in baza do
                  result:=Concatenation(result,[u*b]);
              od;

              return List(result,i->LieAlgebraCoeffs(i){[1..pr_len]});
end);

InstallMethod(RestrictedAdu,
              "Ad(u) restricted to a subspace of Lie(U) spanned given (positive) roots",
              [IsUnipotent,IsList],
              function(u,roots)
              local sys,adu,pr,
              croots,check;

              if not IsInt(roots[1]) then TryNextMethod(); fi;

              sys:=chevalleyAdj(u);
              adu:=PositiveAdu(u);
              pr:=positiveRoots(sys);
              croots:=Filtered([1..Length(pr)],i->not i in roots);

              check:=Filtered(Concatenation(List(croots,i->adu[i]{roots})),j->j<>Zero(ring(sys)));
              if check<>[] then Error("Ade cannot be restricted like this."); fi;
              check:=Filtered(Concatenation(List(roots,i->adu[i]{croots})),j->j<>Zero(ring(sys)));
              if check<>[] then Error("Ade cannot be restricted like this."); fi;

              return List(roots,i->adu[i]{roots});
end);

InstallMethod(RestrictedAdu,
              "Ad(u) restricted to a subspace of Lie(U) spanned given (positive) roots",
              [IsUnipotent,IsList],
              1,
              function(u,vectors)
              local result,vectors_len,
              adu,img,
              CPR,cvar_names,cvars,cvectors,sysCPR,uu,
              tmp,generic,relations,relations_len,sol,i;

              vectors_len:=Length(vectors);

              cvar_names:=List([1..vectors_len],i->Concatenation("c_",String(i)));
              CPR:=PolynomialRing(ring(chevalleyAdj(u)),cvar_names);
              cvars:=IndeterminatesOfPolynomialRing(CPR);
              sysCPR:=ChevalleyAdj(chevalleyAdj(u),CPR);

              cvectors:=List(vectors,i->Ascend(i,sysCPR));
              uu:=Ascend(u,sysCPR);
              img:=List(cvectors,i->uu*i);

              result:=[];
              for i in [1..vectors_len] do
                  tmp:=ShallowCopy(cvars);
                  generic:=Sum(List([1..vectors_len],i->tmp[i]*cvectors[i]));
                  relations:=List(coefficients(img[i]-generic),i->i[2]);
                  relations:=Filtered(relations,i->i<>Zero(CPR));
              #Error("!");
                  tmp:=SolveRelations(relations,cvars,tmp,CPR);
                  if tmp[2]<>[] then
                      #Error("!");
                      Print("RestrictedAdu: did not solve all relations! ",relations,"\n");
                  fi;
                  tmp:=List(tmp[1],i->Value(i,cvars,cvars));
                  #tmp:=Sum(List([1..vectors_len],i->tmp[i]*vectors[i])); # I want the matrix
                  Add(result,tmp);
              od;

              return result;
end);

InstallMethod(PositiveFixedspace,
              "Ker of PositiveAdu",
              [IsUnipotent],
              function(u)
              local result,
              adu,sys,unu,pr,pr_len,ns;

              sys:=chevalleyAdj(u);
              pr:=positiveRoots(sys);
              pr_len:=Length(pr);

              adu:=PositiveAdu(u);
              unu:=DiagonalMat(List([1..pr_len],i->One(ring(sys))));
              ns:=NullspaceMat(adu-unu);

              result:=List(ns,v->List([1..pr_len],j->[j,v[j]]));
              result:=List(result,i->NilpotentChv(sys,i));

              return result;
end);

InstallMethod(RestrictedFixedspace,
              "Ker of RestrictedAdu",
              [IsUnipotent,IsList],
              function(u,roots)
              local result,
              radu,sys,unu,roots_len,ns;

              sys:=chevalleyAdj(u);
              roots_len:=Length(roots);

              radu:=RestrictedAdu(u,roots);
              unu:=DiagonalMat(List([1..roots_len],i->One(ring(sys))));
              ns:=NullspaceMat(radu-unu);

              result:=List(ns,v->List([1..roots_len],j->[roots[j],v[j]]));
              result:=List(result,i->NilpotentChv(sys,i));

              return result;
end);


# Copyright (c) 2018-2021, Carnegie Mellon University
# See LICENSE for details

Class(JoinDirectSums, RuleSet);
RewriteRules(JoinDirectSums, rec(
    combine := ARule(Compose, [@(1, DelayedDirectSum), @(2, DelayedDirectSum,
        e -> let(c1:=@(1).val.children(), c2:=e.children(), Length(c1) = Length(c2) and ForAll([1..Length(c1)], i->c1[i].dims()[2] = c2[i].dims()[1]))) ],
        e -> let(c1:=@(1).val.children(), c2:=@(2).val.children(), [ DelayedDirectSum(List([1..Length(c1)], i -> c1[i] * c2[i])) ])
    )
));

Class(TerminateDirectSums, RuleSet);
RewriteRules(TerminateDirectSums, rec(
    terminate := Rule(@(1, DelayedDirectSum), e-> DirectSum(@(1).val.children()).sums())
));


# Copyright (c) 2018-2021, Carnegie Mellon University
# See LICENSE for details

# ==========================================================================
# Sparse
# ==========================================================================
Class(Sparse, BaseMat, rec(
    new := meth(self, L)
        Constraint(IsList(L));
	if not ForAll(L, t -> IsList(t) and Length(t)=3 and
		              IsInt(t[1]) and IsInt(t[2])) then
	    Error("<L> must be a list of triples [i, j, a_(i,j)]");
	fi;
        return SPL(WithBases( self, rec( element := L,
				       dimensions := [ Maximum(List(L, t->t[1])),
				                       Maximum(List(L, t->t[2])) ]
                       )));
    end,
    #-----------------------------------------------------------------------
    dims := self >> [ Maximum(List(self.element, t->t[1])),
	              Maximum(List(self.element, t->t[2])) ],
    #-----------------------------------------------------------------------
    isPermutation := False,
    #-----------------------------------------------------------------------
    isReal := self >> ForAll(self.element, t -> IsRealNumber(t[3])),
    #-----------------------------------------------------------------------
    toAMat := self >> AMatMat(List(MatSparseSPL(self), r -> List(r, EvalScalar))),
    #-----------------------------------------------------------------------
    transpose := meth(self)  # we use CopyFields to copy all fields of self
        local L, t;
	L := [ ];
	for t in self.element do
	    Add(L, [t[2], t[1], t[3]]);
	od;
	return CopyFields(self, rec(element := L,
		                 dimensions := Reversed(self.dims())));
    end,
    conjTranspose := meth(self)  # we use CopyFields to copy all fields of self
        local L, t;
	L := [ ];
	for t in self.element do
	    Add(L, [t[2], t[1], Global.Conjugate(t[3])]);
	od;
	return CopyFields(self, rec(element := L,
		                 dimensions := Reversed(self.dims())));
    end,
    #-----------------------------------------------------------------------
    arithmeticCost := meth(self, costMul, costAddMul)
        local cost, row, elms;
	cost := costMul(0) - costMul(0); # will work even when costMul(0) <> 0
	for row in [1..self.dimensions[1]] do
	    elms := Filtered(self.element, e -> e[1]=row);
	    if Length(elms) > 0 then
		cost := cost + costMul(elms[1][3])
		             + Sum(elms{[2..Length(elms)]}, e -> costAddMul(e[3]));
	    fi;
	od;
	return cost;
    end
));


# Copyright (c) 2018-2021, Carnegie Mellon University
# See LICENSE for details

#F RuleTreeOps.\=( <ruletree1>, <ruletree2> )
#F    returns true if <ruletree1> and <ruletree2> are equal, i.e., they
#F    represent the same breakdown strategy.  Otherwise, false is returned.
#F

RuleTreeOps := CantCopy(OperationsRecord("RuleTreeOps"));

IsRuleTree := T -> IsRec(T) and IsBound(T.isRuleTree) and T.isRuleTree;

#F Ruletrees
#F =========
#F
#F A ruletree is a record with the following mandatory fields:
#F
#F   isRuleTree  = "true"              # identifies ruletrees
#F   operations  = RuleTreeOps         # operations record
#F   node        = <spl>               # the spl expanded
#F   transposed  = false/true          # indicates whether <rule> or
#F                                       transpose - <rule> - transpose
#F   rule        = <rule>              # the rule chosen
#F   children    = <list>              # the children, a list of
#F                                       ruletrees/non-terminal spls
#F
#F Note that children can be a list of ruletrees as well as non-terminal spls.
#F The field children is empty iff the ruletree is a leaf.
#F In general, the .node is a non-terminal spl that is expanded by .rule.
#F
#F The following fields are optional:
#F   splOptions = [ <option1>, <option2>, .. ]
#F     valid <options> : "unrolled", "pcl"
#F

Class(RuleTreeClass, rec(
    spl  := self >> SPLRuleTree(self),
    measure := self >> SPLRuleTree(self).measure(),
    shortPrint := false,
    dims := self >> self.node.dims(),
    dmn  := self >> self.node.dmn(),
    rng  := self >> self.node.rng(),

    print := meth(self, i, is)
        local ch;

        if Length(self.children) = 0 then
           # print leaves in one line
            Print(self.rule, "( ");
            When (self.transposed, Print("\"T\","));
            self.node.print(i+is, is);
            When (IsBound(self.splOptions),
                Print(", ", self.splOptions, " )"),
                Print(" )"));
        else
            # print children on separate lines
            Print(self.rule, "( ",
                When(self.transposed, "\"T\", ", ""),
                When(not self.shortPrint, self.node.print(i+is, is), ""), ",\n", Blanks(i+is)
            );
            self.children[1].print(i+is, is);
            for ch in Drop(self.children, 1) do
                Print(",\n", Blanks(i+is));
                ch.print(i+is, is);
            od;

            When (IsBound(self.splOptions),
                Print("\n", Blanks(i), ", ", self.splOptions, " )"),
                Print(" )"));
        fi;
    end,

    # new with tSPL and opts.baseHash:
    # we cannot blindly transpose everything, as vector
    # permutations that are automatically generated
    # may not support transposition
    transpose := self >>
        When((not self.node.transposeSymmetric()) and self.node.isSymmetric(),
            self,
            RuleTree(
                self.rule,
                When(self.transposed, "", "T"),
                self.node.transpose(),
                List(self.children, x->x.transpose())
        )
    ),
    # RuleTreeRewriting enabled by default
    # one can disable it with "EnableRuleTreeRewriting(false)"
    rChildren := (self) >> [ self.node, self.children ],
    rSetChild := rSetChildFields("node", "children"),
    from_rChildren := (self, rch) >>
        RuleTree(self.rule, When(self.transposed, "T", ""), rch[1], rch[2])
));

RuleTreeOps.\= := function( T1, T2 )
    return
        IsRuleTree(T1) and IsRuleTree(T2) and
        T1.rule = T2.rule and
        T1.transposed = T2.transposed and
        IsIdenticalSPL(T1.node, T2.node) and
        T1.children = T2.children and
        (   (   IsBound(T1.splOptions) and
		        IsBound(T2.splOptions) and
                T1.splOptions = T2.splOptions
			) or
            (   not IsBound(T1.splOptions) and
			    not IsBound(T2.splOptions)
			)
        );
end;

#F RuleTreeNC( <rule>, <non-terminal spl>, <children> )
#F   returns the corresponding ruletree without any checking.

RuleTreeNC := function ( R, S, C )
    return WithBases(RuleTreeClass,
        rec(
            isRuleTree := true,
            operations := RuleTreeOps,
            node       := S,
            transposed := false,
            rule       := R,
            children   := C
		)
    );
end;

#F RuleTree(
#F   <rule>,
#F   [ "T" ,]
#F   <non-terminal spl>
#F   [, <children> ]
#F   [, <list-of-sploptions> ]
#F )
#F
#F   Creates a ruletree for <non-terminal spl>
#F   using <rule> with <children>. The <children> can be non-terminal spls
#F   or ruletrees. It is checked whether <children> can be derived
#F   from <non-terminal spl> using <rule>.
#F   If the string "T" is supplied, then instead of <rule>, the
#F   sequence transpose - <rule> - transpose is denoted. In this case
#F   <non-terminal spl> and <children> are also transposed. This construction
#F   avoids unnecessary recoding of rules that are just transposes of
#F   other rules. In this case, <rule> must have the field
#F   .forTransposition set to true.
#F
#F   The argument <list-of-sploptions> provides options used for spl compilation.
#F   See top of this file for valid options; examples are "unrolled" and "pcl".
#F   <children> might be empty (default) in which case the ruletree
#F   returned is a leaf.
#F
RuleTree := function ( arg )
    local a, R, t, S, C, u, S1, C1, RT;
    S := false;
    t := false;
    u := [ ];
    C := [ ];
    R := false;

    for a in arg do
        if IsString(a) and a = "T" then
	        t := true;
        elif IsList(a) and ForAll(a, IsString) then
	        u := a;
        elif IsSPL(a) then
			if S=false then
				S := a;
			else
				Add(C, a);
			fi;
		elif IsRuleTree(a) then
			Add(C, a);
		elif IsList(a) then
			Append(C, a);
		elif IsRule(a) and R=false then
			R := a;
		else Error(
			"usage: \n",
			"  RuleTree( \n",
			"    <rule>, [ \"T\" ,] <non-terminal spl> \n",
			"    [, <children> ] [, <list-of-sploptions> ] )");

		fi;
    od;

    Constraint(IsRule(R));
    Constraint(IsSPL(S));

    if not IsList(u) and ForAll(u, IsString) then
        Error("<u> must be a list of spl options");
    elif not (IsList(C) and ForAll(C, c -> IsSPL(c) or IsRuleTree(c))) then
        Error("<C> must be a list of ruletrees or non-terminal spls");
    fi;

    RT := RuleTreeNC(R, S, C);
    if u <> [ ]
	    then RT.splOptions := u;
	fi;
    return RT;
end;

#F ExtractChildrenSPL ( <spl> )
#F   returns the list of non-terminal spls contained in <spl>.
#F   The order is left first - depth first (non-terminals are
#F   always leaves.
#F
ExtractChildrenSPL := S -> Collect(S, @.cond(IsNonTerminal));

#F CopyRuleTree( <ruletree> )
#F   returns a copy of <ruletree>. All fields apart from .children
#F   are copied by reference.
#F
CopyRuleTree := function( orig )
    local new;
    new := ShallowCopy(orig);
    if IsRuleTree(new) then
	    new.children := List(orig.children, CopyRuleTree);
	fi;
    return new;
end;

_matchChildren := (C1, C2) ->
    Length(C1)=Length(C2) and
    ForAll([1..Length(C1)],
       j -> IsHashIdenticalSPL(HashAsSPL(C1[j]), HashAsSPL(C2[j])) or
            (ObjId(C1[j])=InfoNt and ObjId(C2[j])=InfoNt));

#F ApplyRuleTreeSPL( <ruletree>, <spl>, <opts> )
#F   creates a ruletree for <spl> using rules from the <ruletree>
#F   (if possible)
#F   Uses: opts.breakdownRules (for backwards compatibility of R.switch only)
ApplyRuleTreeSPL := function ( rtree, spl, opts )
    local i, R, C, C1, CC, children, L;
    Constraint(IsRuleTree(rtree));
    Constraint(IsSPL(spl));

    if rtree.transposed then
        return ApplyRuleTreeSPL(rtree.transpose(), spl.transpose(), opts).transpose();
    fi;

    R := rtree.rule;
    children:=[];

    # if <spl> was not compatible with <ruletree>.node to start with
    # then at some point there will be a split in <ruletree> that is not
    # applicable to the generated ruletree.
    if not IsApplicableRule( R, spl, opts.breakdownRules ) then
        Error("<rtree> can not be expanded from <spl>");
    fi;

    # determine the right set of children from allChildren to be used
    # in expansion
    C1 := _allChildren(R, rtree.node, opts);
    C := List([1..Length(rtree.children)], c -> rtree.children[c].node);
    i := PositionProperty([1..Length(C1)], j -> _matchChildren(C1[j], C));

    # DEBUG HACK: AppendTo("applytree", R, "\n", C, "\n", C1, "\n---------\n");
    if i = false then
        Error("<ruletree> can not be expanded from <spl>, could not find matching children");
	fi;

    # determine all children for <spl> and pick the right set
    L :=  _allChildren(R, spl, opts);
    if i > Length(L) then
        Error("<ruletree> can not be expanded from <spl>");
    else
        C1 := L[i];
    fi;

    # recurse and return
    children := List([1..Length(rtree.children)], i -> ApplyRuleTreeSPL(rtree.children[i], C1[i], opts));

    return RuleTreeNC(R, spl, children);
end;

#F Printing RuleTrees
#F ------------------
#F

#F RuleTreeOps.Print( <ruletree> [, <indent> , <indentStep> ] )
#F   prints <ruletree> with <indent>. Further indenting is done
#F   in steps of size <indentStep>. The default is
#F   indent = 0, indentStep = 2.
#F
RuleTreeOps.Print := function ( arg )
    local T, indent, indentStep;
	if Length(arg) = 1 then
        T          := arg[1];
        indent     := 0;
        indentStep := 2;
    elif Length(arg) = 3 then
        T          := arg[1];
        indent     := arg[2];
        indentStep := arg[3];
    else
        Error("usage: RuleTreeOps.Print( <ruletree> [, <indent> , <indentStep> ] )");
    fi;
    Constraint(IsRuleTree(T));
    Constraint(IsPosInt0(indent));
    Constraint(IsPosInt0(indentStep));
    T.print(indent, indentStep);
end;

PrintRuleTreeCustom := function (T, i, is, spl_print, ruletree_print)
    local ch;
    Constraint(IsRuleTree(T) or IsSPL(T));
    Constraint(IsPosInt0(i));
    Constraint(IsPosInt0(is));

    if IsSPL(T) then
        spl_print(T);
    else # T is a ruletree
        Print(Blanks(i), ruletree_print(T), "\n");
        for ch in T.children do
            PrintRuleTreeCustom(ch, i+is, is, spl_print, ruletree_print);
        od;
    fi;
end;

#F PrintNodesRuleTree(
#F   <ruletree/non-terminal spl> [, <indent> , <indentStep> ]
#F )
#F   pretty prints <ruletree> by displaying only the nodes (non-terminals)
#F   and its parameters. The tree structure is expressed by indentation.
#F   Non-terminals are being marked by (nt).
#F

PrintNodesRuleTree := function ( arg )
    local T, i, is;
    if Length(arg) = 1 then
        T := arg[1];
		i := 0;
		is := 2;
    elif Length(arg) = 3 then
        T := arg[1];
		i := arg[2];
		is := arg[3];
    else
        Error("usage: PrintNodesRuleTree( <ruletree> [, <indent> , <indentStep> ] )");
    fi;
    PrintRuleTreeCustom(T, i, is,
        T -> Print(T, " (nt)"),
        T -> Print(T.node));
end;

#F PrintRulesRuleTree(
#F   <ruletree/non-terminal spl> [, <indent> , <indentStep> ]
#F )
#F   pretty prints <ruletree> by displaying only the rules and
#F   the node sizes in parentheses. Transposed rules are marked
#F   by ^T. The tree structure is expressed by indentation.
#F   Non-terminals are denoted by nt.
#F
PrintRulesRuleTree := function(arg)
    local T, i, is;
    if Length(arg) = 1 then
        T := arg[1]; i := 0; is := 2;
    elif Length(arg) = 3 then
        T := arg[1]; i := arg[2]; is := arg[3];
    else
        Error("usage: PrintRulesRuleTree( <ruletree> [, <indent> , <indentStep> ] )");
    fi;
    PrintRuleTreeCustom(T, i, is,
    T -> Print("nt (", T.dimensions[1], ")"),
    T -> Print(T.rule, When(T.transposed," ^ T",""), "(", T.node.dimensions[1], ")"));
end;

#F PrettyPrintRuleTree(
#F   <ruletree/non-terminal spl> [, <whiteIndent> [, <linedIndent> ] ]
#F )
#F   pretty prints <ruletree> by displaying only the nodes (non-terminals),
#F   their parameters, and the rules. The tree structure is expressed by
#F   indentation.  Non-terminals are being marked by (nt).
#F

PrettyPrintRuleTree := function ( arg )
    local T, chIndent, whiteIndent, linedIndent, oldLinedIndent, newline, params, i;

    # new line plus indents
    newline := function ( )
    local i;

    Print("\n");
    for i in [1..whiteIndent] do
        Print(" ");
    od;
    if not linedIndent = "" then
        Print( linedIndent, "--" );
    fi;
    end;

    # decode arg
    if Length(arg) = 1 then
        T           := arg[1];
        whiteIndent := 0;
        linedIndent := "";
    elif Length(arg) = 2 then
        T           := arg[1];
        whiteIndent := arg[2];
        linedIndent := "";
    elif Length(arg) = 3 then
        T           := arg[1];
        whiteIndent := arg[2];
        linedIndent := arg[3];
    else
        Error("usage: PrintNodesRuleTree( <ruletree> [, <indent> , <indentStep> ] )");
    fi;

    # check arguments
    if not ( IsRuleTree(T) or IsSPL(T) and T.type = "nonTerminal" ) then
        Error("<T> must be a ruletree or a non-terminal spl");
    fi;
    if not IsInt(whiteIndent) and whiteIndent >= 0 then
        Error("<whiteIndent> must be pos-int");
    fi;
    if not IsString(linedIndent) then
        Error("<linedIndent> must be a string");
    fi;

    chIndent := whiteIndent+Length(linedIndent)+4;
    # spl case
    if IsSPL(T) then
        T.print(chIndent, 2); Print(" (nt)");
        return;
    fi;

    T.node.print(chIndent, 2);
    Print("     {" );
    Print(T.rule);
    if T.transposed then
        Print(" ^ T");
    fi;
    if IsBound(T.splOptions) then
        Print(", ", T.splOptions);
    fi;
    Print( "}" );

    if Length(T.children) > 0 then
        if linedIndent = "" then
            oldLinedIndent := linedIndent;
        else
            oldLinedIndent := Concatenation( linedIndent, "  " );
        fi;
        linedIndent := Concatenation( oldLinedIndent, " |" );
        for i in [1..Length(T.children)-1] do
            newline();
            PrettyPrintRuleTree(T.children[i], whiteIndent, linedIndent);
        od;
        linedIndent := Concatenation( oldLinedIndent, " \`" );
        newline();
        linedIndent := Concatenation( oldLinedIndent, "  " );
        PrettyPrintRuleTree(T.children[Length(T.children)],
                            whiteIndent, linedIndent);
        linedIndent := oldLinedIndent;
    fi;
    if whiteIndent = 0 and linedIndent = "" then
        newline();
    fi;
end;

#F Converting RuleTrees
#F --------------------
#F

#F AMatRuleTree( <ruletree> ) - returns an amat corresponding to ruletree.
#F
AMatRuleTree := T -> AMatSPL(SPLRuleTree(T));

#F MatRuleTree( <ruletree> ) -  returns the matrix corresponding to ruletree.
#F
MatRuleTree := T -> MatSPL(SPLRuleTree(T));

#F Creating Random RuleTrees
#F -------------------------
#F

Declare(SemiRandomRuleTree, _SemiRandomRuleTree);

#F RandomRuleTree( <spl> , <opts>)
#F    returns a random rule tree for <spl>
#F    or 'false' if no breakdown rule combination leads to a
#F    fully expanded ruletree.
#F
#F   Uses: opts.baseHashes
#F         opts.breakdownRules
#F   Optional:  (see Doc(_allChildren) ) for usage
#F         opts.restrictSplit
#F         opts.restrictSplitSize
#F
RandomRuleTree := (spl, opts) -> _SemiRandomRuleTree(spl, false, s->false, opts);

RandomRuleTreeCutoff := (spl, cutoff_func, opts) -> _SemiRandomRuleTree(spl, false, cutoff_func, opts);

RandomRuleTreeDP := (t, opts) -> let(res := spiral.search.DP(t, rec(measureFunction := (R,O)->Random([1..10000]), verbosity := 0), opts), When(res = [], false, res[1].ruletree));

#F SemiRandomRuleTree( <spl>, <top_rule>, opts )
#F    returns a random rule tree for <spl> with fixed top-level rule
#F    or 'false' if no breakdown rule combination leads to a
#F    fully expanded ruletree.
#F
#F   Uses: opts.baseHashes
#F         opts.breakdownRules
#F   Optional:  (see Doc(_allChildren) ) for usage
#F         opts.restrictSplit
#F         opts.restrictSplitSize
#F
SemiRandomRuleTree := (spl, top_rule, opts) -> _SemiRandomRuleTree(spl, top_rule, s->false, opts);

_SemiRandomRuleTree := function(spl, top_rule, cutoff_func, opts)
    local ch, i, R, rules, rule, rand, candidates, ch, children, ch_candidates;

    if not (IsSPL(spl)) then
        Error("usage: SemiRandomRuleTree( <spl>, <top_rule>, <cutoff>, <opts> )");
    elif cutoff_func(spl) then
	    return spl;
    elif MultiHashLookup(opts.baseHashes, spl) <> false then
        return MultiHashLookup(opts.baseHashes, spl)[1].ruletree;
    else
        if top_rule <> false then
            rules := [top_rule];
        else
            rules  := AllApplicableRulesDirect(spl, opts.breakdownRules);
            if rules = [ ] then
                return false;
            fi;
        fi;

        candidates := Set([1..(Length(rules))]);
        while candidates <> [] do
            rand := RandomList(candidates);
            RemoveSet(candidates, rand);

            if rand <= Length(rules) then
                rule := rules[rand];
                ch_candidates := _allChildren(rule, spl,opts);
                ch_candidates := Permuted(ch_candidates, Random(SymmetricGroup(Length(ch_candidates))));
                for children in ch_candidates do
                    children := List( children, s -> _SemiRandomRuleTree(s, false, cutoff_func, opts) );
                    if not ForAny(children, c -> IsBool(c) and c=false) then
                        return RuleTreeNC( rule, spl, children );
                    fi;
                od;
            fi;
        od;
        return false;
   fi;
end;

#F Creating all RuleTrees
#F ----------------------
#F
#ExpandSPLRules := function( S, ruleset )
ExpandSPLRules := function( arg )
    local i, L, R, S, ruleset, opts;
    S := arg[1];
    ruleset := arg[2];
    Constraint(IsSPL(S));

    if IsBound(arg[3]) then
	    opts := arg[3];
    else
	    opts := rec();
	fi;

    L  := [ ];
    for R in AllApplicableRulesDirect(S, ruleset) do
        Append(L, List(_allChildren(R,S,opts), c -> RuleTreeNC(R, S, c)));
    od;

    return L;
end;

#F ExpandSPL( <spl>, <opts> )
#F   performs one expansion of <spl> in all possible ways,
#F   and returns the list of the derived ruletrees.
#F   If there are no breakdown rules for <spl> (i.e., not a non-terminal)
#F   then the default base cases rule @_Base is used, which means that
#F   the recursion will continue on its children
#F
#F   Uses: opts.baseHashes
#F         opts.breakdownRules
#F
ExpandSPL := (S, opts) -> ExpandSPLRules(S, opts.breakdownRules, opts);

_AllRuleTrees := function ( S, cutoff, memohash, opts )
    local L, L1, T, Lc, i, cs, T1, lkup;

    # check cutoff, baseHashes, and our memoization hash
    if cutoff(S) then
		return [S];
	fi;
    lkup := MultiHashLookup(opts.baseHashes, S);
    if lkup <> false then
		return [lkup[1].ruletree];
	fi;

    lkup := HashLookup(memohash, S);
    if lkup <> false
		then return lkup;
	fi;

    L := ExpandSPL(S, opts);
    if ForAll(L, r -> r.children = [ ]) then
		HashAdd(memohash, S, L);
		return L;
    fi;

    L1 := [ ];
    for T in L do
        Lc := [ ];
		for i in [1..Length(T.children)] do
            Lc[i] := _AllRuleTrees(T.children[i], cutoff, memohash, opts);
		od;
		for cs in Cartesian(Lc) do
            T1 := ShallowCopy(T);
			T1.children := cs;
			Add(L1, T1);
		od;
    od;

    HashAdd(memohash, S, L1);
    return L1;
end;

#F AllRuleTrees( <spl>, <opts> )
#F   expands <spl> in all possible ways and returns the obtained list
#F   of fully expanded ruletrees (ruletrees containing no non-terminals).
#F   Note that subtrees are "by reference", i.e., modifying one subtree
#F   alters the same subtree in some other expansions.
#F
#F   Uses: opts.baseHashes
#F         opts.breakdownRules
#F   Optional:  (see Doc(_allChildren) ) for usage
#F         opts.restrictSplit
#F         opts.restrictSplitSize
#F
AllRuleTrees := (S, opts) -> _AllRuleTrees(S, x->false, HashTableSPL(), opts);

#F AllRuleTreesCutoff( <spl>, <cutoff_func>, <opts> )
#F   expands <spl> in all possible ways and returns the obtained list
#F   of fully expanded ruletrees (ruletrees containing no non-terminals).
#F   Note that subtrees are "by reference", i.e., modifying one subtree
#F   alters the same subtree in some other expansions.
#F   <cutoff_func> is of the form e -> e = <cutoff>
#F
#F   Uses: opts.baseHashes
#F         opts.breakdownRules
#F
AllRuleTreesCutoff := (S, cutoff_func, opts) -> _AllRuleTrees(S, cutoff_func, HashTableSPL(), opts);

_NofRuleTrees := function ( S, cutoff, memohash, opts, level, trace )
    local p, Cs, n, lkup, indentstr, i;
    Constraint(IsSPL(S));
    Constraint(IsRec(opts));

    if trace then
        indentstr := "";
        for i in [2..level] do
            indentstr := Concat(indentstr, ".  ");
        od;
        indentstr := Concat(indentstr, String(level), " ");
        Print(indentstr, ">> _NofRuleTrees(", S, ")\n");
    fi;

    # check cutoff, baseHashes, and our memoization hash
    if cutoff(S) then
        if trace then
            Print(indentstr, "<< (cutoff) 1\n");
        fi;
        return 1;
    fi;
    if MultiHashLookup(opts.baseHashes, S) <> false then
        if trace then
            Print(indentstr, "<< (MultiHashLookup) 1\n");
        fi;
        return 1;
    fi;
    lkup := HashLookup(memohash, S);
    if lkup <> false then
        if trace then
            Print(indentstr, "<< hashed: ", lkup, "\n");
        fi;
        return lkup;
    fi;

    # first apply rules as they are
    Cs := List(AllApplicableRulesDirect(S, opts.breakdownRules), r -> _allChildren(r,S, opts));
	if trace then
        Print(indentstr, "   Cs: ", Cs, "\n");
    fi;

    if Cs = [ [ ] ] then
        n := 1;
    else
        n := Sum(Cs, c -> Sum(c, l -> Product(l, x -> _NofRuleTrees(x, cutoff, memohash, opts, level+1, trace))));
    fi;

    HashAdd(memohash, S, n);

    if trace then
        Print(indentstr, "<< (", S, ") ", n, "\n");
    fi;

    return n;
end;

#F NofRuleTrees ( <spl>, <opts> )
#F   returns the number of ruletrees for <spl>.
#F
#F   Uses: opts.baseHashes
#F         opts.breakdownRules
#F   Optional:  (see Doc(_allChildren) ) for usage
#F         opts.restrictSplit
#F         opts.restrictSplitSize
#F
#F   if opts.verbosity > 3 prints trace info

NofRuleTrees := (spl, opts) -> _NofRuleTrees(spl, x->false, HashTableSPL(), opts, 1, IsBound(opts.verbosity) and (opts.verbosity > 3));

NofRuleTreesCutoff := (spl, cutoff_func, opts) -> _NofRuleTrees(spl, cutoff_func, HashTableSPL(), opts, 1, IsBound(opts.verbosity) and (opts.verbosity > 3));

#F RulesInRuleTree( <ruletree> )
#F   return the set of rules contained in <ruletree>.
#F
RulesInRuleTree := function ( T )
    local C;
    Constraint(IsRuleTree(T));

    # base case
    if T.children = [ ] then
	    return Set([T.rule]);
	fi;

    # recurse with ruletree children
    C := Filtered(T.children, IsRuleTree);
    C := Set(Concatenation(List(C, RulesInRuleTree)));
    AddSet(C, T.rule);
    return C;
end;

#F RulesInRuleTreeAll( <ruletree> )
#F   return the histogram of all rules contained in <ruletree> as set
#F   of pairs [rule, number].
#F
RulesInRuleTreeAll := function ( T )
    local C, C1, c, i;
    Constraint(IsRuleTree(T));

    # base case
    if T.children = [ ] then
	    return Set([ [T.rule, 1] ]);
	fi;

    # recurse with ruletree children
    C := Filtered(T.children, IsRuleTree);
    C := Concatenation(List(C, RulesInRuleTreeAll));

    # merge
    C1 := [ ];
    for c in C do
        i := PositionProperty(C1, p -> c[1] = p[1]);
        if i = false then
            AddSet(C1, c);
        else
            C1[i][2] := C1[i][2] + c[2];
        fi;
    od;

    i := PositionProperty(C1, p -> p[1] = T.rule);
    if i = false then
        AddSet(C1, [T.rule, 1]);
    else
        C1[i][2] := C1[i][2] + 1;
    fi;
        return C1;
end;

ApplyRuleTreeStep := function ( rt, recurse )
    local nt, C, Nonterms, S, c;
    if IsNonTerminal(rt) or IsSPL(rt) then
	    return rt;
	fi;

    Constraint(IsRuleTree(rt));

    C := List(rt.children, recurse);
    nt := rt.node;
    Nonterms := List(rt.children, c -> When(IsRuleTree(c), c.node, c));

    if rt.transposed then
        nt := TransposedSPL(nt);
        C := List(C, TransposedSPL);
        Nonterms := List(Nonterms, TransposedSPL);
    fi;

    S := _apply(rt.rule, nt, C, Nonterms);
    if rt.transposed then
	    S := TransposedSPL(S);
	fi;

    trace_log.addTreeExpansion(rt.rule,nt,S,rt.children,CopyFields(rt.node, rec(params := C)), var);

    S.root := rt.node;
    return S;
end;

SPLRuleTreeStep := rt -> ApplyRuleTreeStep(rt, c -> RecursStep(
    When(IsRuleTree(c), RTWrap(c), c)));

_SPLRuleTree := rt -> ApplyRuleTreeStep(rt, _SPLRuleTree);

SPLRuleTree := function(rt)
    local spl, t, tag;
    trace_log.beginStage("tSPL","SPL", rt.node);
    spl := ApplyRuleTreeStep(rt, _SPLRuleTree);

    # tags may inject container objects
    t := rt.node;
    if IsBound(t.getTags) then
        for tag in Reversed(t.getTags()) do
            if IsBound(tag.container)
			    then spl := tag.container(spl);
			fi;
        od;
    fi;
    trace_log.endStage("tSPL","SPL", spl);
    return spl;
end;

# forward declaration for function that does all the work.
Declare(_ruleTreeN);

#F RandomRuleTrees( <spl>, <num>, <opts>)
#F    returns <num> random rule trees for <spl>
#F    uniformly distributed amongst the potential trees
#F    or 'false' if no breakdown rule combination leads to a
#F    fully expanded ruletree.
#F
#F   Uses: opts.baseHashes
#F         opts.breakdownRules
#F   Optional:  (see Doc(_allChildren) ) for usage
#F
RandomRuleTrees := function(spl, num, opts)
    local n, rand, memohash;

    memohash := HashTableSPL();

    n := _NofRuleTrees(spl, s -> false, memohash, opts, 1, false);

    if num >= n then
        rand := [1..n];
    else
        rand := [];
        while Length(rand) <> num do
            rand := Set(Concat(rand, [RandomList([1..n])]));
        od;
    fi;

    return List(rand, e -> _ruleTreeN(spl, e-1, memohash, opts));
end;

# helper functions/arrays for _ruleTreeN and _ruleTree1

_getNumTrees := (s, memohash, opts) -> let(
    n := HashLookup(memohash, s),
    When(n = false,
        _NofRuleTrees(s, e -> false, memohash, opts, 1, false),
        n
    )
);
#F RuleTreeN(<spl>, <num>, <opts>)
#F   returns ruletree of index <num>
#F
#F   Uses: opts.basHashes, opts.breakdownRules.
#F
RuleTreeN := function(spl, num, opts)
    local memohash, n, r;

    memohash := HashTableSPL();
    n := _NofRuleTrees(spl, s -> false, memohash, opts, 1, false);

    if num in [1..n] then
        r := _ruleTreeN(spl, num-1, memohash, opts);

    else
        r := false;
    fi;

    return r;
end;

# fold list inside out, favoring the right hand side

_foldedMidFirst := function(origlist)
	local len, midpt, front, back, newlist;

	len := Length(origlist);
	if len < 2 then
		return origlist;
	fi;
	midpt := Int(len/2);
	front := Reversed(Sublist(origlist, [1..midpt]));
	back  := Sublist(origlist, [midpt+1..len]);

	newlist := [];

	if IsOddInt(len) then
		Append(newlist, [back[1]]);
		back := Drop(back,1);
	fi;

	while (front <> []) or (back <> []) do
		if back <> [] then
			Append(newlist, [back[1]]);
			back := Drop(back,1);
		fi;
		if front <> [] then
			Append(newlist, [front[1]]);
			front := Drop(front, 1);
		fi;
	od;

	return newlist;
end;

Declare(_ruleTreeMid);

#F RuleTreeMid(spl, opts)
#F
#F   Similar to RuleTree1, but tries to grab the middle of each range of choices
#F

RuleTreeMid := (spl, opts) -> _ruleTreeMid(spl, HashTableSPL(), opts);

_ruleTreeMid := function(spl, memohash, opts)
    local h, R, r, C, c, ch, npc, idx, idxlist, ridx, ridxlist;

    h := MultiHashLookup(opts.baseHashes, spl);

    if h <> false then
        return h[1].ruletree;
    fi;

    R := AllApplicableRulesDirect(spl, opts.breakdownRules);
	ridxlist := [1..Length(R)];
	if Length(ridxlist) > 1 then
		if Length(_allChildren(R[1], spl, opts)) < 2 then
			ridxlist := _foldedMidFirst(ridxlist);
		fi;
	fi;
    for ridx in ridxlist do
		r := R[ridx];
		C := _allChildren(r, spl, opts);
		idxlist := _foldedMidFirst([1..Length(C)]);
		for idx in idxlist do
			c := C[idx];
			npc := Product(c, e -> _getNumTrees(e, memohash, opts));
			if npc > 0 then
				ch := List(c, e -> _ruleTreeMid(e, memohash, opts));
				if not ForAny(ch, e -> IsBool(e) and e=false) then
					return RuleTreeNC(r, spl, ch);
				fi;
			fi;

        od;
    od;
	# no tree, return original spl
    return spl;
end;

Declare(_ruleTree1);

#F RuleTree1(spl, opts)
#F
#F   returns the first ruletree. this is often useful for
#F   testing whether a particular rule is firing, and you
#F   want to avoid the randomness of RandomRuleTree
#F
#F   NOTE: this function does not check if a tree can
#F   actually just be created, it just tries to return
#F   the first one. RuleTreeN(spl, 1, opts) does the
#F   same thing but first checks.
#F

RuleTree1 := (spl, opts) -> _ruleTree1(spl, HashTableSPL(), opts);

_ruleTree1 := function(spl, memohash, opts)
    local h, R, r, i, c, ch, npc;

    h := MultiHashLookup(opts.baseHashes, spl);

    if h <> false then
        return h[1].ruletree;
    fi;

    R := AllApplicableRulesDirect(spl, opts.breakdownRules);
    for r in R do
		for c in _allChildren(r, spl, opts) do
			npc := Product(c, e -> _getNumTrees(e, memohash, opts));
			if npc > 0 then
				ch := List(c, e -> _ruleTree1(e, memohash, opts));
				if not ForAny(ch, e -> IsBool(e) and e=false) then
					return RuleTreeNC(r, spl, ch);
				fi;
			fi;
        od;
    od;
	# no tree, return original spl
    return spl;
end;

#F _ruleTreeN(<spl>, <num>, <memohash>, <opts>)
#F   internal function which returns a ruletree for
#F   <spl> with index given by <num>. This is a recursive
#F   routine. Note that <num> is 0-based, unlike everything
#F   else in SPIRAL, which is 1-based.
#F
_ruleTreeN := function(spl, num, memohash, opts)
    local R, rtNC, h, r, c, npc, off, ch, i, n, p, offset;

    # lookup stuff like SSE base cases.
    h := MultiHashLookup(opts.baseHashes, spl);

    if h <> false then
       return h[1].ruletree;
    fi;

    # get list of normal rules
    R := AllApplicableRulesDirect(spl, opts.breakdownRules);
    offset := 0;

    for r in R do
        for c in _allChildren(r, spl, opts) do
            # figure out which group of children we should be looking at.
            npc := Product(c, e -> _getNumTrees(e, memohash, opts));

            # is this the correct group?
            if num < offset + npc then
                off := npc;
                ch := [];

                # step through the kids, and push down the ruletree number
                for i in [1..Length(c)] do
                    n := _getNumTrees(c[i], memohash, opts);

                    off := When(n > 0, off / n, off);

                    p := When(n <= 0 or num <= offset,
                        0,
                        RemInt(QuoInt(num - offset, off), n)
                    );

                    # all the kids get evaluated
                    Add(ch, _ruleTreeN(c[i], p, memohash, opts));
                od;

                # no kids should be false, but just in case.
                if not ForAny(ch, e -> IsBool(e) and e=false) then
                    return RuleTreeNC(r, spl, ch);
                fi;
            fi;

            # fixup offset
            offset := offset + npc;
        od;
    od;

	# no tree, return original spl
    return spl;
end;

Declare(_ruleTreeGetN);

#F RuleTreeGetN(<ruletree>, <opts>)
#F given a ruletree, determine the index
#F
#F uses:
#F      opts.breakdownRules
#F      opts.baseHashes
#F

RuleTreeGetN := function(ruletree, opts)
    local memohash, r;

    memohash := HashTableSPL();

    # prime the memohash.
    _NofRuleTrees(ruletree.node, s -> false, memohash, opts, 1, false);

    r := _ruleTreeGetN(ruletree, memohash, opts, 0);

    # ruleTreeGetN may return false if we couldn't match the tree in the passed
    # in breakdownRules/baseHashes.
    return When(IsInt(r), r + 1, false);
end;

_ruleTreeGetN := function(rt, memohash, opts, offset)
    local h, R, r, c, ch, off, n;

    # is the ruletree something from the baseHashes?
    h := MultiHashLookup(opts.baseHashes, rt.node);
    if IsList(h) and rt = h[1].ruletree then
        return offset;
    fi;

    R := AllApplicableRulesDirect(rt.node, opts.breakdownRules);

    for r in R do
        # does rule match?
        if r = rt.rule then
            for c in _allChildren(r, rt.node, opts) do
                # does child match?
                if c = List(rt.children, e -> e.node) then
                    off := Product(rt.children, e -> _getNumTrees(e.node, memohash, opts));
                    for ch in rt.children do
                        n := _getNumTrees(ch.node, memohash, opts);
                        off := When(n > 0, off / n, off);
                        offset := offset + (off * _ruleTreeGetN(ch, memohash, opts, 0));
                    od;
                    return offset;
                # child doesn't match, increase offset
                else
                    offset := offset + Product(c, e -> _getNumTrees(e, memohash, opts));
                fi;
            od;
        # rule doesn't match, so increase offset by all possible expansions we didn't use
        else
            offset := offset
                + Sum(_allChildren(r, rt.node, opts), C ->
                    Product(C, c -> _getNumTrees(c, memohash, opts))
                );
        fi;
    od;

    return offset;
end;


# Copyright (c) 2018-2021, Carnegie Mellon University
# See LICENSE for details

benchMACRO := function()
    return rec(
        2xf := rec(
            wht := _defaultSizes(s->doSimdWht(s, MACRO_2xf, rec(verify := true, oddSizes := false, svct := true)), List([3..8], i->2^i)),
            1d := rec(
                dft_ic := rec(
                    small := rec(
                        real := _defaultSizes(s->doSimdDft(s, MACRO_2xf, rec(verify := true, globalUnrolling := 10000, tsplBluestein:=true,
                          PRDFT:=true, URDFT:= true, CT := true, minCost := true, tsplPFA := true, tsplCT := true, oddSizes := true, PD := true, PFA := true,
                          RealRader := true, Rader := true, realVect := true, cplxVect := false, stdTTensor := false, interleavedComplex := true)), [2..64]),
                        cmplx := _defaultSizes(s->doSimdDft(s, MACRO_2xf, rec(verify := true, globalUnrolling := 10000, tsplBluestein:=true,
                          PRDFT:=true, URDFT:= true, CT := true, minCost := true, tsplPFA := true, tsplCT := true, oddSizes := true, PD := true, PFA := true,
                          RealRader := true, Rader := true, realVect := false, cplxVect := true, stdTTensor := false, interleavedComplex := true)), [2..64])
                    ),
                    medium := _defaultSizes(s->doSimdDft(s, MACRO_2xf, rec(verify := true, globalUnrolling := 128, tsplRader:=false, tsplBluestein:=false, tsplPFA:=false, oddSizes:=false,
                            interleavedComplex := true, cplxVect := false, realVect := true)),
                          List([2..16], i->2^i)),
                    medium_cx := _defaultSizes(s->doSimdDft(s, MACRO_2xf, rec(globalUnrolling := 64, tsplRader:=false, tsplBluestein:=false, tsplPFA:=false, oddSizes:=false,
                            interleavedComplex := true, cplxVect := true, realVect := false)),
                          List([1..16], i->2^i))
                ),
                dft_sc := _defaultSizes(s->doSimdDft(s, MACRO_2xf, rec(verify := true, globalUnrolling := 10000, tsplRader:=false, tsplBluestein:=false, tsplPFA:=false, oddSizes:=false, interleavedComplex := false)), 8*[1..16])
            )
        ),

        4xf := rec(
            wht := _defaultSizes(s->doSimdWht(s, MACRO_4xf, rec(verify := true, oddSizes := false, svct := true)), List([4..8], i->2^i)),
            1d := rec(
                dft_ic := rec(
                    small := rec(
                        real := _defaultSizes(s->doSimdDft(s, MACRO_4xf, rec(verify := true, globalUnrolling := 10000, tsplBluestein:=true,
                          PRDFT:=true, URDFT:= true, CT := true, minCost := true, tsplPFA := true, tsplCT := true, oddSizes := true, PD := true, PFA := true,
                          RealRader := true, Rader := true, realVect := true, cplxVect := false, stdTTensor := false, interleavedComplex := true)), [2..64]),
                        cmplx := _defaultSizes(s->doSimdDft(s, MACRO_4xf, rec(verify := true, globalUnrolling := 10000, tsplBluestein:=true,
                          PRDFT:=true, URDFT:= true, CT := true, minCost := true, tsplPFA := true, tsplCT := true, oddSizes := true, PD := true, PFA := true,
                          RealRader := true, Rader := true, realVect := false, cplxVect := true, stdTTensor := false, interleavedComplex := true)), [2..64])
                    ),
                    medium := _defaultSizes(s->doSimdDft(s, MACRO_4xf, rec(verify := true, globalUnrolling := 128, tsplRader:=false, tsplBluestein:=false, tsplPFA:=false, oddSizes:=false,
                            interleavedComplex := true, cplxVect := false, realVect := true)),
                          List([4..16], i->2^i)),
                    medium_cx := _defaultSizes(s->doSimdDft(s, MACRO_4xf, rec(globalUnrolling := 64, tsplRader:=false, tsplBluestein:=false, tsplPFA:=false, oddSizes:=false,
                            interleavedComplex := true, cplxVect := true, realVect := false)),
                          List([2..16], i->2^i))
                ),
                dft_sc := _defaultSizes(s->doSimdDft(s, MACRO_4xf, rec(verify := true, globalUnrolling := 10000, tsplRader:=false, tsplBluestein:=false, tsplPFA:=false, oddSizes:=false, interleavedComplex := false)), 16*[1..8])
            )
        ),
        8xf := rec(
            wht := _defaultSizes(s->doSimdWht(s, MACRO_8xf, rec(verify := true, oddSizes := false, svct := true)), List([6..8], i->2^i)),
            1d := rec(
                dft_ic := rec(
                    small := rec(
                        real := _defaultSizes(s->doSimdDft(s, MACRO_8xf, rec(verify := true, globalUnrolling := 10000, tsplBluestein:=true,
                          PRDFT:=true, URDFT:= true, CT := true, minCost := true, tsplPFA := true, tsplCT := true, oddSizes := true, PD := true, PFA := true,
                          RealRader := true, Rader := true, realVect := true, cplxVect := false, stdTTensor := false, interleavedComplex := true)), [2..64]),
                        cmplx := _defaultSizes(s->doSimdDft(s, MACRO_8xf, rec(verify := true, globalUnrolling := 10000, tsplBluestein:=true,
                          PRDFT:=true, URDFT:= true, CT := true, minCost := true, tsplPFA := true, tsplCT := true, oddSizes := true, PD := true, PFA := true,
                          RealRader := true, Rader := true, realVect := false, cplxVect := true, stdTTensor := false, interleavedComplex := true)), [2..64])
                    ),
                    medium := _defaultSizes(s->doSimdDft(s, MACRO_8xf, rec(verify := true, globalUnrolling := 128, tsplRader:=false, tsplBluestein:=false, tsplPFA:=false, oddSizes:=false,
                            interleavedComplex := true, cplxVect := false, realVect := true)),
                          List([6..16], i->2^i)),
                    medium_cx := _defaultSizes(s->doSimdDft(s, MACRO_8xf, rec(globalUnrolling := 128, tsplRader:=false, tsplBluestein:=false, tsplPFA:=false, oddSizes:=false,
                            interleavedComplex := true, cplxVect := true, realVect := false)),
                          List([4..16], i->2^i))
                ),
                dft_sc := _defaultSizes(s->doSimdDft(s, MACRO_8xf, rec(verify := true, globalUnrolling := 10000, tsplRader:=false, tsplBluestein:=false, tsplPFA:=false, oddSizes:=false, interleavedComplex := false)), 64*[1..4])
            )
        )
    );
end;


#
# usage:
# Read("~/Workspace/Chevalley.gap/init.gap"); Read(Filename(home_dir,"load.gap")); Read(Filename(home_dir,"main.gap"));
#
# Read(Filename(home_dir,"main.gap"));
#

ITER_POLY_WARN:=false;
Read(Filename(home_dir,"lib/io.gi"));
Read(Filename(home_dir,"handle.gap"));

alg:=AlgebraicU("G",2,GF(2));
#alg:=AlgebraicU(sys);

orbs:=UnipotentClasses(alg,"");

infos:=List(AllClasses(orbs),orb->handleClassShort(orb));


##  Copyright (c) 2018-2021, Carnegie Mellon University
##  See LICENSE for details

#F FDataNT(<datavar>, <nt>)
#F
Class(FDataNT, Function, rec(
    __call__ := (self, datavar, nt) >> WithBases(self, rec(
        var := datavar,
        operations := PrintOps,
        nt := nt,
        domain := self >> Rows(self.nt)
    )),

    rChildren := self >> [ self.var, self.nt, self._domain, self._range],
    rSetChild := rSetChildFields("var", "nt", "_domain", "_range"),
    from_rChildren := (self, rch) >> ObjId(self)(rch[1], rch[2]).setDomain(rch[4]).setRange(rch[5]),

    domain := self >> self.len,
    print := self >> Print(self.name,"(",self.var,", ",self.nt,")"),

# these are not yet right
    at := (self, n) >> When(IsInt(n) and IsValue(self.ofs) and IsBound(self.var.value),
        self.var.value.v[n + self.ofs.v + 1],
        nth(self.var, n + self.ofs)),

    tolist := self >> List([0..EvalScalar(self.len-1)], i -> nth(self.var, self.ofs+i)),
    lambda := self >> let(x := Ind(self.domain()), Lambda(x, nth(self.var, self.ofs+x))),
# up to here

    domain := self >> Rows(self.nt),
    range := self >> When(self._range=false, self.var.t.t, self._range),
    inline := true,
    free := self >> self.ofs.free()
));

# These need to be implemented properly -- for now they are just markers in TDAG
Class(TColMaj, TPtr);


# Copyright (c) 2018-2021, Carnegie Mellon University
# See LICENSE for details

# ================================================================
# Simplification of Intervals
# ================================================================

full_II := (int, size) ->
    [int.target(II), size, 0, @.cond(e->e=size.val)];

empty_II := (int, size) ->
    [int.target(II).cond(e->e.params[2]=e.params[3]), size, @, @];

left_II := (int, size, endd) ->
    [int.target(II), size, 0, endd];

right_II := (int, size,start) ->
    [int.target(II), size, start, @.cond(e->e=size.val)];

Class(RulesII, RuleSet);

RewriteRules(RulesII, rec(
    # full II o index mapping func @(3) = II with domain of @(3)
    # II(n, 0, n) o f = II(domain(f), 0, domain(f))
    FullII_f := ARule(fCompose, [ full_II(@, @(1)), @(2) ],  e -> [ II(@(2).val.domain()) ]),
    EmptyII_f := ARule(fCompose, [ empty_II(@, @(1)), @(2) ],  e -> [ II(@(2).val.domain(),0,0) ]),

    # partial II o dirsum of perms
    # II(n, 0, k) o fDirsum(perm_k, perm_?) -> II(n,0,k)
    LeftII_fDirsum := ARule(fCompose,
        [ left_II(@(0), @(1), @(2)), [fDirsum, @(3).cond(e -> is_perm(e) and e.range()=@(2).val), ...]],
        e -> [ @(0).val ]),

    # partial II o dirsum of perms
    # II(n, n-k, n) o fDirsum(perm_?, perm_k) -> II(n,n-k,n)
    RightII_fDirsum := ARule( fCompose,
        [ right_II(@(0), @(1), @(2)), [fDirsum, ..., @(4).cond(e -> is_perm(e) and e.range() = @(1).val-@(2).val)]],
        e -> [ @(0).val ]),

    # fTensor(IIn, IIk) -> II(n*k)
    fullII_tensor_fullII := ARule(diagTensor, [full_II(@(0), @(1)), full_II(@(0), @(2))],
        e -> [ II(@(1).val * @(2).val) ]),

    # fDirsum(IIn, IIk) -> II(n+k)
    fullII_dirsum_fullII := ARule(diagDirsum, [full_II(@(0), @(1)), full_II(@(0), @(2))],
        e -> [ II(@(1).val + @(2).val) ]),

    diagTensor_emptyII := Rule([diagTensor, ..., empty_II(@(1), @(2)), ...], e -> II(e.domain(),0,0)),
    diagTensor_fullII_Id1 := ARule(diagTensor, [@(0), full_II(@(1), @(2).cond(e->e=1))], e -> [ @(0).val ]),
    diagTensor_fullII_Id2 := ARule(diagTensor, [full_II(@(1), @(2).cond(e->e=1)), @(0)], e -> [ @(0).val ]),

    II_dirsum_emptyII := ARule(diagDirsum, [[II, @(1), @(2), @(3)], empty_II(@(4), @(5))],
        e -> [ II(@(1).val + @(5).val), @(2).val, @(3).val ]),

    emptyII_dirsum_II := ARule(diagDirsum, [empty_II(@(4), @(5)), [II, @(1), @(2), @(3)]],
        e -> [ II(@(1).val + @(5).val), @(5).val + @(2).val, @(5).val + @(3).val ]),

    # II Shifting, we support shift left (shift <= start), and right (shift >= end)
    # II o Z = shifted II#
    II_Z := ARule(fCompose, [[II, @(1), @(2), @(3)], [Z, @(4), @(5).cond(e -> (e<=@(2).val) or (e >= @(3).val))]],
        e -> [II(@(1).val, (@(2).val-@(5).val) mod @(1).val,
               # this makes 0->size, because size mod size = 0
                let(last:=(@(3).val-@(5).val) mod @(1).val,
                    When(last=0, @(1).val, last))) ]),

    DiagFullII_toId := Rule([Diag, full_II(@(1), @(2))], e -> I(@(2).val)),
    Diag_fConst_toId := Rule([Diag, [fConst, @(2), @(1), 1]], e -> I(@(1).val)),
    Diag_fConstV_toId := Rule([Diag, [fConst, @(2), @(1), _1]], e -> I(@(1).val)),

    COND_fullII := Rule([COND, full_II(@(1), @(2)), @(3), @(4)], e -> @(3).val),
    COND_emptyII := Rule([COND, full_II(@(1), @(2)), @(3), @(4)], e -> @(4).val)
));


# Copyright (c) 2018-2021, Carnegie Mellon University
# See LICENSE for details

Class(VDirectSum, DirectSum, rec(
    abbrevs := [(L, v) -> [L, v]],
    new := meth(self, L, v)
        if ForAll(L, IsIdentitySPL)
            then return I(Sum(L, Rows));
        else
            return SPL(WithBases( self,
            rec( _children := L,
            v := v,
            dimensions := [ Sum(L, t -> t.dimensions[1]),
                            Sum(L, t -> t.dimensions[2]) ] )));
        fi;
    end,
#    sums := meth(self)
#        nblocks, c, cols, rows, bkcols, bkrows, gcd;
#        nblocks  :=  self.numChildren();
#        c := self.children();
#        cols  :=  EvalScalar(Cols(self));
#        rows := EvalScalar(Rows(self));
#        bkcols := List(c, i->EvalScalar(Cols(i)));
#        bkrows := List(c, i->EvalScalar(Rows(i)));
#        gcd := self.v;
#        bkrows_by_gcd := List(bkrows, i->i/gcd);
#        psr_by_gcd := List(DropLast(_partSum(bkrows), 1), i ->idiv(i,gcd));
#    fi;
#    psc := DropLast(Concat([0], List(Drop(_partSum(bkcols), 1), i->i-overlap)), 1);
#    return SUM( Map([1..nblocks],
#        i -> Compose(
#                    Scat(fTensor(fAdd(rows/gcd, bkrows_by_gcd[i], Cond(i=1, 0, i=2, bkrows_by_gcd[i-1], psr_by_gcd[i])), fId(gcd))),
#                    self.child(i).sums(),
#                    Gath(fTensor(fAdd(cols/gcd, bkcols[i]/gcd, psc[i]/gcd), fId(gcd))))
#            ));
));


InstallGlobalFunction(IsCommutator,
function(f, w)
	local fp, hom, res;
	fp := CommutatorFactorGroup(f);
	hom := GroupHomomorphismByImages(f, fp, [f.1, f.2], [fp.1, fp.2]);
	res :=  IsOne(Image(hom, w));
	return res;
end);