
// Make newform 8048.2.a.b in Magma, downloaded from the LMFDB on 24 April 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_b();".
// 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_b();".
// 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], [-4], [-1], [3], [-3], [-4], [0], [3], [-10], [-10], [2], [2], [-5], [-11], [-14], [4], [-7], [3], [-6], [-10], [0], [-9], [6], [-14], [-20], [-12], [-8], [2], [21], [18], [-9], [20], [-22], [-12], [20], [12], [2], [18], [-13], [-6], [20], [2], [-26], [-17], [16], [-2], [7], [0], [7], [-13], [0], [4], [-30], [-5], [7], [-2], [-7], [-10], [15], [28], [-11], [-16], [-18], [10], [-17], [10], [14], [10], [18], [6], [18], [17], [-3], [19], [-24], [-6], [-7], [-19], [-10], [22], [-6], [20], [6], [-6], [27], [4], [20], [-26], [-15], [-20], [18], [-14], [-4], [30], [-1], [9], [-5], [22], [-2], [-20], [-3], [-30], [-30], [-22], [32], [28], [20], [-12], [-23], [27], [-2], [4], [42], [-43], [33], [28], [-32], [-15], [-17], [1], [-22], [8], [-4], [-44], [13], [-4], [16], [11], [-2], [-43], [-12], [14], [-44], [-17], [14], [-26], [-24], [22], [-12], [-17], [-42], [20], [-28], [-4], [15], [-19], [-7], [-16], [-32], [28], [-18], [52], [-48], [-46], [10], [-16], [-32], [52], [33], [44], [9], [-26], [-3], [34], [-34], [23], [62], [-30], [46], [11], [2], [-48], [1], [47], [-21], [-40], [-58], [-24], [7], [43], [-43], [14], [37], [29], [0], [6], [27], [-6], [43], [55], [54], [-34], [-3], [-8], [24], [-12], [21], [-10], [-24], [-27], [9], [-31], [-15], [-60], [42], [39], [-54], [19], [-44], [23], [-42], [-9], [-32], [2], [10], [-12], [-42], [3], [15], [-50], [-23], [-30], [12], [-48], [19], [-9], [-69], [-43], [23], [4], [56], [23], [-10], [54], [70], [41], [4], [-60], [-61], [45], [47], [-64], [62], [-45], [1], [-30], [20], [52], [-66], [62], [-30], [57], [57], [-14], [-58], [-22], [8], [-67], [-24], [52], [60], [-28], [-51], [-33], [73], [-37], [30], [-34], [14], [-68], [54], [-17], [44], [-61], [-56], [-36], [-68], [-41], [30], [-18], [-59], [53], [10], [46], [-6], [-55], [-18], [27], [-65], [27], [-63], [4], [-28], [-32], [12], [64], [72], [-32], [-66], [1], [-14], [0], [-12], [-10], [52], [-36], [-10], [59], [74], [-24], [-32], [-22], [-2], [-55], [-53], [38], [-63], [-60], [-19], [82], [52], [-73], [4], [2], [-58], [-20], [-28], [26], [-79], [90], [54], [28], [-61], [-52], [48], [2], [76], [-26], [-64], [-59], [-6], [-73], [25], [-12], [14], [58], [-42], [-65], [-16], [-20], [-38], [6], [-20], [10], [-54], [-27], [-6], [-38], [-4], [64], [17], [87], [36], [40], [64], [35], [38], [47], [65], [1], [-87], [4], [41], [-94], [43], [54], [1], [-54], [10], [-6], [-21], [19], [72], [-44], [6], [65], [-55], [61], [-1], [8], [12], [75], [-3], [96], [-79], [-66], [-18], [25], [-70], [-77], [66], [-39], [-42], [-46], [-35], [-18], [-3], [49], [14], [-28], [-45], [-78], [45], [-44], [-50], [40], [14], [38], [-37], [3], [-70], [14], [-66], [33], [84], [-69], [-90], [-62], [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_b();".
// 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_b(: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_b();".
// 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_b( : 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![4, 1]>,<7,R![1, 1]>,<13,R![3, 1]>],Snew);
    return Vf;
end function;