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