
// Make newform 126.4.g.e in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_126_g();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_126_g_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_126_4_g_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_126_4_g_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 := [2304, 48, 49, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [2304, -49, 49, -1], [97, 0, 0, 1]];
    Rf_basisdens := [1, 1, 2352, 49];
    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_126_g();"
function MakeCharacter_126_g()
    N := 126;
    order := 3;
    char_gens := [29, 73];
    v := [3, 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_126_g_Hecke();"
function MakeCharacter_126_g_Hecke(Kf)
    N := 126;
    order := 3;
    char_gens := [29, 73];
    char_values := [[1, 0, 0, 0], [0, 0, -1, 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, -2, 0], [0, 0, 0, 0], [0, 1, 3, 0], [2, -2, -1, 1], [-16, 7, 9, 7], [-27, 0, 0, 5], [44, 10, -54, 10], [0, 3, 58, 0], [0, 14, 54, 0], [-40, 0, 0, 7], [-63, 28, 35, 28], [0, -21, -134, 0], [-168, 0, 0, 0], [143, 0, 0, 21], [0, -24, 348, 0], [-156, -63, 219, -63], [412, -61, -351, -61], [0, -34, 220, 0], [503, 35, -538, 35], [-812, 0, 0, -28], [143, -97, -46, -97], [0, 98, -311, 0], [-188, 0, 0, 89], [0, 54, 1170, 0], [46, 0, 0, -155], [352, 206, -558, 206], [0, 81, 40, 0], [0, 147, -783, 0], [467, -217, -250, -217], [-1540, 0, 0, 28], [-309, 0, 0, -70], [0, 37, 2211, 0], [-1424, 182, 1242, 182], [1723, 0, 0, 19], [0, -210, -1374, 0], [-244, 245, -1, 245], [-1910, -198, 2108, -198], [0, 210, -218, 0], [-940, 0, 0, -206], [0, -254, 2514, 0], [-1716, -336, 2052, -336], [1725, 0, 0, 239], [0, -168, 900, 0], [-1799, -42, 1841, -42], [-284, 0, 0, -490], [-2396, 396, 2000, 396], [-2032, 0, 0, 504], [4364, 0, 0, 57], [1608, 333, -1941, 333], [0, -593, 244, 0], [0, 98, 810, 0], [-696, 0, 0, 378], [-2510, 15, 2495, 15], [2556, 0, 0, -135], [0, 810, -174, 0], [1080, 336, -1416, 336], [-3536, 317, 3219, 317], [0, 59, 121, 0], [3019, -231, -2788, -231], [4420, 0, 0, 14], [-2539, -529, 3068, -529], [456, 0, 0, 327], [-911, 0, 0, -299], [-8600, 92, 8508, 92], [0, 1030, -977, 0], [0, 651, 2205, 0], [0, -21, -2984, 0], [-1227, 0, 0, -616], [7188, -504, -6684, -504], [-2618, 0, 0, -56], [-804, -996, 1800, -996], [0, -770, 4938, 0], [-9509, -204, 9713, -204], [0, -1211, -5060, 0], [1393, 0, 0, -749], [0, -550, -6606, 0], [-20, -658, 678, -658], [0, -79, 12664, 0], [0, -588, -1128, 0], [5263, 1038, -6301, 1038], [3216, 0, 0, 540], [1569, 0, 0, 287], [2004, 588, -2592, 588], [-3277, 0, 0, -537], [0, -109, -10463, 0], [0, 161, 6771, 0], [2972, 0, 0, 1834], [0, 2170, -3131, 0], [-3904, 0, 0, -410], [913, 0, 0, 763], [0, 2272, 180, 0], [8156, -548, -7608, -548], [3127, -1008, -2119, -1008], [6456, 0, 0, 21], [0, 259, 13126, 0], [-5304, 0, 0, 1746], [0, -1293, -3711, 0], [-2232, 768, 1464, 768], [0, -1011, -2102, 0], [0, -1897, -1424, 0], [5544, 0, 0, 1778], [1816, -49, -1767, -49], [-11740, -677, 12417, -677], [0, -1974, 1998, 0], [-387, -1253, 1640, -1253], [7709, 1676, -9385, 1676], [-4736, 0, 0, -2281], [0, -1278, 11286, 0], [-8784, -378, 9162, -378], [7463, 0, 0, -2256], [0, -184, -16337, 0], [-19998, -644, 20642, -644], [-27488, 0, 0, 308], [-3373, 1265, 2108, 1265], [10820, 0, 0, -2331], [-1324, -1064, 2388, -1064], [803, 0, 0, -129], [9936, 372, -10308, 372], [0, 273, -15345, 0], [-11684, 0, 0, -112], [14885, -1357, -13528, -1357], [-613, 0, 0, -3738], [0, 633, 19959, 0], [3660, -819, -2841, -819], [0, 531, -19064, 0], [-8020, 0, 0, 1603], [0, -924, 16954, 0], [0, -42, 20202, 0], [12385, 0, 0, -158], [0, -3505, 12844, 0], [57, 2401, -2458, 2401], [-9336, 0, 0, -1302], [0, 1862, 14731, 0], [-18606, 0, 0, 1610], [0, 686, 3822, 0], [-11873, 0, 0, -2210], [26056, -298, -25758, -298], [-18168, 2512, 15656, 2512], [9684, 0, 0, -111], [-19748, -280, 20028, -280], [-17912, 0, 0, -2690], [0, -301, 16137, 0], [13868, -1204, -12664, -1204], [-11604, 0, 0, -1071], [38027, -13, -38014, -13], [-8000, 0, 0, 1874], [-30865, 0, 0, 627], [42864, -804, -42060, -804], [0, 5016, -2144, 0], [0, -2702, -15738, 0], [0, -350, -4760, 0], [-18132, 0, 0, -2250], [29861, 0, 0, 735], [0, -3054, 6966, 0], [24111, 2947, -27058, 2947], [1092, 0, 0, 1764], [0, 3283, 14476, 0], [0, -2052, 312, 0], [-17557, 0, 0, 744], [-32804, -3013, 35817, -3013], [0, -3500, 25428, 0], [17212, 0, 0, -4984], [-1987, 0, 0, 210], [0, -225, -3195, 0], [-1968, 756, 1212, 756], [18348, 1188, -19536, 1188], [2281, 1512, -3793, 1512], [-1307, -2895, 4202, -2895]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_126_g_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_126_4_g_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_126_4_g_e(:prec:=4)
    chi := MakeCharacter_126_g();
    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_126_4_g_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_126_4_g_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_126_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![1296, 252, 85, -7, 1]>],Snew);
    return Vf;
end function;