
// Make newform 462.6.a.a in Magma, downloaded from the LMFDB on 16 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_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_462_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_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 := 6;
    raw_aps := [[-4], [-9], [-78], [49], [121], [-502], [-642], [-520], [1020], [4818], [1784], [7958], [2430], [22904], [-11316], [-12222], [-15852], [46298], [19412], [-17292], [-30214], [35672], [-43428], [-14934], [85106], [-72150], [11744], [-217932], [87674], [145818], [-341752], [-110460], [-123510], [134720], [-387270], [-463792], [-371122], [-76156], [172200], [-601134], [223572], [181622], [117252], [-410758], [190506], [-893560], [294392], [-399040], [86100], [371126], [-57786], [725616], [625370], [267084], [-63726], [-153888], [1799970], [-1051288], [2143130], [893910], [665216], [-2200086], [-491272], [-105180], [-1891750], [-672198], [-1072900], [1035818], [-1528212], [-1452046], [-184926], [1096608], [3735872], [4107242], [3186380], [-4203828], [-3325398], [5344670], [-17262], [-591886], [1643028], [-2625754], [-1798680], [-2841742], [6942776], [887508], [-6173910], [-6545638], [3592482], [-1519744], [1333524], [5157048], [-3428992], [9448404], [-4077172], [-4323384], [-6350430], [3127770], [-6768976], [-7427590], [-11404984], [-7507326], [3677820], [330558], [-3916576], [-1643710], [14385972], [-14962554], [904956], [6111842], [-10351288], [4543538], [-8349294], [5159540], [3338120], [-15100422], [-17104996], [-8362428], [12948450], [-5399820], [-3639082], [5824826], [-1366566], [5678652], [2894828], [17569890], [-19261282], [-2883372], [5012168], [416426], [22684160], [27474960], [-2968048], [17559302], [-22345002], [-11130046], [13730010], [-18631288], [5060394], [-11968434], [-26946040], [-9467238], [-22056208], [12272364], [6344534], [18204828], [-595294], [-19871994], [-4440916], [-30457956], [41101298], [24520026], [-3082828], [23752464], [25097348], [23962860], [9360560], [2561586], [12273842], [37648626], [-20630196], [-27968082], [27478640], [-32690412], [-49440558], [7238916], [35907776], [351914], [25454834], [-16806414], [37423476], [-38100034], [6229584], [-55849342], [-29224384], [24249906], [36080528], [-4642182], [-534328], [-35687662], [-52960864], [-25925340], [-56355130], [-20384394], [-2773716], [17814450], [-64501822], [-77930500], [10582922], [38479464], [-57703630], [8827932], [76880540], [54515370], [-62030508], [77764554], [67630658], [33240710], [-83717970], [-83324472], [-4894686], [-93298624], [-59929306], [11802626], [98888124], [-6568062], [-37259032], [39491772], [-28002642], [24375788], [42278618], [-103217646], [88076216], [-11537700], [-16990272], [-93920326], [-15928000], [-694602], [-17987052], [-29671998], [62555930], [118088000], [-55911486], [-67479448], [-53500116], [-78771910], [-36732510], [-79962468], [-99137128], [-33165084], [-70729138], [37616], [72703328], [-46528434], [-21947116], [121503216], [34741250], [41172090], [18708204], [106247484], [-53271516], [-21843040], [-98846128], [75372470], [-39861402], [107966592], [-45575824], [7040172], [182904704], [143046264], [161291762], [111175734], [-126654036], [-189608854], [81879426], [137096964], [73387286], [-147269896], [85792266], [86927354], [27508088], [-49990788], [134608706], [-91182958], [-53364534], [57631892], [125306226], [-180928974], [69220616], [-151159158], [-243199522], [55201628], [-3457462], [216725936], [81575282], [257415224], [-198584988], [-140992774], [175655114], [121930668], [-90625368], [-43168672], [4733616], [-79467334], [91487288], [-111001116], [272774690], [-132503526], [-283607752], [241941558], [-268060206], [-258444852], [-63575130], [-106520604], [225150242], [256475058], [33672128], [-131400918], [162258036], [4738928], [-16613926], [194581218], [-149307808], [-192039468], [-114553540], [-138332542], [-164624460], [210901670], [335460324], [4370906], [-285739824], [-286613310], [46505406], [-160149124], [202674816], [-153387718], [244375284], [-332641728], [318034274], [-319539114], [72366680], [333235226], [-221329038], [-205407496], [35532630], [-152920222], [-242584684], [-74923132], [312625248], [-106864950], [-158332558], [-84483150], [314717072], [-245525340], [-370534840], [-109238628], [-209282026], [75416214], [196830602], [256152872], [-230471650], [-146241342], [184764522], [-175672048], [170846154], [414334020], [49072934], [-293513668], [-131590200], [-4767990], [-469212400], [145716890], [-538962150], [-320218888], [223972082], [256835814], [25207956], [324837660], [205509414], [-245738772], [-413037694], [451827870], [5334852], [-359343420], [29644100], [-279907510], [-231365118], [-184162576], [-230821726], [-254105172], [53957036], [485778816], [71392506], [-176992168], [-340019746], [236358876], [-361903584], [-344920150], [-48288162], [328050626], [152304090], [-336893550], [-476513872], [238827846], [-122608744], [-691559964], [-434738848], [-151264450], [-210027016], [110071548], [-12262606], [338515986], [490857660], [-449812372], [-48851436], [-571867462], [151803632], [-691598142], [-337596100], [582633594], [606879530], [250689810], [-366757984], [-366897198], [-713154390], [-624189640], [-24097186], [-701393538], [-499902844], [-569719812], [-806616886], [26420922], [789572628], [-53200360], [273666050], [-348263358], [568182576], [-344036992], [128451882], [670388280], [285553986], [14126402], [295678188], [-146747220], [37599818], [660696834], [-14819604], [-234797226], [-748513924], [267593520]];
    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_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_462_6_a_a(:prec:=1)
    chi := MakeCharacter_462_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(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_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_462_6_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_462_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<5,R![78, 1]>],Snew);
    return Vf;
end function;