
// Make newform 1008.4.k.a in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1008_k();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_1008_k_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_1008_4_k_a();".
// 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_1008_4_k_a();".
// 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 := [9, 0, 8, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 17, 0, 1], [4, 0, 1, 0], [0, -31, 0, -5]];
    Rf_basisdens := [1, 3, 1, 3];
    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_1008_k();"
function MakeCharacter_1008_k()
    N := 1008;
    order := 2;
    char_gens := [127, 757, 785, 577];
    v := [2, 2, 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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_1008_k_Hecke();"
function MakeCharacter_1008_k_Hecke(Kf)
    N := 1008;
    order := 2;
    char_gens := [127, 757, 785, 577];
    char_values := [[1, 0, 0, 0], [1, 0, 0, 0], [-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, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, -7, 0], [0, -6, 0, -7], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, -16, 0, 9], [0, 13, 0, -24], [0, 0, 0, 0], [0, 0, -4, 0], [0, 0, 0, 0], [0, 0, 202, 0], [0, 0, 0, 0], [0, -9, 0, 76], [0, 0, 0, 0], [0, 0, 0, 0], [-740, 0, 0, 0], [0, -44, 0, 97], [0, 0, 0, 0], [1384, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, -218, 0, -143], [0, 0, 860, 0], [0, -231, 0, -26], [-2000, 0, 0, 0], [0, 0, 0, 0], [0, 107, 0, 362], [0, 0, 0, 0], [0, -343, 0, -16], [0, 0, 850, 0], [0, 0, 0, 0], [1780, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, -478, 0, -141], [0, 0, 0, 0], [0, -56, 0, 523], [0, 0, -1048, 0], [0, -303, 0, 352], [0, 0, 0, 0], [0, 0, 670, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 709, 0, 414], [0, -372, 0, -829], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, -756, 0, -751], [0, 0, 0, 0], [0, 0, 0, 0], [7310, 0, 0, 0], [0, 461, 0, -658], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, -1133, 0, -588], [0, 0, 1930, 0], [0, 0, -4504, 0], [0, 802, 0, -713], [0, 0, 0, 0], [0, 0, 0, 0], [0, 1368, 0, 671], [0, 0, 0, 0], [13970, 0, 0, 0], [0, 0, -3290, 0], [0, 0, 0, 0], [0, -353, 0, 1404], [0, 0, 0, 0], [0, 1431, 0, -158], [0, 0, 0, 0], [0, 0, 0, 0], [-15262, 0, 0, 0], [0, 1796, 0, 627], [0, 0, 0, 0], [0, 0, 0, 0], [0, 1166, 0, 2031], [0, 1433, 0, -694], [0, 0, -6736, 0], [0, 0, 0, 0], [8440, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1246, 0], [0, -1774, 0, -2103], [0, 0, -7970, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [15878, 0, 0, 0], [-12980, 0, 0, 0], [0, -2523, 0, -1928], [0, 0, 0, 0], [0, 1797, 0, 2914], [-6788, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 52, 0, 3079], [0, 0, 0, 0], [0, 0, 0, 0], [-15010, 0, 0, 0], [0, -1907, 0, -3342], [0, 0, 0, 0], [-26192, 0, 0, 0], [0, 2129, 0, -1642], [0, 0, 0, 0], [0, 0, 0, 0], [0, -139, 0, 3416], [0, 2938, 0, -509], [0, 0, 0, 0], [0, 0, 3632, 0], [0, 0, 0, 0], [0, 994, 0, 4009], [0, 0, 0, 0], [0, -3331, 0, 308], [0, 0, 13460, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [25324, 0, 0, 0], [0, -4064, 0, -2129], [0, 0, -15530, 0], [0, 0, 8636, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, -4333, 0, -3586], [0, 0, 0, 0], [0, 921, 0, 5212], [46240, 0, 0, 0], [0, -2118, 0, 3527], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, -2784, 0, 3221], [-6550, 0, 0, 0], [0, 0, 0, 0], [0, 0, 16258, 0], [0, 0, 0, 0], [0, 0, 5386, 0], [0, -5404, 0, -3603], [0, 0, 19390, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 38, 0, 6093], [0, 2659, 0, -4326], [-52040, 0, 0, 0], [0, 0, 0, 0], [0, -1967, 0, 5178], [0, 0, 0, 0], [0, 0, 9130, 0], [0, 0, 0, 0], [-6046, 0, 0, 0], [0, 0, 0, 0], [0, -6262, 0, -729], [0, 0, 0, 0], [0, 2204, 0, 7473], [29990, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [53548, 0, 0, 0], [0, -1969, 0, -7768], [0, 0, 0, 0], [0, 0, 0, 0], [53440, 0, 0, 0], [0, 0, 0, 0], [-4930, 0, 0, 0], [0, 0, 0, 0], [0, 3676, 0, 8221], [0, 0, 0, 0], [0, 0, 12956, 0], [0, 0, 0, 0], [63866, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, -5826, 0, -8191], [0, 0, -2750, 0], [0, 0, 0, 0], [0, 5978, 0, -3267], [0, 0, 0, 0], [0, 0, -25240, 0], [12710, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 7957, 0, 7044], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 8934, 0, 6389], [0, -5757, 0, 5086], [0, 0, 0, 0], [92990, 0, 0, 0], [0, 0, 0, 0], [0, 0, 7762, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, -19526, 0], [0, 0, 0, 0], [0, -8508, 0, 2087], [0, 8061, 0, -3044], [0, 0, -37660, 0], [0, 0, 0, 0], [0, -9977, 0, -474], [0, 0, -19382, 0], [0, 0, 0, 0], [0, 0, -29740, 0], [0, 0, 0, 0], [0, 8252, 0, 11109], [0, 0, 0, 0], [0, 4086, 0, 12497], [0, 0, -31612, 0], [0, 0, 0, 0], [0, 0, -37130, 0], [0, 11349, 0, 6718], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 11161, 0, 8196], [0, -10202, 0, 1471], [0, 0, 0, 0], [0, 386, 0, -12219], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 18140, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [-18884, 0, 0, 0], [0, -7096, 0, -13971], [0, 0, -19516, 0], [0, 0, 0, 0], [0, -9148, 0, -13503], [0, 0, 0, 0], [0, 0, 0, 0], [0, 13118, 0, 5451], [45362, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [-112880, 0, 0, 0], [0, 5938, 0, 15373], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, -11733, 0, -13436], [0, 0, 0, 0], [37060, 0, 0, 0], [0, -6129, 0, -16304], [0, 0, 0, 0], [0, 0, -40766, 0], [0, 0, 0, 0], [0, 0, 14170, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, -10658, 0, 6607], [0, 0, -51340, 0], [0, 0, 56480, 0], [0, 0, 0, 0], [0, 0, 0, 0], [143008, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 5484, 0, -13507], [0, 0, 49568, 0], [0, 3527, 0, 18092], [0, 0, 0, 0], [0, 0, 0, 0], [0, 7829, 0, 18668], [0, 0, 0, 0], [0, -3739, 0, 15386], [0, 0, 0, 0], [-75850, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, -12827, 0, -18612], [153736, 0, 0, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1008_k_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1008_4_k_a();".
// 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_1008_4_k_a(:prec:=4)
    chi := MakeCharacter_1008_k();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(1999) - 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_1008_4_k_a();".
// 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_1008_4_k_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1008_k();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![0, 1]>],Snew);
    return Vf;
end function;