
// Make newform 4016.2.a.c in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4016_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_4016_2_a_c();".
// 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_4016_2_a_c();".
// 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, 1, -3, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [-1, -1, 1, 0], [1, -2, -1, 1]];
    Rf_basisdens := [1, 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_4016_a();"
function MakeCharacter_4016_a()
    N := 4016;
    order := 1;
    char_gens := [2511, 3013, 257];
    v := [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_4016_a_Hecke(Kf)
    return MakeCharacter_4016_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, 0], [0, 0, 1, 0], [-1, 1, 1, -1], [1, 1, -1, 0], [1, 1, 0, -1], [-3, 0, -1, 1], [1, -3, -1, 1], [2, -3, 1, 1], [0, -2, -2, 1], [-1, -2, -4, 1], [2, 2, -3, -1], [-5, 3, 2, 0], [0, 1, -2, 2], [3, -7, -1, 1], [-4, -2, 1, 2], [3, 1, 0, -4], [-5, 0, 2, 5], [-6, 1, 0, 1], [-8, 5, 5, 0], [2, -6, 1, 3], [3, -4, -1, -4], [6, 7, 3, -8], [-1, 9, 2, -4], [-2, 1, 0, 6], [5, 2, -2, -6], [-7, 2, 12, -1], [3, 1, -3, -2], [1, 2, -2, 4], [-8, 4, -2, -7], [1, 7, -1, -4], [0, 0, -3, 8], [0, 8, -2, -11], [-8, 2, 2, 11], [-2, -2, 2, 3], [2, -13, 2, 7], [5, 9, 3, -10], [-1, 4, 11, -4], [-5, -6, 6, 9], [-1, -7, -4, 1], [-4, 7, -3, -2], [-5, -4, -1, 3], [-4, -9, 3, 3], [1, -2, -8, 7], [-6, 4, 5, 9], [-3, -7, -2, 11], [-13, 2, 1, 11], [-3, -5, 4, 9], [-12, 1, 6, -5], [-9, 4, 8, -6], [-8, -4, 10, -3], [0, -14, 7, 7], [5, 6, -4, -2], [-3, -2, -1, 15], [1, 0, 0, 0], [2, 3, -3, 15], [-8, -1, 7, -10], [-16, 4, 9, -5], [7, -3, -8, -9], [-5, 3, 11, -12], [5, 7, -10, -13], [3, -5, -1, -9], [-5, 5, -5, -2], [5, 14, -1, -17], [6, -7, 3, -7], [-1, -5, -6, 13], [-4, 16, -1, -14], [7, -8, -1, 13], [5, -5, -6, -6], [-13, 3, 2, 5], [-11, -13, -14, 12], [11, -9, 2, -16], [6, -6, 6, 7], [-17, 10, 22, -2], [-2, -12, -3, 3], [-9, 0, -4, 21], [5, -12, -13, 6], [-17, 8, 12, -3], [-1, 7, 11, -6], [11, -3, -7, 3], [3, 21, -1, -10], [9, -15, -7, 4], [-17, 3, 1, -6], [-12, 13, 3, 13], [3, 5, 14, -17], [6, -3, -6, -12], [21, 19, 12, -26], [14, 9, 7, -22], [4, 1, 3, 5], [3, -1, -8, -6], [-8, 3, 2, -10], [27, -13, -13, -9], [8, -6, -13, -12], [-3, -2, 13, -8], [8, 6, -10, 11], [7, -10, 11, 3], [-8, 12, 5, 7], [23, -14, -6, -1], [27, -8, -4, -19], [-16, -13, 3, 18], [7, -9, -22, -1], [0, 7, -3, -24], [-11, -7, 14, 0], [-1, -5, 2, 13], [5, 20, 3, -10], [19, -15, -29, 5], [-7, -4, -1, 2], [5, -4, -17, 16], [21, -2, -6, 0], [-7, -17, 12, -2], [-20, 16, 7, 11], [-13, -7, -3, 26], [7, -3, -15, -18], [14, -5, 5, 4], [-25, 19, 13, -3], [9, -8, 0, -5], [10, 10, -5, -13], [-2, 6, -18, -14], [27, -4, -7, -4], [3, 24, -3, -17], [1, 12, -9, -18], [-18, -29, -9, 30], [-19, 0, 1, 21], [-14, -5, -11, 0], [12, 25, 5, -25], [-30, -4, 2, 19], [-18, 12, 2, -8], [-18, -3, 3, 0], [3, 10, 22, -11], [0, 9, 9, 3], [-13, 6, -6, 0], [-11, 2, 4, -17], [-13, -9, 13, 14], [30, 0, -2, -26], [-30, -3, -11, 7], [13, 17, 0, -22], [-8, 12, -2, 6], [7, 15, 15, -3], [18, 5, -4, 4], [-11, -17, -1, 18], [-14, 14, 8, 0], [26, -9, -4, -14], [-22, 0, 23, -4], [-6, 3, -11, 6], [32, -9, -4, -23], [16, -15, -32, 8], [-23, -9, -9, 18], [28, -4, -3, -3], [7, -3, 9, 17], [6, -6, 5, 20], [-23, 12, 5, 18], [-4, -11, 2, -5], [18, -4, -15, -2], [-19, -3, -4, -7], [10, -2, -7, -20], [30, 2, -10, -11], [-20, 15, 28, -8], [-25, -5, 14, 8], [8, -20, 12, 4], [20, 9, -9, -2], [41, -10, -17, 3], [-3, -22, 6, 25], [-19, -1, 2, 18], [41, -7, -8, -8], [-16, 1, -1, -17], [9, 5, -2, -30], [-1, -3, 15, 20], [-28, 13, 2, 16], [5, 12, -8, 2], [-9, 16, 23, -2], [21, 6, 11, -19], [1, 8, 28, 5], [9, 2, -11, -5], [-8, -6, -12, 17], [4, -26, -28, 16], [-13, 12, 26, 6], [38, 11, -13, -2], [22, -1, -7, 2], [-12, -5, 18, 34], [11, 26, -3, -5], [-17, -10, 4, 8], [29, 1, 5, -15], [-19, 7, 2, 7], [-9, -4, 18, 32], [-29, -14, -5, 25], [7, 10, -7, -31], [8, 19, -6, -7], [-11, -29, -5, 10], [-23, 5, -7, -3], [-7, -6, 12, -3], [38, 0, -5, 5], [27, 20, -10, -9], [19, -19, -26, 0], [28, 15, -25, 8], [-22, -7, -3, 17], [16, 12, 21, -24], [1, -15, -6, 23], [-30, -30, 0, 20], [-20, -17, -24, 18], [31, -2, -21, -4], [-4, 11, -7, 8], [-27, -10, -2, 26], [-20, 23, 24, -8], [1, 26, -2, -19], [19, 1, -16, -38], [0, 7, 26, -11], [10, 1, 12, -30], [-12, -15, -22, 35], [-42, 4, 3, 16], [-2, -20, -2, -8], [32, -8, -24, 10], [8, 9, -10, -16], [-25, 25, -7, -16], [7, 3, 3, -24], [28, 19, 10, -8], [-4, 5, -6, -7], [-6, -17, 1, -2], [52, -17, -26, 0], [-32, -17, -15, 28], [-13, -1, 5, 5], [2, 3, 6, 8], [-19, 16, -6, -5], [1, -7, 19, -30], [-30, 18, 24, 16], [18, 37, 4, -33], [-1, -6, -9, 33], [-8, 29, -5, 14], [16, 6, 11, -5], [-6, 1, 27, -34], [14, -10, 1, 31], [-39, -3, -5, -2], [15, -14, -24, -1], [10, 17, 1, -25], [9, 3, 16, 7], [-6, 34, 13, -8], [-9, -15, -4, -16], [38, 12, 3, -11], [15, 30, -14, -14], [38, -9, -21, -8], [12, 7, -1, -25], [5, 32, 14, -32], [-24, -4, -21, -11], [23, -20, -45, 11], [10, 10, -22, -41], [-2, 21, 3, 16], [-6, 23, -20, -14], [-14, -25, 4, 11], [10, -32, -2, 23], [-11, -2, -3, 6], [-27, 22, 10, -12], [4, 14, 2, 9], [1, -21, 16, 26], [-11, 18, 5, 15], [-32, 12, 5, 5], [-8, 22, -12, -2], [-9, 19, -4, -38], [2, 22, -14, -12], [12, 0, -29, -10], [-16, 6, 19, 6], [-29, -24, 17, 7], [-11, -32, 7, 6], [-23, -6, 10, 2], [17, 10, -6, 7], [-36, -4, 4, 4], [-1, -5, 10, 7], [9, 13, 23, -24], [-6, 15, 1, -26], [-38, -15, 9, 8], [19, -17, -4, -20], [-31, 1, -5, 0], [-14, -1, 14, -22], [-45, 10, 23, -3], [0, -3, -15, -6], [31, 6, 11, -11], [-5, -1, 13, -4], [21, -6, 4, -13], [-14, -31, -7, 35], [-35, 27, 26, 16], [-26, -12, 13, 15], [-54, 20, 3, -2], [-24, 2, -6, 15], [22, 17, -22, 8], [-46, 10, 19, 0], [-42, -23, -3, 28], [-7, 30, 5, 1], [59, -42, -17, -1], [34, -38, -22, -4], [5, 21, 10, -13], [8, 10, -25, -2], [-23, -28, -7, 28], [32, -26, -47, 12], [-6, -57, -5, 30], [37, -19, -3, -14], [13, 12, -36, 2], [4, 3, 12, -35], [30, -9, -19, -23], [-57, 22, -2, -5], [-46, 30, 17, 4], [6, -6, -35, 2], [40, 12, -6, 11], [-29, -19, -20, 41], [33, -41, -13, 3], [37, -13, -29, -31], [-23, -20, 20, -22], [42, 12, 11, 0], [-24, 8, -18, -33], [27, 7, 8, -25], [-28, -12, -2, 27], [24, 21, -4, -54], [-49, 11, 2, 12], [6, 13, 1, 18], [15, -20, 15, 26], [-15, -4, 1, 25], [-9, -8, 36, 2], [20, 11, 20, -33], [33, 0, -7, -17], [-12, 39, 15, -32], [-56, -9, -10, 15], [-31, 2, 8, 15], [52, -37, -29, 2], [-11, -57, 4, 33], [-13, -6, 9, 13], [22, 0, -27, 34], [40, 21, -12, -25], [-48, -33, -14, 27], [-21, -9, 20, -13], [1, -27, -10, 6], [2, -7, -33, 10], [-36, 0, 0, 46], [-21, -17, -14, 3], [58, -30, -14, -3], [9, 28, -8, -10], [-11, -6, -24, 25], [17, 5, -8, -9], [18, 5, 3, -27], [35, -30, -28, 20], [22, 5, 2, -21], [-3, -35, 4, 31], [-13, 16, 17, -37], [25, 8, 16, 0], [-55, 7, 21, -5], [-5, 22, 9, -39], [16, 26, 17, -41], [-26, 1, 14, 20], [31, -5, -21, 18], [43, 12, -21, 25], [16, 37, -17, -11], [-26, 33, 26, -25], [-26, 24, 17, -16], [38, -42, -18, -7], [5, 19, -20, 9], [-54, 36, 5, 6], [-29, -6, 35, 16], [-21, -43, 6, 49], [-31, -18, -25, 39], [16, 28, -12, -14], [-44, 8, -4, -5], [34, -2, -4, -45], [34, -11, -7, 7], [17, 38, 15, -13], [-36, 9, 5, 5], [22, -9, -12, 35], [24, 30, 5, -19], [26, 11, -6, 27], [-8, 12, 47, -26], [-4, -10, -5, 38], [-42, 11, 43, -4], [-13, -21, -11, 43], [8, 17, -14, 7], [-66, 14, 18, 10], [-26, 18, -4, 11], [29, -51, -6, 2], [54, 2, -42, 19], [8, 13, -14, 29], [-16, 21, 4, 6], [18, 17, 18, -31], [43, -15, -11, 11], [11, 18, 28, -6], [-37, 19, 4, -20], [-36, 20, -10, 23], [-41, -47, -8, 33], [-31, -9, -4, -9], [-57, 41, 30, 11], [6, -18, -20, 10], [26, -30, -8, 0], [-1, -9, -5, -3], [14, -3, -33, -16], [45, -11, 5, 23], [-30, -8, -19, 47], [27, 4, 9, -17], [20, 4, -20, -10], [46, -11, -10, -10], [-21, -11, 8, 28], [1, -28, -18, -5], [3, -35, 13, 0], [-33, -14, 27, -1], [-4, -18, -16, -10], [-11, -6, -22, 10], [-42, -26, -10, 45], [-16, 5, -6, 65], [9, -16, -8, -35], [-35, 5, 9, -27], [-49, -9, 7, -14], [15, 13, -22, -4], [-28, -13, 24, -25], [13, 5, 2, 7], [54, 31, 18, -58], [-21, 26, 40, -22], [70, 22, 17, -31], [-24, -19, 1, 7], [4, -39, -19, 16], [-31, -16, 8, -11], [23, -31, -4, 10], [-13, -10, 11, 37], [-25, -39, -4, 13], [-37, 21, 10, 3], [73, -8, -4, 14], [-2, 40, 15, -46], [-28, 12, 22, -10], [17, -22, 12, 50], [10, 4, 10, 21], [57, -22, 7, -2], [20, -14, 1, 34], [12, 22, -11, -40], [-31, 2, -28, -9], [-49, 16, 46, -12], [18, -4, 22, 27], [-26, -8, 32, 16], [-14, 44, 7, -16], [-41, -33, -11, 69], [-71, -18, -13, 37]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4016_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4016_2_a_c();".
// 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_4016_2_a_c(:prec:=4)
    chi := MakeCharacter_4016_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_4016_2_a_c();".
// 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_4016_2_a_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4016_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![1, 3, -2, -2, 1]>],Snew);
    return Vf;
end function;