
// Make newform 4014.2.a.a in Magma, downloaded from the LMFDB on 17 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4014_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_4014_2_a_a();".
// 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_4014_2_a_a();".
// 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_4014_a();"
function MakeCharacter_4014_a()
    N := 4014;
    order := 1;
    char_gens := [893, 2233];
    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_4014_a_Hecke(Kf)
    return MakeCharacter_4014_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], [-3], [0], [2], [-2], [-4], [2], [2], [0], [8], [3], [2], [6], [-9], [6], [-14], [10], [-3], [-10], [13], [-15], [7], [-4], [2], [10], [19], [-6], [-19], [6], [-8], [-17], [17], [-14], [2], [3], [-10], [-5], [16], [-23], [-17], [-3], [6], [-22], [0], [4], [0], [-1], [17], [-16], [-10], [-15], [-17], [-19], [18], [12], [-5], [-16], [-8], [-20], [6], [-18], [7], [-12], [16], [-18], [-28], [2], [20], [33], [14], [-12], [-4], [-14], [-14], [-4], [20], [8], [-22], [-18], [-12], [28], [-24], [-1], [-3], [-3], [-30], [0], [28], [-26], [-24], [24], [-34], [42], [16], [-30], [36], [21], [20], [-10], [-44], [13], [26], [-11], [35], [-7], [-34], [-31], [16], [-40], [-3], [-38], [14], [21], [1], [-25], [20], [-8], [26], [9], [42], [-34], [-2], [1], [-5], [-24], [10], [31], [2], [34], [29], [27], [2], [-34], [-21], [-10], [26], [-15], [10], [39], [-20], [30], [-17], [36], [0], [40], [46], [26], [-3], [-12], [-2], [-12], [-15], [27], [-22], [17], [-24], [0], [24], [10], [-12], [-15], [40], [-16], [-31], [-12], [4], [7], [22], [14], [24], [46], [27], [-28], [-36], [-42], [-7], [0], [14], [-38], [37], [-7], [-25], [-47], [21], [18], [59], [6], [65], [-11], [14], [-6], [24], [20], [-1], [-48], [31], [-1], [-33], [-66], [25], [52], [-14], [-24], [44], [4], [20], [-26], [-55], [27], [-30], [-43], [-36], [40], [-60], [10], [-14], [53], [-19], [-14], [-27], [-56], [-21], [64], [57], [-28], [57], [-24], [-44], [60], [14], [50], [56], [50], [-1], [32], [38], [21], [-64], [-6], [-12], [-59], [53], [5], [-54], [64], [-72], [-70], [-14], [10], [-37], [-77], [20], [-76], [-3], [1], [29], [-12], [-38], [-40], [-7], [-25], [6], [-62], [-46], [48], [24], [-9], [-46], [-38], [20], [55], [-28], [12], [12], [11], [-22], [-20], [-47], [-60], [-39], [8], [-12], [28], [-48], [-24], [-79], [15], [25], [38], [31], [-52], [21], [8], [22], [-34], [8], [61], [30], [26], [-18], [28], [-13], [-8], [10], [-8], [-14], [-86], [-13], [-40], [-19], [-56], [10], [-28], [-76], [37], [-78], [-58], [-46], [-41], [62], [-55], [-59], [0], [54], [82], [32], [28], [-25], [33], [-32], [-66], [-30], [94], [40], [83], [-46], [42], [70], [-7], [89], [21], [-45], [-26], [-39], [-57], [82], [-79], [-65], [78], [75], [-12], [39], [-26], [76], [-26], [21], [5], [-3], [88], [28], [58], [-28], [-42], [-6], [76], [51], [94], [-15], [-9], [34], [-20], [63], [24], [12], [-36], [-1], [-44], [6], [29], [1], [-66], [-19], [9], [-59], [-91], [94], [-58], [78], [56], [-74], [54], [18], [-12], [-66], [93], [-52], [-12], [-86], [-78], [59], [27], [48], [-66], [-90], [64], [18], [18], [-68], [-65], [12], [-3], [-88], [32], [-17], [-98], [75], [-60], [45], [-59], [-6], [-87], [25], [-54], [-16], [-78], [-45], [38], [-46], [24]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4014_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4014_2_a_a();".
// 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_4014_2_a_a(:prec:=1)
    chi := MakeCharacter_4014_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_4014_2_a_a();".
// 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_4014_2_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4014_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![3, 1]>,<7,R![0, 1]>,<11,R![-2, 1]>],Snew);
    return Vf;
end function;