
// Make newform 37.4.a.b in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_37_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_37_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_37_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 := [144, 176, 3, -27, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [12, 3, -15, -1, 1], [92, 11, -23, -1, 1], [-140, -71, 23, 5, -1]];
    Rf_basisdens := [1, 1, 8, 8, 8];
    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_37_a();"
function MakeCharacter_37_a()
    N := 37;
    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_37_a_Hecke(Kf)
    return MakeCharacter_37_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], [3, 0, -1, 0, 0], [2, 2, 1, 1, -1], [4, 3, 1, 0, -1], [12, 1, -2, 1, 6], [-3, -5, -4, 8, 7], [18, -6, 6, -16, 2], [-10, -10, 14, -8, -6], [19, 9, 8, 28, 9], [57, 11, -4, -4, -25], [72, -18, 13, 3, 1], [-37, 0, 0, 0, 0], [101, 8, -43, 18, -10], [-72, -22, 0, -12, -50], [50, 37, -5, -6, -13], [-16, -39, 57, -64, 33], [54, -82, -52, -6, 4], [-338, 88, 13, 75, 15], [114, 156, -27, -39, 37], [384, -171, -35, 62, 23], [-34, 23, -100, 5, 44], [75, 87, 26, -20, 15], [530, -53, -41, 92, 91], [-452, -188, -20, -22, -22], [-570, -42, 70, -200, -54], [436, 167, -49, 88, 143], [420, -100, -61, 19, -161], [-49, -61, 84, -132, -229], [-430, 18, 22, -200, -50], [358, -264, 12, 96, -92], [-300, 121, -155, 446, 43], [822, 46, 216, -206, -8], [-332, 476, 41, -123, -71], [145, -27, 56, -332, -107], [-336, 183, 13, 66, -199], [-200, 276, -194, 206, 50], [-48, -217, 319, 0, -185], [162, -128, 112, -356, -56], [-316, -338, -83, -317, 213], [1500, 133, 13, 32, 125], [296, 198, -244, 392, 10], [-1302, -261, -253, 190, 377], [-2132, 146, 233, -365, -279], [-64, 466, -42, -154, -180], [1940, -577, -65, -236, -93], [-716, 158, -350, -354, 152], [-1928, -239, 428, -165, 8], [-742, -139, 279, -426, -209], [3160, 380, 162, 80, -164], [830, -515, 327, 208, 221], [-303, -777, 518, -70, -331], [-2220, 552, 183, -97, 419], [-272, 622, 78, 250, -492], [1412, 422, -350, 374, -192], [1510, -606, 110, 196, 338], [454, 501, -309, 562, 235], [360, 410, 610, 326, -232], [262, -513, -313, 856, 507], [206, -680, -69, -179, -531], [-1148, 528, 296, -1254, -614], [-424, -536, -782, 16, 452], [3032, 346, -132, 184, -94], [2025, -288, 149, -392, 168], [627, 283, -578, 808, 139], [-770, -8, 530, 210, 786], [2308, 914, -486, 682, 556], [2856, -1250, 190, -214, -8], [318, 47, -52, 693, 244], [1708, 598, -612, 212, 118], [3816, 514, 280, 892, 94], [-844, -348, 1302, -848, -836], [2480, -11, -903, 1050, -181], [1790, -1018, 8, -988, -70], [-2674, -2173, -411, 332, 311], [1741, -2188, -719, -224, 236], [-1368, 364, -196, -914, -574], [-2147, -1347, 278, -340, -211], [-594, 1561, -573, -436, -635], [-9982, 32, -70, 526, -114], [-4844, 156, 694, 1104, 32], [-1189, -486, 2129, 208, -214], [1095, 1911, -822, 344, 407], [-7524, 162, -434, 814, 520], [2443, 1704, 347, 626, -42], [4383, -353, -206, 52, -93], [4008, 103, -222, 1131, 110], [-278, -1298, -562, -524, 638], [3630, -1100, 194, 614, 1358], [-6898, 1556, -596, 1004, -128], [-4085, 2441, 128, 484, -315], [2624, -282, 1746, -546, 580], [1277, -153, 200, 1872, -273], [6124, 544, 572, -666, -338], [3849, 1357, 460, -2292, -107], [-2382, -1778, 0, -1274, 1132], [3608, -2070, 179, -963, 1051], [-2068, 177, 1007, 634, -437], [-9228, 507, -381, -1068, -9], [4946, 2000, -86, 1214, 74], [-146, 220, -1667, 2751, -61], [5376, -3132, -966, -284, 344], [2854, -2330, -1665, 3, 601], [8462, 2072, -1408, 1036, 1836], [-76, -92, -96, 598, 654], [7675, -174, 265, 896, 386], [5718, -1062, -1196, 366, -732], [10570, -1854, -3178, 1116, -222], [3918, -149, 276, -555, 1736], [-6680, 2357, -2359, -1790, 115], [3092, -660, -971, 3093, 153], [7469, 341, 1314, -4, -207], [-3994, 4997, -805, 1064, -515], [-4787, -1214, -1805, 1966, -828], [-11960, 2321, 2014, -939, 458], [-5965, -2217, 1958, -3476, -2213], [-7535, -1876, -2591, 2714, 3010], [-5710, 3272, -106, -1026, 810], [-10091, 3813, -1026, -400, -1927], [-14763, 1561, 1046, -760, 797], [-2735, 778, 2499, -2432, -886], [1243, -505, 22, 1056, -217], [-11231, -464, 1809, -1834, -942], [16332, -195, -537, 1182, -733], [4580, 2146, -154, 1034, -1100], [-3984, 1156, -292, -2204, -2016], [5102, -3688, -1081, -3863, -723], [-2318, 568, -263, 2523, 1035], [14030, -1651, 1497, -1976, 177], [-1012, -234, -3749, 2801, 1047], [852, 1291, -581, 1872, 3395], [-6544, 817, 2522, 1505, 142], [9062, 407, -771, 5134, 2649], [3460, -1279, 601, 550, 1915], [17074, -2834, 269, -823, 995], [-6361, -3320, 2751, -3698, -1822], [-9890, -2828, 2544, 968, -2408], [-4806, 2333, -607, -1750, -3889], [-6018, -6727, 29, -532, 913], [10123, 4563, -3902, 2968, -509], [1382, 2248, -2082, 270, 4326], [2764, 3045, -2590, 917, 1486], [-14004, 2325, 2381, -2500, -2255], [3144, 4704, 2656, -1332, -2612], [5150, -2860, -430, 3158, 4554], [-5057, 1455, 1722, -3848, -1233], [5140, -2372, 2966, -2526, -106], [6362, 1122, -267, 3485, 3767], [-2402, 766, 2462, -584, 1430], [-918, -1178, -1358, -2712, -2842], [-7148, -212, 1136, -180, -1184], [12814, -2332, -3716, -1048, -2372], [-12988, -1968, 3399, 447, -3281], [9240, 3440, -3704, 1946, 38], [16956, 2361, 561, -586, -1109], [-25978, -2210, 4890, -2508, 454], [-8824, -3202, -2058, 2586, 3312], [-3880, 9074, 3350, -3310, -2968], [6856, 2124, 2287, -69, -5633], [8477, -594, 643, -1898, 4040], [-2200, 786, -1680, 84, 3918], [-10332, 4548, -2064, -1162, 3206], [-5574, -4103, 1434, 685, 1446], [13432, -508, 6783, 4791, -1113], [-9110, 6688, 2819, 1635, -141], [-13758, -1454, 5182, -3840, 26], [8874, 3211, -2749, 512, 2087], [14731, -4743, -2184, 2784, 2641], [-13702, 1412, -864, 2868, -5308]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_37_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_37_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_37_4_a_b(:prec:=5)
    chi := MakeCharacter_37_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_37_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_37_4_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_37_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![-296, 102, 74, -21, -4, 1]>],Snew);
    return Vf;
end function;