
// Make newform 8016.2.a.i in Magma, downloaded from the LMFDB on 19 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_i();".
// 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_i();".
// 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], [1], [3], [-4], [2], [-6], [-2], [-6], [0], [3], [7], [-6], [-12], [-3], [-9], [-11], [0], [5], [14], [4], [0], [-9], [-13], [-1], [15], [-14], [-18], [2], [4], [7], [-4], [15], [-11], [-5], [0], [18], [5], [1], [16], [-12], [14], [1], [6], [6], [-24], [18], [5], [3], [-8], [25], [8], [2], [-6], [12], [5], [21], [-8], [19], [-27], [4], [24], [-11], [-21], [-4], [-4], [8], [-31], [-3], [9], [29], [16], [-8], [-23], [21], [-36], [30], [-28], [4], [-10], [4], [28], [19], [-11], [4], [-9], [35], [-10], [-24], [6], [-22], [0], [2], [12], [12], [-40], [20], [-4], [-24], [-7], [-41], [-16], [22], [-34], [-15], [-13], [9], [24], [-35], [-21], [42], [-22], [-18], [-40], [-15], [-38], [12], [20], [-24], [43], [6], [-26], [-12], [24], [44], [8], [-41], [-44], [-22], [28], [15], [-21], [50], [-4], [33], [-40], [-3], [44], [-38], [-41], [11], [-10], [8], [12], [-35], [-31], [12], [21], [36], [52], [48], [-8], [-52], [-30], [8], [-23], [17], [21], [-20], [18], [-54], [-12], [-40], [-5], [18], [36], [34], [-6], [21], [-62], [39], [38], [-5], [19], [26], [-50], [-28], [19], [0], [49], [-3], [-18], [-63], [50], [-32], [0], [16], [66], [-3], [-2], [-56], [11], [-38], [-18], [-32], [-31], [-27], [-34], [-49], [39], [46], [57], [67], [8], [-24], [52], [14], [-22], [-30], [8], [-7], [30], [-64], [-12], [-32], [17], [20], [18], [57], [-47], [58], [-35], [0], [48], [-8], [42], [-39], [-40], [-8], [-22], [31], [-28], [-72], [36], [10], [30], [0], [18], [-35], [-72], [24], [-40], [4], [37], [6], [-16], [13], [63], [58], [-66], [-2], [-18], [30], [-40], [-54], [34], [7], [68], [-43], [13], [22], [-27], [-47], [-5], [6], [58], [6], [76], [-67], [26], [-18], [-58], [-36], [-49], [-22], [22], [27], [75], [42], [56], [56], [-56], [-36], [-14], [-19], [18], [-61], [66], [3], [26], [62], [-87], [-41], [-22], [26], [-40], [25], [9], [-57], [-4], [50], [15], [-73], [36], [80], [-6], [-3], [22], [-54], [50], [54], [14], [-18], [4], [90], [74], [-69], [9], [-30], [-84], [8], [29], [30], [44], [60], [-55], [86], [18], [-25], [6], [20], [30], [34], [44], [-8], [-22], [-76], [-60], [16], [1], [26], [-78], [-9], [70], [40], [-30], [38], [80], [-8], [2], [-58], [44], [13], [10], [-23], [86], [49], [37], [20], [57], [40], [8], [26], [66], [-65], [22], [57], [-28], [-65], [-17], [-80], [32], [-80], [14], [57], [28], [-12], [31], [50], [-50], [22], [-65], [52], [65], [-96], [72], [36], [1], [-32], [18], [8], [-42], [-30], [29], [62], [-6], [49], [-1], [23], [-61], [-80], [-46], [-92], [-4], [1], [10], [-33], [-20], [-83], [-18], [-48], [-6], [-32], [46], [-90], [-36], [-90], [11], [-87], [-49], [-30], [-35], [-35], [-24], [101], [42], [-24], [71], [-40], [2]];
    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_i();".
// 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_i(: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_i();".
// 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_i( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8016_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-1, 1]>,<7,R![-3, 1]>,<11,R![4, 1]>,<13,R![-2, 1]>],Snew);
    return Vf;
end function;