
// Make newform 324.8.a.a in Magma, downloaded from the LMFDB on 11 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_324_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_324_8_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_324_8_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
        poly := [15456, -3156, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [-2, 6, 0], [-6318, 15, 3]];
    Rf_basisdens := [1, 1, 5];
    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_324_a();"
function MakeCharacter_324_a()
    N := 324;
    order := 1;
    char_gens := [163, 245];
    v := [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_324_a_Hecke(Kf)
    return MakeCharacter_324_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]];
    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 := [[0, 0, 0], [0, 0, 0], [-19, 1, 0], [38, 0, -1], [146, -2, -3], [5, -9, 4], [-413, -28, 24], [-2842, 90, 25], [2734, -124, 21], [7613, -341, -144], [5396, -684, 84], [-34915, 1251, -76], [71246, -794, -156], [-28270, -54, -171], [-124560, -288, 858], [-34594, 124, -396], [400468, 2768, 582], [-263515, -477, -1008], [-663802, 3834, 1545], [-373826, 7940, -705], [-670321, -11646, 108], [-1703710, 7704, 5191], [-2802960, 8352, -4182], [-1137653, -9100, 9060], [-3644290, -17910, -5540], [-7269446, -25342, 1932], [-4782976, -46944, 3546], [-1665508, 2344, -15048], [-8037943, 33327, -3972], [17771271, -44448, -8364], [-15605242, 46224, -1395], [32471426, -2054, 2979], [-29401465, -62708, -4452], [-19387288, -60840, -20590], [52364785, 48605, 1800], [-30279964, 95508, -7458], [-40465075, 182637, 43188], [-36459004, -113256, 57074], [76485018, 106260, -13011], [-67633431, 162915, -1548], [-18116068, 210784, 47376], [-52656106, -127152, 50372], [-89816546, -155752, 35727], [-55495825, 32418, -108408], [-17996855, -291361, -106224], [-44972260, 59292, -191192], [-41212966, -151866, -52739], [-32496670, -64008, -122997], [-97888902, 249234, 33135], [-55204195, -530613, 175728], [-37306705, 220486, -73056], [74319120, -909792, 37782], [-52531909, 146448, 175712], [45398870, -831122, 159567], [-60354477, 1029078, 197076], [2734696, -480664, -119838], [-63470107, 970705, 228000], [108596690, -898452, 304467], [15287390, 558504, 86136], [207756815, 1827994, -311604], [100934996, 1395072, 264336], [-269444255, -45859, -658176], [278909660, -207000, -119640], [-424890136, 949456, 18264], [184444175, 592884, -396304], [535286785, 255371, 728364], [443184686, -1928322, 90397], [230269310, -941022, -606268], [-670268298, -1425066, 149697], [264938258, -323046, -451804], [977743970, -1737728, -250632], [486513942, -3521148, -212127], [664131488, 1411992, 437412], [259044134, -1064772, 827944], [706138484, 4132800, -627220], [-706906810, -1379132, -531093], [1211530150, 484424, -788244], [63993377, 935541, 605280], [-885874585, -1921478, 716268], [79104275, 1919430, -294520], [-613844640, 4791168, 990852], [12614429, -2249739, 1927124], [700483816, 1640240, 421470], [-3730309, -2550744, -689508], [413446472, 6254424, -1166934], [-1033943760, 6760944, 2075466], [-785488362, 1859802, -2153112], [-556739965, -3322530, -2557380], [801235406, 1823524, 2956416], [590563076, -706428, 1767166], [807476972, 8589856, 332910], [-2006702502, 1264488, -1648443], [785772380, -3860244, -2296136], [1375503694, 7226426, -2172021], [-206631058, -829926, -3870369], [1059081120, -5881320, 1909560], [1342134254, 1923460, 2868600], [-2108656250, 1227158, 1104312], [-956491390, 14055894, 2395701], [-2350844239, 8042913, 1719032], [-812465320, -13419216, 1931526], [-2796329615, -1966837, 1940712], [3805336080, 9538848, -3399978], [14637907, 8596286, -4458516], [-6303916, -103176, 2261606], [-4479269293, 7477902, 127824], [-6574125590, 7075610, 2604945], [-3564496841, -132808, 1299180], [4311398308, 1634684, 5756976], [-4861530721, -882342, -2085648], [-1371310030, 7273440, -1356595], [-4068906130, -721908, -3651492], [2534737055, 22749394, -5050824], [258666440, -17788752, -421608], [-153790204, -17169588, -3808762], [1595545499, 16667764, 4751316], [1046622200, 24307128, 4533462], [3083497338, -4075788, -3476343], [1552219750, 7233524, -5917404], [-6144055634, 1110530, -6890175], [-5893782091, -6143841, 3394296], [-3644552521, -443826, 5661988], [4170240382, 30697520, -5373144], [4004852400, 27959616, 1339554], [5924622770, -22991742, -2541033], [-4403805799, -134681, 7057896], [-4136938651, -12241485, -9805640], [4893722058, -23969496, 5579691], [5071742450, -14706468, 9130113], [2287492214, -21306636, -10526052], [2109763172, 34600104, -12772524], [-2277163284, -27849396, -8824956], [9017817650, 2565864, 1210421], [3946564430, 32472540, 3989100], [-7978531089, -8060568, -9834792], [-4938647233, -7615998, 9292108], [-4305245475, 11300745, -6284460], [11536720562, -21961710, -2919999], [-11913487995, 3615465, 7702260], [-3324467213, 9583082, -7080132], [15481674704, 5439744, -7745904], [16467318061, -19586833, -1105872], [4078240916, -5269572, -15346700], [9622471690, -2159614, 2523549], [1703585342, -13258728, -18232292], [3404199462, -2656248, 2094153], [12543767654, -39296484, 4160136], [15444070275, 28628214, 6997776], [13257065672, -11946888, -3749962], [2738511934, 41314772, 18700185], [-10566735535, 96291045, 11990600], [-3223729474, 39342598, 10614552], [5288978144, -22729032, 20226994], [-7956718570, 30815632, -7264167], [17017047512, 38818728, -13361372], [2060893314, 25530552, -19409637], [21083883122, 31970844, -9531309], [8990970363, 9836064, 3072456], [-3259219513, 6679818, -13366672], [-5492030495, -7198537, 1278912], [176951658, -101258130, -14948301], [-2948884845, -78012822, 10004532], [11664560882, -26896032, -8689767], [8122136994, 24634674, -24219009], [4659970150, -80541334, -19287936], [-7381673864, 131805008, 19598940], [-7450050034, 111166668, -2420673], [-6120480043, -9972603, -45389676], [-23558753413, 79012620, 24746640], [12448468046, 83848480, -5558712], [-12523195588, -19060808, 40797348], [2264561609, 38685177, -16453272], [18455330744, 37322128, 3779382], [-24474957469, 9667620, -28812112], [-31272084490, -40992696, 3797041], [21648671662, 12624458, -33293808], [-19133045470, 19141614, -44300319], [2829486369, -85389105, 26136600], [-29489581780, 41788908, -15972698], [14210190233, 47659293, 20166912], [-36628246420, -205180092, 32826302], [-17531514302, 72370670, -38633085], [-40031225599, 3097071, -13306548], [-2248765242, 221417322, 21976212], [38083038500, -11951660, 35552490], [-36406412927, 99363311, 12958044], [-34861768687, -86413617, 14776236], [2665562906, 102035754, 27290009], [1455269882, 183820248, 2767552], [-28280555270, 7093916, 36045789], [-13424408989, 31578534, -64528856], [-6716056320, -206584920, -4011420], [-65872942402, -118988010, 24545915], [-46082196971, 147034997, 1671708], [-5654399750, 11518838, 45899907], [32807252554, 57576872, 65388480], [-49005010129, -174149676, -400484], [1196871470, -106214976, -50670284], [-43452834257, 32317148, 15588156], [-49477850630, 59450300, -28124145], [21902033637, 154532151, -76006596], [36780791246, -230282712, -55646543], [34096456202, -279474174, -26180180], [-36547147273, 108749880, -23458260], [1486718354, -255744470, -7804845], [28761232150, 96860672, -29527872], [-55389713002, 180313128, 18303487], [-12894529660, -399180152, 12550722], [-30672296810, -52498702, 20813412], [2213298530, -248569758, -44625097], [78980308718, 107310330, 5787404], [-7301382011, -293690377, 20882232], [-42043940410, -118336212, -36340473], [43426248490, 31191722, -5780667], [-5450128392, 67908864, -92912904], [11896686374, 124380954, 52519836], [43928593538, -93326724, -32195517], [-931931409, -233662614, -116957736], [-52364709952, -201170864, 8939358], [-30524341259, 273877859, -26101236], [85535368961, 343673847, -49777412], [59992955618, 213506532, -35541147], [-47844490033, -309596318, -34625232], [23180172140, -197153820, -600090], [-118728140568, 47942376, 53244060], [47243137193, -145195821, 111217216], [2774000574, 387038142, -44815980], [-82120844614, 536360260, 94346445], [105627654380, 357547356, -3330586], [97278866912, -73717760, 147517530], [117566172341, 182623185, -3863992], [73172051282, -47828826, 125937351], [-66206329690, -39668796, -5968659], [-82543076137, 226591570, -15277620], [-6773203780, -67808952, -48375088], [127526929772, 23413540, -58717254], [136406631443, -325178028, -95963312], [38184648426, -351529470, -24965412], [-121921352640, -289172664, -17720916], [-54011723614, -96055436, -202456839], [108554969686, -208711894, -79596693], [-67306658590, 253162494, -43880439], [120553746860, -572547420, -117936750], [-7200595090, -212821704, -214524036], [-8178102170, -480645046, -48232224], [109121089838, -162220496, 65989941], [133641472430, 33044148, 220472107], [-137220343210, 223703074, -35186889], [-149482557508, -510627456, 45644166], [-133829540640, -484362360, 75321480], [100423738805, 329697063, 87726192], [62936342871, 204147624, 246424176], [29947556860, -442223548, 6795138], [151078417367, -82338210, 40142940], [-20644364595, 159727575, -104206980], [46924880410, -432917218, 142663743], [115735508981, 288035667, 103116708], [-188784452422, 75908394, 83619495], [61547997357, -539696457, 107787168], [-82967326738, -504341730, 114211236], [143140785200, 329896512, 107827168], [-10290003840, 351316368, -235566888], [56491207013, -570069423, 109322328], [-4504967230, -814681206, 194437076], [38470767650, 475292800, -1151760], [-110776429972, 78532848, -156488688], [25866160465, 173521643, -159414228], [-114159567369, 11587746, 61961244], [-21009332920, 668821824, -243551904], [155757325809, 220221267, -140854020], [-217712464219, 73049301, -66788056], [-162472882090, -139108986, -147449989], [-77610735730, 950238666, 239256264], [26199009092, 1021448196, 142127484], [-4169064073, -266903964, -130210020], [187804473920, -493527312, -83059698], [17761362608, -601032080, -10470816], [-188597356003, 495607293, 42247092], [-140076336985, -750812274, 134821404], [-154398404588, -415887160, 138121590], [333192314180, 33920116, -25735506], [40405558280, -326786544, 39960444], [-108223691908, -895986716, -88056708], [-155578999315, -262312929, -230980956], [-110662044748, -468416880, 107514906], [-69497558796, -1444422156, 107351556], [-148266410065, 767016486, 315399004], [117776313910, -73067284, 93906384], [121176970142, 517493376, -129781281], [-21166927845, -736256262, -117859248], [-92027161746, 689842872, -394575492], [190923577910, 745077286, 211845969], [239588572999, -951797686, -109439052], [-76121761524, -339885480, -299539500], [95239892141, 196880031, 399238452], [60424771753, -186455053, 354626748], [-116742572704, 972579528, 289317952], [53702499709, -1289266795, 29929812], [298064098662, -892274658, -185301417], [-15535975492, -530188200, 225471394], [17095725419, 634239162, 185545240], [-62887537758, 932597634, 91864452], [300612103238, -307023084, -218127381], [-17154163294, 592466686, -24593043], [-257432909824, 972614952, 4151948], [-114496761058, 93027726, 100427280], [-195910124962, 674054986, 76260723], [16001393582, 1366493796, -270903936], [141653127748, 1105383884, 295380036], [116558817905, -185358195, -90775700], [-26662045846, 1245789256, 189595941], [-62972135322, 313039572, -221025312], [242420190039, -1440605358, -5703912], [116266207190, -845193294, 333826069], [-30392799968, 299376344, -368483238], [-60421315858, -1571677866, -146331924], [-104964206548, -112850528, -249570762], [6529991896, -1140584032, 51119232], [49901782715, -347588442, 213197112], [-205292194602, -1124053662, -21433368], [-59148148612, 453486240, 126843210], [-5505841282, 2165564754, 100709080], [62986242749, -384847367, 437129532], [370597227824, -237032064, 388742556], [323904905830, 1213117694, 251146536], [-48298449541, 265526172, 10343828], [18786083078, 426175542, 313365183], [174794080454, 787206222, 383844195], [138352009710, -1700068764, -620999241], [-310049241874, -2019202400, 245288928], [-79959666586, -943223760, 523142880], [-428132529455, -190821289, 212638344], [487364049848, 799571880, 181529840], [72917827074, -318289662, -71224671], [-78687155014, 844860546, -481403541], [733805813540, -701414336, 41584566], [-17772695083, -683665245, 583030260], [-84227049109, -1442942594, -434753628], [-138889780186, -240583446, 529740756], [394729058540, 1264307292, -94679982], [109697623505, 141709383, 450903172], [424161367622, -1658047238, 136483764], [-312037700007, -979000665, -59434260], [335787267260, -376401708, -560599792], [541219415821, -287769247, 555200400], [699920808470, 950222206, 191167029], [-210079467439, 1191312369, 586973796], [4190306480, 946113480, 201244140], [-113194365956, 1843983548, 867493632], [-4895017315, 90364867, -914734032], [-113047525234, -1114650810, -708990865], [-210877977265, -1278212688, 303313648], [-530455262183, 1646378315, 423778620], [680959655414, -1626369696, -168304647], [-71737668907, 738189513, -80957508], [-537247368469, 1178933680, -29307012], [199269971832, 656211912, 47043198], [921276141694, -120737266, -109029249], [-306572460818, -1679291986, 666214272], [-42523431130, -500921360, 161675535], [-27410870935, 1595052225, -195725180], [208942811219, 2150111320, -708136200], [-62910892938, -833733816, -240429483], [-251705996492, 206551976, -663063546], [-243962241460, -1153892808, 191410018], [-34930142665, 765941994, -675339844], [80467691590, 2599139852, 516603228], [663221916380, 706312548, 61002532], [-81583331686, -1968300864, 352555024], [-587258788486, -446870822, 187884537], [-490212089050, -594921834, 407529659], [1140585876584, -1138090112, -175278216], [-160114691447, 846238265, 257841180], [287915675396, 1603006452, -194025782], [-312631741267, -763884279, 457968748], [-112615007178, -4189278210, 275715855], [-32320536324, 309710004, 459895026], [-493358098345, 1001508948, 382846672], [-76498900622, -3819779848, 6231528], [-522613243633, -1231355232, 533486748], [-137840413843, -469046105, 241853460], [930756019279, 576177008, 957738264], [-267060274600, 3775493808, 179788922], [-351368605937, -2066480980, 242767884], [3731627858, -1245903462, 113608897], [-1410486504314, 309050780, 10144473], [187389783692, -3049222500, -1023352500], [-593322025807, -3216948939, -25942492], [-95487575974, 352118682, 105559471], [823191804888, 1284366936, 887507088], [-647225355205, 601357950, 359859900], [-212412740135, -1942720573, -225980952], [799600307770, -2334890746, -211405059], [-523941340210, 1780746102, -218571097], [-397312702404, 22307028, 177956862], [385212554975, 2055550176, -367271256], [28830930098, 2552008356, -123430171], [-611640498525, 1918684482, 827449548], [-1312513809610, -752179806, 176698911], [253188174749, -4890776657, -906428928], [-685654702267, 2393398017, 748813608], [-846312664490, 150442982, -1343653332], [644850744818, -3643537212, -1067760759], [293167138203, 883386738, -151692564], [-195108505803, 1547773425, 853998780], [-90151277650, 1440934236, 435441909], [-713809346887, -1194895611, 915949240], [-659812764505, 1135306714, -481624464], [-616351248610, 2578457898, -329888193], [-295085759612, -4907270776, 1362244536], [226812246479, -933490008, -1113063660], [-453616106347, 1820671687, -75196788], [-142268777134, -922623230, -842597517], [-1841680749244, 128873736, -173786046], [-517180710565, 1996828524, -1442347804], [718647429553, 756207245, 662559780], [-948130504708, 2564034484, -779246304], [234724420952, -3089749176, 901250392], [490999977647, 5896223038, -116470812], [1474279194356, 384597532, 318144186], [-458546233870, 828063226, -369923196], [-1110244492870, -2273691438, 499947948], [87873368380, 4647340964, -1091849994], [-829224644568, 632793840, 615734640], [1561283622335, -792378900, 1042762920], [-437266807655, -1227595315, -1078426200], [469812402740, 7330922512, -92987712], [525392230731, 375252060, 49471620], [-1660912566424, 1023248160, -796767120], [1040601962140, 2331058556, -446543886]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_324_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_324_8_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_324_8_a_a(:prec:=3)
    chi := MakeCharacter_324_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_324_8_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_324_8_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_324_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<5,R![959175, -112545, 57, 1]>],Snew);
    return Vf;
end function;