
// Make newform 4018.2.a.g 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_g();".
// 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_g();".
// 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], [-1], [0], [0], [-2], [5], [4], [6], [-9], [-3], [-8], [1], [7], [10], [-13], [14], [5], [4], [-15], [6], [-3], [-16], [-13], [-17], [-18], [-7], [3], [-2], [1], [18], [18], [-18], [-14], [7], [-5], [0], [12], [-12], [-9], [-24], [0], [-15], [16], [0], [-4], [20], [-3], [-7], [-8], [-12], [0], [20], [8], [-3], [-24], [-10], [-16], [20], [-22], [12], [-24], [16], [-18], [6], [-22], [26], [-35], [24], [-2], [2], [0], [19], [-16], [-15], [8], [20], [2], [-5], [-32], [14], [-7], [8], [-6], [28], [39], [-33], [-18], [-21], [-24], [-32], [26], [26], [5], [-2], [10], [42], [-14], [-6], [12], [-36], [13], [-20], [27], [-26], [42], [15], [-15], [6], [37], [-3], [28], [-26], [4], [14], [38], [-19], [-39], [-9], [-18], [50], [-32], [6], [-12], [13], [-42], [-1], [30], [32], [-19], [19], [2], [-32], [15], [14], [42], [2], [40], [17], [32], [6], [52], [5], [6], [-37], [-20], [7], [30], [-22], [-34], [12], [-40], [0], [10], [35], [36], [-9], [-34], [-9], [-10], [-36], [-26], [-27], [21], [-6], [21], [8], [14], [-23], [-46], [54], [43], [8], [-13], [42], [33], [32], [-28], [-34], [-38], [8], [-18], [-50], [28], [-2], [7], [12], [44], [-58], [-6], [12], [-6], [-4], [49], [-47], [-34], [44], [46], [9], [-2], [-16], [-40], [-56], [-46], [21], [-2], [-31], [4], [62], [-54], [24], [26], [62], [10], [10], [28], [41], [32], [-13], [-30], [7], [25], [-12], [-40], [4], [-1], [-66], [-64], [10], [60], [28], [-35], [28], [31], [-16], [60], [23], [66], [41], [-10], [-54], [7], [26], [-42], [-12], [-45], [32], [63], [-27], [4], [54], [68], [-26], [26], [72], [-7], [-7], [52], [-42], [25], [-70], [24], [-76], [-28], [-38], [80], [-73], [34], [4], [-66], [52], [-13], [16], [-22], [49], [-32], [22], [-26], [8], [-12], [-78], [51], [-67], [-2], [23], [-1], [10], [24], [-30], [-3], [29], [-14], [-68], [-16], [-18], [-18], [20], [38], [87], [-56], [88], [50], [44], [4], [75], [41], [52], [-34], [3], [3], [12], [-9], [-69], [-59], [30], [16], [-10], [16], [5], [-22], [-40], [2], [46], [-7], [-21], [66], [46], [35], [-73], [-54], [37], [58], [-1], [-71], [-53], [-36], [55], [-44], [65], [-52], [-61], [-21], [38], [-35], [24], [1], [-70], [-12], [-78], [-14], [7], [-77], [-59], [-40], [-17], [-26], [89], [0], [-38], [-32], [-50], [-69], [76], [-9], [-63], [-66], [-4], [74], [-21], [-2], [-31], [-74], [6], [-32], [-71], [42], [-70], [18], [-26], [41], [-72], [-69], [-24], [-33], [35], [-35], [-33], [-66], [5], [-30], [22], [2], [-50], [6], [86], [-74], [-76], [44], [-15], [-35], [-21], [-8], [-64], [-58], [81], [-14], [-14], [54], [61], [20], [-22], [60], [-40], [-22], [68], [-45], [-60], [-6], [-19], [-87], [-60], [-60], [46], [20], [58], [-10], [12], [-74], [-28], [-90]];
    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_g();".
// 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_g(: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_g();".
// 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_g( : 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![1, 1]>,<11,R![0, 1]>],Snew);
    return Vf;
end function;