
// Make newform 1849.4.a.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1849_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_1849_4_a_b();".
// 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_1849_4_a_b();".
// 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 := [1, 11, -10, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [-2, -8, 1, 1], [-11, 19, 1, -2]];
    Rf_basisdens := [1, 1, 3, 3];
    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_1849_a();"
function MakeCharacter_1849_a()
    N := 1849;
    order := 1;
    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;

function MakeCharacter_1849_a_Hecke(Kf)
    return MakeCharacter_1849_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 := 4;
    raw_aps := [[1, 0, 0, -1], [3, 0, -1, 1], [6, 3, 0, 1], [6, -8, 4, -2], [-12, -19, 5, 6], [-2, 7, -1, -2], [-65, 15, 38, 5], [-30, 21, 0, 17], [-20, -12, -11, 29], [9, -42, 105, 57], [15, 79, -8, -1], [115, 22, -33, -7], [-104, -66, -9, -95], [0, 0, 0, 0], [17, 98, -147, -113], [-306, -99, 103, 52], [222, -94, 22, 12], [-98, 50, -30, -86], [-26, 89, 125, -14], [-50, -92, -176, -116], [-122, 94, -234, -130], [320, -29, -156, 135], [-212, -41, -91, -276], [222, -198, 48, -228], [-468, 170, -63, -241], [-926, -271, 279, 224], [-540, 36, -129, -261], [238, -158, -14, -260], [156, -19, -355, 166], [456, -322, 8, 74], [-243, -319, 176, 365], [-611, -396, -87, 145], [-494, 426, 164, -102], [-1024, 601, 65, -226], [553, -218, 237, -373], [118, 412, 328, 206], [322, 377, 464, 37], [1190, 213, -166, -459], [-440, 217, 481, 538], [-2048, 144, -246, 194], [-1026, 491, -1304, -391], [1292, 372, -430, 358], [-1432, 382, -620, -146], [104, -66, 1125, 219], [-1090, 780, -672, -200], [-1304, 272, 586, 734], [-263, 1226, -585, -723], [1174, -816, -1160, -166], [-2730, -775, -110, 47], [-1818, 1, -1235, -1270], [-780, 1868, 94, 1062], [-1264, 1281, -2204, -315], [-1500, -1578, 1844, 582], [-2146, 555, -1015, -328], [3084, 822, 390, 834], [2378, -1572, 80, 844], [-394, -197, -169, 662], [-1444, -1832, 1493, 617], [-1918, 429, -232, 547], [-1109, 1193, 768, -1795], [-2294, -29, -1347, 744], [118, -1322, 240, 366], [5020, 397, -1257, -720], [-3604, 993, -779, 1566], [-3086, -736, -1018, -56], [-2148, 371, 1311, -454], [700, 2852, -1880, -2050], [2677, -1489, 320, -293], [-1894, -1279, -1096, 587], [637, 1054, 877, -1687], [-2418, 1385, -191, 1922], [-1957, -2273, 3024, 655], [-925, 18, 47, -1499], [-750, 1301, 2078, -53], [176, -1273, 3333, 788], [1448, -3516, 2680, -324], [2814, -1412, 512, -752], [-1920, -812, 242, 506], [2467, 1699, -1302, -91], [914, -1590, -1004, 3548], [-6804, -730, 2702, 894], [5350, -183, -2456, 761], [-2339, -1805, 906, 45], [-3364, 3450, -1270, 2706], [109, -1303, 1648, 1261], [186, -4936, -478, 350], [-5456, 1632, -172, 2142], [-5508, -2848, 116, -806], [-6604, 236, 1542, 450], [258, -2522, -20, 2420], [-4931, 722, -2337, -2947], [5736, -734, 2243, -615], [6332, -2397, -1832, -2705], [-4414, -745, 5162, 2423], [-2463, 2494, -197, -697], [-372, -2146, 1078, -3856], [1058, 1867, -1363, -3616], [1910, 1760, 3962, -748], [2793, 1482, 1551, -1809], [-3854, 725, -2771, 1586], [-4694, -241, -1769, -3218], [1720, -2549, 1323, 3722], [-4340, 4079, 2719, 4206], [-2242, 4199, -249, 2666], [1625, 5668, 321, 5151], [5976, 1880, -2342, -3156], [-10072, 2250, 1482, 212], [-2396, 380, -4330, -3088], [7791, 1927, 3792, 1423], [1222, 2540, -474, -5618], [-3164, 2548, -6284, -2524], [2818, -4302, -736, 142], [12956, 3060, -3671, -27], [-2916, 4328, 536, -4928], [3768, -5270, -1082, -5828], [14904, -3186, -3182, 2890], [-3714, 2418, 1566, 804], [9128, -2218, 4544, 1434], [16750, -4067, 2252, -687], [16546, -667, 2981, 986], [-14224, 2625, -3151, -2490], [244, -4538, 2224, 2720], [-4498, 4115, 1536, 6383], [-1734, -3657, -1521, -2022], [8612, -5702, -1262, -2530], [-8916, 7382, 2806, 2540], [14744, 1469, 1117, -1150], [-3077, 1970, -7821, 2813], [11632, -2634, 6652, 942], [-3063, -1162, -1303, 4495], [-3855, 3042, -181, 5169], [-17736, -30, -4284, -134], [15628, 356, -8080, -62], [-11509, 614, -5709, -4451], [9032, 3758, 1616, 866], [8181, -3346, 2043, -1003], [10161, -3008, -4993, 4501], [9398, -2414, -8106, -2700], [-15262, 7250, 1196, 2390], [-11089, 1298, 3245, -2209], [26730, -4083, -7026, -747], [21230, -1479, -4239, 154], [-656, -3727, -6655, 602], [-12958, -7522, 5430, 1100], [12709, -8546, -2727, 4063], [-23756, 144, 2158, -388], [6790, -3231, 4603, 4832], [-17024, -7609, 2530, -2547], [8442, 1771, -6914, -1999], [15826, -3954, 8484, 8410], [12150, 7367, 513, 6368], [10885, -7257, 9568, 3091], [22284, 4325, -5291, -2486], [-22296, -426, -390, -5304], [1814, -269, -6793, 5426], [-2936, -5810, 9144, -496], [909, 3351, -10902, -2907], [-10934, 388, -6076, -4008], [-22502, 2046, 6776, -944], [12222, -3313, -2459, 1136], [6258, -5537, -1249, 4762], [20230, -11274, 3706, -4598], [14612, -5758, -10129, -3971], [-9956, 14087, -3547, 6680], [-18735, 5854, -9985, -10523], [9536, -5488, 698, 8072], [26530, 1810, -2986, -8090], [4217, 10884, -10405, -4343], [15432, -6120, 6406, 10494], [-2810, -10958, -3068, 4734], [14297, -9654, 567, -2147], [-7402, 1895, -5196, -389], [1250, -1254, -5154, 5818], [-16812, 1304, -1169, -7649], [7488, -13486, 12754, -2444], [10508, 4809, -7406, 3915], [-1677, 1376, -7841, 639], [3174, 3669, -13980, -3081], [-25340, -2984, 1944, -3456], [-5438, 6141, 5734, 5651], [5056, 6696, 94, 12620], [-30084, -4414, 9076, 230], [-6582, -7758, 2122, 4518], [5592, 6354, 498, -6306], [2350, 8964, 7982, -6832], [4034, -14678, -414, 9268], [15406, 1223, 4436, 7669], [-19451, 10094, -9445, -9841], [15121, 15454, -11669, -4351], [-4778, 2208, 5582, -14592], [1809, -15049, 14344, -3369], [11202, 15959, -2004, 7445], [-50, 16110, -7986, 2928], [-9770, 3671, -722, -12479], [-12672, -13250, 11710, 10342], [28008, 16398, -1488, 3150], [4148, 3916, -5529, -3705], [30460, -4607, -2809, -8444], [-10520, 2791, -15270, 13649], [10306, -7616, -4082, 10324], [3044, -20879, 7913, 326], [21280, -11010, 21054, 4212], [-13878, -2954, -7450, -15516], [8874, -3506, 1768, 2446], [-23875, -9042, -8101, -15649], [15861, 13654, 2737, 7191], [-2542, -5474, 16378, 12476], [9062, 15993, -12061, 7156], [13846, 24966, -8928, -14938], [11266, -8957, -13449, 2806], [35706, 12336, -5520, -11580], [-15448, -2253, -4251, -5432], [-44728, 1525, 18997, 1198], [-45284, 7565, 6919, 2748], [9078, 6048, -14114, -3764], [561, 7953, 15130, -2481], [-1226, 23842, -21486, -1238], [5322, -3632, -3454, 19786], [4286, 5376, 7576, 11890], [-28172, 13521, -4783, 11910], [-37514, 12011, 15380, 1105], [17900, 3916, 19712, 24568], [31318, 5132, -15286, -1140], [16400, -8278, 18951, 9849], [2214, -13693, -8834, -6535], [-878, 5671, -18387, 3564], [-1450, -7587, 1057, -8982], [-30796, 14186, -372, -10860], [-52618, 6842, 14328, -2842], [-21778, 6885, 11460, -11315], [-41159, 2594, 12869, 7249], [-19760, -21231, 6043, -6568], [-32484, 14168, 14581, -4415], [50342, -4690, -8322, -2816], [-22882, -2041, -8517, 6546], [-15181, 17882, -18693, -23499], [12438, -1030, 20250, -9808], [-20544, 3139, 21871, 16746], [-9514, -17503, 16454, 1853], [-24308, -16692, -7101, -7237], [29123, 6564, -29837, -9765], [36066, 5493, -858, 3531], [51412, 9247, 6412, -769], [-16394, -5280, -692, -11880], [43976, -9262, -7004, 2286], [21324, -11925, 8656, -21617], [-9352, -6540, 22608, -10260], [28492, -15725, -10589, -18210], [23446, -3717, 9565, 11588], [8556, -2038, 24643, 20657], [10794, -19942, 19112, 15526], [-1850, 13941, -24699, -18110], [62576, -2697, -4145, 16074], [50606, -3956, 8416, -10744], [26847, -31312, 18873, -10345], [-28203, 7616, -5251, 11577], [-34850, -18269, -9254, -25231], [27602, -5749, 21723, 9166], [2970, 11452, -36244, -4802], [-59732, -4250, 7875, -2999], [37722, -19402, 13780, -7242], [-15290, -12107, 3732, 3095], [266, 24928, 14968, 16160], [-9730, -36466, 31940, 1630], [-13346, 3416, 4926, -10252], [24489, 4922, -73, 27901], [9685, -35196, 5191, -7633], [51868, -2496, -37543, -9447], [-32624, -26134, 14682, 11572], [20668, 5171, -17957, -19482], [36318, 2245, 6145, -21598], [5242, 9321, 18948, 12867], [-48222, 18646, 15328, 4148], [42962, 29288, -21476, -7690], [-22591, -28818, 23067, 17493], [14444, -1882, -6700, 16788], [-67908, -1003, -6011, -17526], [60314, 8535, -5890, -16133], [-5771, -22205, 3756, 29115], [29550, 13935, -6402, 20151], [-33459, -16875, 6592, 3613], [7449, -13594, -18607, 16303], [53939, 1329, -4202, -20121], [39683, 23292, -25119, 471], [-6549, 2940, 8415, 9013], [-35608, 14980, -8642, 4788], [-960, 6114, 33975, 20961], [-17109, 41532, -807, 6111], [-34002, 12444, -31354, 456], [11172, 11476, 1637, 6277], [66272, 23733, -11483, -9138], [37844, -27669, 49933, 7380], [46912, -4373, 29850, 15957], [-751, -24282, -4409, 10957], [-14466, 25689, -605, -1092], [63810, 18671, -25511, -13716], [-71052, 25146, 777, 21075], [23006, 9389, 15279, -9408], [-91264, 7548, 30880, 10584], [-48622, -27552, 14114, -326], [-17169, -24209, -3658, -5899], [45086, 8081, 11946, 1705], [-64844, -34707, 4801, 7418], [-49890, -18867, 15875, 28568], [-44845, 850, -1171, -16291], [21424, -37574, 10486, 21224], [-63952, 29763, -6861, -8858], [58262, -11819, -1986, 2319], [-36550, 538, 2460, 24232], [70978, -16231, 26246, -7781], [-32854, 25310, -21144, -24990], [34678, 43756, -20968, -2238], [87492, 19866, -4191, 4323], [45662, 1759, -22292, -3253], [7985, -843, -35044, 6965], [-664, 15716, -27309, -29673], [-8724, 2784, -34092, -1136], [-23148, 33632, 20744, -8404], [19103, -23919, -26948, 13597], [-40468, -22936, -5430, 15646], [57344, -14779, 10685, -19622], [-7488, -23236, -6196, 14896], [44581, -23536, 3463, 14719], [-16336, -5528, -31912, -21784], [35600, -22290, -3860, -7598], [-66933, 13395, 12942, -29931], [65644, 20714, -51262, -17966], [34960, -51791, 22983, -20312], [-24876, 9641, -13604, 31529], [-30419, 16388, -18369, -4747], [16674, -30640, 17284, 17074], [-64482, 1215, -12705, -45090], [52570, 1830, -25106, -7542], [2545, -4184, 35173, 27811], [43020, 14376, -7476, -12120], [-18872, 25829, 367, 5208], [21798, -21090, 56766, -204], [-131554, 3964, -4416, 18124], [-106654, 24730, 182, -18940], [-81218, 9280, 6834, -2912], [22064, -5599, 32025, -7598], [-6232, 33004, -67820, -31794], [28178, -20866, 19954, 11220], [-69646, 20440, 15034, -23812], [-24334, 26235, 16609, 36024], [28668, -34657, 16889, -16948], [-100253, -11704, -15095, -16981], [-3862, 19754, -44570, -25072], [-64486, -1600, 43138, -1710], [-129764, 26587, -15253, 12822], [44680, -32290, 21460, -37790], [120462, -8844, 9946, 15408], [-82252, 17318, -9098, -3456], [-115052, -8642, 18654, 17666], [121776, -15958, -5926, 616], [19624, -16440, 25806, -4724], [31812, 2550, 16104, -33094], [37529, -37616, -17099, 14821], [-2127, -3155, -2638, -30809], [62519, 1817, 8662, 23511], [-16387, -25192, 495, -19269], [-48112, -17264, 66530, 32976], [-23772, -10276, 49174, -3228], [53702, 39857, 8424, 38409], [59818, 537, -39045, 15360], [28404, -6444, -23668, -14100], [78218, 885, -8594, -28785], [-1927, 9850, 6077, 41179], [56498, -2410, -38668, 6220], [33706, -26267, -8520, -12987], [-85545, -73434, 18993, 9453], [-7516, -15339, 21608, -35651], [14658, 43141, -28288, -38921], [-51763, -31115, -9360, -23227], [-10358, -17866, 34318, -29528], [-100066, 29061, 8148, -4499], [-25964, -9592, 32104, -13340], [-99375, 40763, -34708, 23947], [-115572, 45204, -54228, 18178], [41202, 33392, -34852, -31402], [-104020, -10083, 26125, 5770], [25388, 20734, 5075, 12657], [100821, -3161, -10426, 25149], [-21510, 26238, -1208, 856], [16008, -13785, 10659, 29358], [20591, 26729, -12574, -55873], [-28998, 42312, 42312, -18054], [-25030, -28437, -40471, 324], [84496, -44094, 40408, 1154], [-124768, 30595, 5531, 22802], [-64307, 18091, -24792, -46789], [-36971, 8952, 4515, -7807], [95500, -2316, 16088, -1464], [14086, -23533, 8027, -40934], [66920, -14944, 21612, -11894], [60436, 48269, -84542, -40885], [139021, 10285, 2932, 31821], [6250, 19600, -59430, -44816], [-24282, -50431, 61616, 167], [-40678, 25987, -53214, -14261], [23870, 54685, 14829, -1846], [-80648, -62449, 15370, 35269], [72684, -29886, 27483, 39225], [129596, -39571, 33750, -26477], [3865, -64824, 36827, 13431], [-54590, -17916, 75606, 33948], [1055, -3608, -34519, -23323], [-29279, -7638, -33513, -31675], [-23530, -34099, 50578, -18781], [-181250, -23863, 14163, -5224], [24144, -56747, 106150, 38675], [59718, -81108, 14796, -54848], [-12315, -12282, -47349, 41361], [9426, -35575, 35837, 5434], [-54212, -70606, 15862, 13138], [-77992, 29801, 24315, -25916], [44856, 16661, 13381, 26162], [18364, -23781, 36472, 36855], [27206, 22588, -59068, -43872], [-13018, 40084, -9410, -7536], [-58162, 3741, -14098, -56075], [78886, 21322, 31020, 32550], [-89918, -54474, -3202, 20486], [74090, 30447, 23201, 12812], [-35570, -1974, 44148, 19554], [176142, -28543, 3934, -17313], [26893, 16450, -83637, -52419], [-143902, 46064, 11334, -11424], [-33346, -75829, 97601, 31996], [-18612, -39318, 59370, 65538]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1849_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1849_4_a_b();".
// 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_1849_4_a_b(:prec:=4)
    chi := MakeCharacter_1849_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    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_1849_4_a_b();".
// 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_1849_4_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1849_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![2, 14, -9, -4, 1]>],Snew);
    return Vf;
end function;