
// Make newform 507.4.b.e in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_507_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_507_b_Hecke();"
// 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_b_e();".
// 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_b_e();".
// 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 := [289, 0, -17, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 0, 0, 1], [-17, 0, 2, 0], [0, 34, 0, -1]];
    Rf_basisdens := [1, 17, 17, 17];
    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_b();"
function MakeCharacter_507_b()
    N := 507;
    order := 2;
    char_gens := [170, 340];
    v := [2, 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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_507_b_Hecke();"
function MakeCharacter_507_b_Hecke(Kf)
    N := 507;
    order := 2;
    char_gens := [170, 340];
    char_values := [[1, 0, 0, 0], [-1, 0, 0, 0]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
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, 3, 0], [0, -3, 11, 0], [0, 1, -21, 0], [0, 0, 0, 0], [36, 0, 0, -1], [0, -9, -37, 0], [69, 0, 0, 7], [3, 0, 0, 22], [0, -42, 78, 0], [0, 45, 82, 0], [0, 1, 30, 0], [-235, 0, 0, 15], [0, 79, 111, 0], [-567, 0, 0, -18], [0, -8, -360, 0], [80, 0, 0, 87], [0, 21, -83, 0], [0, -17, 219, 0], [0, 0, 225, 0], [2, 0, 0, 126], [0, 241, -177, 0], [0, -242, -42, 0], [0, 402, 56, 0], [-159, 0, 0, 112], [29, 0, 0, 93], [-927, 0, 0, 73], [0, -18, -234, 0], [-426, 0, 0, -157], [1124, 0, 0, 192], [258, 0, 0, -30], [0, -197, 588, 0], [-100, 0, 0, 0], [0, 28, 153, 0], [0, 87, 669, 0], [-628, 0, 0, 291], [0, 162, -1738, 0], [0, -386, 942, 0], [390, 0, 0, -322], [1017, 0, 0, -71], [830, 0, 0, 183], [1344, 0, 0, 380], [0, -366, 367, 0], [0, 532, 1128, 0], [1997, 0, 0, 57], [998, 0, 0, 402], [0, 348, 796, 0], [0, -467, -1497, 0], [0, 246, 168, 0], [1818, 0, 0, 216], [0, -389, -1809, 0], [0, -462, -1663, 0], [2574, 0, 0, 98], [4182, 0, 0, 331], [381, 0, 0, -41], [-3942, 0, 0, -82], [0, 72, -4976, 0], [-568, 0, 0, 483], [0, 565, 282, 0], [-2557, 0, 0, 321], [0, -692, -831, 0], [0, -1413, -693, 0], [3597, 0, 0, -345], [-2158, 0, 0, -438], [0, -1640, 1143, 0], [0, 204, 4520, 0], [-2185, 0, 0, -888], [-6447, 0, 0, -859], [0, 546, 3874, 0], [0, -377, -2196, 0], [0, -623, 3045, 0], [8567, 0, 0, 267], [-6400, 0, 0, -891], [0, -1746, 2818, 0], [0, 1522, -2478, 0], [6501, 0, 0, -456], [0, -2178, 3014, 0], [0, -1295, 4236, 0], [0, -816, -1057, 0], [-4494, 0, 0, -326], [0, 825, 2388, 0], [0, 37, -7827, 0], [3833, 0, 0, 1944], [485, 0, 0, -1071], [-6270, 0, 0, -546], [0, 3646, 1218, 0], [0, 690, 6493, 0], [0, -2492, 4245, 0], [0, -411, 2175, 0], [-2799, 0, 0, 765], [0, 442, 1962, 0], [0, 45, -7165, 0], [-9291, 0, 0, -1451], [0, -1248, -3792, 0], [1827, 0, 0, 1565], [0, -2312, 2427, 0], [-324, 0, 0, -1593], [971, 0, 0, -771], [0, 2853, -3798, 0], [-6667, 0, 0, 1719], [0, 2494, 5697, 0], [-17292, 0, 0, 140], [-7404, 0, 0, -428], [7265, 0, 0, -1329], [0, -1854, 7119, 0], [0, 3376, -1056, 0], [0, 871, -5616, 0], [-1614, 0, 0, -642], [14489, 0, 0, 540], [10988, 0, 0, -84], [0, -2619, 5770, 0], [0, 2035, 3942, 0], [0, 1482, 3090, 0], [0, -1188, 5408, 0], [-5064, 0, 0, -151], [0, 3504, 3632, 0], [-6066, 0, 0, 730], [-390, 0, 0, -974], [8670, 0, 0, 542], [0, 2163, -3854, 0], [3491, 0, 0, 180], [-11490, 0, 0, 498], [0, 856, -15648, 0], [0, -1785, -2993, 0], [16650, 0, 0, -2250], [0, -543, 13268, 0], [5058, 0, 0, -4258], [-9583, 0, 0, -3339], [0, -2409, 3516, 0], [0, -3060, -9476, 0], [0, -566, -18858, 0], [-16003, 0, 0, 777], [-24274, 0, 0, -540], [0, -6362, 2970, 0], [0, 858, 9232, 0], [0, 982, -19284, 0], [0, -3750, 6350, 0], [15018, 0, 0, 2768], [-7542, 0, 0, -431], [0, 3186, -4410, 0], [0, -1754, 16476, 0], [-10144, 0, 0, -1008], [0, 1546, -6642, 0], [-2476, 0, 0, 1533], [0, 6148, -3576, 0], [0, -453, 11514, 0], [-6090, 0, 0, 3801], [21809, 0, 0, -2925], [0, -5681, 2307, 0], [0, -3363, 16592, 0], [8820, 0, 0, 577], [1676, 0, 0, 3348], [-2568, 0, 0, 880], [-12106, 0, 0, 3510], [714, 0, 0, -5226], [6002, 0, 0, 2010], [0, -7919, -7170, 0], [-17269, 0, 0, 1470], [0, 4498, 10476, 0], [0, 8812, 8388, 0], [-21168, 0, 0, 1304], [0, 2037, 18627, 0], [21654, 0, 0, 4994], [0, 2719, -8220, 0], [0, 2248, -24948, 0], [29843, 0, 0, -1149], [-5806, 0, 0, 3561]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_507_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_507_4_b_e();".
// 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_b_e(:prec:=4)
    chi := MakeCharacter_507_b();
    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_507_4_b_e();".
// 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_b_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_507_b();
    // 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;