
// Make newform 20.6.a.a in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_20_a();"
// 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_6_a_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_6_a_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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_20_a();"
function MakeCharacter_20_a()
    N := 20;
    order := 1;
    char_gens := [11, 17];
    v := [1, 1];
    // 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;

function MakeCharacter_20_a_Hecke(Kf)
    return MakeCharacter_20_a();
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 := 6;
    raw_aps := [[0], [22], [-25], [218], [-480], [-622], [186], [-1204], [-3186], [5526], [9356], [5618], [-14394], [-370], [16146], [-4374], [-11748], [13202], [-11542], [-29532], [33698], [31208], [-38466], [119514], [94658], [101046], [-143434], [-57054], [-3118], [-54534], [24698], [236640], [-22158], [-193204], [448554], [-140860], [-335878], [-101650], [139242], [-265014], [-142812], [109670], [294948], [1003034], [-823998], [-906712], [506384], [-542050], [-1448574], [-478786], [374106], [169416], [-353746], [1255200], [-1128774], [-263082], [-1187742], [431300], [743114], [1921926], [-1630714], [71250], [-1617622], [-682788], [-2704438], [2603466], [-661432], [1717058], [131370], [3509510], [2219922], [4397304], [-2298238], [-1735606], [-5391148], [3272814], [603114], [-749422], [5313570], [999326], [2037396], [-5114614], [-3304044], [-2016382], [6583208], [-4817826], [-6203994], [2843834], [-1756050], [7668566], [-1359030], [-2027064], [2464274], [10384800], [6494156], [-10356522], [5873046], [2172954], [1073606], [7090334], [6697634], [11900850], [8756358], [-1156770], [-7078072], [-3134038], [11383266], [-15865518], [-15099840], [-8087050], [-710398], [5964338], [14843226], [-18204220], [1094612], [7440462], [-10791514], [-9629982], [-10001862], [-4010604], [12033818], [2012306], [16240962], [4629102], [11679368], [19954290], [-4883314], [-13577784], [6424106], [9085562], [20245700], [-5448306], [-1140724], [19015322], [22355082], [-10070398], [-4059630], [-17225566], [-21079302], [-18787686], [-13245616], [-7669254], [-8827858], [-30692310], [32841362], [-8421168], [23512610], [-11305038], [10007756], [26142894], [-1920406], [-25659378], [-20564314], [31686762], [3969626], [-13794468], [8081264], [29995638], [-20762038], [-3476418], [1867002], [-38550054], [16487522], [-23697624], [-57758958], [-11010282], [37080716], [4529354]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_20_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_20_6_a_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_6_a_a(:prec:=1)
    chi := MakeCharacter_20_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 6));
    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_6_a_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_6_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_20_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;