
// Make newform 40.6.a.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_40_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_40_6_a_b();".
// 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_40_6_a_b();".
// 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_40_a();"
function MakeCharacter_40_a()
    N := 40;
    order := 1;
    char_gens := [31, 21, 17];
    v := [1, 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_40_a_Hecke(Kf)
    return MakeCharacter_40_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], [-8], [25], [-108], [-604], [-306], [930], [-1324], [-852], [5902], [-3320], [10774], [-17958], [9264], [-9796], [-31434], [33228], [-40210], [58864], [-55312], [27258], [31456], [24552], [-90854], [154706], [-72714], [-129396], [-206680], [-70146], [-151854], [-336596], [275308], [-228502], [224284], [-183802], [296032], [134766], [-60248], [-62012], [-591682], [-241404], [187622], [37560], [164434], [360518], [-654168], [-693156], [494756], [907088], [1089494], [499706], [1620384], [1001218], [368980], [279010], [811740], [353214], [-1716216], [-245882], [-1676182], [-1258816], [719158], [1860128], [278384], [-474182], [-1837378], [2997428], [1875314], [180312], [-87058], [2659010], [-2149368], [-3082580], [2287270], [-1301540], [-2032764], [94230], [-5555506], [-784814], [-4594006], [1413012], [5945558], [-6481144], [4055970], [-1214504], [-5531536], [2201106], [3298346], [-3942658], [8820404], [-1287088], [6511792], [-5795228], [990276], [2914996], [2478716], [-6758066], [-6339030], [231920], [-9444402], [3101616], [-1223298], [-14089592], [1484682], [-2864700], [4217282], [-2010472], [7336914], [1148840], [11640938], [-155540], [-11813690], [6422522], [3852524], [-6751360], [-7354926], [15969448], [17266692], [-1362514], [-8818076], [-1520346], [-315086], [-1740922], [-19893488], [20151916], [8107662], [-13561258], [4283136], [11208388], [4705486], [-23109868], [5752940], [-19242376], [4492102], [7335002], [-5855262], [13455750], [10070608], [-11884418], [10605258], [-1389436], [-11266138], [-27709332], [12366168], [12318190], [-11727656], [15705878], [25239002], [3662476], [-41793772], [10898974], [-30433630], [6090280], [27790788], [37151032], [-7859592], [-16269304], [-36936462], [4897050], [-6839426], [10379040], [25958746], [-39211980], [-10687596], [27726562], [-9492724], [-20324312], [47050758]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_40_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_40_6_a_b();".
// 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_40_6_a_b(:prec:=1)
    chi := MakeCharacter_40_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_40_6_a_b();".
// 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_40_6_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_40_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<3,R![8, 1]>],Snew);
    return Vf;
end function;