
// Make newform 4018.2.a.bb 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_bb();".
// 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_bb();".
// 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, 0], [0, 0], [2, 1], [2, -3], [5, 2], [0, 2], [-4, 0], [1, -2], [7, -1], [0, 1], [-1, 0], [1, 5], [10, 1], [3, -4], [-2, 2], [-3, -4], [6, 2], [-3, -3], [4, 0], [1, 3], [-6, -5], [5, -2], [15, 2], [6, 4], [-1, -7], [3, -7], [0, -10], [-5, 0], [-10, -3], [4, 3], [-4, -4], [10, 6], [15, -4], [13, -5], [-8, 4], [-14, 8], [8, -12], [-7, 2], [0, -3], [0, -7], [1, 3], [-2, -7], [12, -8], [0, -8], [4, 4], [-1, 19], [5, 9], [2, -1], [-20, 3], [12, 6], [-20, 1], [-20, -5], [17, -6], [-8, -12], [-14, -8], [6, 0], [-18, -4], [-26, 0], [-14, 6], [14, 0], [8, -12], [14, 6], [12, 12], [-16, 4], [8, 17], [-3, 4], [0, 4], [0, -14], [-12, -4], [0, -13], [-1, -3], [-16, -3], [-9, 13], [-2, -9], [-2, 0], [10, 1], [-3, -12], [-24, -7], [-16, 3], [5, -22], [12, 0], [8, -12], [-18, -6], [5, -3], [17, 2], [-24, 9], [-15, 12], [0, -4], [-26, 6], [14, -15], [-20, -1], [-11, -19], [14, -6], [-16, -4], [16, -8], [12, 2], [-4, 8], [-18, 0], [2, -14], [19, 14], [2, 24], [25, 2], [0, 32], [-12, -4], [21, 7], [27, 0], [-4, 31], [11, 8], [-11, -13], [14, -17], [-12, 14], [-4, 5], [-2, -15], [-8, -19], [-9, -11], [7, 13], [-3, 16], [24, 16], [-8, 18], [0, -11], [-8, -14], [-4, -4], [-7, 9], [8, 12], [3, -14], [8, 25], [-36, 5], [-21, 2], [3, -11], [0, 24], [26, 4], [-21, -18], [8, 11], [-6, -12], [-8, 11], [20, 4], [15, 16], [0, -3], [-40, 11], [-6, 3], [-13, 27], [-10, 18], [37, 10], [-34, 2], [-21, 0], [6, -24], [-36, -7], [8, -11], [40, 4], [-2, -39], [-10, 18], [8, 32], [49, -3], [-18, -26], [21, 15], [28, 2], [-13, -12], [-8, -24], [4, -26], [12, 6], [-41, 5], [1, -3], [22, 20], [-19, 3], [14, 4], [8, -20], [21, 24], [44, -8], [-26, 15], [1, -26], [-2, -24], [55, 2], [4, 7], [15, -12], [-24, 4], [12, 21], [4, 29], [0, -5], [-46, 7], [-10, -13], [38, 16], [-4, 1], [-22, 9], [11, 14], [-22, -12], [-8, -17], [-8, 20], [-26, 3], [20, 28], [-18, 30], [-4, 14], [-1, -28], [43, 13], [-30, 23], [18, 19], [-16, -10], [21, 0], [38, -13], [12, 7], [-20, 2], [-18, -27], [-12, -4], [7, -35], [40, 16], [-25, -13], [-20, 3], [-30, -8], [-28, -1], [-18, 0], [40, 3], [36, -16], [18, 3], [4, 35], [64, 4], [-51, -7], [-44, -5], [29, -25], [54, -1], [-37, 4], [-3, -41], [-24, -12], [-24, -12], [-4, -8], [-3, -10], [-6, 32], [-14, 39], [12, 33], [12, -10], [52, -4], [15, 15], [2, -42], [-37, 14], [-48, 2], [6, -38], [33, 12], [56, -6], [-37, -15], [42, 7], [12, -21], [3, -13], [-4, 12], [-18, 35], [52, -8], [27, 7], [50, -12], [53, -7], [1, 41], [-30, -15], [16, 7], [10, 8], [4, 19], [-10, 15], [46, 0], [49, -13], [37, -6], [-36, 4], [10, -1], [1, -52], [24, 17], [6, -27], [4, 35], [28, 0], [30, 17], [30, -22], [-3, -10], [-32, 13], [-16, 6], [-12, 36], [22, -17], [-45, -9], [2, -25], [24, 35], [-17, -20], [-36, 14], [28, -20], [32, -2], [16, 47], [4, -46], [-52, -7], [9, 15], [75, 3], [24, 6], [1, 25], [-11, 21], [-20, -8], [2, -28], [-14, -1], [-3, -6], [-1, 44], [48, 4], [22, 45], [-30, 38], [0, -18], [-38, -28], [26, 33], [-16, -32], [7, 23], [-4, 20], [-16, -16], [-44, 4], [16, 43], [20, -27], [15, -11], [29, -16], [38, 24], [10, -8], [-25, 21], [45, -28], [4, 45], [23, 8], [-39, -14], [-41, -29], [36, 12], [18, 28], [14, -14], [-72, 0], [5, -18], [-26, 21], [-30, 2], [-28, -6], [-8, 40], [5, 13], [-35, 4], [-22, 0], [48, -15], [33, -9], [-39, -21], [-44, -1], [-55, 24], [40, 28], [-1, -51], [3, 5], [-9, -35], [44, -24], [77, 10], [30, -15], [21, -28], [-20, 18], [25, -2], [-33, -14], [22, 44], [-3, -45], [-6, 45], [-51, -1], [-24, 28], [-68, 12], [58, 18], [-4, 8], [27, 21], [-25, -46], [-59, -24], [-72, 16], [55, -8], [-12, -10], [57, 25], [-4, -44], [40, -24], [-52, -17], [24, -47], [35, 16], [-16, 0], [29, -11], [-39, -23], [4, 23], [58, 4], [-62, 6], [47, 20], [-38, -2], [-1, -21], [-44, 11], [4, 32], [2, -4], [29, -4], [12, -40], [38, -19], [-2, -12], [60, -18], [-1, 54], [40, 37], [-17, 18], [-46, 37], [-3, 40], [7, -21], [25, -7], [-17, 47], [32, -21], [-25, -35], [58, -10], [-72, -8], [44, 20], [54, 7], [4, -25], [-14, -27], [72, 8], [-84, -8], [-56, 21], [45, 9], [-5, -34], [47, -2], [-42, 8], [16, -44], [28, -26], [-21, -50], [2, -15], [8, 55], [60, 4], [-5, -25], [6, 35], [-10, -32], [10, 21], [32, 39], [-64, -1], [-14, 52], [-55, -10], [-56, 15], [-28, 13], [-9, -18], [3, -27], [-26, 8], [8, -17], [-2, 6], [0, 70], [18, 29], [44, 24], [16, -44], [-30, 41], [-58, 16], [8, -27]];
    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_bb();".
// 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_bb(: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_bb();".
// 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_bb( : 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![1, 1]>,<11,R![2, -4, 1]>],Snew);
    return Vf;
end function;