
// Make newform 4018.2.a.u in Magma, downloaded from the LMFDB on 23 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_u();".
// 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_u();".
// 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 := [-8, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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], [0, -1], [0, 1], [0, 0], [-2, 1], [3, -1], [-2, 2], [2, 1], [-2, -2], [3, -3], [4, -2], [-2, 0], [1, 0], [-3, -1], [-8, 0], [-10, 0], [-3, 1], [-6, 1], [-8, 0], [-5, -2], [-3, -3], [-8, 1], [-3, 1], [2, 0], [-8, 0], [-10, 2], [4, 2], [-9, 3], [9, -1], [-7, 4], [-6, 0], [12, 0], [-4, 2], [-12, 2], [13, -3], [-11, 3], [-1, 5], [3, 3], [17, -1], [0, 3], [10, 1], [-10, -4], [-17, 1], [8, -2], [4, 1], [1, -1], [2, -3], [-10, 2], [-8, -1], [9, -1], [-10, -4], [-16, -1], [17, -3], [3, -5], [0, 0], [-17, 1], [2, -9], [22, 2], [-6, 7], [-14, 2], [23, -1], [5, -1], [-7, -5], [-7, -1], [0, 0], [-18, -2], [6, -2], [-5, 0], [8, -7], [-16, -2], [35, 0], [22, 4], [10, 0], [-18, 1], [17, 5], [5, -12], [8, -3], [14, 0], [-7, -2], [9, 2], [-17, 5], [2, 0], [4, -4], [14, -1], [-3, 2], [3, 9], [3, 6], [6, 6], [-20, 7], [11, 2], [24, -4], [-11, -4], [-20, 2], [-7, -3], [2, -2], [32, 1], [-7, 11], [8, -2], [-12, 12], [2, 8], [-8, 4], [-5, -9], [14, 1], [1, 13], [2, 7], [-28, 2], [-34, -1], [-6, -6], [20, -4], [-26, 0], [-2, 4], [-24, -1], [33, -4], [23, 7], [14, 0], [2, -12], [6, -11], [12, -6], [-13, 5], [-8, 4], [-26, -1], [-22, 4], [24, -3], [-14, -2], [-4, 1], [-6, 3], [39, 1], [9, -6], [-7, -9], [36, -2], [15, -1], [10, -10], [-13, 16], [-21, 9], [22, 8], [14, -1], [-18, 4], [-20, 0], [48, -2], [-12, 4], [-11, 13], [14, 7], [8, 3], [8, 8], [16, -10], [-41, 6], [6, -7], [-15, -6], [11, -9], [-38, -4], [0, 3], [-9, -8], [6, 6], [-11, -6], [-11, 5], [-50, -2], [33, 0], [-34, 0], [0, 12], [-24, 7], [19, -11], [-25, -5], [29, -4], [-12, 0], [8, 2], [-12, 0], [3, -8], [-1, -9], [-27, -7], [-10, 0], [2, 5], [12, 2], [-12, -5], [-3, -8], [-24, -3], [18, -6], [8, 9], [-10, -9], [-5, 14], [14, 4], [-10, 0], [27, -3], [33, 5], [-19, -11], [-8, -12], [30, -7], [24, -6], [48, -2], [4, -10], [32, -1], [47, -1], [6, 9], [-23, -7], [-36, -7], [-55, 1], [-1, 3], [22, -10], [-58, -2], [-20, -2], [-13, -8], [4, -7], [-6, 4], [-1, 13], [-2, -4], [38, 2], [-9, -13], [-40, 6], [16, 9], [43, -4], [3, 9], [22, -20], [3, 7], [-16, 6], [4, 0], [-1, -2], [29, -14], [-1, 6], [34, 8], [27, -11], [16, -2], [18, -10], [-20, 0], [-16, 8], [-11, 2], [-25, 1], [2, -6], [5, 20], [28, 8], [28, 9], [-27, 15], [12, -19], [18, 2], [-18, -4], [15, 8], [-24, -5], [-37, 14], [8, -6], [-15, 21], [15, 1], [-13, 21], [16, 4], [34, 9], [-41, 8], [32, -10], [-6, 0], [-8, -8], [-6, 16], [-8, 7], [-21, -15], [-16, 10], [64, 1], [-33, -8], [34, 12], [-10, -3], [51, 3], [-9, -17], [53, -9], [6, 14], [-47, 13], [-18, 14], [32, -18], [-34, -1], [3, -17], [15, 13], [-49, 10], [-11, 5], [-7, -3], [21, -4], [27, -7], [-7, 23], [31, -3], [-5, 17], [38, 1], [-10, 6], [8, -8], [-42, 14], [34, -6], [27, 11], [18, -14], [52, -11], [-25, 5], [-44, 1], [-42, -2], [18, -19], [4, 8], [44, -3], [-8, -18], [-18, -15], [4, 4], [-4, -10], [-18, -14], [-48, -10], [22, 6], [-19, 3], [11, -1], [-31, 5], [-24, 1], [32, -9], [-22, 11], [-18, 17], [-7, -2], [-75, 1], [-46, -6], [4, -24], [48, 0], [-30, -8], [-44, 0], [-4, -3], [-35, -14], [-10, 20], [5, -13], [34, -8], [22, -11], [27, 17], [58, -8], [-43, 4], [-12, 4], [-26, 18], [-4, -12], [-80, 2], [3, 13], [-2, 11], [44, -17], [24, -23], [-45, -9], [-2, -2], [-30, 5], [12, -23], [-21, -14], [-60, -3], [39, -13], [36, -22], [-40, 12], [-6, 5], [-24, 2], [80, -4], [-53, 15], [-7, 1], [-18, 16], [44, -12], [65, -9], [-18, 21], [6, -8], [11, 10], [-22, 8], [-39, 5], [-50, -7], [28, 10], [49, -17], [52, -6], [-2, 6], [-69, -1], [-33, 5], [70, -10], [5, 13], [0, -4], [20, 6], [51, 3], [-11, 21], [6, -4], [-54, 17], [8, -28], [12, 11], [-12, 6], [38, 14], [14, -16], [-9, 13], [-54, 4], [-26, 19], [26, -29], [82, 5], [48, -16], [-42, 0], [54, 8], [27, 5], [-39, 15], [-44, -4], [-6, 3], [-4, -8], [40, -16], [2, -4], [67, 10], [-22, -22], [22, 1], [0, -5], [-2, -6], [-29, -8], [-58, 8], [35, -15], [12, 9], [0, -16], [41, 9], [4, 9], [-39, -15], [24, -20], [14, -8], [-13, -12], [-14, -19], [76, -8], [42, -10], [-38, -5], [-14, 30], [-54, 10], [-44, 14], [-50, -8], [5, 19], [-38, 9], [-44, -8], [4, 21], [-28, -18], [-11, 20], [-96, 3], [28, 17], [-2, 5], [-10, 12], [-2, -20], [-60, -6], [9, 0], [44, -12], [48, 12], [20, 2], [-27, -15], [-74, 12], [-4, -9], [-11, 6], [58, 5], [18, -9], [2, 10], [-14, -19], [-53, 10]];
    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_u();".
// 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_u(: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_u();".
// 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_u( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4018_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-8, 1, 1]>,<5,R![-8, -1, 1]>,<11,R![-6, 3, 1]>],Snew);
    return Vf;
end function;