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