
// Make newform 8330.2.a.d in Magma, downloaded from the LMFDB on 20 September 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_8330_a();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_8330_2_a_d();".
// 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_8330_2_a_d();".
// 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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_8330_a();"
function MakeCharacter_8330_a()
    N := 8330;
    order := 1;
    char_gens := [1667, 2551, 2451];
    v := [1, 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;

function MakeCharacter_8330_a_Hecke( : Kf := Kf)
    return MakeCharacter_8330_a();
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]];
    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 := 2;
    raw_aps := [[-1], [-1], [-1], [0], [0], [-5], [1], [1], [6], [-9], [1], [-4], [6], [2], [9], [-9], [-3], [7], [14], [3], [-11], [8], [0], [9], [7], [-18], [16], [12], [-7], [9], [-7], [-6], [-6], [-20], [-12], [14], [-2], [-16], [-6], [-6], [12], [-2], [24], [2], [24], [7], [-22], [19], [-27], [-14], [9], [-30], [-26], [12], [-18], [3], [3], [28], [14], [-21], [-11], [9], [16], [24], [10], [24], [-7], [-13], [33], [-8], [6], [6], [-26], [14], [-16], [27], [-24], [16], [-24], [-5], [-6], [-16], [0], [-26], [-32], [24], [0], [-10], [-42], [-25], [0], [-9], [-34], [33], [38], [-24], [36], [12], [-38], [-34], [-7], [21], [-18], [-21], [-34], [-44], [-30], [6], [-30], [-20], [-2], [5], [-15], [10], [20], [30], [4], [27], [-30], [-15], [-44], [-25], [6], [-3], [-8], [-12], [-13], [-27], [-23], [22], [-43], [-18], [53], [41], [-6], [-41], [-6], [7], [54], [42], [-14], [-3], [14], [36], [28], [39], [10], [15], [-47], [-24], [-40], [-30], [-34], [42], [23], [0], [38], [-18], [-8], [45], [3], [-36], [-40], [-3], [-12], [-18], [47], [-26], [-28], [-42], [-45], [-26], [-45], [-34], [58], [-36], [-22], [-33], [-11], [16], [-1], [-18], [20], [36], [48], [-12], [-16], [10], [8], [-12], [-26], [51], [-49], [-12], [3], [-30], [44], [8], [33], [-45], [6], [-35], [-26], [7], [12], [18], [4], [24], [24], [31], [-7], [0], [8], [18], [-3], [40], [-43], [15], [-6], [-15], [-34], [-5], [-30], [-70], [18], [8], [51], [-45], [73], [-48], [-43], [70], [2], [-27], [34], [-24], [40], [-24], [54], [24], [39], [52], [-32], [20], [9], [39], [10], [-30], [-16], [9], [77], [-24], [45], [-26], [39], [45], [-49], [-44], [54], [-38], [26], [-24], [-26], [28], [-21], [-44], [18], [21], [-49], [54], [-5], [8], [64], [-4], [-14], [-59], [0], [32], [-13], [-39], [-24], [23], [18], [-8], [4], [-6], [-7], [84], [52], [9], [15], [15], [-12], [-12], [-10], [63], [-62], [39], [48], [-2], [4], [-45], [5], [-33], [62], [-34], [42], [-53], [-54], [-73], [-24], [18], [60], [-4], [-3], [-59], [3], [0], [-74], [-57], [-20], [77], [-48], [8], [-51], [79], [56], [1], [-48], [6], [26], [18], [-80], [6], [68], [-60], [-82], [-54], [70], [88], [-19], [54], [21], [26], [-30], [6], [34], [38], [81], [78], [76], [26], [24], [34], [68], [-48], [0], [30], [-78], [-3], [62], [-36], [-36], [72], [-74], [-28], [-18], [11], [38], [-63], [-26], [-30], [-6], [40], [-28], [-12], [-99], [-20], [-51], [70], [87], [96], [-46], [-21], [28], [78], [50], [-74], [11], [36], [-52], [72], [-33], [28], [-6], [74], [-56], [-66], [74], [-102], [94], [-66], [5], [-27], [12], [52], [-67], [-78], [-50], [-6], [37], [57], [-78], [80], [98], [-39], [24], [-14], [-27], [-57], [-30], [64], [-30], [-33], [-83], [-27], [-24], [-30], [1], [48], [-41], [69], [-34], [-12]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8330_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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