
// Make newform 462.4.a.f in Magma, downloaded from the LMFDB on 25 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_f();".
// 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_f();".
// 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], [1], [-7], [-11], [-43], [100], [-87], [-58], [-223], [88], [37], [128], [-458], [-341], [-342], [-105], [190], [-579], [128], [-161], [-396], [-420], [-798], [1414], [-1764], [978], [481], [-284], [1498], [-616], [2748], [2184], [908], [421], [948], [2368], [-2603], [-1402], [836], [-3292], [4838], [-1232], [2366], [318], [-806], [4462], [3802], [-3370], [-898], [-462], [-5665], [1593], [4545], [-2859], [-6279], [-6270], [6721], [-5388], [4765], [-903], [672], [-1876], [-4688], [4912], [-558], [4660], [-9906], [-10664], [5489], [-1845], [-848], [-8474], [11396], [2155], [-5864], [10104], [-5868], [-2418], [-2354], [-11443], [-13325], [-8503], [4496], [-7247], [8568], [-7812], [4262], [8316], [-16103], [-8913], [-4100], [10496], [2625], [-12397], [-1854], [17530], [-22255], [-7789], [8288], [-6432], [-9815], [-20042], [-8562], [27004], [14360], [15453], [18000], [27180], [-16153], [-11443], [-9488], [22902], [19448], [-13608], [17628], [-21074], [-4899], [7760], [18365], [13286], [-15242], [14170], [-7116], [2010], [7794], [29991], [19613], [16646], [-36346], [-25698], [-27543], [26005], [17621], [22968], [28273], [-13239], [1775], [-16481], [-29079], [-40381], [-6345], [7757], [-15659], [-5384], [40395], [-33202], [21034], [20920], [8354], [19564], [32061], [46711], [-7120], [-21940], [66], [-42594], [-6609], [10098], [-41824], [-28838], [16165], [-40114], [-6515], [12790], [4788], [26989], [-55518], [1654], [-39911], [-24939], [36278], [-11368], [-53912], [43562], [46994], [16130], [-44426], [831], [-55757], [43461], [-18818], [-58586], [-58792], [-53092], [-17055], [-1184], [-5808], [69956], [-59824], [-14258], [65183], [46805], [-27469], [23501], [-48213], [-2848], [-22810], [-49842], [-60702], [15158], [-13325], [-14030], [41279], [-43620], [30450], [-2632], [-84333], [59490], [-706], [-42598], [38207], [79067], [31012], [19626], [-81110], [-37888], [60], [-87612], [-41264], [-31944], [6163], [31386], [86359], [32556], [-10334], [-21121], [-10746], [-37205], [-8321], [-38602], [63539], [104914], [-76839], [51126], [39042], [-57360], [-81363], [57840], [-5021], [-11958], [-68300], [50724], [-51994], [540], [-21840], [44200], [4559], [1824], [98853], [18], [-41558], [66218], [-20342], [-61730], [58956], [15773], [-89693], [103], [20483], [-69458], [71324], [66522], [-136825], [1911], [-50988], [-107526], [108941], [92992], [17086], [-12896], [112069], [13242], [49256], [-72497], [-94492], [39384], [-61798], [9364], [90630], [-14982], [33824], [-113592], [-4553], [92161], [-27762], [96139], [78410], [-9792], [32232], [-78732], [25936], [43005], [-43934], [-88196], [-155990], [93666], [63722], [113026], [45717], [59093], [-99577], [28166], [-69330], [40265], [-156841], [-79138], [179206], [156606], [-39390], [-107142], [1408], [127789], [122148], [-39456], [-69762], [12560], [-19512], [60678], [30930], [174185], [-164485], [-137650], [-139028], [177147], [152052], [-51724], [12882], [28335], [35469], [61740], [9280], [54593], [-95960], [196222], [117531], [-1625], [94530], [52392], [-23952], [-52498], [-7776], [-106484], [29815], [-128472], [77433], [3170], [-43004], [-190860], [175110], [8472], [6113], [-89764], [1073], [92664], [144828], [70427], [73412], [161321], [-132570], [-19780], [-92488], [-215056], [-150735], [52784], [-51117], [148256], [-176210], [-98218], [-21192], [111092], [-34213], [-243893], [-211240], [71391], [-78075], [4183], [83531], [1390], [-35406], [-101921], [28718], [64586], [-7715], [-16100], [46595], [12641], [-240546], [-187724], [-226175], [-24839], [52250], [149270], [-218314], [8670], [233532], [-97711], [48811], [-110668], [-14903], [6009], [-109188], [-249276], [-139734], [-187350], [120043], [-142589], [-250410], [213686], [-110716], [-24258], [-209694], [-127248], [-172562], [-226094], [165537], [-120623], [96032], [35234], [55096], [-268372], [-181263], [-16], [110696], [76646], [-152259], [-210492], [-147111], [161930], [-161128]];
    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_f();".
// 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_f(: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_f();".
// 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_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_462_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![-1, 1]>],Snew);
    return Vf;
end function;