
// Make newform 507.4.b.f in Magma, downloaded from the LMFDB on 24 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_f();".
// 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_f();".
// 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 := [49, 0, 0, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 0, 2, 0], [0, 7, 1, 1], [0, 14, 0, -2]];
    Rf_basisdens := [1, 7, 7, 7];
    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, 0, 1, 0], [-3, 0, 0, 0], [0, 7, -2, 0], [0, -1, 2, 0], [0, 5, 12, 0], [0, 0, 0, 0], [-82, 0, 0, -2], [0, 11, 2, 0], [-4, 0, 0, -24], [202, 0, 0, -12], [0, 23, -26, 0], [0, 39, -28, 0], [0, 3, 94, 0], [308, 0, 0, -26], [0, 97, -32, 0], [-82, 0, 0, -60], [0, -15, -40, 0], [314, 0, 0, 68], [0, -33, -170, 0], [0, 149, -84, 0], [0, 263, -76, 0], [-216, 0, 0, -44], [0, -379, 64, 0], [0, -335, 190, 0], [0, -23, -220, 0], [182, 0, 0, -14], [280, 0, 0, -218], [984, 0, 0, 20], [0, -155, 280, 0], [702, 0, 0, 116], [-24, 0, 0, 170], [-736, 0, 0, -72], [0, 13, -190, 0], [460, 0, 0, -20], [0, -685, 290, 0], [0, 267, -114, 0], [-322, 0, 0, -264], [0, 1107, -238, 0], [0, -211, 112, 0], [2862, 0, 0, -130], [-1572, 0, 0, -248], [-1286, 0, 0, 144], [1736, 0, 0, -104], [0, -673, 392, 0], [0, -2177, 102, 0], [-2680, 0, 0, 50], [-3852, 0, 0, 24], [0, 2131, 174, 0], [0, 157, -620, 0], [0, 659, -632, 0], [-3118, 0, 0, 330], [0, 533, 876, 0], [0, -937, -892, 0], [-5256, 0, 0, -156], [-910, 0, 0, 286], [2628, 0, 0, -420], [-3606, 0, 0, -204], [0, -1117, 1378, 0], [1274, 0, 0, 72], [0, 1349, 290, 0], [-4236, 0, 0, -282], [0, 2057, 902, 0], [0, 335, -986, 0], [-2484, 0, 0, -448], [-1290, 0, 0, 408], [0, -2157, -378, 0], [0, 3143, 254, 0], [-5182, 0, 0, -572], [5324, 0, 0, 820], [0, -733, 1084, 0], [0, 2875, -498, 0], [0, 285, 1920, 0], [264, 0, 0, -1200], [178, 0, 0, 600], [0, 1235, 1770, 0], [0, 3377, 696, 0], [-7990, 0, 0, 210], [0, 4559, 612, 0], [0, 2093, -1642, 0], [0, 801, -348, 0], [-1312, 0, 0, 272], [0, -3307, -1736, 0], [0, -4247, 2024, 0], [-5990, 0, 0, -388], [4032, 0, 0, 918], [7264, 0, 0, -1476], [0, -4411, 1838, 0], [0, -3075, 1036, 0], [0, 531, 1630, 0], [0, 3083, -486, 0], [640, 0, 0, 1116], [0, 2225, -1820, 0], [0, -5329, -1642, 0], [-5968, 0, 0, -284], [0, -6073, 254, 0], [-340, 0, 0, -1192], [0, -1845, -1330, 0], [11398, 0, 0, 4], [-5020, 0, 0, 28], [0, 1639, -1200, 0], [-9036, 0, 0, 620], [0, 1769, 2942, 0], [-5780, 0, 0, 1692], [-590, 0, 0, -1890], [-9652, 0, 0, 690], [0, 4909, -4296, 0], [0, -591, -96, 0], [0, -8847, 774, 0], [-2388, 0, 0, 368], [1126, 0, 0, -1504], [-10696, 0, 0, 1740], [0, -8171, 3580, 0], [0, 6935, -3554, 0], [0, -10173, 1094, 0], [0, -5849, 1294, 0], [6974, 0, 0, 2294], [0, -2065, 502, 0], [5904, 0, 0, -988], [-886, 0, 0, 1544], [-11748, 0, 0, -292], [0, -333, 636, 0], [12246, 0, 0, 2696], [-1850, 0, 0, 104], [0, -3119, 1380, 0], [0, 7999, -2914, 0], [9466, 0, 0, 762], [0, 13067, -3616, 0], [-2252, 0, 0, -828], [14536, 0, 0, -790], [0, 1413, 5968, 0], [0, -1781, 4478, 0], [0, -4435, -2628, 0], [-17056, 0, 0, 714], [3594, 0, 0, -2700], [0, 1041, 5754, 0], [0, -75, -6000, 0], [0, 13095, -2678, 0], [0, -9861, -1298, 0], [8850, 0, 0, -4114], [-9114, 0, 0, 1964], [0, 10991, -1910, 0], [0, -15513, 4402, 0], [-14912, 0, 0, 1554], [0, 17465, -3636, 0], [-15522, 0, 0, -3028], [0, -6651, -6452, 0], [0, 6611, 1420, 0], [-10510, 0, 0, -2074], [-7692, 0, 0, -5068], [0, 1005, -848, 0], [0, 4253, -9548, 0], [-8566, 0, 0, -3586], [13260, 0, 0, -178], [-1240, 0, 0, -4236], [3932, 0, 0, -726], [26104, 0, 0, -852], [10144, 0, 0, 1316], [0, -2133, 494, 0], [-21418, 0, 0, 1492], [0, 4815, 298, 0], [0, -10135, -2496, 0], [11434, 0, 0, -262], [0, 13471, -6802, 0], [12268, 0, 0, 1604], [0, -8893, -6710, 0], [0, 9641, -14060, 0], [-32864, 0, 0, 3196], [1854, 0, 0, -2240]];
    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_f();".
// 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_f(: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_f();".
// 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_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_507_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![169, 0, 30, 0, 1]>,<5,R![7744, 0, 400, 0, 1]>],Snew);
    return Vf;
end function;