
// Make newform 8016.2.a.l in Magma, downloaded from the LMFDB on 18 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_l();".
// 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_l();".
// 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 := [1, -3, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-2, -1, 1]];
    Rf_basisdens := [1, 1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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, 0], [1, 0, 0], [-1, 0, 1], [2, -1, 1], [-1, 3, 1], [-3, 1, -1], [-5, -1, -1], [1, -1, -3], [1, 1, -1], [-1, 3, 1], [5, -6, -2], [-1, -2, -3], [-4, -2, 2], [1, -1, 3], [0, 3, -3], [-6, 3, 4], [3, -4, 1], [0, -4, -4], [-3, 6, 3], [1, 1, -3], [1, 3, -5], [-10, -2, -2], [-2, 3, 0], [-8, -3, -5], [2, -1, 3], [-2, 3, -6], [-2, -4, -6], [-2, -8, -2], [9, 5, -1], [-2, -6, 6], [3, 4, 2], [-11, -1, 3], [0, 1, -3], [-7, 6, 1], [4, 1, 8], [2, -2, 8], [-6, 0, 6], [-4, -1, -6], [-1, 0, 0], [-6, -6, -8], [-3, 9, 3], [-5, 5, 5], [-5, -2, -6], [1, 5, 9], [-11, 13, 7], [-6, 6, 4], [-10, 4, 8], [14, 1, 5], [-5, 6, 7], [0, 12, 0], [-17, 6, 0], [6, 2, 8], [11, -9, -3], [-9, 13, 1], [9, -5, -7], [18, -11, -5], [-5, -4, -1], [-9, 9, 5], [-8, -3, -4], [1, -8, -2], [12, -16, -6], [6, -10, 2], [-2, 1, 0], [3, -4, 2], [-15, -1, 3], [7, -1, 9], [5, -17, -1], [11, -6, -2], [-21, -4, -3], [-15, -2, 3], [-15, 0, -4], [10, 10, -4], [6, 2, 8], [0, -13, -10], [-10, 1, -10], [-4, 0, 2], [-21, 19, 7], [1, 5, 9], [-4, -12, -14], [2, 0, -12], [8, 4, 8], [-23, 1, 1], [3, -4, 12], [-21, 6, -4], [6, -6, -4], [-3, -8, 3], [-14, 3, -7], [4, 6, 2], [-19, 5, -3], [6, -12, -8], [6, 0, 0], [2, -18, 8], [9, 9, 11], [6, -14, -10], [-9, -3, -11], [16, 8, -2], [16, -4, -6], [5, -13, 1], [17, 1, 5], [4, 7, 4], [9, 8, 3], [9, -15, 5], [9, 3, 9], [-13, -1, -1], [6, 5, 14], [4, -19, -5], [-32, -1, 4], [-5, -7, 7], [-2, -15, -23], [10, 7, 21], [10, 16, -14], [-31, -5, -1], [6, 12, -6], [9, 11, 17], [11, 10, 12], [22, 8, 12], [7, 1, -3], [4, 0, -6], [-2, 14, 18], [-12, 5, -6], [-1, -1, 17], [15, -13, 1], [5, 1, -7], [17, 11, 9], [7, -7, -3], [4, -16, -8], [-12, -7, -14], [-24, 8, 4], [29, -3, -7], [-5, 3, -1], [16, -7, 8], [-8, 17, -1], [-11, 5, 15], [27, -9, -1], [-21, 2, -2], [-9, 17, 3], [-6, 5, 12], [-13, -7, -15], [-3, -11, 19], [32, -11, -7], [19, 6, 1], [3, 15, -7], [-35, 15, 9], [-11, -17, 15], [12, 3, -10], [11, -26, -6], [20, -20, -12], [5, -4, 8], [-29, 7, -7], [32, -4, 0], [17, -3, 7], [-26, 6, 4], [33, -7, -11], [10, -4, -10], [23, -13, -11], [23, -18, -18], [16, -5, -17], [21, 12, -14], [7, 1, -23], [-11, -11, 7], [-12, 22, 2], [-23, 11, -13], [0, 8, 8], [-24, 1, 0], [25, 1, -5], [-30, -2, 0], [-31, 1, 5], [1, -1, -3], [-2, 13, 25], [20, 10, 2], [-2, 7, -6], [-10, 0, 22], [-24, -3, 17], [-29, 12, 16], [-18, 4, 8], [2, 12, -6], [-24, 24, 6], [25, 12, -9], [20, -4, 8], [-6, -23, 18], [-15, -8, -20], [2, -4, 22], [26, -7, -12], [23, -5, 13], [-23, 15, 3], [15, -21, 5], [3, 7, 3], [-31, 15, -1], [37, -24, -14], [31, -9, 13], [-8, -8, 6], [-6, 3, -18], [4, -26, -4], [-19, 27, 17], [19, 11, -15], [-32, 3, 11], [2, 9, 21], [-14, -12, 4], [-18, 15, -5], [15, 4, 2], [-21, 7, -7], [15, -2, 0], [-2, 31, -2], [2, 2, -8], [-19, -9, -3], [-7, 29, -3], [33, -3, -1], [15, -19, -17], [1, -11, -31], [-38, 6, 4], [5, 8, -4], [-5, 13, 21], [35, -19, -11], [-25, 1, -3], [2, 22, -2], [15, -2, -18], [-5, -7, -7], [-18, 0, -10], [-31, 16, 12], [-17, 12, -5], [31, 19, -13], [-39, 12, 4], [0, 20, 22], [18, 10, 34], [-3, -1, 23], [14, 24, 8], [-6, -7, 3], [-12, 12, 20], [23, 17, -11], [24, 2, -12], [-26, 3, 4], [-11, -9, -3], [7, -3, -23], [-21, -11, -21], [-24, 22, -10], [47, 7, 15], [-24, -28, 18], [-11, -1, 1], [10, 21, -10], [0, 8, -28], [-3, -13, -5], [30, 2, 24], [14, -6, 24], [-10, -11, 8], [-30, 4, 16], [14, -22, -20], [-12, 11, -25], [-3, -4, -27], [24, -14, -6], [58, -16, -4], [2, 0, 30], [16, -18, 4], [-2, -20, 10], [-29, 17, 1], [-33, -13, -11], [5, -21, 5], [9, 4, 21], [31, 15, 23], [-52, -5, -10], [15, -10, 12], [-5, 15, 1], [-18, 29, -4], [3, 16, -5], [1, -20, -9], [2, 36, -12], [-39, 23, 9], [-49, 3, -7], [-28, -16, 2], [13, 6, -29], [26, 12, -10], [-51, -3, -3], [-20, 10, -4], [-5, 25, -9], [-50, 15, 15], [6, 4, -24], [11, 3, 5], [-34, 23, 18], [7, 2, -1], [25, -7, -21], [-32, 4, -2], [-8, -20, -6], [0, 0, -28], [25, -5, -7], [-8, 14, 24], [10, -31, 6], [7, -21, -31], [-11, -18, 6], [-17, -9, 1], [9, 14, -4], [-37, 19, 23], [-12, 2, -26], [-5, -4, -27], [8, 29, 19], [-39, -1, 17], [4, -18, 4], [4, -28, -6], [66, 5, -1], [50, -11, -12], [-50, -13, 0], [30, -6, -16], [0, 34, 12], [7, -18, 3], [36, -19, -21], [-17, -3, 13], [16, 16, -12], [60, 2, 2], [-22, -7, 0], [-47, 7, 5], [-22, 4, 16], [3, 17, -13], [-4, -18, -12], [19, 5, 17], [0, 14, -20], [-31, -17, 21], [-31, 13, 17], [34, 0, -12], [-13, -22, 13], [38, -15, -19], [30, -12, -20], [-22, 2, -20], [-34, 10, -10], [-10, -35, 23], [18, -16, -16], [-14, -6, 16], [4, 12, 44], [-24, 15, -15], [-20, -22, -18], [39, 9, 11], [-10, -9, 17], [-11, 35, 5], [5, 1, -13], [5, -25, -13], [-73, 15, 11], [35, 11, 1], [-15, 27, 27], [-5, -11, 13], [22, 2, 8], [14, 26, 28], [43, -11, 5], [47, 6, 20], [37, -21, -9], [-26, 32, -2], [58, -9, 2], [-17, -5, -25], [49, -31, -15], [-10, 8, -10], [18, 20, -14], [2, -14, 10], [-27, -21, 7], [-22, 24, -12], [27, -27, -9], [-50, 14, 2], [39, -16, -21], [-2, 28, -24], [-34, -27, 6], [-28, 26, 20], [19, 14, 24], [11, -4, -21], [55, -11, 5], [23, 14, -12], [7, -13, 29], [18, -50, -14], [-4, 38, 12], [-34, -16, 14], [-32, -15, -26], [-30, 0, -4], [-7, 14, 13], [-48, 52, 22], [-49, 4, -10], [4, -15, 32], [9, -17, -29], [-8, -16, 16], [-36, -8, 4], [-2, -8, -20], [10, 1, 22], [17, -17, -17], [17, 11, -33], [47, -24, -16], [-36, -10, 16], [30, 28, 20], [36, -42, -30], [-27, 6, 2], [-46, 42, 10], [59, -10, -16], [-11, 29, 19], [-17, -3, -15], [58, -30, -14], [27, 24, 13], [22, -42, -12], [-19, 13, -15], [-13, -15, -13], [16, -30, -6], [-13, 1, 29], [50, -11, -24], [22, -44, -4], [-33, 27, -5], [30, 21, 9], [-44, 5, -17], [18, 3, 32], [7, -26, -11], [-16, 32, 2], [32, 22, -16], [-23, -17, -11], [-37, -3, -21], [28, -1, -12], [-25, -13, 1], [-27, -4, 15], [10, 18, 8], [-62, 29, 4], [5, -17, 35], [12, 16, -6], [35, -29, -1], [55, 11, 29], [-15, 3, -13], [-38, -8, 26], [18, 10, 32], [8, 22, 28], [-19, 46, -12], [-7, 24, 30], [8, -31, 17], [-11, -7, -1], [-39, 6, -9], [14, -19, -5], [9, 17, 25], [-53, 26, 16], [29, 9, 47], [10, 22, -20], [28, -3, 7], [-22, -14, 12], [19, -21, 27]];
    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_l();".
// 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_l(:prec:=3)
    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_l();".
// 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_l( : 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, 3, 1]>,<7,R![1, -1, -5, 1]>,<11,R![-52, -28, 0, 1]>,<13,R![4, 12, 8, 1]>],Snew);
    return Vf;
end function;