
// Make newform 507.4.a.l in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_507_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_507_4_a_l();".
// 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_507_4_a_l();".
// 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 := [48, 0, -23, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [0, -19, 0, 1], [-12, 0, 1, 0]];
    Rf_basisdens := [1, 1, 2, 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_507_a();"
function MakeCharacter_507_a()
    N := 507;
    order := 1;
    char_gens := [170, 340];
    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_507_a_Hecke(Kf)
    return MakeCharacter_507_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], [3, 0, 0, 0], [0, 2, 1, 0], [0, 2, 2, 0], [0, 8, -3, 0], [0, 0, 0, 0], [-30, 0, 0, -12], [0, -10, 8, 0], [72, 0, 0, 0], [-102, 0, 0, -12], [0, -38, -20, 0], [0, 68, -4, 0], [0, 66, -13, 0], [328, 0, 0, 12], [0, -36, 1, 0], [222, 0, 0, -48], [0, 52, -1, 0], [58, 0, 0, -28], [0, -54, 54, 0], [0, 168, 41, 0], [0, -156, 42, 0], [1072, 0, 0, -16], [0, -188, -63, 0], [0, -138, 25, 0], [0, -68, -14, 0], [1074, 0, 0, 48], [316, 0, 0, 36], [-564, 0, 0, 120], [0, -152, 82, 0], [534, 0, 0, -60], [632, 0, 0, -56], [-516, 0, 0, 96], [0, 58, 47, 0], [56, 0, 0, 84], [0, 222, -229, 0], [0, -522, 0, 0], [-646, 0, 0, -60], [0, -350, -188, 0], [0, 92, -267, 0], [306, 0, 0, 360], [1812, 0, 0, -96], [-194, 0, 0, -420], [-696, 0, 0, 24], [0, 984, -60, 0], [0, -406, -227, 0], [1520, 0, 0, 184], [2212, 0, 0, -376], [0, 1306, -116, 0], [0, -504, 359, 0], [0, -248, 346, 0], [-1794, 0, 0, 312], [0, -496, 73, 0], [0, 820, 46, 0], [516, 0, 0, -168], [1062, 0, 0, -216], [-2400, 0, 0, -24], [-4062, 0, 0, -84], [0, -438, -438, 0], [1042, 0, 0, -192], [0, 842, 447, 0], [-172, 0, 0, 40], [0, -1138, -681, 0], [0, 2142, -450, 0], [-8640, 0, 0, -72], [1522, 0, 0, 680], [0, 1050, -559, 0], [0, 2258, 134, 0], [1214, 0, 0, -176], [900, 0, 0, -432], [0, 1036, 460, 0], [0, -750, 167, 0], [0, -28, -351, 0], [1628, 0, 0, 348], [1486, 0, 0, 32], [0, 814, -464, 0], [0, -1884, -73, 0], [-4410, 0, 0, 324], [0, 740, 56, 0], [0, -754, -15, 0], [0, 3196, 46, 0], [-2748, 0, 0, 744], [0, 136, 46, 0], [0, 724, -105, 0], [8234, 0, 0, -308], [7108, 0, 0, 924], [6732, 0, 0, -504], [0, -586, -1179, 0], [0, 1460, 362, 0], [0, -446, -555, 0], [0, 1426, 166, 0], [-4836, 0, 0, 312], [0, 40, -1021, 0], [0, -1622, -200, 0], [8292, 0, 0, 1128], [0, 1746, 810, 0], [2184, 0, 0, 672], [0, 4778, -407, 0], [11934, 0, 0, -252], [-17992, 0, 0, -108], [0, -2320, -214, 0], [728, 0, 0, 1932], [0, -2778, 199, 0], [-7620, 0, 0, 552], [8430, 0, 0, 888], [-6940, 0, 0, -1256], [0, 936, 1116, 0], [0, -4228, -383, 0], [0, 2054, 549, 0], [3552, 0, 0, -264], [7858, 0, 0, -184], [7292, 0, 0, -468], [0, 2484, 936, 0], [0, -5646, 1499, 0], [0, -90, 2286, 0], [0, -2386, -460, 0], [-13494, 0, 0, 492], [0, 810, 198, 0], [2688, 0, 0, -1416], [22218, 0, 0, 564], [-11076, 0, 0, 1920], [0, 12, -816, 0], [-7022, 0, 0, 1284], [8934, 0, 0, 1608], [0, -3704, -643, 0], [0, -6286, -454, 0], [20958, 0, 0, -1308], [0, -6272, 1342, 0], [-20256, 0, 0, -384], [2264, 0, 0, 1552], [0, -2224, -1882, 0], [0, 7294, 1462, 0], [0, 504, -1423, 0], [11572, 0, 0, 3348], [2794, 0, 0, 2444], [0, 5634, 1027, 0], [0, 2944, -1520, 0], [0, 2422, -2025, 0], [0, -6882, -2706, 0], [-16482, 0, 0, -1596], [870, 0, 0, -2964], [0, -5290, 1748, 0], [0, -8586, 2063, 0], [-9380, 0, 0, 1716], [0, -3840, 1081, 0], [-14626, 0, 0, -1580], [0, -7608, 1951, 0], [0, -804, 3048, 0], [966, 0, 0, -1680], [-34244, 0, 0, -88], [0, -3844, -1829, 0], [0, -9476, -8, 0], [-18954, 0, 0, -2520], [14372, 0, 0, -704], [-27528, 0, 0, -1536], [-15580, 0, 0, -1976], [3984, 0, 0, 816], [29992, 0, 0, 2384], [0, 8426, 715, 0], [22366, 0, 0, -2668], [0, -6730, 1291, 0], [0, -2012, 2219, 0], [-5658, 0, 0, -1680], [0, -942, -492, 0], [1212, 0, 0, -5736], [0, -5594, -5019, 0], [0, -4544, -2341, 0], [-18964, 0, 0, 2844], [11006, 0, 0, -1440], [0, 4188, -3894, 0], [23826, 0, 0, 480], [0, 7064, -677, 0], [0, 8052, -1560, 0], [14760, 0, 0, 2448], [0, 7948, -3122, 0], [-24836, 0, 0, -132], [-12294, 0, 0, -1008], [0, -7210, -3394, 0], [0, 950, 1779, 0], [2428, 0, 0, 1860], [11782, 0, 0, -592], [0, 5866, 898, 0], [-22212, 0, 0, 3672], [22810, 0, 0, 1484], [0, 11142, 2177, 0], [0, 13924, -1411, 0], [-11970, 0, 0, 1260], [18358, 0, 0, -372], [0, -11602, -1324, 0], [0, -10948, -850, 0], [0, -5044, 3873, 0], [12782, 0, 0, -5892], [0, 2628, -1027, 0], [12656, 0, 0, 84], [0, -7234, 3603, 0], [-28932, 0, 0, 1560], [18126, 0, 0, 2664], [0, 11080, -620, 0], [32114, 0, 0, -200], [0, 5494, -427, 0], [-47664, 0, 0, 1800], [0, 7482, 445, 0], [17228, 0, 0, 3468], [0, -8816, 2290, 0], [55358, 0, 0, -1620], [0, 292, -1345, 0], [-10386, 0, 0, -216], [0, 2018, 2648, 0], [24036, 0, 0, 5928], [0, -10266, -5839, 0], [-44836, 0, 0, 2464], [11594, 0, 0, 6796], [-18714, 0, 0, 1752], [23512, 0, 0, 224], [0, -2648, -3115, 0], [0, -11652, -4273, 0], [0, 5040, 1908, 0], [-73448, 0, 0, -520], [-20982, 0, 0, -4008], [0, -2648, 1233, 0], [0, 11574, -2825, 0], [75134, 0, 0, 1632], [0, -1974, 3750, 0], [0, 5190, 2141, 0], [0, -13510, -7372, 0], [8580, 0, 0, -4704], [42226, 0, 0, -4896], [68778, 0, 0, 2304], [-59352, 0, 0, 3144], [15608, 0, 0, 5920], [0, -1172, -4069, 0], [1510, 0, 0, 2412], [-12544, 0, 0, -60], [0, 10022, 4118, 0], [30066, 0, 0, 6252], [-66928, 0, 0, 4236], [0, -19472, -3639, 0], [0, 11276, -4654, 0], [0, 16190, 8631, 0], [-64068, 0, 0, 768], [1968, 0, 0, 2472], [0, -5588, 4077, 0], [7700, 0, 0, 7240], [-8432, 0, 0, 1640], [0, 2580, -8436, 0], [0, -3074, 7593, 0], [-65760, 0, 0, 5016], [0, -4774, 7034, 0], [0, -10996, -6369, 0], [0, -98, 4816, 0], [40248, 0, 0, 3096], [0, 9984, -5982, 0], [0, 21698, -1313, 0], [0, -3040, 1683, 0], [-3034, 0, 0, -8480], [-28254, 0, 0, -6420], [0, 3864, 49, 0], [41302, 0, 0, 2696], [0, 8222, -9112, 0], [39534, 0, 0, 204], [0, 320, 7484, 0], [-33136, 0, 0, -8480], [-93948, 0, 0, -4488], [0, -18552, 510, 0], [-22858, 0, 0, -7632], [0, 750, 2281, 0], [-5300, 0, 0, 7520], [0, 14522, 4633, 0], [0, -19622, 1167, 0], [0, -7514, 5446, 0], [82890, 0, 0, -4248], [24038, 0, 0, -5204], [0, 13258, -3554, 0], [0, 3268, 1234, 0], [16756, 0, 0, 7620], [-24398, 0, 0, 11840], [0, -3834, 1170, 0], [0, 4064, -7817, 0], [0, -3196, 656, 0], [0, 13048, -5168, 0], [96948, 0, 0, 1296], [12384, 0, 0, 4176], [0, -13486, 4586, 0], [18000, 0, 0, -4392], [0, -35796, 2796, 0], [0, -10914, -222, 0], [-20376, 0, 0, -5544], [113950, 0, 0, -1156], [0, -10954, -209, 0], [0, -8178, 3540, 0], [-16146, 0, 0, -5904], [73590, 0, 0, -1344], [8316, 0, 0, -5760], [0, -24834, -8659, 0], [0, -30276, -2333, 0], [67418, 0, 0, -2604], [18870, 0, 0, 2364], [-31588, 0, 0, 3276], [-30378, 0, 0, 9636], [31260, 0, 0, 1992], [0, 1454, 2894, 0], [71590, 0, 0, -960], [0, -18198, -10043, 0], [145708, 0, 0, -1236], [73044, 0, 0, -5544], [-23632, 0, 0, -9060], [0, 6596, 5102, 0], [97620, 0, 0, 4200], [-48106, 0, 0, 13344], [0, 4296, 4375, 0], [29618, 0, 0, 10384], [66528, 0, 0, 12240], [0, -2654, 12729, 0], [-19938, 0, 0, 1500], [70300, 0, 0, 6512], [0, -18980, -10843, 0], [-73334, 0, 0, -5200], [0, -22288, 1483, 0], [0, 17204, 4783, 0], [0, -18428, -2930, 0], [74514, 0, 0, -8700], [2512, 0, 0, -9684], [0, -46216, 7712, 0], [70254, 0, 0, -4536], [0, 30590, 7208, 0], [0, -41090, 7853, 0], [-101666, 0, 0, 4700], [0, -3582, -14652, 0], [0, 35730, 3438, 0], [-77712, 0, 0, -11472], [105978, 0, 0, -5316], [0, -27168, -4614, 0], [-87366, 0, 0, -8652], [-31880, 0, 0, -6904], [0, 22800, 4429, 0], [0, -3090, 24, 0], [0, -7332, -535, 0], [0, -11292, -1248, 0], [0, -2886, -13613, 0], [0, 10964, -5254, 0], [-131732, 0, 0, -3732], [0, 30668, -3244, 0], [25614, 0, 0, 10692], [0, 2246, -7509, 0], [13588, 0, 0, -6276], [0, 3290, 16801, 0], [93588, 0, 0, -4944], [-3578, 0, 0, -13240], [0, -20998, -12700, 0], [0, 9452, 7065, 0], [-58290, 0, 0, -8772], [0, -18262, -19738, 0], [0, 6116, 410, 0], [0, -7426, -7481, 0], [-81184, 0, 0, -6272], [-66034, 0, 0, -6548], [-152346, 0, 0, -516], [0, 16804, 7733, 0], [0, -17768, 8835, 0], [33102, 0, 0, -13536], [0, -580, 103, 0], [0, 43660, -5012, 0], [15066, 0, 0, 12564], [-12648, 0, 0, 1032], [0, -48696, 11747, 0], [84184, 0, 0, 1188], [-50650, 0, 0, -14244], [0, 29866, 11765, 0], [0, 2102, -6520, 0], [-75206, 0, 0, -16428], [-10812, 0, 0, -4440], [-17048, 0, 0, 7116], [0, 32260, 2477, 0], [-8754, 0, 0, 17760], [47824, 0, 0, -10600], [58118, 0, 0, -11424], [0, 39292, 17349, 0], [45288, 0, 0, 21672], [0, -11992, 15692, 0], [62658, 0, 0, 13680], [-150518, 0, 0, -1884], [0, -50002, -549, 0], [0, -48374, 7895, 0], [0, 30658, 17338, 0], [0, -25290, 3221, 0], [0, -37598, 13828, 0], [0, -22184, 12599, 0], [0, -20514, -14862, 0], [105446, 0, 0, 196], [0, 10822, 2308, 0], [-22344, 0, 0, -26952], [0, -39844, -11278, 0], [0, -22162, -4469, 0], [0, 22752, -3647, 0], [17620, 0, 0, -232], [0, -15668, -14299, 0], [-136634, 0, 0, -3316], [0, 31358, 11162, 0], [-130398, 0, 0, 8508], [964, 0, 0, -23296], [0, -43438, 9357, 0], [0, -33940, 16856, 0], [-101454, 0, 0, -18060], [0, 13874, -1642, 0], [0, 50006, -12231, 0], [0, 39882, -14759, 0], [880, 0, 0, -6904], [0, 41840, -16210, 0], [0, -48130, -4247, 0], [0, -13386, -16878, 0], [0, -33288, -8087, 0], [-72290, 0, 0, 5136], [-84042, 0, 0, 17352], [-47916, 0, 0, -20448], [29872, 0, 0, -10212], [86614, 0, 0, -16176], [-204690, 0, 0, -3144], [0, -44368, 577, 0], [67888, 0, 0, 15080], [0, 14782, -4999, 0], [181056, 0, 0, 5880], [-69726, 0, 0, -8472], [0, -56068, 5492, 0], [0, 55320, -5467, 0], [-28236, 0, 0, -3216], [0, 24040, -8720, 0], [0, -3026, -2241, 0], [-8340, 0, 0, -7512], [0, 43094, 289, 0], [0, 35418, 1308, 0], [-40296, 0, 0, -9120]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_507_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_507_4_a_l();".
// 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_507_4_a_l(:prec:=4)
    chi := MakeCharacter_507_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_507_4_a_l();".
// 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_507_4_a_l( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_507_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![48, 0, -23, 0, 1]>,<5,R![15552, 0, -260, 0, 1]>],Snew);
    return Vf;
end function;