
// Make newform 546.6.a.d in Magma, downloaded from the LMFDB on 25 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_d();".
// 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_d();".
// 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], [33], [49], [-94], [-169], [-1112], [-1909], [961], [-6337], [7015], [928], [11442], [-12711], [-15107], [18691], [-12360], [14110], [-53746], [-47748], [-25301], [-5447], [29393], [-77621], [73607], [-44706], [13572], [71900], [-115806], [-229915], [134652], [-6608], [385392], [-248546], [-284302], [313644], [-406868], [-480068], [-326393], [-178548], [-336439], [-308606], [-299672], [151822], [-150606], [652312], [790909], [652701], [489132], [-147026], [168831], [682836], [196647], [1142566], [488762], [-122445], [1409264], [-249784], [-2253763], [-968738], [899352], [88569], [-66007], [1912354], [1742514], [-3332768], [1014018], [-3117807], [1949936], [710591], [3718086], [-3176216], [223286], [2563414], [-1241182], [-3306612], [-143426], [3592719], [844840], [-3223869], [6272070], [4624384], [2437770], [-2132860], [-2689322], [3597861], [-388068], [-2580412], [2656338], [4420074], [7870554], [9496567], [-9612518], [4529852], [-9861860], [7858534], [2148081], [-2793448], [-355354], [-7456452], [1108855], [536532], [-88124], [8437431], [290617], [-14964382], [10354041], [-353147], [7974513], [8152718], [2047738], [8154184], [12892962], [-5763468], [-1976150], [-10463127], [-13358520], [3418482], [-3418110], [13117975], [743707], [169353], [-7326846], [-22771044], [-9314427], [-2742087], [-25834534], [21822618], [-25130350], [-18970225], [5697254], [-535500], [-25057403], [-22599123], [1988001], [4409789], [12949406], [-23981101], [-12081678], [-4192575], [7729276], [-35846046], [27059472], [-5345052], [-27530670], [-2808512], [13073489], [17346466], [32282954], [12118080], [39551500], [-42814038], [30292500], [-17198796], [-8870071], [-33081757], [1928560], [30795509], [14143492], [26503851], [31518610], [28986609], [46878806], [25680642], [23991278], [-4775509], [-9820396], [-40914324], [13276962], [-24596844], [18350], [-28838298], [-6137829], [-34779240], [-12649256], [-8493784], [55384040], [-16659018], [56339966], [40636470], [-32989244], [62257906], [78176702], [31110897], [-11196654], [-21613922], [-39495325], [49088587], [-7375370], [-59839077], [-16233808], [-17026566], [43033649], [51260641], [67611267], [-55053208], [22455602], [-29748335], [-21978333], [68208078], [-92800752], [-24722880], [-17689837], [41825660], [-26166639], [7327148], [851515], [-22525551], [15735576], [28486250], [85462059], [55580], [-27198616], [-85470332], [9104357], [52693169], [-113253127], [-8108000], [-53810708], [13510076], [-103584831], [-13425616], [-79430156], [74348746], [70598934], [20565155], [87412272], [-39664041], [58622002], [29712098], [-115415550], [-97739836], [393316], [-30896530], [-56028978], [-72923363], [43199462], [130819204], [-69316089], [-32514690], [132459268], [12448210], [-15313300], [-144601382], [-71966961], [9383612], [52890717], [-186724833], [97329260], [128549175], [121535676], [146007278], [-132525066], [107585590], [-7687094], [-12451848], [118183523], [37350956], [158779032], [-145884021], [-20230423], [43169083], [61915655], [-105934686], [-198958690], [-159625758], [-94514570], [155098591], [2896314], [76960170], [27497302], [-27647192], [-153923750], [-211574081], [-134340484], [205119168], [27856000], [-129528548], [-113194988], [-173713348], [-137663294], [120294838], [156435828], [-40519185], [176660196], [-166535043], [-114302613], [-136447552], [-22164056], [97043820], [-36859217], [-101712824], [234798798], [-215554389], [-300910199], [-23129376], [-222556046], [11120032], [-221500064], [-56814665], [-260224682], [131987712], [138447165], [10681113], [204307311], [-88878998], [148274892], [92315764], [-317281430], [-318239981], [-323596124], [-281926862], [-177025034], [-149848673], [203545908], [-154972664], [83207519], [136200918], [-379641071], [136348029], [34468960], [236595604], [10009382], [349607940], [396161096], [-277301500], [-409375188], [163622053], [184596564], [-199214826], [-364100158], [119043635], [6590090], [-413780927], [308136062], [-208627417], [-123308348], [-336921726], [27820930], [-115094410], [-169540544], [-125801802], [-313536222], [-161737024], [-150302724], [-369353400], [-9110606], [-143420148], [453284163], [-330952802], [-445286305], [492448566], [257436796], [538133654], [323685326], [-249915570], [-465061395], [98168273], [-388411358], [-207607662], [-202135242], [189685314], [180834493], [-311888632], [-170136466], [-175616705], [-21461755], [76855078], [80884735], [-260061012], [-469029614], [12508580], [-120188433], [-628015360], [-230247041], [294460132], [-570747240], [380553926], [451383884], [196029202], [-487047347], [-568572830], [-253928772], [115102212], [-3337991], [301379541], [119481916], [-488942920], [-447354056], [73671688], [167544710], [437241303], [205007028], [-344980988], [21858996], [12987097], [-241748728], [679048462], [473801843], [-337800800], [-448171295], [649084491], [536951098], [197352771], [38004037], [-25341268], [810252664], [-430675664], [163211505], [-288357483], [-837385336], [-463697915], [-675198657], [-636177612], [315519623], [-352866712], [394748892], [186207904], [299181186], [55512556], [311306043], [-161174058], [-306818408], [-732493932], [842186314], [542365621], [130781949], [768845780], [-567992663], [-71608230]];
    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_d();".
// 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_d(: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_d();".
// 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_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_546_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<5,R![-33, 1]>,<11,R![94, 1]>],Snew);
    return Vf;
end function;