
// Make newform 507.4.b.g in Magma, downloaded from the LMFDB on 25 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_g();".
// 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_g();".
// 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 := [18, 54, 81, 24, 2, -2, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0], [-1523, 24, 48, 44, -33, 4], [1638, 4434, 1150, 184, -138, 58], [-5007, 408, 816, 748, -334, 68], [3870, 10446, 3867, 310, -346, 152], [11466, 31038, 9412, 1288, -966, 406]];
    Rf_basisdens := [1, 227, 681, 681, 681, 681];
    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, 0, 0], [-1, 0, 0, 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, 0, 0, 1, 0], [3, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, -1], [0, 0, -7, 0, 6, 0], [0, 0, 3, 0, 2, -3], [0, 0, 0, 0, 0, 0], [50, 0, 0, 4, 0, 0], [0, 0, 11, 0, 6, 8], [8, -8, 0, -16, 0, 0], [0, -8, 0, 10, 0, 0], [0, 0, 33, 0, 54, -2], [0, 0, -51, 0, -48, 14], [0, 0, 73, 0, 4, 17], [98, 4, 0, 30, 0, 0], [0, 0, -21, 0, 54, 21], [-240, 12, 0, -54, 0, 0], [0, 0, 271, 0, 82, -1], [90, 28, 0, 12, 0, 0], [0, 0, 39, 0, -42, -38], [0, 0, -205, 0, 134, 7], [0, 0, 119, 0, -240, -6], [8, 24, 0, 24, 0, 0], [0, 0, -121, 0, -30, -5], [0, 0, -273, 0, 116, -15], [0, 0, 525, 0, 48, -2], [-244, -128, 0, 170, 0, 0], [710, 76, 0, -42, 0, 0], [-788, -40, 0, -152, 0, 0], [0, 0, 171, 0, -288, -32], [446, -68, 0, 116, 0, 0], [394, 72, 0, -54, 0, 0], [268, -152, 0, -96, 0, 0], [0, 0, -593, 0, 268, -73], [-32, 116, 0, 276, 0, 0], [0, 0, 317, 0, 176, -47], [0, 0, -579, 0, -234, -10], [238, -172, 0, -240, 0, 0], [0, 0, -277, 0, 222, 144], [0, 0, 63, 0, 230, 117], [1816, 248, 0, -2, 0, 0], [-374, 300, 0, -226, 0, 0], [1822, 348, 0, 228, 0, 0], [1210, 12, 0, -46, 0, 0], [0, 0, 907, 0, -72, -140], [0, 0, -1679, 0, -176, -13], [-530, 480, 0, 54, 0, 0], [384, -272, 0, 132, 0, 0], [0, 0, 561, 0, -282, -158], [0, 0, -641, 0, 506, 245], [0, 0, 1517, 0, -216, -96], [914, 380, 0, 276, 0, 0], [0, 0, -309, 0, 1086, -69], [0, 0, -2493, 0, 96, 98], [2542, -580, 0, 10, 0, 0], [594, -124, 0, -628, 0, 0], [1282, 188, 0, -14, 0, 0], [3120, 512, 0, 218, 0, 0], [0, 0, -539, 0, 918, 136], [2038, -552, 0, -12, 0, 0], [0, 0, -1105, 0, -612, 7], [-838, 40, 0, 318, 0, 0], [0, 0, -449, 0, 40, -145], [0, 0, -1861, 0, 102, 210], [-1152, 0, 0, -360, 0, 0], [3442, -64, 0, -24, 0, 0], [0, 0, 817, 0, 120, 83], [0, 0, 1159, 0, -2034, 210], [2846, -272, 0, -624, 0, 0], [-124, 232, 0, -256, 0, 0], [0, 0, 213, 0, 2184, -358], [0, 0, 1377, 0, -788, -171], [0, 0, -2061, 0, -1074, 369], [-4524, 148, 0, -276, 0, 0], [-3342, 1456, 0, -156, 0, 0], [0, 0, 635, 0, 942, -364], [0, 0, -357, 0, -970, 105], [2504, 44, 0, 78, 0, 0], [0, 0, 581, 0, -1224, -294], [0, 0, 1239, 0, -1396, 237], [0, 0, -243, 0, -1704, 214], [-4540, -624, 0, 1288, 0, 0], [0, 0, -2021, 0, 72, -624], [0, 0, -5789, 0, 214, -223], [-7662, -148, 0, 60, 0, 0], [-2054, -1620, 0, 378, 0, 0], [-8692, 296, 0, 312, 0, 0], [0, 0, -3241, 0, 76, -359], [0, 0, 541, 0, -1080, 990], [0, 0, 2905, 0, -952, 119], [0, 0, -3687, 0, 126, 380], [8226, -444, 0, 438, 0, 0], [0, 0, -4009, 0, 1526, -509], [0, 0, -1555, 0, -282, -550], [-1150, 868, 0, -1474, 0, 0], [0, 0, -637, 0, -1362, 98], [930, 428, 0, -1726, 0, 0], [0, 0, 1497, 0, -3560, -489], [6906, -2316, 0, 696, 0, 0], [6728, 516, 0, 1116, 0, 0], [0, 0, -2867, 0, 528, -1052], [-7788, 628, 0, 864, 0, 0], [0, 0, 3671, 0, -2944, -233], [-3002, -844, 0, 106, 0, 0], [-13274, -1380, 0, 824, 0, 0], [10, 912, 0, 2334, 0, 0], [0, 0, 2521, 0, 2832, 264], [0, 0, -1185, 0, 2314, -993], [0, 0, 3631, 0, 2316, -175], [10046, -148, 0, -1698, 0, 0], [9930, 544, 0, 2400, 0, 0], [-892, 348, 0, 252, 0, 0], [0, 0, 8163, 0, -2976, -170], [0, 0, 8377, 0, 1940, -475], [0, 0, -209, 0, -6954, 298], [0, 0, 2141, 0, 918, 1062], [-6214, 1704, 0, -1460, 0, 0], [0, 0, -6125, 0, -1602, -318], [-9644, 552, 0, 188, 0, 0], [-8828, -1240, 0, -1538, 0, 0], [2628, 104, 0, 824, 0, 0], [0, 0, 1777, 0, -792, -182], [19410, 500, 0, -1092, 0, 0], [-4076, -428, 0, 1902, 0, 0], [0, 0, 547, 0, -878, -655], [0, 0, 1127, 0, -2442, 350], [-3308, 676, 0, -2766, 0, 0], [0, 0, -9987, 0, 120, 476], [-6320, -2464, 0, 976, 0, 0], [-206, 2496, 0, -3030, 0, 0], [0, 0, -7657, 0, -3624, -448], [0, 0, 851, 0, -1146, -246], [0, 0, 6091, 0, 886, 335], [16150, -2460, 0, 1134, 0, 0], [630, -548, 0, -672, 0, 0], [0, 0, -6321, 0, 3964, 1335], [0, 0, -4027, 0, 4056, 1040], [0, 0, 3949, 0, 1376, 1361], [0, 0, 6915, 0, -5466, 230], [-13344, 1876, 0, -386, 0, 0], [-29702, -388, 0, 712, 0, 0], [0, 0, -2033, 0, 870, 1992], [0, 0, 5237, 0, 5104, 253], [1394, -2228, 0, 498, 0, 0], [0, 0, 1783, 0, 2970, 17], [-12038, 636, 0, -360, 0, 0], [0, 0, -1433, 0, 2014, -277], [0, 0, 8113, 0, -2472, -554], [-2546, -1532, 0, -912, 0, 0], [16188, -704, 0, -1176, 0, 0], [0, 0, 4151, 0, -4290, 2329], [0, 0, 1267, 0, 1800, 354], [-10478, -3036, 0, -1540, 0, 0], [-1086, 4016, 0, 1278, 0, 0], [15202, 524, 0, -3086, 0, 0], [-14290, -1208, 0, -2238, 0, 0], [18950, 308, 0, 798, 0, 0], [7996, 3392, 0, -540, 0, 0], [0, 0, 7697, 0, -724, -515], [-22762, 1068, 0, -1164, 0, 0], [0, 0, 6173, 0, 7464, -1259], [0, 0, 5795, 0, -7342, 2035], [-1850, 1340, 0, 3352, 0, 0], [0, 0, 925, 0, 10950, 402], [16568, -2216, 0, 1124, 0, 0], [0, 0, -599, 0, -12236, -13], [0, 0, -15221, 0, -6522, 947], [-5268, -3044, 0, -6108, 0, 0], [-1026, 4240, 0, -1536, 0, 0]];
    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_g();".
// 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_g(:prec:=6)
    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_g();".
// 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_g( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_507_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![576, 0, 321, 0, 34, 0, 1]>,<5,R![746496, 0, 56592, 0, 520, 0, 1]>],Snew);
    return Vf;
end function;