
// Make newform 20.8.e.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_20_e();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_20_e_Hecke();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_20_8_e_a();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose lines below.
// The precision argument determines an initial guess on how many Fourier coefficients to use.
// This guess is increased enough to uniquely determine the newform.
// To make the Hecke irreducible modular symbols subspace (type ModSym)
// containing the newform, type "MakeNewformModSym_20_8_e_a();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose line below.
// The default sign is -1.  You can change this with the optional parameter "sign".
function ConvertToHeckeField(input: pass_field := false, Kf := [])
    if not pass_field then
        poly := [1, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_20_e();"
function MakeCharacter_20_e()
    N := 20;
    order := 4;
    char_gens := [11, 17];
    v := [2, 3];
    // chi(gens[i]) = zeta^v[i]
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    F := CyclotomicField(order);
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),[F|F.1^e:e in v]);
    return MinimalBaseRingCharacter(chi);
end function;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_20_e_Hecke();"
function MakeCharacter_20_e_Hecke(Kf)
    N := 20;
    order := 4;
    char_gens := [11, 17];
    char_values := [[-1, 0], [0, 1]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
end function;


function ExtendMultiplicatively(weight, aps, character)
    prec := NextPrime(NthPrime(#aps)) - 1; // we will able to figure out a_0 ... a_prec
    primes := PrimesUpTo(prec);
    prime_powers := primes;
    assert #primes eq #aps;
    log_prec := Floor(Log(prec)/Log(2)); // prec < 2^(log_prec+1)
    F := Universe(aps);
    FXY<X, Y> := PolynomialRing(F, 2);
    // 1/(1 - a_p T + p^(weight - 1) * char(p) T^2) = 1 + a_p T + a_{p^2} T^2 + ...
    R<T> := PowerSeriesRing(FXY : Precision := log_prec + 1);
    recursion := Coefficients(1/(1 - X*T + Y*T^2));
    coeffs := [F!0: i in [1..(prec+1)]];
    coeffs[1] := 1; //a_1
    for i := 1 to #primes do
        p := primes[i];
        coeffs[p] := aps[i];
        b := p^(weight - 1) * F!character(p);
        r := 2;
        p_power := p * p;
        //deals with powers of p
        while p_power le prec do
            Append(~prime_powers, p_power);
            coeffs[p_power] := Evaluate(recursion[r + 1], [aps[i], b]);
            p_power *:= p;
            r +:= 1;
        end while;    
    end for;
    Sort(~prime_powers);
    for pp in prime_powers do
        for k := 1 to Floor(prec/pp) do
            if GCD(k, pp) eq 1 then
                coeffs[pp*k] := coeffs[pp]*coeffs[k];
            end if;
        end for;
    end for;
    return coeffs;
end function;


function qexpCoeffs()
    // To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
    weight := 8;
    raw_aps := [[-8, -8], [0, 0], [278, 29], [0, 0], [0, 0], [11003, -11003], [17139, 17139], [0, 0], [0, 0], [0, -120844], [0, 0], [157829, 157829], [-882568, 0], [0, 0], [0, 0], [-1406907, 1406907], [0, 0], [532572, 0], [0, 0], [0, 0], [4644013, -4644013], [0, 0], [0, 0], [0, 9562096], [-10692851, -10692851], [-13304198, 0], [0, 0], [0, 0], [0, 24116226], [9745903, -9745903], [0, 0], [0, 0], [3622129, 3622129], [0, 0], [0, 73157686], [0, 0], [28684219, 28684219], [0, 0], [0, 0], [-92476087, 92476087], [0, 0], [-145860138, 0], [0, 0], [-32264717, 32264717], [145581449, 145581449], [0, 0], [0, 0], [0, 0], [0, 0], [0, -290889644], [17847123, -17847123], [0, 0], [420719032, 0], [0, 0], [-131406481, -131406481], [0, 0], [0, 78033566], [0, 0], [36172859, 36172859], [-388433888, 0], [0, 0], [-20919817, 20919817], [0, 0], [0, 0], [211915703, -211915703], [-595124961, -595124961], [0, 0], [936650729, 936650729], [0, 0], [0, 508393836], [-773037207, 773037207], [0, 0], [0, 0], [336983713, -336983713], [0, 0], [0, 0], [0, -1941622054], [376230049, 376230049], [-884816798, 0], [0, 2389678026], [0, 0], [-2619877708, 0], [0, 0], [876482573, -876482573], [0, 0], [0, 0], [0, -2711110514], [-2486487881, -2486487881], [4076572322, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 38439676], [2384517542, 0], [0, 0], [-7362347118, 0], [0, 0], [2856859269, 2856859269], [0, 0], [0, -6846174634], [0, 0], [5745860759, 5745860759], [0, 0], [7180745233, -7180745233], [0, 0], [-1411737648, 0], [0, 0], [1601920403, -1601920403], [-4648967061, -4648967061], [0, 0], [0, 0], [11948722232, 0], [0, 0], [0, 0], [-9879567507, 9879567507], [0, 0], [-14805122628, 0], [9470423413, -9470423413], [-5636485791, -5636485791], [0, 0], [0, 0], [4351438052, 0], [0, -9487569124], [0, 0], [0, 0], [3990482273, -3990482273], [0, 0], [0, 0], [0, 0], [-396359981, -396359981], [-19986080278, 0], [0, -225801384], [11394579963, -11394579963], [0, 0], [18239713399, 18239713399], [0, -9869245624], [0, 0], [-13272634508, 0], [0, 0], [0, 0], [0, -25944602994], [0, 0], [-16929116357, 16929116357], [-4512987831, -4512987831], [0, 0], [0, 0], [22612848659, 22612848659], [30324250912, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, -14341488494], [24546711379, 24546711379], [34980773182, 0], [0, 0], [29226998543, -29226998543], [0, 0], [0, 0], [-40961322891, -40961322891], [0, 0], [0, 0], [23239811999, 23239811999]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_20_e_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_20_8_e_a();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose lines below.
// The precision argument determines an initial guess on how many Fourier coefficients to use.
// This guess is increased enough to uniquely determine the newform.
function MakeNewformModFrm_20_8_e_a(:prec:=2)
    chi := MakeCharacter_20_e();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 8));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(997) - 1;
    while true do
        trunc_vec := Vector(Kf, [0] cat [f_vec[i]: i in [1..prec]]);
        B := Basis(S, prec + 1);
        S_basismat := Matrix([AbsEltseq(g): g in B]);
        if Rank(S_basismat) eq Min(NumberOfRows(S_basismat), NumberOfColumns(S_basismat)) then
            S_basismat := ChangeRing(S_basismat,Kf);
            f_lincom := Solution(S_basismat,trunc_vec);
            f := &+[f_lincom[i]*Basis(S)[i] : i in [1..#Basis(S)]];
            return f;
        end if;
        error if prec eq maxprec, "Unable to distinguish newform within newspace";
        prec := Min(Ceiling(1.25 * prec), maxprec);
    end while;
end function;


// To make the Hecke irreducible modular symbols subspace (type ModSym)
// containing the newform, type "MakeNewformModSym_20_8_e_a();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose line below.
// The default sign is -1.  You can change this with the optional parameter "sign".
function MakeNewformModSym_20_8_e_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_20_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<3,R![0, 1]>],Snew);
    return Vf;
end function;