
// Make newform 546.8.a.e in Magma, downloaded from the LMFDB on 23 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_8_a_e();".
// 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_8_a_e();".
// 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
        poly := [97248, -33506, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [-22392, 163, 1], [22278, 179, -1]];
    Rf_basisdens := [1, 114, 114];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
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 := 8;
    raw_aps := [[8, 0, 0], [27, 0, 0], [-126, 0, 1], [-343, 0, 0], [-2023, -13, 4], [2197, 0, 0], [-4077, -9, -44], [-1612, 148, -109], [37202, 34, -85], [35138, -168, 77], [-87687, 755, -215], [-195323, 129, -864], [-216536, 386, 536], [-25394, -14, 527], [-423073, -1755, 1063], [108837, -5635, 2983], [-1144772, -4680, 3524], [-483499, 4897, -2470], [-434124, -9488, 4866], [-1728692, 1100, -13388], [-1220980, 9738, 6511], [-983417, -3531, 11521], [-4550075, -11333, -5169], [2126511, 13335, 11553], [-6010745, 19319, -38671], [-2468288, -12034, 12504], [5327705, 36319, -26114], [-1180190, -25758, 21668], [-10746435, -22823, 7198], [-5437725, -54565, 32237], [-6579722, -6022, 13504], [-8605255, 48579, 12562], [-6079949, 141615, 17832], [16773158, -98970, -17602], [881060, -33846, 62584], [-14215061, -23987, -45486], [8933795, -47925, -55444], [9623002, 142738, -86864], [-3931928, 115676, 72963], [13392728, -210874, -88646], [31245089, -301065, 131689], [-6316158, 104572, -146136], [-22429415, 15223, -6926], [39126372, -121698, -330868], [-57485336, 447958, 162544], [70136771, 522581, -398598], [-62090020, -388244, -154169], [-38328563, 19799, 145239], [52751046, -229642, 195716], [61102658, -131812, 15756], [-88029545, 712407, 229831], [-18712088, 175288, -462824], [-63598273, -413873, -23095], [-180652013, -865319, 863784], [-110837378, 256820, 552900], [111378831, -405955, -742597], [76987252, -350870, 705082], [-149366050, -50174, 976092], [-164893217, -306885, -94383], [268909894, 344132, -972032], [-61972480, 1021860, 629988], [-431440743, 1059865, 320335], [123221975, 1347449, 913391], [141011736, -954936, 1557294], [54216148, 2457606, -199792], [-9724542, -2420628, -232594], [-660021888, -135108, 229458], [-42678158, -358820, -1323101], [-92356608, 1919844, 2231452], [-401919233, -2583565, 1119641], [-164248002, -1049564, -2072136], [271842496, -1557480, -1044484], [-1117524706, -499846, -1408426], [234304924, 4851938, -4107404], [108130394, -2812718, 1790326], [-1111010281, 2390649, -16478], [647776006, 4477096, -241728], [859398371, -4919905, -146399], [-471200828, -2142922, 5864198], [268258794, 337276, -4076837], [535287161, -3120053, 3248420], [579968776, -2301218, 3511274], [417820740, -3136684, 4091862], [-407454766, 6664984, 2945598], [595590915, -3597491, 1651020], [-1172661081, -2610503, 5366153], [2346937207, 5372723, -4750080], [143863730, -125632, 5267126], [-1494111103, 1778389, -3204962], [-483039955, 7943339, -1701604], [-341599741, -5342991, 12035360], [138945390, 3295390, 5201927], [-137403926, 2292502, -9836718], [1106375474, 706866, -7843852], [-48593988, 5927488, 9585996], [-172548096, -11307392, 6224686], [1319427584, 12416266, -8401707], [266974161, -6096623, -421732], [163190946, 4018250, 5190862], [-484673641, -17768153, 10496584], [1912963511, 12010985, -3965189], [-4184019438, -14150580, 8762614], [320954277, -619625, 10723246], [-299391755, -2941847, -3844725], [1863843309, 9141043, -16947803], [3973403866, 4612504, -707060], [3232227249, -3443945, 2114519], [3961513233, -9478731, 11437955], [-244888184, 16457868, -7224481], [728163228, -12740482, 16221336], [2662759823, -849623, 10730000], [1451882879, -21039241, -5507240], [2192988459, -13213793, 23516514], [3278691871, -7778907, -453914], [1828222421, 26032171, -4995872], [3555381671, -22477097, 16689737], [1551238037, 11702407, 28003550], [4134743250, 20645982, -22296654], [3728579669, 37140465, -35450126], [1140932553, 14290039, 13015931], [5883396871, 15460771, 1016885], [8542100182, -17546616, 8429267], [-6236692318, -13274212, -5419692], [3914524641, 1831555, -10358070], [-722420777, -2455415, -13785493], [5362055595, -32419097, 24484433], [13265118634, -1749868, -8130324], [-1899451864, 47659168, 4222334], [3839634027, 36563685, -36426684], [-1716375179, 4951293, -35999587], [6233157684, 13663392, 24105838], [7333952722, -28011338, 12761852], [15982069997, 2892319, 17135257], [-1624462893, -21275129, 27960849], [-1591778153, 26818711, -9975925], [2050202500, 13559346, -9371043], [-4174535971, 39635217, 26333850], [-890167244, 422004, 36623395], [-10054895348, -47976166, 57304056], [-1608462117, -24710021, -68073583], [-3753744149, 18625785, 5780254], [-8755050516, 8902738, -48703452], [-576069972, -11304676, 41071736], [-6561141945, 24596989, -81860646], [3333376357, -70077967, 25304178], [-14119941788, -24938428, -45432288], [-3928061797, -50380649, 38506183], [-12142602182, 29054464, -16277996], [4158040318, -56147010, 30690462], [-6446026550, -4685466, 21952820], [7160253372, -33274446, 2440454], [-1416736403, 15986613, -2520282], [-1360963975, -22562637, -44136798], [-4257298688, -70479224, 5367048], [3434624281, 111338455, -79513835], [7188887186, 117499298, -97094599], [2325987058, -28930634, 17259108], [-26340650769, -24885401, -40585249], [-6115713892, 42799526, -112338898], [-13431331977, -103704213, 103623065], [-9244398695, -85465477, 16027696], [-21508023683, 64568809, -65740251], [13492033323, 8641333, 94800008], [-11204777638, 58971138, -97937558], [-2018342509, 109193663, -44705458], [1608704026, -129751606, -626461], [-8703506778, -27925910, -98690776], [1614135172, -6408286, -90298574], [-16050281346, 92867828, 18866768], [-25885353666, -97722952, 101421030], [-11064453963, -10274905, -45990972], [-16031520102, -87564052, 95437276], [-8685987157, -29372511, 106102315], [-10347901708, 71733310, -108090126], [6385691179, 147081701, -171540438], [-12136183856, 185419714, -102436010], [-14335476002, -130990314, 29718824], [-33276139572, -145901198, 122090096], [6724016646, 172547386, -195390170], [-14491170857, 54895199, -90199342], [-3358524787, 117626835, -66697998], [-58170902134, 8759578, -51041630], [26159658526, -178714000, 40527272], [-35985765983, 27928445, -54302425], [-2020230814, -100547322, -70742162], [22955757180, 76250706, 132339696], [15379079659, -95522721, 162485183], [-25267133292, -7208412, -64903853], [14231350738, 127547912, 79126112], [-20920852992, -59288900, -89962169], [-12563751790, -75712632, -899746], [-8632174291, -112760369, 200568608], [-3455932103, -67208465, 241819213], [-70086231850, 147983924, -32111791], [53090800421, 64974971, 125142343], [39376283243, -29604617, 222273792], [-18358837320, -212872278, -43318408], [13033150459, 88585031, -98413203], [-15283998513, -15511761, -269895319], [-9119102522, -73379046, 127392014], [5150574050, 365007292, -289241058], [-26260808211, -64621197, 16087394], [8793833779, 284639159, -109820787], [82346777408, 91745530, -150615374], [-6428289441, -284690455, 148129755], [-19356082856, -200273802, 232364314], [22891557585, -154377429, -125615771], [49262869005, -212680261, 35694505], [17851286105, -111768135, -290268200], [-47326313466, -70138938, -66022810], [63860859818, 3561772, 245198221], [29713571278, -31464504, 75244950], [65409904294, 434819202, -368132436], [45703287710, -26075258, 116556340], [-2688289298, -315682034, 272657445], [-10201637608, 137771334, -410458247], [42455608199, -29286603, -77983543], [-25954800401, 341890379, -116042808], [27805475604, 13917524, 293097580], [65773714054, 401923608, -354210918], [83854704317, 24667069, 402019777], [-25711851928, -127280240, -19440856], [-46579694761, -96279069, 299604868], [76271721850, -368601074, 13067982], [-75083473463, 441216843, -34019572], [7068450729, -111325975, 24938115], [-89927840289, 123502083, 51126180], [11688747927, -304441363, 403907183], [30626148207, 39127889, -93512484], [52930734199, 184967589, -164188188], [-49864451070, 63496916, 271427756], [18763471246, -363702882, 488821464], [-68830175955, 657962259, -178336558], [-52108252964, -4453042, -586843112], [-59633534160, 23139516, -69462782], [-2574177634, 310445942, -297391867], [-95604672263, -316972791, 255534190], [76468219422, -56963352, -243993614], [-46300722343, 233149119, -219652485], [51113606558, 370467474, -125074710], [21961351120, -11961492, 417753892], [-136370892146, -31369618, 72331822], [41144871361, -649643289, 116888218], [-53698291690, 438816528, -238835280], [-98111522001, 486328175, -516625035], [-40186917772, -508725996, -277855568], [80575426625, 320406371, 37208343], [61890600401, -727475049, 20248789], [134142302666, 241185322, -287002420], [-19209440818, -566899018, 498678229], [-12142014643, 160162337, -329352512], [33383970380, -196203626, 346866084], [149261772526, -631960414, 195728798], [78457842718, 71122172, -230945392], [-178096622524, 35310082, 255133516], [121596206893, -34073009, 147187250], [47071705743, 866104931, -654569537], [-43039544188, -789735968, 413932960], [-69932468596, 291011578, -813488566], [-203515558544, 141077686, -680800409], [-45808585963, 207052535, 99489789], [53676880075, 403345205, -330062685], [-85234202701, 275209495, -514396607], [183586283789, 432588345, -510025594], [-43880402912, -15585182, 221233548], [-287216513736, 28595948, 227318342], [-296227537362, -22706344, 168803440], [1770304803, -1196685197, 129053813], [6296894406, -571645466, 1060104662], [170605758478, 350437936, 73822116], [-250503220286, -254855564, -224313732], [-16633185546, 596406470, 264604128], [149661226671, 280670011, 411340590], [-43589196269, 639743873, 282493203], [21097655098, 732286416, -845360186], [-81130528596, 71070860, 620120840], [44916497149, -275666281, 939436842], [94932816669, -1266248367, 81366368], [34648512490, 241317896, 71721990], [-20655464872, 1304623860, -454656052], [81655317518, 367218026, -9554214], [-245146315905, 486377313, -22890608], [-174607229668, -1365546308, 970348260], [-78871965035, -326622507, -172856187], [-229731671605, -133870791, -48714202], [14847558289, -224533941, -167368995], [-243683512988, -480255886, -489365959], [-250859436928, -174456602, 381432678], [299671814764, 635422700, -556050704], [-184905846027, -99672347, 564970088], [306899610940, 377828878, 516661433], [-156502958817, 199831101, -746113830], [-88265229583, -1199975671, 389477366], [140128326553, -61417737, -200041287], [-61948007085, 571577279, 604476837], [355219265586, -570644788, 613774794], [-21670103922, 462136218, -229054506], [-13832234728, 758584942, 130107798], [54925192407, -213165171, 1263700222], [22970582370, -1045810554, -215918381], [-276152824936, -213945854, -895429652], [-82375088282, -540536248, -431882646], [147442215475, -570920647, 251707629], [-174921699255, -251774737, 939114241], [230016089794, -606922122, -379633295], [-431226854484, -640123642, 192982336], [284176764164, 406404704, -143153072], [191911792541, 1439039217, -903301788], [-25702606588, 1277864172, -1884444362], [-245158208911, -431017935, 17018435], [-210925543028, -17019404, -2054993036], [143537580048, 1277081694, -858601824], [-48481886992, 613942850, -98266632], [-141845463012, -2091307212, 1816858357], [167664767190, -1797313870, 71605276], [211454865202, 1588557824, -518082978], [230950375789, -1083454965, -160757363], [-74077379686, 6736718, 138836850], [132177957572, -1018533822, 124907745], [-344908228473, 1100896823, 868362781], [34028214934, 1608087126, -259982456], [-438037121658, 790889068, -555695198], [-8168082596, -706325542, 1243260012], [-270638372485, 20455221, -626166526], [-343880625793, -597336797, -497480360], [-290333514764, 44202790, -2499568522], [222225236788, 830632216, 112708236], [269639161094, 2033145218, -669911127], [32453427013, 1329245051, -2891449146], [23438359121, -1048162323, -820678610], [249875230603, -22886917, 1783377918], [155096986324, -683936266, -918642147], [-14304265938, 2838296922, -1129692362], [560123096677, -531801973, -206422673], [337359974946, 1698225306, -2375012402], [-292092958137, -408777015, -1817008279], [353641119173, -191587927, 1771478712], [-572795666820, -618381226, -891767368], [408278442954, -2497717728, -763087852], [383988511923, -1005859019, 1182970448], [68271074144, -3449119322, 1340871910], [-708519693768, 1654614498, -60159832], [27337840111, 1159948911, 597638050], [208229079686, -771773166, -2055494292], [-218337410740, 1233237234, -2442115898], [116834279166, -197404450, -2003633576], [125087055055, -889654305, 1837912910], [87172969234, 2184640146, -464346736], [-150794560975, -236692245, 1641642161], [-543303690694, 1525109764, 39282472], [-364132627983, -709746873, 1947182729], [-50949366428, 773525046, -332840940], [-413386658010, -1400125800, 2085284622], [751775895970, 1488264886, -14002482], [-166004205778, -1912805712, -182751392], [306072784269, 3013959613, -2793424182], [146050405356, -2290044296, -1406816215], [91853427445, -961535885, 2467463695], [380913247158, 2628119196, -2666514104], [-694826820838, 1004721926, -397121650], [-55140141235, 2255288929, 870787210], [356341638573, 1064204077, -1363237414], [240465518182, -2395966554, 3464225359], [405030479160, -322772612, 3081977276], [155410400232, -24678724, 395718318], [116826399061, 294628585, 3656198959], [-429023617576, -2030827630, -115455383], [571659997927, 2236381881, -545227304], [317422408554, -2531511660, 1947928777], [-646787558, -3673940078, 584812252], [-231635476130, 1233793742, -1057770794], [524852733388, -881976524, 4186268176], [-276040914314, -608759540, -2226696699], [872055993767, 639653473, -260898494], [-367066368221, 965743543, 120343867], [-15457796224, -889070452, -2910698256], [175938991279, 753415617, 579262670], [89206653813, -996342083, 1298671110], [-86778177805, 882886231, 487447116], [-403067528754, 3519140716, 1226374660], [214420855001, -1804591503, -1714922453], [-572701812157, 915601815, 1297935118], [-157191640043, 2637700467, -1870708350], [363792598080, 2971336618, 1518055022], [382067793955, -835852035, -393059433], [-219447052050, 482383950, -4555213123], [269618595129, 1795631399, -2755014418], [63362378191, 1255730631, -43132028], [78744865516, -3328502056, -1927484384], [-997738337986, 1005277074, 1333370384], [-8552795358, -4278256992, -1690266704], [706202719679, 3882446235, -2379739587], [-69150485396, 1237119928, -1644324540], [480261279588, -5081145960, 960362232], [1255197281924, -827042812, -1282634644], [-1179982648761, -262327473, -1015386539], [242046325050, 1151467734, -2310873652], [-223276195523, -5999205187, 3671778442], [-198932529980, -1641550796, -2126986255], [-271586516412, 4710300034, -2890880722], [-430327704245, 6044182815, -1522909281], [-45435506791, -674719091, 27302199], [-440816490403, -2682907077, 3238363212], [-50578140355, -3499686839, 3461469333], [-85428824419, 58428581, -1660254661], [49146550641, -1694487945, -78617022], [-670193614084, -165601278, -2592452774], [-416149145629, 3143014999, -1672349704], [685176491834, 292787558, -492779355], [-777870039809, -5957698847, 5226302583], [193602081724, 710738254, -3758249830], [507120723604, -3416602650, 1427029623], [-670402725417, -2150109055, 1536039415], [199440939919, -24179923, 4149109702], [-643636513050, 4518042248, -2097147679], [62293455198, 28641824, -2468013962], [-300936962726, 4831501414, 2199303008], [118819805575, -2784312223, 6204085982], [-428868700622, 5836586000, -4870642044], [485268570024, -5228422480, 937205628], [154430704760, -5455176366, -1753905779], [-106570230154, -1879786584, 4161387204], [-861903901744, -528072192, -4160743436], [1122127935376, -2103705548, 1223777356], [-926601602321, 3819061003, -250728210], [-1095225089587, -5382167987, 5564485803], [138829101327, 5301958777, -6262893391], [-805552897976, -2375502598, 12966834], [176320403438, 1596477642, -3552057091], [595842211218, 5600600158, -4363943930]];
    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_8_a_e();".
// 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_8_a_e(:prec:=3)
    chi := MakeCharacter_546_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 8));
    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_8_a_e();".
// 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_8_a_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_546_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<5,R![2461550, -66705, 378, 1]>],Snew);
    return Vf;
end function;