
// Make newform 8048.2.a.d in Magma, downloaded from the LMFDB on 09 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8048_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_8048_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_8048_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_8048_a();"
function MakeCharacter_8048_a()
    N := 8048;
    order := 1;
    char_gens := [1007, 6037, 2017];
    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_8048_a_Hecke(Kf)
    return MakeCharacter_8048_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+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 := 2;
    raw_aps := [[0], [-1], [-2], [-1], [-1], [-3], [4], [0], [7], [-4], [-2], [8], [-10], [11], [1], [0], [4], [5], [3], [8], [10], [-12], [11], [-16], [2], [0], [6], [-2], [10], [5], [-2], [11], [-12], [-6], [-6], [2], [-4], [-12], [-2], [-21], [-12], [8], [0], [-8], [27], [0], [-22], [-5], [2], [11], [27], [-28], [-14], [-2], [3], [-25], [26], [5], [-12], [-33], [20], [9], [-14], [-24], [28], [-1], [4], [4], [-22], [-12], [26], [26], [13], [5], [-1], [0], [-26], [-27], [-27], [10], [-4], [-18], [30], [-30], [28], [7], [2], [10], [-28], [41], [32], [-26], [-16], [14], [-22], [1], [29], [-17], [-34], [-2], [-44], [-23], [6], [-30], [10], [-28], [-36], [-20], [36], [-23], [-33], [-6], [-8], [-6], [29], [-23], [-38], [-32], [29], [31], [-7], [-2], [30], [4], [32], [-23], [-34], [24], [-13], [2], [25], [22], [-32], [32], [39], [-44], [-6], [18], [-2], [-12], [7], [30], [-16], [12], [8], [3], [-23], [17], [10], [-18], [-58], [42], [20], [0], [10], [-30], [-48], [-48], [10], [33], [48], [21], [44], [-39], [-2], [-4], [11], [18], [-18], [-22], [15], [28], [0], [-19], [-13], [11], [4], [0], [18], [-29], [-25], [29], [16], [-3], [29], [66], [-34], [-45], [-24], [47], [23], [32], [-60], [-19], [-28], [14], [-42], [17], [64], [58], [-51], [-19], [13], [-35], [-12], [-6], [15], [60], [15], [20], [-17], [42], [27], [-60], [36], [8], [-22], [-26], [-21], [-9], [40], [65], [-6], [6], [-20], [23], [-9], [-9], [-23], [-13], [-38], [10], [23], [-24], [6], [-54], [-55], [40], [-20], [7], [-59], [-45], [-26], [64], [3], [-7], [-54], [-40], [58], [18], [10], [-2], [-51], [-75], [10], [24], [22], [-32], [9], [-60], [-32], [48], [-4], [-39], [31], [-59], [-81], [2], [4], [-42], [36], [54], [3], [-28], [-37], [44], [-36], [38], [-29], [-36], [50], [-23], [65], [-46], [14], [10], [5], [14], [39], [-17], [35], [-35], [-6], [18], [-8], [30], [-36], [66], [6], [36], [-7], [-6], [78], [-56], [22], [52], [82], [-58], [-45], [20], [54], [-32], [-54], [-46], [5], [-1], [10], [-51], [30], [-15], [-60], [56], [87], [-4], [74], [28], [-70], [56], [-26], [69], [44], [-10], [72], [35], [-68], [-8], [18], [12], [-80], [64], [-95], [22], [75], [-19], [10], [16], [-70], [12], [-69], [-50], [16], [-86], [-38], [-74], [-38], [-66], [-55], [72], [-48], [12], [-64], [-7], [-77], [-86], [-56], [8], [31], [88], [-9], [9], [-35], [33], [-48], [49], [2], [-85], [72], [-31], [-30], [46], [-30], [-53], [-85], [64], [20], [36], [25], [-35], [-43], [-61], [-10], [24], [15], [37], [56], [-35], [-96], [34], [-95], [-102], [-9], [-12], [93], [82], [18], [-79], [-10], [-15], [69], [70], [-50], [-29], [-12], [-75], [52], [6], [-28], [52], [0], [3], [39], [26], [50], [-54], [81], [-34], [-69], [-74], [-64], [-80]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8048_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_8048_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_8048_2_a_d(:prec:=1)
    chi := MakeCharacter_8048_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(2999) - 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_8048_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_8048_2_a_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8048_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![1, 1]>,<5,R![2, 1]>,<7,R![1, 1]>,<13,R![3, 1]>],Snew);
    return Vf;
end function;