
// Make newform 20.9.f.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_20_f();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_20_f_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_20_9_f_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_20_9_f_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 := [2405464244, -1770757924, 307615702, 50131374, 4820745, 22254, 8, -4, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [-17201080872629180854312, 7291639495351253703576, 1137909215057435119602, 85750328228947671494, 392578007792287377, -231983879936421, -16868436332617, 17543298991401], [-4411956177038996224861776, 5023198320683908632074548, 544847636925257658459946, 41049105751594389826687, 183559907930723391146, 246098412514904642, -9230087829528791, 8397852473178398], [-17013291057978802265646116, 4389568545832306052594368, 648976954800072987776086, 64687778407869036729017, 299911930994464745036, 242583434153172547, -59832656428792231, 13457794229710593], [2351099624835978245751547, -330544148248550425211036, -61939253049816035577182, -5011260047344589826739, -22540552675418216062, 12425146347198256, 1833256378746647, -1028024730304656], [-111948976722265098250898362, 12232929887608677736990976, 1541673738389898660923402, 161762277349949492884594, 707846830264245732427, 998791185526011929, -163005900253367417, 33439120500318801], [36593076596292260533747944, -20355655902990107229901912, -3038353980754440303475474, -243829259109382716241378, -1121418502899574600349, 238364561878400677, 85892049784663529, -49991586418956737], [-91270018197703135167207336, 45353464038745268052047128, 6555611328264449485284706, 480384742045082514781582, 2192985283134569394281, -1436445366484139413, -43412380202449001, 98099736970983353]];
    Rf_basisdens := [1, 11186314553142756537500, 75507623233713606628125, 75507623233713606628125, 3020304929348544265125, 75507623233713606628125, 60406098586970885302500, 60406098586970885302500];
    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_20_f();"
function MakeCharacter_20_f()
    N := 20;
    order := 4;
    char_gens := [11, 17];
    v := [4, 3];
    // 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_20_f_Hecke();"
