
// Make newform 4018.2.a.f in Magma, downloaded from the LMFDB on 18 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_f();".
// 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_f();".
// 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], [0], [-2], [3], [4], [-6], [-3], [1], [-4], [-1], [-1], [6], [9], [6], [1], [8], [3], [10], [-1], [12], [-3], [1], [6], [-11], [3], [2], [9], [14], [-18], [-6], [10], [-3], [17], [4], [20], [0], [-21], [0], [-8], [3], [-16], [24], [16], [-4], [1], [15], [16], [-24], [-24], [28], [12], [27], [24], [6], [16], [8], [-18], [-8], [24], [-8], [-6], [-14], [30], [2], [-19], [0], [-26], [-6], [36], [7], [32], [-7], [24], [-36], [22], [3], [28], [30], [11], [12], [-14], [28], [15], [15], [-34], [15], [-16], [24], [-6], [2], [-3], [-34], [-30], [6], [-18], [-38], [-28], [44], [-9], [-12], [-21], [-10], [-2], [33], [39], [30], [-29], [25], [-16], [-18], [4], [-34], [18], [-29], [45], [21], [42], [10], [8], [6], [24], [-5], [42], [-19], [6], [-44], [25], [-37], [42], [-40], [-43], [-6], [-2], [-18], [4], [-51], [0], [-2], [-24], [23], [30], [-41], [12], [19], [-42], [-14], [-30], [32], [12], [8], [-30], [-13], [0], [5], [-6], [-47], [6], [36], [6], [-49], [-45], [-42], [33], [-40], [58], [41], [18], [30], [31], [-48], [11], [-14], [39], [-40], [12], [-38], [-14], [-4], [-6], [2], [24], [42], [-45], [-16], [16], [62], [-6], [64], [6], [20], [-3], [57], [-42], [-28], [-46], [-9], [-54], [-60], [16], [4], [46], [27], [-30], [-11], [-60], [30], [-14], [20], [-30], [-10], [-42], [-30], [-32], [-13], [-36], [-63], [-30], [-43], [-11], [-48], [56], [0], [-19], [30], [-72], [-2], [12], [-64], [-29], [-16], [-33], [16], [72], [25], [6], [-63], [-18], [6], [-71], [10], [74], [0], [15], [-8], [33], [5], [36], [26], [-12], [6], [46], [0], [-63], [11], [-44], [54], [-65], [26], [36], [-56], [-32], [78], [-68], [-27], [54], [-4], [-54], [-8], [-61], [40], [14], [-41], [16], [-18], [-46], [20], [-60], [-30], [17], [-15], [22], [49], [-75], [-22], [-48], [10], [27], [63], [66], [-36], [-60], [-70], [18], [-32], [54], [57], [-56], [40], [30], [56], [0], [-13], [-55], [-12], [-50], [9], [89], [-24], [45], [3], [5], [54], [88], [42], [72], [19], [-54], [-20], [-46], [54], [35], [51], [-74], [38], [13], [69], [54], [23], [18], [-5], [15], [35], [-84], [29], [-24], [31], [4], [-7], [27], [-6], [71], [24], [-39], [-38], [-28], [42], [90], [-23], [11], [-57], [64], [29], [90], [-27], [-24], [-30], [0], [62], [-27], [12], [39], [-65], [26], [60], [86], [83], [-42], [-41], [30], [-30], [40], [59], [30], [-6], [-2], [-54], [-17], [0], [-45], [20], [-57], [13], [81], [-43], [94], [-67], [-6], [14], [54], [30], [-26], [6], [-94], [88], [36], [-55], [39], [31], [-84], [68], [42], [-3], [-38], [-82], [78], [-29], [24], [46], [-60], [24], [-10], [-4], [-57], [-36], [-50], [-69], [-27], [-36], [-68], [90], [-12], [-74], [-18], [-96], [30], [-92], [66]];
    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_f();".
// 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_f(: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_f();".
// 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_f( : 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![0, 1]>],Snew);
    return Vf;
end function;