
// Make newform 8016.2.a.k 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_k();".
// 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_k();".
// 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
        poly := [-2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
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, 0], [1, 0], [-2, 1], [0, 2], [-2, 0], [0, 2], [-4, 1], [-4, -2], [0, 4], [2, 0], [-4, 2], [-4, 0], [-4, 1], [4, 3], [10, -2], [-2, 3], [10, 2], [0, 10], [0, -1], [4, 6], [-2, 10], [6, 3], [6, 4], [-2, -8], [-6, -8], [-6, 5], [-2, -5], [10, 0], [-4, 6], [4, -3], [-4, 0], [14, 4], [18, -2], [-4, 7], [2, -3], [10, 3], [2, -12], [4, -7], [1, 0], [6, 10], [-12, -8], [4, 2], [-2, 10], [-10, 12], [2, 11], [-12, -4], [0, -10], [0, -10], [-2, 2], [12, -10], [-10, 8], [-18, -6], [6, -14], [16, -4], [0, -5], [0, 8], [6, -3], [2, 11], [4, 2], [-10, 8], [12, 10], [14, 0], [16, 11], [-16, -8], [-10, 12], [-22, 6], [12, 1], [6, 4], [2, 2], [-16, 0], [14, -8], [-24, 4], [12, 2], [-16, -12], [-16, 11], [18, -2], [14, -11], [16, 6], [8, 1], [0, -8], [6, -24], [-6, 0], [0, 8], [0, 8], [-2, -5], [22, -6], [30, -2], [-6, 18], [-10, -8], [-10, -3], [4, -8], [24, 6], [-22, 1], [-14, -12], [28, -9], [-2, 22], [-2, -8], [-16, -17], [16, 18], [-16, 2], [4, 23], [10, 18], [-8, 0], [20, -13], [-20, -15], [-12, 0], [-18, 6], [-12, -15], [-32, 0], [-10, -16], [-2, 17], [8, 6], [-2, 22], [-24, -15], [12, -20], [-16, 9], [-8, 25], [-16, -6], [6, -18], [26, -6], [12, 4], [-14, 12], [-18, 12], [-18, 8], [40, -5], [-10, -24], [-12, 18], [-8, 14], [2, -7], [-4, -14], [-8, 3], [32, -4], [-22, 15], [-42, -4], [-26, 10], [18, 4], [18, 11], [-20, -17], [10, -3], [18, 10], [-36, 13], [6, -11], [-10, 13], [6, -6], [32, -2], [6, 30], [32, 18], [-10, 18], [0, -32], [0, -32], [22, 0], [20, 9], [-20, 22], [-16, 26], [-40, -12], [24, -8], [-12, -14], [-34, 14], [-10, 12], [14, 5], [-30, 12], [-4, 11], [-12, 0], [38, -12], [-8, 23], [-12, -18], [18, 7], [-16, -22], [16, 0], [-18, 16], [-26, 2], [14, -24], [16, -24], [-12, 12], [-34, -9], [46, -6], [4, -14], [18, -23], [24, -18], [-4, -36], [-16, 18], [4, 36], [0, -2], [-4, -15], [8, 10], [-42, 6], [20, -10], [-16, 4], [20, -24], [24, -2], [-30, -6], [-30, 0], [20, 28], [-6, -12], [2, -28], [-50, 2], [0, -32], [20, 18], [34, 16], [2, -18], [34, -11], [12, -10], [-8, 22], [-46, -10], [-6, -4], [22, -20], [-14, -27], [-6, -4], [-4, -23], [0, 40], [-40, -8], [-18, 20], [-6, 31], [22, 6], [10, -18], [40, 8], [30, 9], [2, 6], [-40, -8], [-30, 19], [16, 10], [-12, -4], [8, 13], [28, 14], [-22, 10], [-32, 2], [30, 26], [-8, -42], [30, 5], [-44, -16], [16, -2], [-20, -27], [-2, 9], [-64, 5], [-4, 14], [4, 18], [42, 6], [54, -8], [34, 18], [-30, 2], [-18, 8], [8, -40], [-10, 19], [-4, 24], [54, 2], [34, 10], [4, 26], [38, -26], [-24, 22], [-16, -14], [-20, 14], [14, -14], [-32, 0], [46, 2], [-22, 17], [40, 16], [24, 30], [-12, 32], [2, -53], [-6, -32], [-6, -15], [-14, 16], [64, 6], [-4, 24], [-14, -6], [48, -10], [-34, -3], [0, 39], [-16, -35], [-14, -24], [36, 12], [40, -18], [-10, -10], [-28, -24], [16, 0], [30, 3], [34, -4], [32, 0], [22, 8], [34, 0], [-40, 10], [-38, 11], [20, 36], [-8, -34], [-16, 13], [-32, -32], [-16, -12], [30, 7], [-44, 16], [-64, -11], [34, -20], [-30, -24], [2, 0], [-14, 24], [24, -22], [-38, 10], [0, 26], [2, 35], [-14, -48], [16, -26], [6, -48], [14, -5], [-36, -12], [-6, 22], [12, 40], [-22, -22], [-42, 10], [74, 4], [52, -14], [-34, 4], [-12, -20], [-10, 30], [-58, 16], [-44, 9], [32, -26], [-54, 8], [-38, 24], [-8, -36], [-14, -46], [-44, -21], [12, -52], [-54, 8], [-18, 22], [-58, -15], [44, -27], [-38, 20], [-40, 30], [8, 2], [50, 26], [-86, 2], [20, -30], [42, 10], [-62, -1], [-74, 0], [40, 5], [-8, 0], [-18, -40], [-16, 9], [2, -42], [-32, -10], [46, 0], [-14, -24], [38, -17], [30, -31], [22, 2], [-48, 24], [-70, 0], [4, -24], [32, -30], [-18, -26], [36, -26], [-6, -10], [-30, -21], [-6, -57], [0, -4], [24, 45], [0, 44], [34, -28], [20, 21], [-26, -6], [6, -12], [-36, -31], [60, 18], [14, -8], [16, 11], [-10, 44], [-42, -31], [50, -5], [-18, 16], [26, 20], [-32, 15], [-40, 12], [34, 16], [-18, -3], [-24, 10], [26, 0], [8, -50], [-16, 44], [-68, 3], [6, 0], [-30, 6], [38, -24], [-82, -3], [38, -20], [60, 14], [28, -6], [50, -17], [-60, -6], [8, -26], [20, 36], [-30, 14], [-18, -20], [72, -4], [80, -3], [-76, -18], [-26, 2], [-52, 20], [46, 2], [36, 29], [26, 21], [-22, -24], [-18, -26], [2, 61], [84, -5], [-78, 3], [94, -7], [0, 14], [28, 33], [-36, 7], [48, -4], [-14, -40], [-30, 23], [18, -4], [16, 48], [-50, 16], [26, 30], [60, 4], [-40, 4], [22, 10], [58, 14], [2, -9], [24, 36], [22, -46], [48, -20], [24, -16], [62, -9], [34, -36], [-6, 16], [0, -22], [60, 12]];
    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_k();".
// 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_k(:prec:=2)
    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_k();".
// 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_k( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8016_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![2, 4, 1]>,<7,R![-8, 0, 1]>,<11,R![2, 1]>,<13,R![-8, 0, 1]>],Snew);
    return Vf;
end function;