
// Make newform 338.4.a.k in Magma, downloaded from the LMFDB on 15 January 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_338_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_338_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_338_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 := [1, -2, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-2, 0, 1]];
    Rf_basisdens := [1, 1, 1];
    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_338_a();"
function MakeCharacter_338_a()
    N := 338;
    order := 1;
    char_gens := [171];
    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_338_a_Hecke(Kf)
    return MakeCharacter_338_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]];
    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 := [[2, 0, 0], [-5, 0, -3], [-1, -8, 1], [8, 8, 5], [-35, 24, 1], [0, 0, 0], [-14, -5, 43], [-57, -15, -56], [63, -102, 68], [-154, 182, -179], [-128, -68, 67], [-55, 59, -22], [-239, 254, -276], [-390, -29, 6], [359, -278, 263], [-332, -63, 36], [351, 166, -194], [-431, 19, -166], [432, 79, -230], [359, 129, 297], [122, -623, 30], [-631, -163, -95], [-207, 141, -289], [-443, 21, -217], [740, -674, 599], [615, -651, -83], [-114, -419, 444], [892, 103, 837], [478, -608, -214], [-235, 729, -971], [-1025, 744, -1755], [-1014, 684, -1659], [-1240, 680, -764], [640, -1277, 1249], [-126, 69, 1539], [-181, 1832, -870], [-2020, 1612, -1203], [46, 165, 655], [880, -2128, 742], [68, -1176, -820], [2293, -605, 891], [-2130, -7, 379], [1326, 1279, 156], [2040, -398, -332], [1127, -1400, 2192], [-675, 1807, -1152], [1345, -1390, 921], [1486, 2194, -2165], [1332, -2943, -448], [164, -1619, 2904], [2803, -1889, 911], [-1230, 355, 1393], [1719, -1390, 3319], [-2504, 3570, 1146], [-2914, -1910, 51], [-3235, -1133, 384], [-511, 3784, -49], [912, -3633, 3050], [-446, -781, 202], [709, -4783, 2721], [1040, -530, 5612], [-1442, -1959, -1565], [-2472, 4800, 2086], [-1614, 3108, -4959], [-5321, 841, -1000], [-3918, 790, 3516], [-3008, 559, -440], [-1977, -1487, 3847], [-4588, 1629, 1438], [5561, 2269, 2104], [3330, -3536, 7161], [-7119, 4326, -5276], [-7552, 2217, 191], [-3986, 4071, -2492], [-8178, 4179, -9829], [1038, 912, -5832], [1606, -6178, -3566], [-5979, 3047, 2491], [-2605, 5992, -243], [-869, 162, 6663], [8423, -7865, 6821], [-8000, 5823, -11474], [7299, -822, 3836], [673, -2468, -4375], [3722, 3385, -2101], [2544, -3134, -354], [2536, 8141, 898], [3589, -2581, 5169], [1566, 725, 6214], [-5017, 3933, -10006], [-8000, 1361, -8209], [12316, 2835, 673], [4564, -3886, 6261], [9901, -3995, -3363], [9325, 1895, -2143], [2424, 3473, -3207], [-5914, -4491, 1974], [-6581, 11940, 3974], [-9614, -335, 4112], [-2453, 5850, -1708], [-4221, 6829, -4498], [-13426, 5515, -15897], [14169, -492, 9097], [-974, -3187, -5198], [10371, 3265, 3753], [5827, 2161, 5904], [-191, 6098, -5083], [10260, -4984, -3606], [-1663, 1963, -5108], [-14605, 12530, -15150], [-5107, 8155, 2325], [-2692, 7381, -7736], [3125, -6064, -6120], [7984, 1945, 10490], [9105, -4939, 10519], [17529, -13935, 18024], [-1510, 566, 7809], [376, -3768, 7336], [5816, 1283, -11781], [-2674, -4762, 5053], [-17975, 4296, 711], [14739, -5383, 8324], [12922, -4186, 16600], [18407, -7153, 7516], [-13153, 16065, -9402], [-8323, 21824, -10124], [-745, -98, -11247], [-10425, 15340, -1773], [60, -23980, 10233], [-1464, -21881, 7818], [3130, -4268, 542], [15120, -13398, -2079], [-10820, -18726, 8142], [-19767, -9214, 5544], [-3551, -16653, 488], [-12853, 8679, -10433], [-13524, 14935, -18868], [-4522, 13987, -9297], [-23230, 4052, -4093], [-5988, -23882, 4312], [-27574, 19768, -29459], [8327, -15355, 2646], [11017, -8942, -5648], [3279, -7863, 22475], [-617, 6217, 290], [-16748, 1356, 542], [-11300, 10640, -984], [2594, 9453, -3268], [-18266, -19796, 4756], [8727, -5194, -2358], [3000, -7298, 5975], [-5546, 4845, 640], [10415, 11279, -7091], [444, -20481, 12223], [-14644, 1354, -24595], [29404, -3685, 4956], [-22670, 18637, -6558], [15058, -24605, 11377], [6697, -7345, -11577], [1215, 9700, -14280], [-25279, 14190, -34990], [19770, -8472, 9231], [2232, -24781, 27615], [26153, -10102, 13870], [-24547, 14389, 3088], [-12205, 10444, -23525], [35624, -11703, 3584], [2318, 7258, 27143], [-5839, 16676, -11230], [-14580, 347, -1871], [-8525, 28072, -6494], [-7438, 10791, 14525], [45427, -4353, -299], [2839, -500, -22210], [-41406, 7461, 576], [-16334, 15083, 4415], [-36171, -691, -1618], [18595, 10217, -16938], [24997, -4275, 8784], [3992, -4446, 3278], [-3355, 8901, -6363], [32215, -8676, -10234], [37189, -26216, 19988], [138, -29382, 7173], [19042, 1713, -21520], [-23079, -4756, -12106], [37802, 14685, -7996], [-33541, 828, -12998], [-27357, 13709, -13231], [46373, -9540, 6742], [-1682, -27156, 663], [-5798, -16327, 19816], [-14922, 21089, 24107], [4192, -12188, 29468], [-14075, 11455, 33311], [5557, -795, 25163], [-17363, 18414, -16099], [-25731, 30690, 3659], [-51615, 639, -5765], [-23975, -20200, 14044], [38073, -14314, 9326], [28321, 25525, -25375], [10442, 27180, -2341], [43734, -26832, 57045], [4154, 12725, -3674], [-5459, -3440, 38566], [58271, -43537, 62622], [-22658, -6138, 11409], [-8968, 15056, -51583], [16998, 12430, 15783], [6896, -45289, 6402], [10929, -21840, -9111], [-23035, 53914, -26810], [-30850, 17780, -48143], [-415, 43975, 1857], [42672, -693, -22027], [3631, -29891, 14500], [16299, -21768, 40598], [-19886, -14193, -817], [28078, -14683, 28353], [11024, 36994, -17080], [21332, -41717, -6374], [18323, 16750, 24656], [24871, -33236, -27171], [28376, 23606, 2270], [-15606, -20684, -2135], [-8914, 19909, -8442], [-15724, 49973, -37957], [-20342, -18424, -24789], [-26252, 11470, -19200], [13216, 31678, -27937], [897, -13643, -19618], [-31179, -5638, 20601], [-42681, 17035, -13168], [-6975, -51568, 10834], [88279, -30845, 48807], [9101, 9298, -22563], [32191, -33358, 26271], [-15693, -14919, 2074], [-9860, 50618, -23330], [-43889, -10241, -36345], [42105, -15927, -43856], [-59287, 16008, -14079], [4355, 4630, 10275], [11364, -41141, 22166], [43753, -37499, 32663], [25705, 56210, -12815], [-34885, -9476, 30366], [64096, -36805, 39536], [-55187, 35289, -53317], [32741, 9848, -4906], [18129, -26637, 22234], [42707, 27204, -8829], [-9377, 41181, -52753], [-28643, 68793, -9801], [-46262, 2533, -19759], [21908, 17940, -13100], [4534, -66626, 40564], [45564, -21896, 40163], [-35017, 26735, -27164], [33069, -2129, 596], [30641, -1233, 54541], [51328, -50205, -21947], [81153, -59806, 90827], [-46132, 49833, -53435], [-23970, -37940, 15638], [-32818, -19853, -57243], [45546, -31266, 31971], [-40474, 16, 24883], [54003, -44653, 36990], [3400, 4522, 9755], [-53687, 26186, -21504], [-67115, 16690, -28993], [13677, 42805, -41263], [-33150, 85748, -61623], [7979, 22450, -11518], [64146, -32199, 84043], [1557, -52797, 33582], [49116, 6999, 62006], [-69673, 60357, -81096], [51496, -61097, 55952], [70595, 20586, -21586], [30642, -51133, -9291], [3270, 21269, 12154], [-88718, 16682, 34601], [-38718, 54958, -5832], [26877, -71312, 15933], [-28854, -35696, 49580], [40506, -30877, -34204], [77352, -30129, 37603], [-39620, 62997, -86380], [-100431, 11660, -613], [-76766, 15613, 1522], [11051, 5942, 74793], [-46761, 60726, -62590], [-45327, 41937, 12933], [9322, 2707, -59541], [21638, 53061, 18222], [21892, -75826, 16879], [-86170, 4789, -17270], [11952, -64244, 42044], [-96043, 32004, -63895], [45215, 19997, 61149], [3606, -93822, 10576], [-2921, -80735, 50934], [-98109, 95280, -90231], [-51242, 595, -84181], [-56599, 61996, 57964], [-29645, -7881, -3027], [-65448, 67468, -28036], [86933, -55385, 94263], [-14350, 73916, 49966], [1460, -34814, -3967], [-39771, -11295, -77629], [28527, 9415, 27370], [-33722, -62672, 46322], [-15190, 51915, -36451], [41641, -39281, 2614], [-34597, 4421, 12302], [-43011, 46725, -13466], [-53833, 20916, -12733], [-114302, 74022, -30841], [-65012, 90860, -103908], [79854, -67345, 26400], [-33187, 17203, 86877], [-119291, 6773, -17780], [-90711, 30001, -90733], [93533, -53762, 56628], [34589, -109588, -32907], [89544, 19628, 4351], [68099, -106954, 11686], [39219, 1809, 3531], [14989, -37030, 2254], [43055, 37357, -49838], [45910, 8843, -41140], [-75739, 96718, -124221], [-103853, -32340, 1546], [40302, 69118, 37293], [-5003, 37396, 42511], [-88411, 84119, -19390], [63663, 41955, -17233], [80169, 3152, 12878], [110051, -71400, 59079], [-84113, 64535, -22740], [-66196, -50548, 54814], [-38987, 39505, -81855], [-112085, 25644, -11719], [123664, 11264, 8302], [114496, 48507, 2567], [131593, -51335, 67862], [-91613, 35536, -122916], [82619, -62726, 4934], [-58320, 44053, -43219], [-25386, 35590, -111766], [-6132, 65170, -31261], [-17687, -1011, 35652], [35439, 60021, 36151], [52923, -59273, -10655], [3815, 37097, 50842], [99917, -63068, 49299], [-48927, -34691, 20647], [158167, 16240, -27546], [-169056, 48592, -2323], [-38932, -37486, -64488], [-72248, -44835, 37597], [122251, 48527, 5547], [-121362, 70811, -152433], [26910, -72897, 61391], [34315, -47477, 83126], [87936, -60391, 23891], [-8819, -52760, 82053], [-43208, 117834, -96297], [109940, -41203, 26023], [-52605, -54276, -39656], [39936, -110690, -1580], [75853, -90942, -57667], [-127550, -6269, 26095], [-36369, -28474, 4519], [69077, 7701, -6527], [-36211, 15234, -6577], [-161064, 57520, -65128], [-86935, -60204, 12038], [-98098, 87306, -26240], [-16107, -21696, 104294], [22236, -92596, 28287], [-38953, -60587, 109553], [10119, -156319, 16225], [12519, -145202, 48644], [77668, -109689, 22915], [52660, -56208, -123874], [43066, 77300, -62204], [98716, -39669, 80783], [76277, -144510, 36779], [-124558, 92666, -51811], [-44443, 67640, -121378], [23464, -117968, 55118], [-44473, 104522, -103165], [96186, 57101, 24201], [4160, 69591, -16507], [-90796, -9027, -47154], [-80520, 127928, -77863], [50571, -96589, 67660], [-108877, 46801, -160], [43255, -102349, -107912], [-44771, -13135, 125423], [-55798, 89598, 113771], [-24673, -28814, -148801], [-180413, -18995, -36651], [122868, 17276, 49442], [-33287, 110211, -43248], [31972, 11798, 35042], [37976, 120426, -113143], [-35, 162073, -74484], [-85197, -55775, 39051], [-151828, 6545, 2614], [-53403, -28371, -14712], [-74696, 154831, -133140], [-113442, 104065, 54318], [79154, -184166, 160971], [-33208, 153972, -5951], [-75385, 15496, -2540], [-40724, 133453, -62124], [133503, -97557, -75385], [27901, -135044, 133088], [134004, -29110, -91515], [-130906, -55871, 35387], [130918, -126051, 69431], [-108537, -55230, -36400], [53926, -80116, -38528], [65829, -147327, 88858]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_338_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_338_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_338_4_a_k(:prec:=3)
    chi := MakeCharacter_338_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_338_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_338_4_a_k( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_338_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![-13, 27, 12, 1]>,<5,R![-769, -85, 12, 1]>],Snew);
    return Vf;
end function;