
// Make newform 546.6.a.h in Magma, downloaded from the LMFDB on 24 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_6_a_h();".
// 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_6_a_h();".
// 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 := 6;
    raw_aps := [[4], [9], [-16], [-49], [-114], [-169], [538], [-536], [-4596], [1594], [9364], [-12002], [4928], [-14284], [-22262], [-474], [-4182], [-21830], [20780], [18682], [-37866], [-27840], [-101914], [-77644], [-60050], [-28238], [192664], [126192], [-161350], [162006], [22752], [-282080], [-83064], [-61420], [-108932], [345292], [331998], [144952], [-28494], [217194], [-594476], [-127050], [-415064], [409546], [757488], [-722336], [255476], [-203212], [116722], [-866934], [-1320890], [311802], [116326], [1030896], [525774], [1962708], [-870174], [-492496], [-201378], [1793816], [1139068], [-545244], [-1673820], [-1175900], [-513002], [-217416], [1794772], [4059646], [-3063068], [3313902], [-160208], [2238954], [1131224], [5033962], [3694384], [3630110], [3042126], [4871838], [-1279620], [3482862], [-4323760], [-924310], [3981542], [6377486], [-4367840], [-2502696], [-6848524], [-897250], [-448184], [6985832], [-175272], [4638798], [648212], [-9817552], [10987316], [-690876], [-8587496], [3131862], [7722876], [660842], [9397948], [-21324], [-8724468], [2976086], [-6803772], [4456802], [-12407698], [-7156820], [-14033860], [-13966138], [-12937848], [2873270], [14711072], [13662428], [-8583324], [-10512702], [-14860804], [-4888768], [-15485334], [-3751980], [-7203794], [-9039118], [-12283050], [-9120258], [8202004], [-19049554], [8840882], [-8917572], [22711296], [192242], [5595892], [12640602], [12162960], [-16758502], [-16543920], [-18475190], [-138524], [-78668], [15235246], [-19547842], [7933400], [-23988588], [-9361584], [-26974042], [-13343566], [-12403890], [-11273890], [17972238], [-33107932], [6899366], [7267678], [-31213882], [10543396], [-5578240], [-5614748], [-4208216], [921112], [-35330864], [-45027458], [-19220892], [33640762], [-42015058], [25705196], [39675740], [18571660], [-41757250], [3035760], [-27308042], [-28124618], [-5894118], [61018106], [1759566], [-15441764], [-24036322], [13418488], [-34173190], [-31130180], [29824188], [28156712], [-2913934], [42338784], [-22405812], [61856362], [16683356], [79636362], [78493094], [20109342], [48797136], [-41783850], [-11816574], [-45695014], [-30996602], [-10021220], [-9811564], [23650092], [-24680762], [-37782414], [-43592414], [80059908], [32587608], [-50781152], [81639584], [-52827854], [-100126342], [63617034], [-45829242], [-95176628], [-16483680], [-58827596], [77257412], [48940670], [-26897314], [-21898096], [-94451322], [-22018218], [-56139790], [72490016], [66873762], [77058210], [-83817556], [-84180602], [-51235904], [95787444], [101592036], [-90742600], [-116171194], [-74068246], [80821212], [-2344592], [99524018], [26222462], [135957332], [21462576], [-49451270], [133578468], [-86628014], [-168318954], [-36774804], [-19462364], [-92240008], [-72116458], [-122431124], [93966440], [-57095658], [48319060], [42923156], [-152131472], [56487570], [-180130144], [-1059964], [62762122], [172044256], [-43097434], [155340166], [188914314], [-142525154], [16586202], [132733136], [-118223482], [-80281438], [66406736], [-150845356], [64941442], [128673966], [-19042372], [-37805652], [-98330120], [150915280], [-7843028], [77646858], [-179876558], [24536172], [92557214], [-168380784], [-151946722], [186841856], [193560138], [-14601482], [68641666], [190677080], [-228022320], [172613480], [-243028712], [32773798], [221015924], [-134092376], [-169900034], [84804588], [135225028], [-272550354], [12605910], [139110116], [-221731284], [38052002], [145611182], [-175537642], [-9391792], [-33869178], [-41565948], [-182110668], [-213711782], [81377480], [12016096], [-257490884], [242209236], [111025342], [201103500], [-38122514], [331834646], [83206946], [184274820], [24641088], [178283198], [-320545180], [141156274], [121094342], [-25850358], [-159870754], [-150456458], [40961402], [-71819980], [-57623878], [-80816466], [218695748], [5271556], [-50145410], [-121252136], [-390319052], [192039736], [79825242], [68143122], [-444209262], [-111783592], [-1931786], [443201504], [-7119698], [-269104138], [164466072], [-101419554], [-172646688], [-186209426], [-375525010], [-77797820], [215234752], [414601080], [-151466556], [397075210], [-212094320], [65105706], [358733910], [219634380], [-64596234], [80508980], [-334661696], [6186474], [-20275090], [73391634], [-427390398], [385671678], [-302173602], [508075110], [277037050], [351708156], [-239651286], [-138057156], [77387578], [-13218368], [-114171636], [510892506], [-104158920], [-161856812], [602148986], [305515438], [-146346520], [-108546530], [374593150], [-456673392], [125382946], [-244470750], [-429331862], [262633980], [-227398704], [436710280], [351711012], [-635038120], [161594774], [362019544], [-409243302], [53447416], [302305684], [41983638], [316245204], [-245100506], [659844764], [-213660270], [-605623802], [759687496], [-291369494], [556662532], [164621816], [134795270], [-342767078], [87456712], [609905172], [-402451136], [-279429776], [-532164878], [621062492], [-595189940], [349896958], [613992914], [-402205650], [-776843036], [-55192628], [16772098], [201601822], [-111972094], [4208880], [457086780], [705934908], [22160090], [-391821674], [485361026], [49276736], [712246306], [307860972], [319983000], [400947228], [-739411280], [-450610816]];
    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_6_a_h();".
// 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_6_a_h(:prec:=1)
    chi := MakeCharacter_546_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_546_6_a_h();".
// 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_6_a_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_546_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<5,R![16, 1]>,<11,R![114, 1]>],Snew);
    return Vf;
end function;