
// Make newform 4018.2.a.q in Magma, downloaded from the LMFDB on 25 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_q();".
// 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_q();".
// 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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
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], [1], [3], [0], [4], [-2], [7], [-4], [-4], [9], [-1], [8], [1], [-3], [8], [-11], [6], [9], [-2], [-7], [4], [-15], [-4], [1], [5], [-14], [11], [-3], [18], [-3], [-8], [14], [-16], [-22], [-15], [19], [4], [4], [24], [-5], [-6], [8], [-15], [-14], [-8], [-8], [-16], [-9], [13], [22], [0], [-24], [0], [10], [-21], [-8], [-10], [-20], [-18], [6], [-10], [-18], [-8], [-30], [26], [22], [10], [-19], [16], [-18], [8], [22], [37], [24], [15], [20], [-34], [10], [27], [20], [26], [-29], [-12], [-16], [-14], [-31], [15], [8], [27], [16], [-22], [-8], [-30], [23], [14], [-36], [0], [38], [-36], [22], [2], [3], [-44], [7], [4], [30], [7], [15], [22], [-41], [7], [26], [6], [2], [-8], [-8], [13], [-13], [29], [-28], [-22], [0], [-26], [-24], [-7], [0], [-35], [-26], [-30], [-7], [-19], [-48], [-24], [5], [28], [34], [-20], [-28], [-3], [24], [-42], [-38], [25], [-22], [-49], [14], [39], [22], [14], [14], [8], [-54], [-14], [32], [-35], [34], [59], [-38], [9], [22], [0], [-50], [9], [-15], [-42], [-9], [-16], [20], [5], [28], [16], [-37], [-36], [47], [42], [59], [-28], [-36], [-2], [40], [4], [48], [2], [0], [-16], [-33], [34], [2], [-28], [60], [-4], [46], [-16], [13], [-21], [14], [46], [26], [63], [-48], [24], [-16], [30], [26], [-3], [0], [47], [50], [-34], [58], [-2], [-60], [8], [-52], [30], [64], [17], [72], [27], [-6], [5], [-41], [12], [32], [56], [53], [18], [-24], [22], [-56], [-40], [-23], [26], [11], [68], [38], [29], [-2], [-69], [-4], [14], [43], [-56], [-2], [-60], [-11], [40], [43], [27], [64], [-64], [-10], [-2], [-26], [-22], [-1], [27], [-44], [-6], [-13], [46], [36], [-20], [-68], [54], [-50], [-19], [0], [44], [54], [-10], [-63], [50], [-78], [19], [-48], [64], [66], [60], [-80], [78], [-17], [51], [70], [15], [63], [-20], [-30], [-36], [15], [-25], [-48], [50], [-42], [2], [46], [-28], [-30], [-81], [-28], [-64], [46], [46], [-6], [-15], [13], [76], [30], [63], [5], [-18], [-7], [39], [59], [-12], [18], [58], [-60], [-9], [70], [50], [-62], [-34], [65], [75], [2], [90], [-85], [-57], [52], [7], [8], [-59], [49], [-23], [52], [77], [66], [31], [-16], [-7], [-33], [-90], [-19], [70], [67], [34], [36], [-50], [-42], [23], [39], [-45], [-32], [1], [90], [-9], [24], [-50], [-42], [-60], [57], [-32], [65], [33], [-68], [62], [-34], [31], [66], [-7], [74], [22], [-32], [11], [4], [40], [-2], [74], [-21], [36], [-73], [64], [-13], [87], [47], [35], [-52], [31], [-62], [52], [24], [52], [78], [32], [100], [-52], [64], [-65], [-13], [27], [-34], [76], [10], [-39], [0], [-104], [52], [77], [16], [-26], [-54], [-54], [42], [22], [-69], [-46], [10], [75], [-41], [42], [32], [-78], [68], [-14], [18], [-92], [-50], [20], [-30]];
    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_q();".
// 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_q(:prec:=1)
    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_q();".
// 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_q( : 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![-3, 1]>,<11,R![-4, 1]>],Snew);
    return Vf;
end function;