
// Make newform 1859.4.a.b in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1859_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_1859_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_1859_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 := [52, -2, -19, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [-10, -1, 1, 0], [8, -12, -1, 1]];
    Rf_basisdens := [1, 1, 2, 4];
    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_1859_a();"
function MakeCharacter_1859_a()
    N := 1859;
    order := 1;
    char_gens := [508, 1354];
    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_1859_a_Hecke(Kf)
    return MakeCharacter_1859_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 := [[0, -1, 0, 0], [-1, 0, 0, -1], [2, 3, 2, 0], [4, 2, -1, -4], [11, 0, 0, 0], [0, 0, 0, 0], [-10, 8, 1, -7], [7, -6, -13, 10], [-38, 2, -16, 1], [-46, 23, 23, 19], [100, 24, 52, -10], [88, -36, 19, 25], [38, -72, -46, 25], [-88, -25, -100, -32], [-170, -32, -119, -21], [103, -62, 69, 18], [132, -17, -13, 61], [-218, -7, -71, 7], [224, -113, 33, 43], [-180, 112, 61, 29], [343, 67, 66, 155], [-46, -142, 94, -50], [-110, 13, -3, -206], [-644, -20, -24, -148], [564, -84, -248, 8], [-444, -144, 78, 0], [-326, 452, 148, 35], [116, -23, -85, -161], [255, 140, -262, 7], [-543, -247, -260, 29], [-1218, 182, 24, -112], [-10, -441, 2, 188], [470, 334, -182, 234], [242, -78, 395, 135], [664, -99, 265, -30], [-1720, 662, 297, 111], [-1978, -273, 159, -132], [-824, -239, -675, -657], [-1027, 621, 698, 85], [2104, 427, -263, 361], [964, 212, 613, 371], [-529, 144, 315, 288], [-239, -87, -266, 375], [956, 325, 1171, 560], [-93, -384, 348, 101], [2307, 381, -42, -415], [-2, -392, -251, 735], [1788, -1314, 19, 403], [-661, -1164, -703, 432], [776, 145, -244, -826], [-1448, 70, -1007, -895], [-2941, -723, 113, 164], [-912, -531, -553, -664], [2296, 389, -497, -184], [-2396, -272, 325, 1106], [1856, 692, 1244, -198], [-1526, 373, 987, -122], [-889, 724, 126, 599], [1388, -387, 829, 917], [-1609, 415, -714, -305], [2504, 343, 37, 1089], [86, -1660, 243, 353], [-60, 962, 1848, 100], [-1227, -472, -761, -1878], [-5480, -326, -219, -978], [2570, -551, 1739, 985], [-628, -801, 351, 1413], [-6582, -1114, 41, 1265], [-988, -1712, -937, 735], [340, -1529, 986, 387], [2698, 174, -548, -1066], [-2196, -1336, 484, 1765], [-1783, -1764, 777, -652], [-500, 765, -247, -1271], [228, 1354, -718, 676], [-3726, 960, 1551, -7], [1173, -2630, 583, 2], [1374, 1111, -275, -2397], [-1620, 1934, 1274, -1184], [852, -223, 191, 203], [2480, -2035, -2970, 133], [-3756, -1525, 188, -1016], [586, 2881, 514, -1373], [-715, -2635, -1759, 836], [2602, 908, -338, -2610], [-3352, 1905, 1926, 1221], [2742, -812, -704, -2106], [1731, -159, -126, 779], [-1631, -158, -206, 3501], [-62, -906, -3360, 1118], [636, -1680, 2220, 2000], [6746, 1873, 2126, -960], [444, -1808, -92, 578], [4740, 2659, 216, -1952], [-7026, -2567, -2271, 2205], [-6526, -1752, 758, 860], [390, 54, 442, 56], [-6376, -226, -5155, -1260], [-1876, -682, -1154, 3128], [4136, 4975, 2596, -2075], [-5516, 155, -1146, 2406], [-4528, -1085, -4284, 3], [-8280, -160, -1421, 695], [442, 5466, 3565, -731], [-940, -1673, -2938, 294], [8942, 2378, 1282, -1818], [-3946, -2585, 2415, 21], [6144, 4461, 871, -2284], [-5283, -657, 2586, 1923], [6814, -2878, -1893, 1597], [2124, -4104, 3560, 914], [3445, 2812, 165, 1474], [10044, -2668, -2342, 1594], [232, -3487, -437, 139], [-13302, -3490, -2759, 823], [-8492, 1120, -746, -25], [-12, 2866, 4964, 3034], [8785, -1120, 1903, 3982], [18534, -4348, -1216, -620], [4052, -1120, 5592, 784], [2810, -3276, 900, -2296], [-7848, 516, 1087, -2133], [-1136, -4094, -7444, -2712], [4966, 6083, 1147, 379], [-7278, -7000, -458, 160], [1468, 4923, 877, 2107], [-5502, -4669, -1271, -889], [-4662, 7201, -1605, -2381], [-13740, 4949, -2073, -1754], [-4524, 8021, 3258, -541], [-7714, -1885, -6873, -2492], [-3238, -2771, 3650, -1804], [11749, -2199, -3684, 457], [-3786, -2225, -1721, -7350], [-7806, 2188, -2294, -3221], [-15618, -3036, -870, -6001], [-1438, 5292, 493, -4047], [9758, 729, 567, -1352], [-7738, 6822, 6936, 1340], [282, -1692, -2889, -3135], [-5218, 7051, -4905, -2056], [-12366, -6952, 2991, 2097], [-20494, 3048, 4566, 1675], [16021, 5134, 1923, 276], [14966, -12437, -5657, -1064], [-11086, -2364, -9702, -3746], [3745, -3050, -7396, -1865], [-14834, -1376, -4212, -2798], [-19949, -2578, -2001, -2984], [17290, -3786, -2349, 2555], [-11149, -988, -6160, -7723], [-8215, 5191, -4287, -4248], [-33245, -2866, 166, -663], [19320, 278, 2144, -3060], [-11144, -6909, -2538, 6533], [-11148, 1251, 2475, 324], [-14794, 924, 1168, 6168], [-8988, -2440, -4398, -9196], [9636, 5394, 11212, 1704], [-32963, -402, -1320, -2361], [26972, -3444, 6802, 6110], [-10328, 794, -1729, 21], [-2175, -573, -181, 6896], [-20419, -236, -7486, -6685], [-20108, -9558, -6374, 7466], [8994, -3242, -8573, 2177], [6581, 6399, 1400, 537], [-6150, 9691, 5265, -3545], [3162, -5216, -7656, -645], [29278, 3736, 3812, -3524], [-15538, -11315, 3297, -1776], [-9072, -6747, 1124, 11012], [942, 4910, -3770, -9356], [9912, -16161, -2907, 621], [770, 7698, 3408, 4581], [2980, -252, -9541, -2540], [14414, -73, -2501, -5278], [13510, 2147, -2243, -12361], [16552, -5812, 9070, 10620], [-7314, 1499, -12277, -5297], [21814, -7994, 4596, 8212], [-25482, 132, -5590, 892], [-21939, -5334, -2845, 4294], [-13676, 13447, 13149, 365], [13852, 11938, 2242, -3108], [16634, -4681, -3551, 3346], [-28962, -9896, -5554, 1682], [17316, 6285, -5841, -6259], [16245, 9111, 28, -3073], [29913, -4473, -3572, -5185], [-21634, 18477, 10878, 5021], [11010, 6613, -361, -2726], [-17671, 5962, 1200, -419], [-10858, 5331, -7891, -4487], [6252, 7894, 17283, -975], [-42035, 586, 1311, -294], [25313, -3036, -6061, -12524], [-14313, -3974, -8981, 4852], [10664, -2001, 1115, -3881], [-12848, 6790, -5466, 2574], [-27446, 1826, -10303, 1203], [7588, 2632, -1324, -3162], [2628, 8335, -2, -242], [-39840, -796, -14717, -6595], [-1182, 4939, 1607, 8541], [18053, 10788, 1913, -1780], [-26140, -14618, 1431, 5887], [18544, 8979, 4997, -8079], [685, 7448, -2573, 2048], [-5896, 3674, 17569, 1059], [-14792, -1340, -3154, -6020], [-16866, -2792, -142, 9354], [-5378, 17797, 11443, -2045], [16106, 1350, -5478, 2602], [-16770, 11879, 5362, 4025], [4678, 758, 15060, 10096], [-13430, -9556, -7386, -4264], [-2274, -26502, 4475, 9181], [35498, -296, -1906, 3030], [-5056, 1624, 1029, 14875], [7806, 5547, 20915, 7093], [-40754, -4299, 3812, -8106], [-33244, 3540, 3420, 9626], [1716, 9588, 21745, -5721], [41212, 5955, -6734, 6154], [22248, 13862, 16574, -3874], [-29118, -11189, 2490, -13425], [-5570, -13313, -14279, 9373], [57554, 2818, 6810, 2804], [35199, 12000, 9999, 3862], [-17779, 2556, -12373, -16114], [37822, -7303, 14953, 7779], [-14165, 17709, -7176, -3987], [-6022, -7354, -6747, -6073], [-4572, 10504, 4031, 4125], [-9593, -10928, -24502, 1079], [-49024, -12156, 1800, -2228], [-12969, -15352, 6060, 7973], [13781, -6234, -4227, -14328], [-19060, -17655, -14489, 8690], [21758, -8309, -1755, -6311], [-7706, 7013, 16780, -6476], [-20586, 13697, -7309, 1067], [16508, -11574, 12959, 8595], [-47063, 13953, 16808, 5843], [-8902, 9504, 656, -11932], [-33816, 18430, 21527, 2519], [-10546, 15161, 1703, 4051], [-6357, -13646, 3551, 14410], [-6786, 14764, 1340, 9116], [-49166, 8620, 17675, -2531], [19757, -4876, -3285, 8760], [-16186, -4130, -2051, -10059], [53048, 652, 13771, 5950], [-7862, -5935, -2963, -13555], [-22233, 4492, -4879, 2036], [-4062, 21391, -6253, 1388], [-37910, 10673, 369, -7980], [45470, 3717, 3701, 1260], [28222, 7172, -9218, 13091], [-23276, 17278, 19676, 1850], [30534, -4415, -4516, 3706], [-60808, 15615, -5524, -2427], [-4698, 10909, -2379, -1635], [40314, 7356, 8628, 2300], [55505, -720, -18128, -511], [3202, -945, -4946, 706], [42074, -10154, -10908, -7474], [-10224, 10978, 1072, -12756], [32136, 17364, -11812, 6490], [-37826, 671, -8865, -20796], [18032, 15440, -4330, 3222], [-60310, -8432, -22856, 3398], [28844, -6510, -12994, -13662], [-52380, 9158, 8324, 5132], [-8148, 25984, -1604, -10634], [-86506, -8811, -14715, 1457], [-26076, 4883, 16191, 14918], [51198, 4926, 3518, 2423], [30758, 9341, -5894, -8900], [-43936, 11958, -13858, -3338], [-61496, -6924, 5045, 4999], [-12650, 356, 2960, 8858], [5232, 5529, 2441, 406], [-54478, 12423, 1429, -10694], [-31307, 31503, 3106, -2137], [-23635, -18248, -14748, 17437], [-27112, -1515, 21614, 12635], [-35720, 8056, -8433, -22877], [20674, -7608, -668, 12450], [9310, 1177, 4869, -3248], [53036, 30368, 10701, -22677], [45973, -21503, -3948, 10759], [-27623, 19364, 6007, -10380], [-74798, -1187, -8947, 3707], [61972, 23717, 3475, -28599], [-17510, -12436, -2368, -22293], [-25453, 1758, 3059, 10736], [-3574, 6873, -12070, -1970], [17421, 13452, 18599, 30964], [-85826, 3112, 4183, 9447], [2208, 8489, 11920, -6691], [63452, -2540, -11816, -9220], [-21837, -24434, -9305, -2592], [-10460, 17081, -14486, -5183], [-5856, -9412, 21274, 19650], [-25640, 4487, -5458, -18493], [6690, 2987, 25569, 24280], [-41638, 2530, -33156, -17642], [3564, -9803, 13863, 11497], [25112, 18178, 24702, 18946], [52504, -7785, -2770, 14372], [-27100, 11806, 4759, -7005], [-7044, -31821, -9479, 3000], [68490, 13359, -5610, 9641], [-56722, -29392, -7643, 18487], [-15102, -11893, 11671, 29733], [27232, 11798, -7654, 6829], [-2096, 38206, -10892, -8234], [53406, -41194, -10690, 3988], [-74768, 14328, 25268, -1496], [14310, -16664, 934, 27194], [-42760, 36311, 11285, -7089], [5340, 1104, 14005, -3885], [100572, -1345, -2263, -6019], [-36788, -13224, -13582, 3123], [-528, 3271, -9161, -2829], [-22986, 23340, -9268, -16794], [3908, 16404, -6150, -19266], [39300, -8402, 9496, 22368], [101458, 19350, 15134, -5318], [24675, 1014, 28677, 15550], [-1130, 778, 12282, -3268], [18522, -34011, -15905, 3824], [-68198, -4059, 29007, -2208], [13828, -21556, -11422, -22230], [-71580, 457, -2443, 18727], [43848, -1265, 16127, -11377], [-10164, 18076, 28350, -16892], [-77138, -25378, -16112, 19252], [119608, -1953, -8152, -4692], [54256, -775, -39426, 14196], [103951, -11428, 19270, 17415], [83660, 3468, -8522, 12770], [46592, 26343, -2542, -4226], [43940, 9175, 29916, 41896], [-33459, 20072, -16603, -27392], [10110, -32049, -569, 8211], [117551, -23535, -8085, 3746], [69567, 21502, 22461, -818], [16866, 11601, 23991, -23846], [58132, -49044, -45610, -15384], [-28598, 28138, 34279, 22045], [89664, -14566, -3166, -16844], [-15796, 11757, 17247, 10257], [67200, 454, -9424, -39844], [34222, -21684, -20549, -7323], [-11544, -272, -9560, -34856], [21076, 14320, -16599, -9143], [-66032, 16084, 20225, 10071], [55380, -4755, -10267, -22518], [-21104, 11962, -8400, -4352], [6234, -16199, 8093, -15383], [-70870, 24209, 7845, 7434], [62708, -38305, -37151, -16316], [-13063, -6577, -7775, 13588], [-74336, -10013, 4212, 12609], [75337, -861, 8936, 11071], [59278, 10812, 35901, -3555], [51752, 4161, -15018, 12505], [-49023, -36784, -18934, 18307], [-21636, 36006, -15453, -5928], [36220, -32826, -27392, 6736], [69356, 33564, 12898, -9516], [48321, -762, 9129, -25652], [110812, 20906, 14570, -34728], [69744, -20084, -5150, 46], [77782, -16464, -35560, 7132], [83202, -45026, -40906, -12804], [-124196, -25584, -11558, 28136], [37872, 7639, 903, 24473], [-99480, 8502, -29140, -11530], [-50954, -26037, 12975, 211], [-108754, -25426, -462, -4455], [44655, -29884, -14179, -18540], [26144, -21466, 3265, 34675], [36240, -112, -28793, 11253], [6819, 18308, -6529, -22052], [54172, -2666, -36396, 16656], [-49596, 37509, -8559, 1768], [-58018, 30104, 23654, 8988], [-94618, 27381, -18982, -26434], [108506, -15612, 9728, -1154], [-61556, -10225, -11422, -11529], [-69506, -25002, -113, -9467], [-171088, -19738, -4099, 18715], [-116811, -2385, 15007, 7004], [123540, -3413, -18394, -17692], [50416, 23596, -7888, -8640], [37444, 20089, 41892, 32431], [77232, -9299, 9264, 16667], [3852, -10360, -14050, -21931], [73961, -37701, -47077, -8012], [-25336, -9538, -48624, -9140], [1485, -14940, -1882, -7179], [-98574, 2282, -6146, -7620], [-7940, 33995, 9118, -22332], [-16638, -15052, -37304, -25867], [-45952, 20137, -15819, -8643], [170298, 6279, -1615, -9401], [-34158, 45322, 7223, -14361], [-86932, 4388, 45374, -7992], [47917, -9200, -51326, -16069], [-89946, -15943, 14028, 8194], [77498, 31326, -27109, -16103], [77466, 54279, 28341, -28948], [133023, -38079, -32009, -5934], [120880, 10344, 42186, 36202], [-61808, -54340, -8220, 33442], [47240, -23912, 6276, 16458], [-97274, -27337, -8857, -36466], [127800, 23761, 15425, -668], [65482, -21346, 8048, -9744], [20290, -68921, -12576, 1906], [10868, -25034, -38472, -6278], [91010, 73, 46071, 46985], [-53944, -24843, 4868, 3197], [27322, -35400, -72560, 472], [-113856, 17893, 41789, 4523], [46384, -20434, 25190, 114]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1859_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1859_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_1859_4_a_b(:prec:=4)
    chi := MakeCharacter_1859_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_1859_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_1859_4_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1859_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![52, 2, -19, 0, 1]>],Snew);
    return Vf;
end function;