
// Make newform 8016.2.a.n 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_n();".
// 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_n();".
// 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], [2, 0, -1], [2, -2, 0], [1, 1, -1], [2, 0, 0], [0, 0, 3], [-2, 2, 2], [8, 0, 0], [-4, -2, 2], [2, -2, -2], [-4, 2, 0], [6, -6, -3], [2, 2, 3], [-1, 3, -3], [-4, 4, 3], [-2, 2, -2], [-1, 5, 5], [8, 2, 1], [4, 4, 6], [8, -2, -4], [-4, 2, -5], [4, -8, -6], [0, -6, 2], [2, 0, -6], [0, 2, 1], [8, -2, -1], [1, -3, -1], [-4, 6, -2], [-2, 4, 1], [4, -4, 2], [2, -2, 4], [-4, 2, -4], [-12, 0, 1], [-2, -2, -9], [0, -4, 3], [-6, 4, -2], [12, 0, -1], [-1, 0, 0], [10, 4, 4], [12, 0, 4], [5, 3, -5], [13, 1, 3], [4, 2, -2], [-8, 2, 7], [0, 4, 6], [-4, 8, -4], [2, -2, 0], [-4, 8, 12], [3, 5, 9], [-12, -6, 4], [5, -11, 3], [-12, 10, 4], [-2, 6, 12], [-14, 8, 3], [2, -10, -12], [-8, 4, 5], [2, -4, -3], [0, -10, 10], [0, -6, 4], [-18, 2, -6], [-6, 16, 8], [0, 4, 1], [-10, 10, 4], [-2, -4, -4], [8, -6, -2], [-4, 10, -9], [-6, -8, -6], [6, 14, -2], [-12, 2, -4], [12, 2, -4], [8, -8, 4], [-2, 14, 2], [10, 4, 18], [2, 2, 15], [-1, 7, 1], [-20, -2, -7], [-3, 15, 7], [-12, -2, -1], [15, 1, -7], [3, -9, 5], [-6, 4, 10], [18, 6, -8], [9, -13, 3], [4, -16, -9], [2, 2, -10], [-18, 16, 6], [2, 4, 6], [30, 0, -2], [-24, 10, 5], [0, -12, 12], [-4, 0, -6], [26, -8, -9], [7, 11, 5], [-8, -6, -7], [17, 5, 7], [6, -16, 2], [0, 12, -7], [20, -12, -12], [2, 0, 16], [-18, -4, 7], [-4, -6, -8], [-10, -10, 4], [8, -22, -11], [4, 8, 15], [-11, 19, 15], [12, 8, 16], [4, 6, 11], [12, -12, 4], [-2, 4, 12], [-26, -2, 3], [-3, 3, -1], [2, -4, 0], [-36, 2, -1], [-8, -12, 2], [-18, -4, -7], [2, -20, 9], [-8, 24, 2], [6, -4, -8], [18, -22, -10], [-16, -10, -20], [14, 4, -12], [10, -20, -8], [0, -8, -2], [-30, 8, 7], [14, 12, -6], [-6, 12, 4], [-16, 0, -10], [-24, -4, -3], [-1, -15, 13], [2, -16, -9], [4, 12, 8], [-42, 6, 1], [6, 8, -16], [10, -8, -8], [10, -12, -8], [24, -6, -5], [0, -4, 1], [24, -4, 1], [8, 6, 20], [-22, 10, 9], [-8, 12, -3], [2, 0, -1], [8, 8, -8], [-20, -6, 2], [13, 13, -9], [1, -5, -5], [6, -4, 4], [4, -4, -2], [18, -2, 16], [10, -20, -2], [8, 2, -5], [-10, 6, -18], [-10, -14, 12], [32, -12, -6], [-16, 0, -24], [24, -24, -10], [-22, 8, -16], [-20, 2, 10], [12, 6, 17], [27, -5, -11], [-22, 0, 7], [8, -20, 2], [-18, -6, -20], [6, -2, -17], [-6, -10, 4], [-20, 18, 11], [-21, -3, -7], [-29, 17, 13], [18, 12, -2], [8, 0, 4], [-22, -12, -26], [-8, 16, 0], [-33, -3, 9], [14, -10, 1], [-18, 28, 0], [-12, 32, 6], [2, -22, 7], [22, -6, 20], [-10, 8, -6], [8, -12, -16], [-18, -2, -12], [-12, -18, -10], [-24, 2, -9], [-4, 4, -6], [2, 0, -18], [-9, -15, -27], [18, -26, -22], [27, 1, 5], [10, 26, -4], [-18, -16, -2], [14, -10, 12], [-16, -4, -12], [0, 10, -20], [-9, 23, 13], [-6, 0, -10], [35, 9, 5], [-7, 23, -9], [-14, -20, 4], [3, -5, 13], [8, -4, 21], [-18, 14, 2], [-32, 6, 2], [4, 6, 8], [-4, -8, 2], [-26, -8, -8], [16, 10, 25], [-41, 3, -3], [-30, 16, 17], [-2, -10, -6], [-31, -21, 11], [34, 0, -6], [-36, -8, -9], [10, -18, 2], [7, 11, -11], [9, 3, 3], [10, -8, 15], [-40, 6, -4], [20, -12, -16], [20, -12, 7], [18, 12, -16], [-24, -12, -14], [-20, -2, -9], [-12, -4, 18], [-18, 6, -10], [3, 13, -7], [-38, 16, 10], [16, 20, 2], [38, 14, -9], [2, -22, -32], [28, -22, -6], [-28, 24, 7], [10, -8, -25], [-20, -4, -21], [-20, -12, -2], [-6, -14, 28], [-30, 8, 10], [-42, -4, -16], [2, -10, -14], [-5, -1, 5], [0, -8, 2], [-42, 38, 18], [-26, 14, -3], [2, -12, -10], [38, -4, 0], [15, -13, -23], [-32, 36, 14], [-20, -4, 12], [26, 14, 24], [-12, 20, 2], [-15, -25, 23], [26, 0, 24], [38, 18, 2], [38, 0, -14], [-38, 24, 15], [-12, -16, -12], [-8, -14, -6], [-56, -8, 4], [-10, 36, 17], [18, -24, -8], [-2, -12, -1], [0, -16, -30], [-50, -4, 4], [-14, -12, 6], [20, -6, 28], [-6, 22, 12], [2, -8, -33], [26, -24, -25], [10, -14, 5], [8, 14, 18], [2, 8, 38], [24, -4, 0], [-34, -8, -22], [-30, 6, 18], [-3, -5, -21], [44, 8, -5], [-38, 6, -16], [50, 0, 18], [2, -24, -4], [-23, -11, -25], [22, -34, -24], [4, -24, 3], [12, 8, -16], [27, -3, 25], [36, -6, 7], [8, 4, -12], [-3, 15, -29], [-10, 4, -11], [-8, 16, -14], [24, 24, -13], [-12, -6, -12], [24, 4, -10], [-14, 20, 4], [-69, 3, 1], [-3, 3, -17], [-4, -2, -8], [18, 2, 32], [-2, -20, 21], [-36, -12, 10], [-8, 12, -16], [12, -26, 2], [-16, 18, 23], [-22, -2, 2], [-30, 30, 14], [-48, 24, 12], [0, -18, 16], [0, 32, -4], [-6, -4, -20], [38, 2, -16], [-38, 20, 12], [-34, 34, 18], [-2, -20, 0], [28, 18, 16], [16, 20, -13], [14, -2, -24], [-14, -8, -6], [-58, 6, 0], [58, 14, 0], [32, -38, -12], [-2, 22, 15], [-10, -30, 0], [-46, 32, 14], [-34, 12, -18], [-10, -2, 31], [-30, -26, 5], [2, 0, 2], [-16, 36, -8], [-10, 42, -8], [55, -1, 5], [-4, 10, 0], [31, 5, -3], [-4, -26, 8], [-30, 10, 5], [-53, 23, 13], [18, -38, -11], [-10, -2, -32], [10, 4, -10], [-8, 32, -9], [-32, 6, -16], [-4, 16, -24], [-54, -4, -20], [6, 4, -20], [26, -10, -19], [-38, 2, -5], [18, -8, 4], [6, -26, 24], [-14, 28, 10], [2, 26, 28], [16, -24, -2], [26, 8, 2], [-12, 8, -2], [-40, 6, -16], [-8, 26, 7], [22, -32, -11], [18, 28, 6], [-18, -6, -11], [-10, -6, -12], [2, 18, 4], [4, -12, 11], [1, 17, -17], [-46, 28, 20], [16, -14, 19], [54, -14, 4], [-17, -29, 17], [40, 2, 9], [56, -18, -2], [48, -52, -19], [24, -20, -17], [30, 12, 22], [-32, 20, 26], [32, -6, 13], [-28, -4, 12], [24, 18, 44], [-30, 0, -13], [30, -36, -16], [6, -10, -24], [14, -26, 16], [33, -5, 19], [10, -40, -25], [-42, 36, 16], [-36, 26, -8], [24, 14, 32], [40, -28, -3], [-16, 10, -36], [-16, 12, -30], [-32, 28, 14], [0, -34, 23], [-12, 26, -18], [30, -38, -28], [-30, -22, 10], [-14, 16, 30], [30, 20, -12], [12, -24, -16], [38, 24, 13], [4, 20, 34], [-40, 18, 8], [42, 26, 2], [-22, -20, -2], [30, -8, -11], [-2, 14, -13], [-6, 4, -2], [-30, -4, 20], [6, -34, -13], [-12, 6, 13], [18, -48, -11], [22, 30, -9], [-18, 36, 12], [8, 0, -25], [-38, -8, -41], [28, 8, 0], [8, -18, 18], [-18, -6, 37], [-29, 39, 25], [-10, -30, -38], [-46, -24, -14], [4, -10, 24], [-20, -12, -4], [-2, -6, 16], [2, 4, 24], [11, 3, -23], [-4, -12, 11], [-40, -6, -16], [-11, -11, -37], [38, -18, -20], [41, -37, -13], [2, -18, 9], [-45, -13, -47], [-6, 8, -36], [-32, 40, 16], [18, -14, 34]];
    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_n();".
// 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_n(: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_n();".
// 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_n( : 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, 8, -6, 1]>,<7,R![16, -8, -4, 1]>,<11,R![20, -4, -4, 1]>,<13,R![-2, 1]>],Snew);
    return Vf;
end function;