
// Make newform 7.9.d.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_7_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_7_d_Hecke();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_7_9_d_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_7_9_d_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 := [197880489, 8271396, 8673408, -348096, 336397, -1176, 592, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [-24226776665406171, -339657458304359916, 327714819020519376, -14228671705925824, 578443118889849, -23107427488768, 933800395536, -40665038848], [-1716105304424448, -66566402858133, -2782472800212, -66871290607312, 117098450112, -113162941609, -206072832, -199147024], [5840361335857569042933, -549685532050544077272, 242602731949311036057, 208410678677305067, 440139351248781243, 1954798485604859, 729234428373612, 4986016178489], [-2293927437451668499449, -2640294308877978734829, -79052178550067250006, -50276218913682852731, -81489430662144594, -89730500895110567, -415028421151641, -142450897959062], [-87312579891615556011, -247240100774204675046, -7203424572374123829, 11054456035703486131, -37683429688699701, 19568825417085307, 17429303220966, 35983628949817], [29414857890603535700697, -1227308896964839498488, -35645454072445999857, 64395501685950743543, -204058573291680393, 112959866406343301, -209968840103652, 206519207416061]];
    Rf_basisdens := [1, 1, 7800602713756314075, 1663596228141675, 21841687598517679410, 109208437992588397050, 21841687598517679410, 109208437992588397050];
    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_7_d();"
