
// Make newform 507.4.a.k in Magma, downloaded from the LMFDB on 23 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_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_507_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 := [-2, 14, -13, -2, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [13, -25, -3, 2], [-7, -1, 1, 0], [-31, 60, 6, -4]];
    Rf_basisdens := [1, 5, 1, 5];
    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, 0, 0, 1], [-3, 0, 0, 0], [0, 3, 0, -2], [0, -11, 0, 3], [0, 21, 0, -1], [0, 0, 0, 0], [-36, 0, 1, 0], [0, -37, 0, -9], [-69, 0, -7, 0], [3, 0, 22, 0], [0, 78, 0, -42], [0, -82, 0, -45], [0, 30, 0, 1], [235, 0, -15, 0], [0, -111, 0, -79], [-567, 0, -18, 0], [0, 360, 0, 8], [80, 0, 87, 0], [0, -83, 0, 21], [0, 219, 0, -17], [0, -225, 0, 0], [2, 0, 126, 0], [0, -177, 0, 241], [0, 42, 0, 242], [0, 56, 0, 402], [159, 0, -112, 0], [-29, 0, -93, 0], [-927, 0, 73, 0], [0, -234, 0, -18], [-426, 0, -157, 0], [-1124, 0, -192, 0], [258, 0, -30, 0], [0, -588, 0, 197], [-100, 0, 0, 0], [0, 153, 0, 28], [0, -669, 0, -87], [-628, 0, 291, 0], [0, 1738, 0, -162], [0, -942, 0, 386], [-390, 0, 322, 0], [-1017, 0, 71, 0], [-830, 0, -183, 0], [1344, 0, 380, 0], [0, -367, 0, 366], [0, 1128, 0, 532], [-1997, 0, -57, 0], [998, 0, 402, 0], [0, 796, 0, 348], [0, -1497, 0, -467], [0, -168, 0, -246], [-1818, 0, -216, 0], [0, -1809, 0, -389], [0, 1663, 0, 462], [-2574, 0, -98, 0], [-4182, 0, -331, 0], [381, 0, -41, 0], [-3942, 0, -82, 0], [0, 4976, 0, -72], [568, 0, -483, 0], [0, -282, 0, -565], [2557, 0, -321, 0], [0, 831, 0, 692], [0, 693, 0, 1413], [-3597, 0, 345, 0], [-2158, 0, -438, 0], [0, 1143, 0, -1640], [0, 4520, 0, 204], [2185, 0, 888, 0], [-6447, 0, -859, 0], [0, -3874, 0, -546], [0, -2196, 0, -377], [0, -3045, 0, 623], [8567, 0, 267, 0], [-6400, 0, -891, 0], [0, 2818, 0, -1746], [0, -2478, 0, 1522], [-6501, 0, 456, 0], [0, -3014, 0, 2178], [0, -4236, 0, 1295], [0, -1057, 0, -816], [-4494, 0, -326, 0], [0, 2388, 0, 825], [0, -7827, 0, 37], [-3833, 0, -1944, 0], [-485, 0, 1071, 0], [-6270, 0, -546, 0], [0, -1218, 0, -3646], [0, 6493, 0, 690], [0, 4245, 0, -2492], [0, -2175, 0, 411], [2799, 0, -765, 0], [0, -1962, 0, -442], [0, -7165, 0, 45], [9291, 0, 1451, 0], [0, -3792, 0, -1248], [1827, 0, 1565, 0], [0, 2427, 0, -2312], [-324, 0, -1593, 0], [971, 0, -771, 0], [0, 3798, 0, -2853], [-6667, 0, 1719, 0], [0, -5697, 0, -2494], [17292, 0, -140, 0], [7404, 0, 428, 0], [-7265, 0, 1329, 0], [0, 7119, 0, -1854], [0, -1056, 0, 3376], [0, 5616, 0, -871], [-1614, 0, -642, 0], [14489, 0, 540, 0], [10988, 0, -84, 0], [0, 5770, 0, -2619], [0, 3942, 0, 2035], [0, -3090, 0, -1482], [0, -5408, 0, 1188], [5064, 0, 151, 0], [0, 3632, 0, 3504], [6066, 0, -730, 0], [-390, 0, -974, 0], [8670, 0, 542, 0], [0, 3854, 0, -2163], [-3491, 0, -180, 0], [-11490, 0, 498, 0], [0, 15648, 0, -856], [0, -2993, 0, -1785], [-16650, 0, 2250, 0], [0, -13268, 0, 543], [-5058, 0, 4258, 0], [9583, 0, 3339, 0], [0, 3516, 0, -2409], [0, 9476, 0, 3060], [0, -18858, 0, -566], [16003, 0, -777, 0], [-24274, 0, -540, 0], [0, -2970, 0, 6362], [0, 9232, 0, 858], [0, -19284, 0, 982], [0, -6350, 0, 3750], [-15018, 0, -2768, 0], [-7542, 0, -431, 0], [0, -4410, 0, 3186], [0, 16476, 0, -1754], [10144, 0, 1008, 0], [0, 6642, 0, -1546], [2476, 0, -1533, 0], [0, 3576, 0, -6148], [0, -11514, 0, 453], [6090, 0, -3801, 0], [21809, 0, -2925, 0], [0, 2307, 0, -5681], [0, 16592, 0, -3363], [-8820, 0, -577, 0], [-1676, 0, -3348, 0], [-2568, 0, 880, 0], [12106, 0, -3510, 0], [714, 0, -5226, 0], [6002, 0, 2010, 0], [0, -7170, 0, -7919], [-17269, 0, 1470, 0], [0, 10476, 0, 4498], [0, -8388, 0, -8812], [21168, 0, -1304, 0], [0, 18627, 0, 2037], [21654, 0, 4994, 0], [0, -8220, 0, 2719], [0, 24948, 0, -2248], [29843, 0, -1149, 0], [-5806, 0, 3561, 0], [0, -15408, 0, -342], [22557, 0, 4080, 0], [0, 2235, 0, -7247], [0, -8816, 0, -5439], [-32046, 0, 2434, 0], [0, -10015, 0, -5988], [-10244, 0, -5340, 0], [-17544, 0, 77, 0], [0, 4289, 0, 11397], [0, 18015, 0, -7648], [21844, 0, 6396, 0], [-14734, 0, -1164, 0], [0, 2787, 0, 2001], [-57090, 0, 438, 0], [-10744, 0, -5343, 0], [0, 18120, 0, 214], [0, -4515, 0, 125], [9051, 0, -1082, 0], [7594, 0, 7800, 0], [0, -14550, 0, 3750], [0, 11513, 0, 3858], [0, -13041, 0, -577], [-10333, 0, -6018, 0], [0, 11139, 0, -12491], [-42973, 0, 129, 0], [0, -22104, 0, -2476], [-29493, 0, 379, 0], [-50442, 0, 2972, 0], [0, 17799, 0, -2238], [-51740, 0, 4395, 0], [0, -15294, 0, 12269], [7884, 0, 648, 0], [0, -8991, 0, 9818], [13220, 0, -6708, 0], [0, -128, 0, -6981], [12935, 0, -3390, 0], [0, -15753, 0, 2681], [43065, 0, -2942, 0], [0, -2511, 0, -10053], [8988, 0, -9112, 0], [0, 8334, 0, 10693], [44560, 0, 720, 0], [24463, 0, 288, 0], [-58998, 0, 906, 0], [-69139, 0, 2529, 0], [0, 18597, 0, 11339], [0, 31518, 0, 2134], [0, 7197, 0, 1020], [13412, 0, 2124, 0], [-54816, 0, 2351, 0], [0, 6444, 0, 928], [0, 7260, 0, 19298], [-6094, 0, -2505, 0], [0, -1356, 0, -6744], [0, -34956, 0, -3089], [0, 29042, 0, -8190], [9345, 0, 4421, 0], [-44066, 0, -747, 0], [17130, 0, 9175, 0], [-12258, 0, -1162, 0], [71488, 0, -4812, 0], [0, -17364, 0, -5848], [20026, 0, -5583, 0], [72044, 0, 3720, 0], [0, -25382, 0, 4530], [20964, 0, 10299, 0], [38522, 0, 30, 0], [0, -33498, 0, 6514], [0, -9608, 0, -16302], [0, 411, 0, 890], [-6810, 0, -7322, 0], [-49419, 0, 7075, 0], [0, -45261, 0, -2807], [23977, 0, -3429, 0], [2591, 0, 8007, 0], [0, 18424, 0, -13131], [0, -43788, 0, 1441], [-40206, 0, 3714, 0], [0, 27388, 0, -10080], [0, 17418, 0, -3862], [0, 2867, 0, -5577], [37233, 0, -2305, 0], [0, -11986, 0, -426], [0, 16992, 0, 11998], [0, 11052, 0, -7468], [-1223, 0, 4656, 0], [-35754, 0, -24, 0], [0, 11697, 0, 8387], [-40078, 0, 6240, 0], [0, -49109, 0, 1491], [-5562, 0, -15534, 0], [0, 9647, 0, -3960], [-5312, 0, 1644, 0], [31512, 0, -212, 0], [0, 14568, 0, -12957], [81404, 0, -3789, 0], [0, 17766, 0, -5617], [-76501, 0, -2631, 0], [0, -24624, 0, 16804], [0, 58242, 0, -4313], [0, 10777, 0, 4785], [118353, 0, 1462, 0], [-15788, 0, -3555, 0], [0, -612, 0, -31896], [0, 17087, 0, 3378], [75556, 0, -1224, 0], [-117802, 0, 2358, 0], [0, 12529, 0, -14913], [0, 19260, 0, 18784], [0, 7098, 0, -18879], [0, -12275, 0, 20466], [-70599, 0, 2473, 0], [-54435, 0, 10735, 0], [0, 14654, 0, 2922], [-27096, 0, 1668, 0], [0, -22874, 0, 20301], [0, -7665, 0, -27021], [66303, 0, 9585, 0], [-99781, 0, 5520, 0], [0, -28599, 0, 7774], [0, 26557, 0, -8241], [66276, 0, 4861, 0], [-53502, 0, -12362, 0], [45, 0, -2755, 0], [0, -26754, 0, -10583], [0, 11766, 0, -982], [-43966, 0, -6072, 0], [-47085, 0, -6810, 0], [17444, 0, -10332, 0], [-111639, 0, 3470, 0], [-7416, 0, -5876, 0], [0, 43889, 0, 22629], [-76589, 0, 8028, 0], [0, 26493, 0, -12094], [-19151, 0, -11739, 0], [82050, 0, 2382, 0], [-49195, 0, 13815, 0], [0, 14635, 0, 5526], [-68295, 0, 2049, 0], [938, 0, -3717, 0], [0, 26484, 0, 3104], [21382, 0, 2511, 0], [40179, 0, -3967, 0], [0, -39492, 0, 11458], [111702, 0, -4947, 0], [-739, 0, -1497, 0], [0, -33537, 0, 7379], [31715, 0, -3444, 0], [0, 40647, 0, 7957], [0, -39723, 0, -12287], [0, 24784, 0, -6090], [35922, 0, -15091, 0], [-31295, 0, -14997, 0], [0, 53880, 0, 6600], [23337, 0, 8360, 0], [0, -7705, 0, -5715], [0, 114, 0, -32146], [5039, 0, 12216, 0], [0, 27801, 0, -11547], [0, 26345, 0, -8247], [69261, 0, 3623, 0], [1197, 0, 16858, 0], [0, -71590, 0, 8841], [-117183, 0, -3468, 0], [88811, 0, 10815, 0], [0, -6591, 0, -1693], [0, -26009, 0, 39555], [0, -16533, 0, -13223], [0, -35918, 0, 22311], [0, 39918, 0, -19810], [0, -33997, 0, 29226], [75112, 0, 11988, 0], [0, -25998, 0, 22218], [-120516, 0, 2045, 0], [0, -62391, 0, 18920], [44533, 0, 14409, 0], [0, -91392, 0, 6418], [132489, 0, -8487, 0], [-69388, 0, 8805, 0], [0, -182, 0, 15618], [0, -30147, 0, -1783], [37386, 0, 20350, 0], [0, 37635, 0, 16707], [0, 95048, 0, -11322], [0, -45435, 0, 9946], [-80045, 0, -17133, 0], [-81620, 0, -16305, 0], [144252, 0, -3123, 0], [0, -62163, 0, 16289], [0, 50646, 0, 17614], [125724, 0, -13785, 0], [0, -60207, 0, 3661], [0, 7220, 0, 8655], [173004, 0, 4559, 0], [-115776, 0, -14480, 0], [0, 24714, 0, 5338], [-94094, 0, -8802, 0], [120590, 0, -3762, 0], [0, -46704, 0, -18406], [0, -8468, 0, 12564], [-141887, 0, -5898, 0], [-45762, 0, -28438, 0], [-96227, 0, -12117, 0], [0, 15507, 0, -9277], [36993, 0, 20712, 0], [-139807, 0, 1509, 0], [-95428, 0, -4731, 0], [0, 38829, 0, -1721], [-24201, 0, 22933, 0], [0, -2152, 0, 39024], [7830, 0, 9791, 0], [73675, 0, 2712, 0], [0, 109872, 0, 7958], [0, -59310, 0, -7903], [0, -13995, 0, 11475], [0, 143058, 0, -5690], [0, -3200, 0, 51252], [0, -6003, 0, -10891], [0, -6055, 0, 44295], [88738, 0, -2928, 0], [0, 10584, 0, 15120], [173472, 0, -3388, 0], [0, -28867, 0, -45726], [0, -30807, 0, -39302], [0, -46107, 0, -5245], [64583, 0, -19791, 0], [0, -27078, 0, -8614], [79838, 0, -11514, 0], [0, 46531, 0, -12879], [12768, 0, 31920, 0], [111350, 0, 18030, 0], [0, -56391, 0, 3806], [0, 112856, 0, -8826], [184914, 0, -6004, 0], [0, -92950, 0, -25050], [0, 6084, 0, -49345], [0, -77409, 0, 9170], [15227, 0, 4131, 0], [0, 9616, 0, 6954], [0, -23142, 0, -18845], [0, 34221, 0, -21531], [0, -84276, 0, 31220], [-31943, 0, 7962, 0], [101955, 0, -17204, 0], [56043, 0, -12421, 0], [200350, 0, -3090, 0], [-38141, 0, 5700, 0], [87525, 0, -23778, 0], [0, -31758, 0, 39142], [-160045, 0, -6189, 0], [0, -38928, 0, 35810], [-45600, 0, -28652, 0], [2301, 0, 25376, 0], [0, -83412, 0, -33795], [0, 126117, 0, -6731], [-63165, 0, -24501, 0], [0, -66248, 0, -19674], [0, -36831, 0, -53774], [48012, 0, 28968, 0], [0, 28830, 0, 27205], [0, -27071, 0, -35127], [-285252, 0, -448, 0]];
    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_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_507_4_a_k(: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_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_507_4_a_k( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_507_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![-17, 0, 1]>,<5,R![1681, 0, -190, 0, 1]>],Snew);
    return Vf;
end function;