
// Make newform 4018.2.a.t in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4018_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_4018_2_a_t();".
// 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_4018_2_a_t();".
// 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, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-1, 2]];
    Rf_basisdens := [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_4018_a();"
function MakeCharacter_4018_a()
    N := 4018;
    order := 1;
    char_gens := [493, 785];
    v := [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_4018_a_Hecke(Kf)
    return MakeCharacter_4018_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 := [[-1, 0], [-1, 0], [0, 1], [0, 0], [0, 2], [-2, -2], [-3, 0], [4, 0], [4, -2], [4, 1], [-4, 1], [-4, -2], [1, 0], [9, 0], [4, -4], [0, -3], [-8, 2], [-8, 3], [2, -6], [0, 1], [-6, 0], [4, -3], [-6, 4], [3, 0], [-1, 0], [-4, 2], [-12, -3], [-7, 4], [0, -6], [5, 4], [-14, 2], [-18, 2], [12, 2], [0, -6], [8, 5], [8, -1], [6, -6], [8, 4], [6, 6], [-12, -3], [0, -2], [14, -4], [0, -3], [-20, -2], [0, -4], [2, 2], [0, 0], [4, -7], [-13, -4], [2, -8], [-6, 8], [-8, 4], [4, 2], [-6, 0], [-23, -4], [-12, 0], [-24, -2], [-4, 0], [-2, 2], [0, 6], [6, 6], [-16, -4], [4, 8], [-10, -4], [14, 8], [-12, 6], [2, -2], [17, -8], [-12, -8], [-8, 6], [4, 10], [-4, -10], [12, 3], [-10, 8], [-5, 12], [-6, -8], [12, 0], [-4, -12], [11, 8], [-10, -8], [-12, -8], [-8, 3], [-6, -6], [-4, 2], [-28, 0], [9, 4], [-5, -16], [-6, -6], [12, -11], [-24, 0], [-16, 8], [-18, -6], [12, 12], [3, -16], [-38, 0], [-4, 14], [26, -4], [-18, 4], [16, 10], [-8, 0], [0, 12], [0, 3], [4, -8], [-13, 4], [14, 0], [38, 4], [-19, 4], [9, 16], [12, -4], [-15, 12], [-24, -3], [24, -6], [-6, 0], [-20, -6], [0, 12], [12, 4], [15, -12], [-8, 1], [0, -15], [4, 10], [12, 6], [28, 6], [12, -6], [6, -18], [7, 16], [12, 2], [16, -3], [-6, 14], [-20, 2], [12, 3], [-19, -4], [26, -8], [0, -12], [16, -7], [6, 2], [-18, 0], [12, -4], [-14, 6], [-12, 11], [-24, -6], [0, 12], [28, 10], [24, 9], [14, 4], [32, 1], [-4, 8], [0, -3], [-8, 14], [8, 16], [42, 2], [-10, -10], [30, -4], [8, 4], [-12, -10], [25, 12], [6, -6], [-4, 15], [10, 8], [-1, -12], [-24, -10], [-48, 0], [-6, 0], [20, 9], [3, -20], [-4, -2], [4, -19], [-44, 0], [-2, 8], [-3, -12], [28, -2], [-30, -10], [40, -7], [-8, -4], [11, 12], [10, 18], [5, -8], [4, -8], [-6, 12], [-24, 12], [2, -14], [-8, 2], [42, -2], [-24, -18], [30, -4], [42, 2], [-28, 1], [-2, 2], [10, -20], [6, -12], [-6, 18], [-30, 6], [28, -10], [-28, 16], [44, -1], [-37, 8], [-48, -4], [-52, 2], [-4, 2], [-27, 4], [-4, 16], [0, 6], [8, 12], [4, 2], [54, 0], [-1, 4], [22, 20], [-64, -3], [8, -2], [-42, -8], [32, 4], [22, 6], [22, 10], [2, 16], [26, 14], [-6, -6], [18, -6], [-32, 1], [6, 0], [48, -1], [24, -16], [40, -3], [-8, 21], [30, -6], [-8, 12], [34, 14], [-8, 1], [-12, 18], [6, 12], [-32, -4], [16, -4], [10, 22], [31, 0], [-38, -2], [-17, 16], [60, 0], [10, -14], [47, 12], [-48, 2], [-45, 12], [-10, 10], [-12, -8], [-23, -12], [-16, -6], [22, 22], [-18, 6], [-12, 19], [-16, -12], [-15, -12], [-17, -8], [14, 16], [-62, 2], [20, -20], [-12, -16], [6, 6], [12, 0], [39, -12], [-8, -5], [0, 12], [-18, -2], [-63, 0], [44, 8], [-42, 0], [40, 6], [-46, -6], [38, -2], [-26, -14], [-20, 5], [10, 22], [-24, 0], [20, 2], [20, 18], [-7, 4], [-32, 6], [6, -6], [-19, -24], [-52, 0], [-6, 8], [44, -6], [-46, 12], [-48, 0], [-42, 14], [24, 15], [0, -23], [40, -6], [-55, 12], [-24, 15], [-2, -12], [-36, 16], [-30, 6], [-15, 0], [20, 11], [0, 22], [24, -26], [58, 2], [-48, 6], [6, 0], [-32, 6], [-4, -10], [1, 4], [28, -16], [-16, -20], [-72, 6], [0, 18], [22, 20], [-19, 4], [5, 0], [12, -16], [32, 22], [-24, -9], [-20, -3], [-20, -22], [-72, 5], [35, -8], [19, 28], [38, 8], [-16, 0], [42, 0], [0, 20], [29, 16], [2, -26], [26, 2], [-18, 12], [40, -10], [4, -7], [3, 20], [-50, -4], [-26, -6], [17, -8], [8, 11], [-18, 14], [-32, 15], [10, 16], [56, -5], [-81, 4], [33, 0], [-24, 0], [-80, -3], [-10, -32], [1, 20], [32, 12], [-40, -23], [-5, -16], [-10, -8], [24, -27], [14, 28], [-9, -20], [32, -14], [-48, 0], [12, 2], [48, 18], [49, 4], [35, 20], [48, 15], [0, 12], [32, 1], [-30, 8], [0, 25], [-28, -4], [18, 16], [30, 12], [-28, 4], [15, 12], [8, 4], [5, -8], [-13, -24], [68, 12], [64, -8], [-16, -34], [-5, 4], [-34, -20], [-49, -12], [-52, -4], [-20, -22], [-56, -4], [68, 3], [-18, -28], [-12, 28], [42, 12], [42, -20], [1, 16], [58, 4], [-25, -16], [-12, 30], [7, -16], [-23, 24], [-36, 17], [-64, -13], [30, -30], [-13, -16], [-8, 10], [6, 12], [42, 16], [30, -14], [36, 24], [60, 12], [-44, -18], [-30, -6], [-28, -10], [-21, 24], [-28, -17], [4, 9], [-78, -2], [66, -6], [66, -12], [-40, 11], [18, 30], [-22, 6], [6, -20], [19, 16], [-10, 8], [54, 12], [30, -16], [-28, 2], [68, -4], [-12, 12], [56, 5], [-62, -16], [40, -12], [9, 4], [-12, -3], [-24, 32], [22, 24], [-56, -2], [8, -40], [18, -6], [24, -14], [6, 18], [50, 2], [-80, -12], [36, 4]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4018_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4018_2_a_t();".
// 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_4018_2_a_t(:prec:=2)
    chi := MakeCharacter_4018_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_4018_2_a_t();".
// 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_4018_2_a_t( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4018_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![1, 1]>,<5,R![-5, 0, 1]>,<11,R![-20, 0, 1]>],Snew);
    return Vf;
end function;