
// Make newform 4020.2.a.b in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4020_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_4020_2_a_b();".
// 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_4020_2_a_b();".
// 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 := [3, 1, -5, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [-3, -1, 1, 0], [0, -4, -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_4020_a();"
function MakeCharacter_4020_a()
    N := 4020;
    order := 1;
    char_gens := [2011, 2681, 3217, 1141];
    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_4020_a_Hecke(Kf)
    return MakeCharacter_4020_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], [-1, 0, 0, 0], [-1, 0, 0, 0], [0, 1, 1, 0], [1, 1, 0, 0], [-1, -1, -1, -1], [0, 0, 1, 1], [-1, -1, 0, 1], [1, -2, -2, 0], [-1, 1, 1, -2], [-4, -1, -2, -1], [1, 0, 0, 2], [-2, -3, -2, 3], [4, -1, -1, 1], [1, -1, 1, -3], [1, 3, -2, -1], [3, -3, 3, 1], [-3, -1, 4, -3], [1, 0, 0, 0], [-3, -4, 1, -6], [0, 4, -3, 1], [0, 3, -1, 3], [5, 0, -1, 2], [-7, 2, -1, 0], [-6, -3, -2, 0], [-7, 2, -2, 4], [-4, 1, 1, -1], [1, 4, -6, 3], [-3, -4, 3, 0], [4, -1, 7, -2], [-8, -2, 0, -3], [-2, 8, 1, 3], [0, -5, 8, -2], [-4, 3, 1, -2], [-1, 5, 2, -3], [-14, 4, -3, -4], [-5, -3, -1, 4], [-2, -8, 0, 3], [3, 2, -2, 5], [-5, -1, 4, -5], [4, -6, -1, 2], [-11, -1, -7, 4], [2, 0, -3, 7], [-2, 3, 0, -2], [3, 7, 2, 0], [-12, -5, -1, 0], [-3, 0, 7, -2], [-10, -3, 4, 1], [2, 3, 1, -2], [-8, -1, 4, 0], [0, 5, -5, 11], [3, -1, -10, 5], [-16, -3, 0, -5], [-1, -2, 8, -9], [-13, 2, -2, -2], [-4, 1, 0, -5], [-4, -5, -2, 1], [-5, 8, -2, 6], [-3, -6, -8, -3], [-14, 6, -1, 6], [7, -2, 4, 5], [-6, -9, -2, -1], [-8, 11, -1, -5], [12, 1, 5, -5], [3, -5, 0, 3], [2, 13, 1, -7], [-3, 0, -2, 0], [-7, 1, -5, -2], [13, 0, 6, 4], [-5, 8, -3, 0], [-15, 4, -8, -5], [6, -6, -3, 6], [-1, -4, 7, -6], [-3, -6, 4, 3], [8, 2, 9, -3], [10, -3, 6, -3], [11, -11, 4, 2], [-10, 13, 3, 5], [0, -3, -5, 1], [-3, -12, -2, 6], [9, -2, -1, -2], [-2, 2, -4, 13], [-4, -1, -8, 14], [10, 0, 13, -4], [-8, 1, 9, -6], [12, 0, -7, -2], [18, 0, -4, 3], [0, 6, 14, -3], [-15, 9, -8, -2], [-7, 13, -2, 1], [-6, -5, 7, -8], [-3, 11, 4, -2], [-12, 2, 3, -2], [1, 14, 4, -7], [-21, -1, -9, -4], [13, -6, 5, 5], [-6, -2, -7, -8], [-17, -2, -1, -13], [13, 10, 5, 1], [-2, -6, 15, -11], [9, -9, -12, 4], [7, 2, 12, -2], [10, -12, -9, -2], [3, -16, -2, -1], [19, -2, 1, 9], [5, -3, 6, 0], [3, 1, 2, 2], [9, -11, 5, 3], [17, -5, 7, 1], [-7, 6, -7, 17], [-7, 7, 3, 7], [-1, 10, -5, 7], [13, 5, 2, 6], [21, 6, -7, 9], [-6, 4, -2, -11], [-21, -4, -1, -5], [9, -8, 5, -2], [0, 8, -12, 4], [0, -11, -10, 5], [9, 0, 8, 1], [22, 5, -1, -4], [25, 5, 3, -8], [-21, 7, 6, 4], [13, 1, 7, 2], [-4, -13, -6, -2], [-6, -9, -11, -2], [-2, 0, 2, 9], [14, -17, -4, -2], [1, 7, -8, 4], [10, 8, -5, 6], [3, -15, 8, 4], [21, 3, 2, 8], [-24, -5, -2, -15], [-12, -15, 8, -18], [-11, 8, 8, -4], [-7, -9, 3, -14], [-14, -1, -8, 2], [-10, 0, -9, 2], [-4, -6, -8, 17], [8, 4, -1, 8], [-12, 2, 2, -18], [0, -2, 14, -6], [12, -3, 7, -10], [24, 5, -3, -3], [-5, 9, -6, 14], [20, 10, 2, 1], [0, -8, 4, -12], [-3, 9, 4, 1], [-14, 0, -12, 8], [3, -5, 12, -2], [19, -9, 8, -6], [-21, -1, 7, -3], [-2, -17, 6, -19], [-6, -6, 3, -13], [14, 4, -8, 14], [1, -8, 8, 2], [-8, 15, -4, 14], [12, 19, -10, 3], [5, 3, -12, 7], [10, 6, -13, -1], [10, -14, -3, -8], [3, 7, 18, -12], [-14, 9, -1, -12], [18, 12, -3, 7], [12, 4, 9, -6], [-5, 15, 1, -1], [-10, 21, 3, 7], [24, 10, 9, -5], [4, -11, 5, 8], [3, -2, 5, 1], [0, -1, 3, -3], [35, 6, 4, -8], [19, -10, 10, -13], [-6, 2, 3, -1], [11, -7, 0, 6], [12, -19, -1, -3], [-5, -14, -13, 12], [-10, -12, -7, -3], [19, -5, -7, 9], [5, 13, 16, -9], [10, 4, 10, 5], [-12, -9, 4, -6], [20, -13, -17, 1], [-17, -10, -11, -10], [7, -8, -13, 12], [-22, 14, 7, 2], [-20, 11, 3, -7], [1, 3, 2, -7], [-6, -9, 13, -10], [-18, 5, -5, -2], [15, -13, -8, 6], [17, -21, -7, 0], [10, 9, -13, 16], [1, -8, -14, 14], [-2, 12, -20, 13], [6, 18, 3, 1], [1, 3, 6, 6], [4, 1, -13, 17], [22, 6, 1, -1], [-23, -4, -8, 10], [15, -8, -6, -2], [-16, -12, 12, -3], [-5, 24, -5, 0], [9, 3, 24, -6], [27, 12, 5, -2], [-6, -12, 6, 6], [-32, 0, 6, 0], [10, 9, 13, 1], [3, -1, -2, -4], [-7, 21, 6, -6], [-16, 19, 9, 11], [18, 2, -12, 9], [2, -1, 6, 1], [-35, -5, -15, -7], [7, 2, 0, 2], [-1, 1, 28, -8], [2, 8, 25, -2], [-29, -12, -3, -1], [1, 3, -7, 10], [16, -16, -11, -4], [-5, -2, -21, 13], [4, 27, -5, -8], [0, 8, 6, -2], [7, -21, -4, 0], [14, -9, 18, -2], [-24, 5, 5, -5], [-24, -3, -8, 13], [3, 8, 12, -20], [-37, 8, -9, -19], [0, -11, 5, 4], [14, -10, 4, -11], [-8, 4, -11, 14], [-13, 9, -4, 8], [-16, 0, 6, -3], [31, 7, 6, 6], [-6, 18, 1, -2], [13, 15, -6, 11], [-29, 9, -14, -13], [32, -2, 23, 4], [-35, -6, -20, -2], [9, 14, -13, 8], [21, 1, 19, -22], [4, 9, -13, 21], [-16, 7, 2, 8], [-9, 0, 1, 15], [-1, 20, 16, -2], [-48, 2, -10, -2], [9, 13, -2, 6], [-1, -16, 1, -4], [-30, 14, 9, -4], [10, -9, -7, 10], [-54, 4, 7, -4], [-23, -4, 14, 0], [-14, -2, -9, 6], [-4, -18, 7, 3], [12, -5, -2, 0], [2, -12, 8, -8], [-34, 14, -8, 7], [-9, -18, 4, -31], [37, 3, 9, 5], [-11, -6, 24, -7], [-15, 10, 5, 8], [-32, 6, -4, -19], [-2, -20, 0, -25], [-13, -22, -6, 3], [-14, 20, 12, -3], [7, 17, -4, 7], [3, -9, 5, -2], [5, 11, 9, -11], [11, 6, 7, -1], [20, -11, -3, 2], [25, -2, -2, -13], [32, -4, 4, -15], [-3, -3, 5, -3], [-4, -1, 18, -6], [2, -3, -14, -9], [-45, 20, 1, -7], [19, -20, 8, -5], [7, -21, 9, -7], [-2, 28, 6, 7], [-51, 15, 6, -6], [13, -32, -10, 3], [-8, -7, -14, 25], [-1, 22, -1, 6], [-4, -6, 19, -7], [-36, -1, -20, 4], [-21, 0, -27, -1], [2, 8, -26, 20], [20, 11, 14, -9], [26, 5, 15, -1], [-18, 28, 0, -5], [-9, -19, 7, -10], [-36, -1, 6, 2], [-18, 11, 3, 13], [9, 21, -15, 18], [30, 1, 10, -7], [-31, -5, 16, -6], [-24, -7, -18, 4], [-4, -12, 20, 0], [34, -28, 5, 9], [-7, -8, -9, 13], [-10, -12, 10, -11], [-6, 9, -22, 1], [-14, -7, -3, -7], [-7, -8, -12, -6], [23, -7, 20, -5], [-44, 9, -1, 3], [2, -18, 12, 8], [9, -2, 13, -22], [21, 8, -3, 10], [-18, -9, 11, -12], [17, 17, 1, -8], [-41, 8, -19, -12], [13, 5, 34, -16], [-17, 24, 6, 4], [-22, 20, -14, -7], [6, 8, -5, -8], [30, 5, -10, 3], [37, 9, 12, 13], [23, -7, 17, 14], [0, 5, 5, -19], [31, -6, 11, -6], [3, -15, 11, 11], [-13, 20, -12, -12], [-21, 25, 2, -1], [4, -2, -19, 5], [-9, -16, -19, -11], [25, 6, 5, 20], [-12, 32, 4, 17], [-36, -8, -13, -7], [-19, 12, -15, 2], [-15, -13, -24, -8], [-49, 0, -6, 8], [-3, -7, 19, -1], [28, 15, 3, 23], [-3, 15, 17, 5], [39, 1, 21, 0], [-36, 12, -5, -13], [43, -39, -12, -1], [-9, -10, -21, -8], [10, -31, 10, -18], [7, -18, 26, -9], [-6, -12, 9, -19], [47, -6, 0, 1], [-40, 15, 12, -7], [-20, 2, -28, 20], [11, -1, 3, -21], [-9, 9, 12, -7], [36, 9, 21, -18], [-3, 10, 3, -9], [-11, 0, -17, 15], [26, 10, -9, 22], [3, -28, 13, 2], [-47, 18, -4, 6], [-10, 2, -25, 7], [15, 24, -7, 8], [31, 12, -20, 15], [19, 12, 15, 8], [73, 5, -1, 1], [9, -5, 8, 21], [13, -31, 4, -12], [-78, 3, -9, -9], [-10, 0, 3, 2], [7, -5, 2, 18], [-4, 20, 8, 18], [-28, -19, -7, 11], [-36, 1, -13, 11], [4, 10, 17, -11], [40, -13, 9, 0], [7, 15, 24, 2], [11, 18, 18, -15], [9, 1, 11, 16], [16, 15, -2, 26], [-23, -12, -31, -2], [10, -2, -17, 29], [-4, 1, 9, -30], [22, -13, 11, -12], [20, -12, -25, 15], [-37, 37, 0, -12], [13, -7, -28, 3], [-27, 0, -13, 1], [-33, 13, 1, -12], [30, -29, -17, -6], [34, -3, -9, -6], [24, -10, 1, -22], [-5, -3, 0, -19], [-5, -5, -22, -9], [-5, -5, -36, 11], [29, -8, 19, 4], [-38, 21, 9, -9], [23, -3, 5, 22], [-46, -11, -6, 5], [1, 4, -9, 34], [-13, -14, -3, -9], [4, -1, -4, -3], [40, 7, -2, -13], [7, -33, 18, -4], [35, 0, -13, -5], [13, -21, 5, -10], [-10, 13, 11, 1], [-20, -2, -6, 10], [-24, 3, -2, 16], [2, 21, -18, 32], [-2, 9, -2, -14], [-51, -10, -2, -22], [31, -11, -10, -11], [19, -8, 26, -11], [-7, -19, -5, 15], [-30, -7, -22, 33], [39, -42, -14, -13], [-8, 26, 10, -27], [0, -20, -7, 8], [15, 13, -26, 26], [-26, -24, -9, 14], [65, -14, 16, 10], [36, 11, 3, 19], [-16, 0, -7, 23], [-5, -28, -23, 18], [17, 1, -21, 25], [-14, 22, -16, 15], [-22, -13, -3, -30], [1, -14, -3, -27], [5, -2, 1, 6], [22, -3, -9, -9], [45, 21, 5, -9], [36, -3, 6, 26], [-33, 10, -30, 4], [22, -21, 15, -11], [20, -27, 14, 2], [42, 7, 11, 22]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4020_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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