
// Make newform 169.4.a.k in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_169_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_169_4_a_k();".
// 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_169_4_a_k();".
// 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 := [-4096, 3584, 4784, -3203, -1501, 680, 145, -46, -4, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], [-149293824, 52058000, 91159455, -12738943, -11530440, 728955, 501446, -12396, -6901], [-6050816, -22791952, 16793257, 4265751, -3297016, -384275, 195178, 11372, -3843], [-646464, -1604391, 252488, 485407, -171803, -72705, 19422, 2737, -595], [-92826880, -97179408, 67400873, 38793527, -14845688, -3803155, 1059978, 103372, -23843], [-122506496, 12391680, 111900273, 12527327, -23878504, -2570427, 1548474, 90940, -32107], [-202361088, 51740944, 217315329, 3078399, -46218904, -2210427, 2837370, 88524, -53515], [59238144, 34322440, -85614861, -25660427, 19362096, 3186791, -1304226, -97156, 27975]];
    Rf_basisdens := [1, 1, 8071424, 4035712, 504464, 8071424, 8071424, 8071424, 4035712];
    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_169_a();"
function MakeCharacter_169_a()
    N := 169;
    order := 1;
    char_gens := [2];
    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_169_a_Hecke(Kf)
    return MakeCharacter_169_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, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0], [-2, -3, -1, -1, 1, -1, 0, 0, -1], [-4, -1, -1, 1, -2, -1, 0, 0, 2], [-21, 2, -2, -2, 0, 3, -2, 0, -1], [0, 0, 0, 0, 0, 0, 0, 0, 0], [-7, -8, 2, -2, 5, 1, -7, 5, -7], [-14, -4, 1, 0, 0, 3, -1, 1, 10], [-22, 8, -6, 0, 1, 14, 2, 0, -3], [16, 29, 3, 3, -6, -5, 6, 0, -8], [-82, 21, 7, -1, 2, -1, 12, -8, -4], [-12, -28, -16, 14, 9, 4, 13, -11, -12], [-150, 14, 3, 6, -2, -21, -12, 2, 7], [-70, 14, 3, -2, -25, 21, -5, -5, -11], [-92, -25, -23, 1, -25, 7, 10, 4, 5], [-18, -18, -12, -12, 36, -6, 13, 3, -43], [-158, -49, 30, 1, 8, -13, -22, 22, -34], [4, 6, 16, 52, 17, -18, 51, -13, 18], [102, 45, 28, -57, 37, -3, -9, -31, 22], [-264, 11, 71, -11, -27, 17, -27, 23, -26], [-52, 85, 50, 5, -45, -39, 19, 15, 2], [44, -5, -3, 77, 3, 9, -5, 21, 38], [-444, -21, 88, 85, -10, -16, -25, 13, -10], [-210, 42, -9, -44, -18, -46, -17, -29, 13], [157, -96, 54, 68, 87, -17, -20, 6, 8], [164, 5, -39, 89, -5, -45, 1, 47, 38], [416, -82, -76, -14, -64, -4, -9, 27, -21], [180, 131, 48, 41, 13, -24, 19, 35, 137], [-246, -72, -102, -152, 94, -44, -76, 32, -46], [-142, -38, 11, -164, 88, 54, -51, -51, 25], [68, 59, 93, -27, 29, 7, 52, -26, 121], [-116, 13, 106, -89, -81, 6, 2, -2, -8], [184, -121, -126, -115, 67, 153, 84, -46, 57], [1213, -88, -92, 80, -39, 43, 59, -53, 21], [-828, 453, -33, 69, -118, -73, 63, -41, 221], [-390, 144, 126, -52, 75, 108, 64, -70, -137], [-412, -189, -205, 77, -134, -3, 92, -2, 222], [-533, -146, -38, -6, -179, -93, -129, -29, -93], [-1246, -126, 62, -158, 94, 90, 4, -16, 66], [910, -320, -152, -76, 2, -96, 72, -48, -2], [-57, -100, 136, 268, 78, 33, -81, -3, -220], [288, -297, 133, -53, -26, -135, 125, -95, 235], [-140, -524, -246, -256, 56, -52, 161, 13, -169], [1734, 68, -225, -176, 87, -61, -44, 16, 16], [706, -26, -202, 130, 101, 218, -104, -22, 131], [-1064, -18, -44, -266, 253, -4, -219, 15, -330], [-1759, 678, 30, 134, -208, 277, 116, -190, 27], [1554, 295, -51, -119, -260, 99, 10, -58, -2], [-1034, -225, -122, -335, 17, -253, 373, 63, -4], [1384, -170, -230, -232, 24, -216, -133, 1, -179], [-1254, 272, 121, 354, 138, -132, 159, 47, 153], [-1166, -497, 39, -179, 334, 45, -161, 71, -263], [1364, 407, 287, -3, -95, -153, -302, 158, 167], [553, -485, -231, 53, -319, -84, -196, 72, 262], [-160, 547, -14, -167, -71, 258, 388, -10, -140], [258, -952, -460, -56, -79, -246, -291, -209, -106], [-2946, 781, 221, 499, -357, 189, -48, -80, -61], [-1632, 322, 162, 106, 108, -172, 179, 259, -121], [1284, 960, -178, 206, -212, 256, -177, 5, 115], [-45, 312, 576, 298, -276, 381, 221, -117, 250], [-1398, 434, 209, 218, -221, 87, -556, 84, 450], [1514, -183, 45, 125, 210, 109, 47, 103, -337], [437, 175, -27, 653, -231, -328, 126, -106, 520], [-2254, -589, -85, 701, -278, -163, -172, 60, 236], [107, 375, 209, 315, -32, 224, 803, -209, -82], [316, 150, -132, -54, -108, -294, 402, 202, 326], [272, -414, -426, -190, -328, 402, 383, -161, -59], [52, 19, -284, -781, 706, -454, -33, -163, 382], [2776, 552, 64, 344, -640, 92, 471, -17, 645], [732, 794, 454, 88, -303, 566, 631, -257, 322], [195, 636, 854, 260, 139, -99, -498, 308, -356], [-4808, -6, -154, -30, 13, -368, -346, 20, 189], [-4678, -181, 629, -1035, 734, -377, 31, -81, -913], [-928, 202, -214, -1080, 894, -372, -461, -227, -141], [172, -1386, 351, -246, 165, -238, 155, 217, -78], [-4484, 8, 320, 876, -398, 144, 260, 96, -186], [-1496, -622, -292, -670, 20, 288, -542, -338, -912], [1444, -955, -349, -871, -207, 141, -471, 87, -706], [-227, -1004, 448, -612, -100, -167, 330, 134, -329], [1914, 750, 785, 50, 422, -368, 378, -348, 2], [378, -367, 267, -721, 267, 121, -259, -189, -50], [410, 1688, 82, 550, -12, 820, 413, 179, 665], [-4282, 240, -860, 592, -775, -56, 374, -28, 771], [-166, -1314, -147, -302, 358, -804, 164, -334, -880], [1242, 1081, -1207, -325, -798, 5, -281, -197, -165], [551, -1155, -265, 923, -729, 580, -232, 340, -266], [2954, -548, 1047, 182, -197, -27, 1041, 291, 311], [2728, -1809, 387, -609, 239, -451, -419, 59, 1046], [-2368, -258, -224, -228, 508, -752, -737, 225, 837], [-194, 814, -304, 378, 203, -530, 1013, -525, 918], [6816, 250, 125, 374, 37, 410, -125, 261, 826], [-812, -205, -283, 245, -52, 971, 251, -129, -1103], [-2020, 1941, -619, 191, -626, 207, -454, -306, -882], [-2948, 178, 511, -262, -418, -1398, -793, -425, -17], [-1079, -758, -622, -1046, 962, 141, -723, 451, -592], [-1174, -1263, 265, -81, -992, 391, -561, -197, -861], [-758, 1302, 320, 1368, 258, 276, -713, 73, 751], [2212, -1369, -420, 665, -458, 483, -208, 356, 1004], [6583, 989, 251, 471, -155, -36, -1003, -87, 391], [-836, 728, -1398, -392, -715, -32, 534, 560, -391], [-2002, 923, 712, 1041, 24, 227, -109, -125, -599], [-1380, -531, 239, -295, 706, -869, -143, 629, -353], [-8522, 1711, -279, -219, -519, -165, 952, 586, -495], [-932, -805, -806, -1329, 389, -457, -1207, 129, -690], [-408, -194, 31, 162, -1172, 1082, 383, 243, -579], [-2302, 2388, 2087, 302, -298, 489, -99, 417, -576], [-1068, -437, 894, 1913, -132, -38, 1652, 30, -163], [3081, -513, 523, 1025, -109, 1354, -714, 90, -60], [-2800, 680, -536, -1492, 1047, 854, -403, -369, 532], [1854, -2168, -941, 980, -176, 119, 656, 130, -925], [3088, 1275, -103, -151, 831, 29, 753, 419, 104], [1796, -886, -1148, -1384, 374, -18, -685, -495, -2215], [-4352, -1601, -406, 797, -424, 1247, -2120, -12, -606], [-3436, -2340, 960, -464, 924, 620, -459, 817, 223], [-5594, 1871, -1167, 1753, -1361, 89, 477, -473, 1216], [1118, -1208, 361, 1026, -1108, 455, 135, 479, 1378], [-232, -2125, -760, -1359, 323, 516, -324, 1080, -1718], [-3602, 946, 718, -646, -86, -1748, 1180, -576, 156], [-5436, 2595, 1003, -1613, 322, -1177, -1637, 431, -1733], [-3756, -986, -1687, -1174, 723, -74, -1318, 152, -1035], [-5294, 379, -657, 2681, -495, 767, 1148, 28, 1673], [-8100, -369, -1644, -661, -38, 977, -1455, 859, 439], [1940, 2238, -14, 3722, -98, 1476, 318, 466, 966], [1750, 2884, -289, -1028, -966, -1007, 805, 127, 1228], [-822, 1810, -839, -850, -370, 3103, -357, -127, -38], [7688, -2166, -1106, -2530, 1043, -602, -1160, -338, -1283], [1722, -597, 187, 2053, -697, 809, -1990, -54, 55], [5618, 315, 1799, -791, 373, -777, 1104, -434, -1475], [-7682, -1027, -251, 675, -644, -1655, 508, -144, 536], [-3242, 710, -582, -2400, 958, -18, 749, -1009, 355], [7702, -2769, 250, -2339, 2603, -1889, -2212, -402, -2273], [-2836, -1067, 87, 71, 2076, -291, -1034, 90, -1682], [11880, -212, 1976, -2000, 212, 362, -138, 298, -716], [5734, -1894, -2798, -222, 301, 674, -682, 664, 393], [-9417, 563, -535, 2075, -28, 156, 1203, 223, -2570], [-3355, -296, -1236, -438, -296, -1809, 1083, 233, 1822], [-1102, 3648, 206, 1118, 16, -370, 273, 1087, 471], [13665, -2404, -1748, -192, 203, 691, 1317, 281, 1325], [6074, 3109, 1143, -973, 240, -331, 1568, 374, 746], [-1754, -3072, 1259, 96, 1159, 487, -1716, 44, -1236], [7805, -3360, -356, 1008, 479, -1117, 1362, 422, 644], [394, -2644, -2546, -374, -1317, -154, -2129, -13, -342], [3364, 638, -836, 834, 681, 558, -258, -64, 2539], [9856, 1458, -81, 406, 498, -626, -2383, -623, -607], [400, -2092, 186, 874, 1059, -262, 525, 9, -2968], [-15322, 1522, -40, -2522, 516, -1268, -1784, 1372, -518], [-7030, 402, -1984, -1462, 812, 110, -2108, -324, -1208], [-3637, 2381, 2953, 705, 2335, 404, 999, -371, 473], [10778, -2719, -444, -165, 135, -511, -736, 2214, 99], [2398, 536, -496, -1984, 845, -1430, -2110, 1328, -363], [2954, 3297, 295, 151, 338, 391, 1042, 420, -604], [5697, -1325, -1981, -3189, 1937, -1172, 1867, -943, -111], [-3663, -708, -1328, -1828, 22, -1013, -1997, 633, 34], [-5464, 5177, -87, -1153, -898, 1753, 241, -183, 2757], [-4760, 2100, -991, 4040, -2349, -572, 390, 520, 1025], [1584, -4518, 68, 2006, -2370, 1852, -827, -487, -2105], [4262, -2468, -2796, -448, 768, 2736, -985, 911, 2309], [-12888, 3349, 1182, 1385, -1237, 1088, 3679, -1005, 2393], [-1448, -1437, -2016, -973, 1172, -798, 2093, -1145, 966], [-1286, 1700, 1442, -1232, 2939, 508, 1168, -466, 805], [12914, 864, -1261, 1160, 584, 609, 1796, -930, 619], [-3100, -3149, 608, -2427, 2211, -2372, 1810, 356, -1014], [2984, -2127, -1121, 1371, -2272, 1433, -889, 227, 2837], [-15010, -6992, 839, 2192, 2030, -1579, -460, -446, -531], [-4310, 3964, 1989, -346, -1194, 1243, 625, 229, -2262], [4414, -4741, -1463, 485, 1933, 1871, 1668, -254, -651], [-6848, 1454, 3368, 966, -1038, 924, -1929, 1639, -3419], [11298, -2659, 1893, -585, -2038, -1689, -2678, 1572, 2000]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_169_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_169_4_a_k();".
// 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_169_4_a_k(:prec:=9)
    chi := MakeCharacter_169_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(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_169_4_a_k();".
// 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_169_4_a_k( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_169_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![344, 1464, -5898, -1257, 2310, 486, -205, -42, 5, 1]>],Snew);
    return Vf;
end function;