
// Make newform 8016.2.a.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8016_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_8016_2_a_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_8016_2_a_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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_8016_a();"
function MakeCharacter_8016_a()
    N := 8016;
    order := 1;
    char_gens := [3007, 2005, 5345, 673];
    v := [1, 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_8016_a_Hecke(Kf)
    return MakeCharacter_8016_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], [-3], [3], [0], [-6], [-2], [2], [6], [4], [-5], [-1], [2], [4], [9], [3], [-7], [-8], [-3], [-14], [-8], [16], [3], [15], [-1], [3], [10], [-2], [10], [4], [-17], [12], [-13], [-11], [-1], [16], [-14], [5], [1], [0], [-16], [-14], [5], [-14], [-10], [16], [-22], [-19], [7], [0], [21], [0], [-26], [-18], [-16], [17], [-15], [28], [19], [17], [-20], [-24], [21], [15], [8], [24], [-8], [-23], [1], [1], [9], [-16], [-16], [-31], [-27], [4], [-2], [-8], [-12], [-26], [36], [-8], [7], [-19], [-20], [35], [-1], [6], [-36], [-26], [-22], [-40], [-2], [-20], [-4], [0], [-36], [-24], [4], [-23], [-17], [-4], [-46], [-30], [33], [43], [-3], [-28], [25], [-37], [-30], [6], [46], [-24], [-15], [-30], [12], [-12], [-8], [39], [22], [-22], [0], [-16], [-12], [16], [31], [-4], [-2], [0], [-25], [-9], [46], [24], [-35], [-12], [-55], [4], [50], [-29], [3], [46], [28], [20], [37], [-35], [44], [33], [-40], [44], [-4], [16], [24], [-14], [-28], [13], [41], [-15], [0], [18], [-54], [24], [8], [-1], [22], [-60], [-34], [54], [61], [-6], [-53], [14], [-17], [11], [50], [-18], [28], [31], [-8], [1], [-51], [46], [9], [-26], [12], [44], [28], [54], [37], [-38], [0], [-17], [42], [-54], [44], [53], [29], [-34], [-21], [-21], [-18], [41], [67], [-8], [32], [-48], [-6], [14], [-66], [-16], [-71], [-6], [-4], [-12], [-24], [-71], [-24], [34], [-3], [37], [26], [-19], [48], [-16], [0], [-22], [13], [16], [28], [2], [31], [20], [-44], [-24], [42], [50], [-8], [54], [9], [-56], [16], [-48], [-4], [-3], [-42], [0], [-27], [75], [-30], [54], [-74], [54], [22], [52], [-6], [-66], [15], [-24], [57], [-43], [18], [9], [-47], [-61], [38], [-10], [-18], [28], [29], [-46], [-18], [22], [-56], [39], [42], [18], [-57], [35], [22], [24], [48], [16], [56], [66], [-59], [78], [-61], [-30], [-61], [-42], [30], [5], [27], [78], [-6], [32], [65], [61], [-53], [60], [50], [75], [-65], [-76], [-56], [-22], [41], [-62], [-38], [46], [-10], [-54], [-26], [52], [62], [-38], [-41], [-3], [-70], [-36], [24], [21], [14], [76], [36], [-71], [-74], [-90], [-13], [26], [72], [-94], [-74], [16], [-56], [-18], [92], [-60], [44], [73], [-26], [-78], [19], [50], [-84], [18], [-26], [-48], [-12], [42], [-38], [-60], [-63], [-86], [-15], [-82], [77], [-15], [48], [13], [28], [0], [2], [18], [15], [14], [-67], [20], [-41], [-69], [64], [24], [96], [22], [17], [-52], [-48], [7], [50], [-34], [6], [-13], [52], [-67], [20], [-36], [84], [-55], [80], [-58], [-76], [-26], [-90], [37], [-34], [22], [1], [-13], [-57], [-73], [-16], [-6], [48], [-72], [-3], [-98], [23], [-52], [85], [26], [8], [-6], [-4], [82], [-42], [-92], [62], [35], [101], [59], [18], [-3], [49], [-60], [61], [-78], [48], [59], [56], [38]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8016_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_8016_2_a_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_8016_2_a_a(:prec:=1)
    chi := MakeCharacter_8016_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_8016_2_a_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_8016_2_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8016_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![3, 1]>,<7,R![-3, 1]>,<11,R![0, 1]>,<13,R![6, 1]>],Snew);
    return Vf;
end function;