function MakeCharacter_20_f_Hecke(Kf)
    N := 20;
    order := 4;
    char_gens := [11, 17];
    char_values := [[1, 0, 0, 0, 0, 0, 0, 0], [0, -1, 0, 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, 0, 0, 0, 0, 0, 0, 0], [-9, -9, 1, 0, 0, 0, 0, 0], [112, -58, 1, 1, 1, 0, 0, 0], [-255, 253, 0, -2, -2, 0, 1, 1], [-45, -10, -8, 16, 7, 1, 19, 9], [4123, 4127, 94, 15, 26, -4, -23, 15], [5594, -5639, 5, 621, -45, -5, 35, 25], [-1, 28846, -1199, -1187, 21, 11, -13, 1], [-83350, -83407, 1822, -100, -143, 57, 214, -100], [233, 22670, -1883, -2179, 107, -63, 529, -233], [-397079, -170, -1000, 700, -535, 235, 105, -65], [664893, -665937, 330, -11727, -1044, -330, 1347, 687], [-1280589, 205, 17522, -15819, 2657, -954, 544, 749], [-1292779, -1294253, 1235, 1020, 3514, 1474, 1928, 1020], [2392773, -2395539, -1200, -37884, -2766, 1200, -1617, 783], [-3029414, -3026081, -45997, -1760, -6853, -3333, -4906, -1760], [4453, 3199282, 8687, 7131, 10247, 2897, 6009, -4453], [-10313733, 4465, -3934, 793, -2479, -662, -8268, -3803], [12586883, -12599567, -6720, 45393, -12684, 6720, -10458, 2982], [-12305007, -1810, -204584, 210468, 7921, -2037, 5657, 3847], [-11689687, -11697123, 1924, 6990, 21416, 7436, 7882, 6990], [-9112, 13406084, 86032, 102916, 6432, 7772, -25996, 9112], [-1345897, -1326401, 138917, 10680, 1864, -19496, -49672, 10680], [5654, 4382972, 170566, 164958, 5746, 46, 11262, -5654], [-22540049, 22533123, -49530, -240602, -6926, 49530, -120362, -21302], [16121856, 28790, 353164, -383178, -30626, 612, -58192, -29402], [2353394, 2316345, 430438, -39360, -41671, 37049, 113458, -39360], [-24340725, 24402721, 48260, -598589, 61996, -48260, 89652, -6868], [16947, -26467769, 255798, 327399, 194043, 88548, -54654, -16947], [34339896, 34349549, -63707, 11755, 13857, -9653, -31061, 11755], [-140535219, 140415733, -72000, 708208, -119486, 72000, -120257, 23743], [176454375, -54910, -1114496, 1260892, 192139, -45743, 155563, 100653], [-180634979, 180762087, 65760, 210922, 127108, -65760, 100846, -30674], [-47107, -137168970, -580673, -413249, 193527, 120317, -214531, 47107], [154141, -112186691, 1801034, 1545917, -47811, -100976, 409258, -154141], [-60732455, 39170, 162592, -589984, -621503, 194111, -272451, -233281], [-169533624, 169803957, 17805, -2726658, 270333, -17805, -90654, -126264], [190700127, 190520359, -1899575, 142320, 464408, 179768, 217216, 142320], [224680353, -224419243, -112160, -2093098, 261110, 112160, -410955, -186635], [-293399930, -293232573, 4456323, 132710, 98063, -167357, -467424, 132710], [-697977, 180446754, -86403, 599261, -722603, -12313, -1383641, 697977], [160574030, 14240, -7167152, 7694504, 798148, -270796, 242316, 256556], [-937265127, 148530, -3023088, 2735776, -356703, 69391, -366451, -217921], [-867884594, -867531955, 7126747, 112545, -127549, -352639, -817823, 112545], [474623083, -474551853, -139440, 3023767, 71230, 139440, -384215, -105335], [815078, -398935456, -2690438, -3643994, 538122, -138478, 1768634, -815078], [-503768221, 1256890, -11876368, 12614236, 1735247, -997379, -1516401, -259511], [322301872, 323655149, 1999300, -1048380, -3450037, -1353277, -1658174, -1048380], [1029522703, -1031565919, -72740, -16790139, -2043216, 72740, 839758, 985238], [-388401, 1961214702, -9106509, -9077817, -1107819, -359709, -417093, 388401], [-1267804756, -1268746655, 5879213, 189345, 1320589, 941899, 1694453, 189345], [53172, -1961047236, 18390888, 18206844, -208572, -130872, 237216, -53172], [791949019, -643275, 17766882, -16604739, 1421577, -259434, 1545984, 902709], [-636715425, -1924250, 1731320, 46360, 1704395, 73285, 3775215, 1850965], [-2139093376, 2137266701, -829205, -23826089, -1826675, 829205, -1159675, 498735], [3175378654, 3174627769, -3614146, -366080, 18725, 750885, 1867850, -366080], [847111, -285820883, -19842916, -19604733, 3017699, 1085294, 608928, -847111], [1563490845, 2655630, -21474792, 19993884, -893547, -587361, -4723899, -2068269], [-3397629460, 3399959469, 887805, 23571404, 2330009, -887805, 1054508, -721102], [3064310571, 2422925, 15701506, -17190187, -1021559, -467122, -4378728, -1955803], [1901726709, 1900417195, 61363543, -1707060, -2104606, 1309514, 4326088, -1707060], [1039036011, 1040958147, 26797890, -1646295, -5214726, -1922136, -2197977, -1646295], [-2174147097, 2174420073, -1281060, 49176033, 272976, 1281060, -3339138, -777018], [-2023906803, -5021830, -3955424, 4078848, -2325779, 2449203, 7594457, 2572627], [-654705302, -655303135, -80015429, 2997855, 6593543, 597833, -1802189, 2997855], [-271340162, 273233961, 3272955, 92360716, 1893799, -3272955, 7235488, 689578], [-759776357, -536390, 45170288, -47713076, -4082377, 1539589, -466809, -1003199], [-3407775715, 3397835463, -592620, -20830792, -9940252, 592620, 3488576, 4673816], [5600988747, -5594239175, 226940, -98667665, 6749572, -226940, -2807436, -3261316], [-1423323, 7686810722, -25954767, -21780171, 4079223, 2751273, -5597919, 1423323], [1769704457, 1769781553, -150523864, -3203760, -6484616, -77096, 3049568, -3203760], [-3551074, -2066435476, -7566626, -11414538, -18349046, -7398986, 296838, 3551074], [5995993237, -5993630363, -4765680, 51078452, 2362874, 4765680, -13095637, -3564277], [-9208502960, -9209310137, 61446657, 225210, 1257597, 807177, 1389144, 225210], [12293565, 1876936694, 11860995, 3718035, 20594775, 4150605, 20436525, -12293565], [3538773112, 3542823197, 27985372, 3803940, 3557795, -4050085, -11904110, 3803940], [1378329, -3755246493, -123663804, -123065427, 5331741, 1976706, 779952, -1378329], [-811686493, 771224963, -12465300, -60554507, -40461530, 12465300, -10932485, 13998115], [-11745678030, 11407900, -21702520, 28939540, 16559480, -9322460, -13493340, -2085440], [-12538132, 14400534635, -27761783, -4243904, 9421362, 10979747, -36056011, 12538132], [494375, -5053212958, -160585535, -169159055, -15663915, -8079145, 9067895, -494375], [-1791707869, -10239715, -130809878, 140275181, 9078097, 387206, 20092224, 9852509], [-13617453099, -19130850, 130114776, -118786852, 7426461, 3901463, 34360237, 15229387], [-126698457, -116324303, 210071014, 5679870, 985586, -10374154, -26428178, 5679870], [6267314, 12748799580, 7663666, 7561858, 18598326, 6165506, 6369122, -6267314], [15049304197, 15062644667, -23754953, -9649980, -32640430, -13340470, -17030960, -9649980], [8434870, -35591309963, 127047305, 113155590, -2478820, -5456845, 22326585, -8434870], [-3286543860, 3307556949, 15135195, 89993409, 21013089, -15135195, 27331443, -2938947], [-13270162560, 20490510, 77590332, -88846114, -6638418, -4617364, -36363656, -15873146], [-15418319668, -15412196779, -493599492, -8076240, -22275369, -6122889, -4169538, -8076240], [-18324862853, 18309371545, -15286720, -88960195, -15491308, 15286720, -30471146, 102294], [-8229788, -17065162376, 305645048, 333944424, 31909388, 20069588, -36529164, 8229788], [-27453691179, 27457785669, -1635660, 281849034, 4094490, 1635660, -6136395, -2865075], [23058551715, -16209590, -83310160, 79618220, -13642705, 9950765, 22468415, 6258825], [810211, 15156379778, 151752869, 124329197, -52416711, -26613461, 28233883, -810211], [35024240478, 35024883537, -320550006, 29706600, 58770141, -643059, -30992718, 29706600], [7606141, -70713355502, -193217191, -176258883, 56735039, 24564449, -9352167, -7606141], [-37871210076, -61430350, -19835180, 7366610, -49418030, 36949460, 85911240, 24480890], [-26858600563, -26866827869, 295043183, -6412920, -4598534, 8227306, 22867532, -6412920], [45828211504, 51121670, -142423700, 181914950, 84797710, -45306460, -56936880, -5815210], [-43818951913, 43894840689, 19610340, -821301091, 75888776, -19610340, 11081462, -28139218], [40981487981, -41031959985, -37882530, 442839095, -50472004, 37882530, -69470323, 6294737], [27314797731, 27284166783, -242072451, -10982760, 8665428, 30630948, 72244656, -10982760], [23677080, -68708678835, -341111205, -366124540, 21004570, -1336255, 48690415, -23677080], [33791041883, 23033510, -216552680, 202675760, -9298625, -4578295, -41488725, -18455215], [4696988596, -4728674203, -9762705, -777892293, -31685607, 9762705, -8563959, 10961451], [35072399007, -35097759279, 32284680, 536465871, -25360272, -32284680, 93391836, 28822476], [50479143909, 50457999337, 762359036, 22233560, 65611692, 21144572, 20055584, 22233560], [24997466, -11558792224, 942240334, 925357442, 41226614, 8114574, 41880358, -24997466], [-48170923525, -69450075, -16378254, 38922333, -908919, 23452998, 115447152, 45997077], [48253693485, -48246995043, -7938720, -77210088, 6698442, 7938720, -23196021, -7318581], [-33385575419, -33399331237, -629205356, 39615855, 92987528, 13755818, -12104219, 39615855], [99747546326, -99748080639, -20754195, -44108269, -534313, 20754195, -51618331, -10109941], [-30632199, 67650176346, 645031059, 698273967, 14589219, 22610709, -83875107, 30632199], [-24427584803, 38477930, -557819168, 553817936, 13237117, -17238349, -59717511, -21239581], [-33249345813, 59643645, -1456304238, 1451397301, 22461417, -27368354, -91918936, -32275291], [-33790357853, -33772582969, 73090753, 16530480, 15286076, -17774884, -52080248, 16530480], [109397664901, -109422166747, -22855340, -1477269822, -24501846, 22855340, -44887427, 823253], [53220107944, 53269180139, -757386611, -48780420, -146633035, -49072195, -49363970, -48780420], [13160123, -141584498710, 415619317, 366232821, -59292623, -36226373, 62546619, -13160123], [-42834752317, -23223195, -1115365302, 1056403029, -100055007, 41092734, 5353656, -17869539], [-75879903501, -75844962775, 2373985418, -27666570, -90273866, -34940726, -42214882, -27666570], [73296353963, -73135338893, 34512320, -437012673, 161015070, -34512320, 5773265, -63251375], [149636674381, 149618673011, 1002094411, 60261540, 138524450, 18001370, -24258800, 60261540], [49044578763, -196950590, 753123392, -851515484, -246063433, 147671341, 246229839, 49279249], [14608502955, -86668015, -1417758638, 1516756201, 105162337, -6164774, 179500804, 92832789], [-39477981, 20676655586, -190985289, -268104057, -272671479, -116596749, 37640787, 39477981], [-120697172, -32577870980, -668299168, -528976284, -83445748, 18625712, -260020056, 120697172], [-14290417915, 14156946741, -39983700, -1113972394, -133471174, 39983700, -33223663, 46743737], [-58805298370, -58955321781, 446964211, 59058690, 268140791, 150023411, 240988132, 59058690], [-104117597, -187883255422, 1702908137, 1900996481, 83823897, 93970747, -302205941, 104117597], [109969994618, 109957802169, 1734959202, -65603660, -119014871, 12192449, 89988558, -65603660], [-54010043319, 82106370, 1729376208, -1713212016, 65299473, -49135281, -115077459, -32971089], [-145298544864, 145538692197, 79903185, -1278757368, 240147333, -79903185, 79684296, -80122074], [-100496721558, -82513780, 1649124520, -1803741740, -273182720, 118565500, 46462060, -36051720], [95403948, 25378434412, -1347120348, -1516243224, -52033908, -73718928, 264526824, -95403948], [68424913397, 68506377739, -1319752540, 39056295, -3351752, -81464342, -201984979, 39056295], [-210741329181, 210715398209, -77704680, -1655432011, -25930972, 77704680, -181296214, -25886854], [-22692990190, 23002888833, -44001105, 2314984328, 309898643, 44001105, -264952084, -176949874], [-154463550, 74916503748, -2729269590, -2458143070, 78862390, 116662970, -425590070, 154463550], [-14770037041, 87656710, 1587477272, -1519652744, 145565147, -77740619, -97572801, -9916091], [7713142011, 86217265, 1644335186, -1504191447, 253324241, -113180502, -59254028, 26963237], [32908154486, 33078148241, 1590733866, 69932760, -30128235, -169993755, -409920270, 69932760], [-127499920357, 127146272505, 55547100, -794564905, -353647852, -55547100, 315691676, 204597476], [2177123, 253476338757, -1265711768, -1129187009, 279580887, 138701882, -134347636, -2177123], [35767274, -168171049612, -1104630314, -1146530082, 23502286, -6132494, 77667042, -35767274], [-79210497087, -78990001549, -2407627480, 19967175, -180561188, -220495538, -460958251, 19967175], [-18078316715, 17675520087, -266227680, 2295262942, -402796628, 266227680, -464170886, 68284474], [-91866587, -200851429270, -3109964833, -3119751229, -295172553, -101652983, -82080191, 91866587], [23281345572, 23389041277, -1866482188, 307435700, 507175695, -107695705, -522827110, 307435700], [149199507184, -149191862331, 38147805, 4366039474, 7644853, -38147805, 91547086, 15251476], [15268455747, -252400435, 3839534242, -3566224859, 283763857, -10454474, 515255344, 262854909], [150326894259, 150418211615, 809976173, -77360520, -246038396, -91317356, -105274192, -77360520], [409241890037, -409158244443, -49079700, -1570716178, 83645594, 49079700, -164522047, -66362647], [69682495927, -69361917079, 71245680, -3492832329, 320578848, -71245680, 17824776, -124666584], [-803293669383, 15701670, 110088888, -279461876, -246208647, 76835659, -108238999, -92537329], [-130664766, 279967724752, 885426366, 776768658, -609309714, -239322474, -22007058, 130664766], [58287386, 446115541427, 924541879, 730756802, -212707996, -135497691, 252072463, -58287386], [132617562170, -132349130983, -5535, -5964764153, 268431187, 5535, -134229431, -134218361], [240769706382, 119156120, -3331564256, 2975620112, -474338156, 118394012, -356706252, -237550132], [-231293217097, 231945509417, -121298900, 3653945137, 652292320, 121298900, -629393410, -386795610], [-228441389618, -228431459119, 7205095423, 402456555, 794982611, -9930499, -422317553, 402456555], [370547282005, -371082012163, 4705620, -7786490958, -534730158, -4705620, 279129129, 269717889], [181336211991, -76945510, 8197856176, -8211857052, -59474069, 45473193, 108417827, 31472317], [123938293367, -123034593933, 233426790, 2330338082, 903699434, -233426790, 131717258, -335136322], [-386862777918, -386945407855, -3547696046, 248055880, 578741697, 82629937, -82796006, 248055880], [37759659421, -265203270, 4173033744, -3764185488, 480670749, -71822493, 602229033, 337025763], [31308479318, -31322442151, 340113735, 8127359074, -13962833, -340113735, 857265754, 177038284]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_20_f_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_20_9_f_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_20_9_f_a(:prec:=8)
    chi := MakeCharacter_20_f();
    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_20_9_f_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_20_9_f_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_20_f();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,9,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;