function MakeCharacter_7_d()
    N := 7;
    order := 6;
    char_gens := [3];
    v := [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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_7_d_Hecke();"
function MakeCharacter_7_d_Hecke(Kf)
    N := 7;
    order := 6;
    char_gens := [3];
    char_values := [[0, 0, -1, 0, 0, 0, 0, 0]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
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 := 9;
    raw_aps := [[0, -1, 1, 1, 0, 0, 0, 0], [-7, 0, 7, 0, 0, -1, 1, 0], [-140, 1, -70, 1, 1, 0, 2, 1], [182, -21, 399, 42, -7, -21, 21, 0], [446, 721, 446, 0, -9, -77, -68, 0], [5915, -1602, 11830, 801, -6, -98, 3, 3], [-11788, 2369, 11788, -4738, -81, -78, -3, 162], [-42924, -2019, -21462, -2019, -157, 0, -865, -157], [0, 296, 87235, -296, -522, -1778, 889, 522], [622897, 0, 0, 11195, 0, -1834, 2231, 1437], [-198058, -27357, 198058, 54714, 475, 3569, -3094, -950], [0, 59724, -123185, -59724, 4082, 21028, -10514, -4082], [817719, -49242, 1635438, 24621, 5722, -14602, -2861, -2861], [556054, 0, 0, 35340, 0, -18074, 34436, 1712], [450688, -100769, 225344, -100769, 7619, 0, -36815, 7619], [570365, 179944, 570365, 0, -15834, -4690, 11144, 0], [2107595, -227362, -2107595, 454724, -27050, 11059, -38109, 54100], [9894794, -294960, 4947397, -294960, -5762, 0, 111362, -5762], [-26777, -819354, -26777, 0, 30738, -67417, -98155, 0], [-10351220, 0, 0, 104234, 0, 20930, -21718, -20142], [9729867, -595146, -9729867, 1190292, 39502, 156708, -117206, -79004], [0, 757308, 12657023, -757308, -80346, -500458, 250229, 80346], [8989554, -921852, 17979108, 460926, 137108, 347704, -68554, -68554], [-387086, -245408, -193543, -245408, -63324, 0, 82020, -63324], [48586783, 999222, 97173566, -499611, -59766, 45962, 29883, 29883], [15983450, 1327655, -15983450, -2655310, 52283, 1003568, -951285, -104566], [-43367786, -1637922, -21683893, -1637922, -25332, 0, 611375, -25332], [0, -2872357, 23358742, 2872357, -183663, 258986, -129493, 183663], [-67784656, -2382609, -67784656, 0, -103229, -797972, -694743, 0], [28489561, 0, 0, -2923213, 0, -792022, 1419599, 164445], [-34590526, 0, 0, 9353688, 0, 214242, 147216, -575700], [271260514, 6444256, 135630257, 6444256, -240716, 0, -214231, -240716], [-79711228, 6001831, -79711228, 0, 707925, -1300418, -2008343, 0], [-77031080, 18702624, -154062160, -9351312, 404344, -206192, -202172, -202172], [0, -17755801, -123913586, 17755801, -1226217, 1021720, -510860, 1226217], [-190439062, 11129097, -190439062, 0, 160333, 1981553, 1821220, 0], [169297653, 13284696, -169297653, -26569392, -470682, 1478462, -1949144, 941364], [0, 10513572, 209949923, -10513572, 307608, 1028034, -514017, -307608], [182264397, -18228030, 364528794, 9114015, -1970474, 6833540, 985237, 985237], [499179142, -4173818, 249589571, -4173818, -1060456, 0, -2663234, -1060456], [-158241904, -23187869, -158241904, 0, -1124823, 4453295, 5578118, 0], [259828296, 36830016, 519656592, -18415008, 348312, -3863160, -174156, -174156], [0, 42735959, -620182898, -42735959, -663417, 14462070, -7231035, 663417], [-35828956, -38370609, -35828956, 0, -366575, -5393850, -5027275, 0], [690817759, 0, 0, -2411743, 0, 10037090, -15438859, -4635321], [-22674512, 2658873, 22674512, -5317746, 1621641, -12675615, 14297256, -3243282], [167655406, 0, 0, 15410292, 0, -1830710, 102716, 3558704], [-744241722, -136594596, -1488483444, 68297298, -3458012, -2418052, 1729006, 1729006], [-1060025428, 11286113, 1060025428, -22572226, -3926917, -11438617, 7511700, 7853834], [406639772, 20301813, 203319886, 20301813, -4630171, 0, 995110, -4630171], [0, 73185461, -2823204152, -73185461, -2579073, -12674452, 6337226, 2579073], [-2551277783, 0, 0, 14146043, 0, -1256038, -2263249, 4775325], [-78032521, -22638558, 78032521, 45277116, 932598, 14143324, -13210726, -1865196], [-917819301, 127575102, -1835638602, -63787551, 394146, -61750584, -197073, -197073], [3969931420, 44980081, 1984965710, 44980081, 8867767, 0, -7374508, 8867767], [2396733701, -190928528, 2396733701, 0, 1058202, -18758915, -19817117, 0], [-2803940881, 72274406, 2803940881, -144548812, 2032416, 16223514, -14191098, -4064832], [-5011391630, 87741516, -2505695815, 87741516, -3798422, 0, 37293655, -3798422], [2500518403, 95740134, 2500518403, 0, -15224904, -23216438, -7991534, 0], [-1848925181, 0, 0, 90761861, 0, -28887250, 53347433, 4427067], [1087685746, -24735861, -1087685746, 49471722, -2452915, 53401991, -55854906, 4905830], [2929834656, -11493552, 5859669312, 5746776, 5174680, 19381460, -2587340, -2587340], [-576539404, -422636808, -1153078808, 211318404, 24201472, 82030116, -12100736, -12100736], [-1542303889, 120316094, 1542303889, -240632188, 21075904, 64072861, -42996957, -42151808], [-3148719126, -70669476, -1574359563, -70669476, 8319504, 0, 9165712, 8319504], [0, -13373248, -8554686965, 13373248, -7847790, 1032276, -516138, 7847790], [0, 320727402, 6148257193, -320727402, 7561214, 72393090, -36196545, -7561214], [-2326235399, 0, 0, -23775933, 0, 51299878, -159329521, 56729765], [282364787, -271037126, 282364787, 0, 31887018, -70759689, -102646707, 0], [1287377189, 113108034, 2574754378, -56554017, -58439482, -92140090, 29219741, 29219741], [-2310119644, -565459855, 2310119644, 1130919710, -3582049, -178498930, 174916881, 7164098], [0, -554691517, 4668733378, 554691517, -53367441, -93755578, 46877789, 53367441], [9180742641, -21955044, -9180742641, 43910088, 5198894, 89645653, -84446759, -10397788], [0, 1067330784, -6169643437, -1067330784, -20543730, 99771196, -49885598, 20543730], [-2014169267, 0, 0, -269570385, 0, -34444550, 116175395, -47286295], [4898512864, -453523793, 2449256432, -453523793, 35018947, 0, -15348787, 35018947], [-1760927086, 245246017, -1760927086, 0, 180117, 5593336, 5413219, 0], [13629983920, -668275425, 6814991960, -668275425, -13681289, 0, 335602570, -13681289], [0, 181419545, -20492190620, -181419545, 78324183, 162884092, -81442046, -78324183], [-3104494400, 971492637, 3104494400, -1942985274, -13167361, -128614690, 115447329, 26334722], [5105621808, 264180240, 10211243616, -132090120, -26849912, -309934996, 13424956, 13424956], [11226410813, 0, 0, 1017870375, 0, 166337514, -218922141, -113752887], [56943290, -1429735823, 56943290, 0, -83970831, -27096895, 56873936, 0], [19763876293, 570594930, 39527752586, -285297465, 31549934, 247408350, -15774967, -15774967], [-33052464816, 432463839, -16526232408, 432463839, 37791183, 0, -214930151, 37791183], [0, 490882901, 34578615016, -490882901, -67762689, 42107394, -21053697, 67762689], [12053307793, 0, 0, -1546241413, 0, -91740586, 223732295, -40251123], [0, -1173836928, -13258151129, 1173836928, -119939572, -354974592, 177487296, 119939572], [-2714531127, 748990506, -5429062254, -374495253, 51343934, 530004286, -25671967, -25671967], [20826150116, 0, 0, 354705618, 0, 76207950, -268528926, 116113026], [-39273277862, 471314860, -19636638931, 471314860, -44321352, 0, -273866187, -44321352], [19771000361, 733585220, -19771000361, -1467170440, -24073066, 208559609, -232632675, 48146132], [4817891489, 1195078140, 4817891489, 0, 53793070, 110344381, 56551311, 0], [4366016041, 0, 0, -105965989, 0, 437561138, -796046689, -79075587], [0, -4148743536, 4146618799, 4148743536, 112609988, 384922370, -192461185, -112609988], [-19081950258, -1550909940, -38163900516, 775454970, -91710492, 222087012, 45855246, 45855246], [5885215756, 1644360421, 2942607878, 1644360421, 17981233, 0, 1126829630, 17981233], [-8994001891, -114224020, 8994001891, 228448040, 75559056, 724897956, -649338900, -151118112], [5938146872, -596675793, 2969073436, -596675793, 102477669, 0, -92572329, 102477669], [0, -1279485219, -78115297756, 1279485219, -50148459, -382350024, 191175012, 50148459], [2048480009, 0, 0, -3380140581, 0, -185032806, 641546751, -271481139], [47178505862, -6067058051, 47178505862, 0, 184667973, -344019424, -528687397, 0], [52887757643, -1775915098, -52887757643, 3551830196, -108399038, -47922533, -60476505, 216798076], [0, 4272053102, -16871496179, -4272053102, 79390902, -677280408, 338640204, -79390902], [55321142560, 3366709131, 55321142560, 0, -90135507, 140374787, 230510294, 0], [-64473117821, -695631114, 64473117821, 1391262228, -89013270, 1189978404, -1278991674, 178026540], [-33230333724, 1112538168, -66460667448, -556269084, -216273232, -775187252, 108136616, 108136616], [-3451758296, -2580600497, -1725879148, -2580600497, -104618247, 0, 473773680, -104618247], [37616218337, 226400668, 37616218337, 0, -253072818, -372500583, -119427765, 0], [15525056351, -7063138410, 31050112702, 3531569205, -208811014, 271126898, 104405507, 104405507], [-125911067772, -534815991, -62955533886, -534815991, 19526497, 0, 979483641, 19526497], [-50993361344, 7346944983, -50993361344, 0, 35604903, -381369912, -416974815, 0], [-14424147113, 0, 0, 3884875481, 0, -660649990, 1380684413, -59384433], [-14567459851, 1026302220, 14567459851, -2052604440, 124589520, 834164575, -709575055, -249179040], [39874489874, 0, 0, -4115131704, 0, 89613090, -21802272, -157423908], [51238075607, 5246936110, 51238075607, 0, 85393218, 1289719396, 1204326178, 0], [32887377082, -6480270156, 65774754164, 3240135078, 899395732, 236116300, -449697866, -449697866], [1286724719, -2648593138, -1286724719, 5297186276, -258379704, -1242631587, 984251883, 516759408], [0, -6019945414, 23995695949, 6019945414, 846015360, 903576940, -451788470, -846015360], [-58463711822, 0, 0, -1652826040, 0, -69242950, -90262720, 228748620], [-67989923211, 440773824, 67989923211, -881547648, 215928210, -129757678, 345685888, -431856420], [-35994073379, 0, 0, -8257559817, 0, 332230150, -271922221, -392538079], [41461102564, 2763846601, 20730551282, 2763846601, 176168753, 0, 1008903526, 176168753], [-103543399402, -11581979591, -103543399402, 0, -430881705, -231321097, 199560608, 0], [-38085498962, 3754621998, -19042749481, 3754621998, 321551462, 0, 143087037, 321551462], [-88311688610, 0, 0, 12097578956, 0, 945673064, -1324419748, -566926380], [0, 1999429998, -21340067447, -1999429998, -461295856, 1714504092, -857252046, 461295856], [84344009722, -5585756288, 42172004861, -5585756288, 226359798, 0, -128893257, 226359798], [7221945808, -8849700192, 14443891616, 4424850096, -927504960, 1603916804, 463752480, 463752480], [133284162046, 3731194050, 66642081023, 3731194050, -305253372, 0, 745807046, -305253372], [-63306014060, 15001718007, -63306014060, 0, -938228955, -1094567733, -156338778, 0], [65519184838, 0, 0, -16010850100, 0, -127291318, -359777068, 614359704], [0, -15783567681, 51407179174, 15783567681, 926396759, 389340854, -194670427, -926396759], [181080959467, 0, 0, -14473285251, 0, 1403907946, -2798101567, -9714325], [28473881548, 6451044721, 14236940774, 6451044721, 67161355, 0, -2730919912, 67161355], [69881853783, -917818938, 139763707566, 458909469, 874588490, 1550080798, -437294245, -437294245], [129430172228, 785234069, -129430172228, -1570468138, 133402749, -3857041980, 3990444729, -266805498], [14898886975, 3794661234, -14898886975, -7589322468, 815867150, 1355691623, -539824473, -1631734300], [-6354274101, 2064713022, -12708548202, -1032356511, 1346282890, 332240090, -673141445, -673141445], [16600323647, -5085509618, 16600323647, 0, 1507677246, 914690084, -592987162, 0], [50760270582, 22973581164, 101520541164, -11486790582, -985798644, -3804369996, 492899322, 492899322], [0, 10247309225, -146677759304, -10247309225, -787300899, 4266748584, -2133374292, 787300899], [-342262804067, -18691068408, -342262804067, 0, -283184622, 737552417, 1020737039, 0], [-301420066232, 0, 0, -5360476954, 0, 116451734, -257243098, 24339630], [63499647750, -8839334397, -63499647750, 17678668794, -506064725, 2506641024, -3012705749, 1012129450], [45633014238, -14682260148, 91266028476, 7341130074, -1064523820, 1980454168, 532261910, 532261910], [102098577147, 7584719070, 204197154294, -3792359535, -1347022742, -2691338818, 673511371, 673511371], [-31755557659, 248902340, 31755557659, -497804680, 419146568, -3080531896, 3499678464, -838293136], [124051080076, -14025899487, 62025540038, -14025899487, 18604875, 0, 1263596159, 18604875], [0, 24398149502, -511958906423, -24398149502, -113196300, -1997642458, 998821229, 113196300], [0, 11056656711, 465193079030, -11056656711, -713786301, -3569145440, 1784572720, 713786301], [-299023576824, -26654318544, -598047153648, 13327159272, -877402848, -1491717780, 438701424, 438701424], [-42679313780, 0, 0, 10918675818, 0, 2477876506, -4253473750, -702279262], [704148584188, 6997564477, 352074292094, 6997564477, -266369375, 0, -3047647783, -266369375], [683969697605, 8922585252, 683969697605, 0, -659120240, -1954241737, -1295121497, 0], [-629952498614, 0, 0, 9871299152, 0, 2047752350, -3047618344, -1047886356], [0, 34336490319, -129417688658, -34336490319, 682409867, -2176081754, 1088040877, -682409867], [82925234854, 4655066314, 41462617427, 4655066314, 1361758478, 0, 4434345844, 1361758478], [-241462439372, 4556888424, -482924878744, -2278444212, 1307008616, 5039525736, -653504308, -653504308], [-89948048827, 8862333128, 89948048827, -17724666256, 684980182, 8646230746, -7961250564, -1369960364], [0, -15097324936, -446091003161, 15097324936, 702031764, -1220192694, 610096347, -702031764], [-170626319771, 0, 0, -9448517329, 0, -2759411662, 5348925371, 169897953], [165732598880, 0, 0, 2481686286, 0, 5346682362, -10438186530, -255178194], [834224631730, -17122561136, 417112315865, -17122561136, -449703144, 0, -6969048099, -449703144], [353365452452, -12700183265, 353365452452, 0, -489414711, 2316716094, 2806130805, 0], [-327050032792, 3774819041, 327050032792, -7549638082, -1856645119, -2654008267, 797363148, 3713290238], [358586693561, -15864909876, 358586693561, 0, 2324904394, 5198506117, 2873601723, 0], [-55344658061, -5390459526, 55344658061, 10780919052, 1342113328, 10339942486, -8997829158, -2684226656]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_7_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_7_9_d_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_7_9_d_a(:prec:=8)
    chi := MakeCharacter_7_d();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 9));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(997) - 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_7_9_d_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_7_9_d_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_7_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,9,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;