
// Make newform 546.4.a.a in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_546_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_546_4_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_546_4_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_546_a();"
function MakeCharacter_546_a()
    N := 546;
    order := 1;
    char_gens := [365, 157, 379];
    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_546_a_Hecke(Kf)
    return MakeCharacter_546_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], [12], [7], [-22], [-13], [-2], [-88], [-80], [-22], [-92], [118], [324], [84], [-134], [-194], [210], [-470], [-292], [-66], [-506], [-776], [-778], [-920], [-490], [966], [-1344], [-52], [-1542], [1766], [696], [1504], [-648], [-1388], [2420], [-2268], [-602], [1216], [-782], [3102], [-2824], [-842], [2548], [-1118], [1992], [-3536], [-2900], [4212], [-2070], [-2198], [-3378], [-2874], [1926], [-2024], [5642], [-4680], [3758], [128], [-826], [-1448], [6084], [-648], [5636], [-4964], [5934], [2560], [4020], [4734], [-7864], [5870], [5532], [5854], [-7576], [-1502], [-8584], [8598], [-11666], [5334], [1852], [-13530], [1320], [6322], [3498], [-394], [10528], [-5028], [-3012], [-5506], [4764], [5016], [-8928], [3166], [-596], [-6940], [1156], [2068], [1140], [-13606], [-6868], [1170], [5524], [6204], [23140], [7086], [5956], [8714], [17286], [4936], [-18936], [62], [22792], [30022], [26640], [-180], [6652], [15258], [-17124], [7032], [-19254], [-1136], [20638], [14466], [3162], [-26334], [11124], [6270], [17186], [29244], [22760], [26546], [932], [-8418], [-15704], [-16734], [-11196], [18938], [-13672], [16484], [17778], [-33138], [10888], [-13716], [-32016], [40050], [46602], [-18578], [21254], [17818], [-29476], [21378], [-9218], [36762], [-40308], [-7512], [-26164], [-28300], [-15896], [-4108], [-43034], [-17568], [-8426], [-25938], [13028], [16524], [-18532], [16646], [-3016], [33438], [49854], [-20610], [-17194], [-45498], [-12984], [16134], [-7208], [14702], [-28708], [-15540], [22208], [-22590], [39904], [68092], [16322], [25512], [-37434], [-18710], [45206], [37600], [-43922], [-57174], [-30142], [-40350], [3116], [-14600], [9432], [-32230], [-36790], [-50678], [-10632], [-71712], [-44280], [-57888], [-8734], [-50014], [63354], [-22678], [35404], [28428], [-65460], [4988], [25710], [-82390], [-66160], [-986], [-31546], [-94246], [-55936], [63478], [54142], [40556], [-21474], [-84256], [54700], [-53492], [62952], [80174], [-97578], [80400], [-68072], [82358], [-15450], [100124], [48544], [28106], [-9924], [22954], [-3610], [85876], [13080], [59736], [82522], [-106676], [-38008], [-28754], [-12696], [60332], [-45264], [100650], [-71080], [-68232], [85530], [-52180], [12522], [-45194], [40318], [75450], [129074], [124736], [50682], [123186], [-3496], [19720], [-42262], [-102858], [46328], [10956], [-85520], [-44852], [110940], [-64182], [-50270], [76348], [-78242], [90136], [-100018], [-120816], [-842], [3934], [-39878], [21392], [-61712], [-61448], [-121224], [41070], [-32988], [37332], [-125922], [-93388], [10132], [89658], [-91730], [91780], [-110532], [-34662], [47734], [-166518], [38896], [-116270], [-24644], [76564], [45130], [59544], [-99072], [-59016], [77100], [-105410], [112416], [-147314], [-32870], [-65606], [-41564], [-136256], [56662], [88060], [139038], [-61274], [-185110], [-52950], [102238], [-143214], [66268], [-111086], [-116734], [83244], [-101812], [-58210], [-61632], [-116156], [63492], [-139350], [-84382], [-77134], [88160], [-98258], [115832], [102326], [120070], [-111396], [71062], [-99736], [56350], [-38058], [175488], [4088], [26904], [112128], [-170270], [130008], [75378], [186010], [22988], [-107826], [-208004], [137456], [199682], [-39966], [46794], [68930], [-190802], [-106926], [-18042], [81510], [9256], [-25066], [37388], [-143222], [33500], [71572], [144250], [151452], [231532], [-41602], [-34506], [159776], [-174794], [-154946], [64548], [153002], [-93130], [228898], [-149504], [952], [-33144], [-127188], [24616], [-185154], [14008], [-97006], [-131168], [-147236], [121406], [-35712], [40770], [4], [7158], [-44714], [28712], [62734], [62228], [-229880], [-23378], [100794], [-179336], [135096], [-215540], [-220960], [109018], [103972], [-256260], [-181194], [-38158], [163918], [188088], [50604], [90434], [-66550], [93726], [200440], [-12264], [-177212], [208026], [69030], [275974], [107568], [163570], [-76832], [-234372], [-153460], [114784], [-163176]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_546_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_546_4_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_546_4_a_a(:prec:=1)
    chi := MakeCharacter_546_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_546_4_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_546_4_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_546_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![-12, 1]>],Snew);
    return Vf;
end function;