
// Make newform 462.4.a.e in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_462_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_462_4_a_e();".
// 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_462_4_a_e();".
// 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_462_a();"
function MakeCharacter_462_a()
    N := 462;
    order := 1;
    char_gens := [155, 199, 211];
    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_462_a_Hecke(Kf)
    return MakeCharacter_462_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 := 4;
    raw_aps := [[2], [-3], [-4], [-7], [-11], [62], [-120], [118], [-188], [62], [-322], [-198], [48], [32], [-326], [-482], [400], [70], [-124], [-712], [304], [-1016], [430], [442], [-966], [546], [98], [-2064], [1306], [478], [824], [-22], [-2106], [578], [946], [2368], [208], [3852], [28], [3646], [948], [2228], [-3412], [-3894], [-1122], [-1406], [-4288], [4102], [-6670], [-1708], [3198], [3760], [6848], [-6800], [1466], [576], [-1420], [-2644], [-78], [-330], [-6078], [5382], [10594], [-1458], [7662], [2262], [-8780], [2174], [7656], [-3046], [-10390], [-1848], [-7314], [-6034], [-3220], [-5074], [-11166], [-4608], [-2498], [8876], [9252], [4750], [-8008], [-11734], [-8212], [-5012], [-13602], [5962], [9506], [-9768], [-2068], [-10920], [-2724], [-12460], [22028], [-20724], [3740], [15090], [-10694], [10438], [-5852], [9990], [3258], [10478], [-8116], [5910], [12888], [16440], [-18840], [-16068], [13272], [25022], [7202], [16228], [-25348], [28938], [20776], [-30114], [9850], [32360], [29836], [-24082], [-3690], [-7596], [12960], [11034], [-694], [-3762], [-1274], [-21026], [12692], [2152], [18740], [-36894], [-18732], [-16292], [23336], [9110], [20144], [10946], [13874], [7570], [14372], [25036], [-38684], [31110], [-25922], [-14076], [-23920], [-34316], [2954], [-35994], [-36844], [35000], [-5460], [27116], [50056], [-28294], [15368], [-2394], [20772], [-49790], [24456], [32820], [21650], [-36502], [-39016], [-12558], [-2566], [-8916], [-57944], [43088], [-48648], [-6402], [-37838], [33974], [-31020], [-37646], [-58364], [-53422], [-20984], [-35638], [33634], [17748], [61008], [-9820], [-59114], [7052], [-26954], [-32324], [-9778], [-11952], [44500], [30096], [5556], [-59958], [7342], [7650], [-6352], [-69292], [16298], [-59580], [-4000], [2864], [68300], [12620], [-33502], [5972], [33090], [83484], [-60858], [-90168], [71792], [-75648], [29796], [-76370], [65752], [71820], [-14272], [-15534], [-10394], [-16412], [-47034], [-93136], [-65214], [-69134], [-18066], [-20016], [-79540], [91944], [-27542], [62614], [96704], [20266], [38116], [-91668], [23770], [26642], [32260], [66514], [-38228], [-18450], [35074], [-114274], [56740], [-20100], [4370], [-30496], [-20796], [93248], [-64442], [-37208], [-59072], [-28022], [-73010], [1936], [-106982], [72242], [-43492], [-87512], [-37248], [62484], [74902], [-132630], [-30314], [126912], [30134], [-121914], [-74468], [7886], [1224], [-31156], [-108218], [2576], [78568], [54198], [36924], [110342], [145874], [36830], [45048], [4624], [22008], [100882], [37326], [-119412], [-55226], [101930], [-45902], [115332], [9638], [-19804], [-39690], [-49674], [-35606], [128450], [-76214], [-88068], [-37894], [-59518], [-24212], [-112662], [152336], [168340], [940], [12814], [-39228], [49196], [116056], [63170], [80928], [19698], [58854], [146908], [-115656], [-53832], [-135200], [148368], [50838], [-98010], [82670], [-44470], [-66890], [-1388], [-54398], [-59698], [-157204], [202332], [31200], [-2046], [-153290], [97290], [-169132], [-135910], [170812], [1476], [-71190], [175460], [136402], [-168392], [-149378], [-172446], [142086], [-144460], [-16222], [65628], [79580], [109396], [103920], [90480], [56642], [-161722], [3886], [221208], [88874], [-8292], [-90598], [-22838], [-95934], [5040], [-142310], [123812], [-18336], [99540], [-145236], [23378], [56406], [-113000], [-28958], [-135292], [43092], [-96448], [-91698], [-242520], [-114394], [84540], [-71352], [-47124], [224560], [-28256], [46064], [-197942], [-139944], [-53910], [-11510], [-79730], [36836], [220964], [-135504], [-32550], [-127094], [-22360], [220700], [68056], [79120], [-116618], [2344], [227646], [-212188], [-130318], [-88746], [-99438], [-194056], [101406], [66430], [159508], [-91634], [29370], [-79564], [-98796], [103272], [-58954], [-228148], [-612], [-109834], [-34288], [-171368], [6862], [-134586], [86936], [-181462], [23742], [57944], [-182674], [308086], [254936], [-101012], [-143346], [83900], [187032]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_462_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_462_4_a_e();".
// 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_462_4_a_e(:prec:=1)
    chi := MakeCharacter_462_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(2999) - 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_462_4_a_e();".
// 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_462_4_a_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_462_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![4, 1]>],Snew);
    return Vf;
end function;