
// Make newform 4140.2.a.h in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4140_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_4140_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_4140_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_4140_a();"
function MakeCharacter_4140_a()
    N := 4140;
    order := 1;
    char_gens := [2071, 461, 1657, 3961];
    v := [1, 1, 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_4140_a_Hecke(Kf)
    return MakeCharacter_4140_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 := [[0], [0], [1], [-1], [-6], [6], [-7], [2], [1], [5], [1], [-5], [7], [8], [-8], [-3], [-13], [-8], [-9], [-7], [-2], [-12], [5], [12], [2], [-7], [8], [-19], [-10], [-3], [-4], [12], [-6], [-13], [24], [-4], [-23], [-14], [-8], [-8], [-24], [-16], [0], [24], [10], [6], [-7], [-6], [28], [0], [4], [-9], [-10], [-2], [-16], [11], [15], [-17], [22], [-18], [-15], [-9], [18], [16], [1], [12], [13], [-6], [28], [-25], [24], [-28], [-11], [-18], [20], [3], [-18], [12], [14], [-3], [-18], [-26], [4], [-29], [0], [24], [-11], [13], [-30], [-38], [-11], [-20], [-12], [9], [15], [39], [30], [-22], [-4], [22], [8], [-3], [11], [-18], [-32], [4], [-46], [4], [-16], [43], [32], [38], [15], [-20], [0], [-6], [19], [22], [14], [10], [-30], [44], [27], [20], [-4], [32], [4], [-41], [37], [19], [41], [-16], [6], [29], [3], [-24], [10], [-29], [27], [17], [-23], [-10], [-16], [7], [33], [22], [18], [-6], [-35], [30], [22], [48], [16], [-18], [55], [16], [-20], [-35], [-54], [8], [-12], [6], [-20], [-12], [-13], [-15], [53], [56], [-20], [-16], [-36], [-39], [50], [-37], [-32], [32], [20], [25], [33], [-10], [-26], [32], [-14], [-10], [-53], [26], [58], [25], [9], [-27], [-34], [26], [18], [7], [64], [51], [2], [-2], [-46], [-6], [-40], [36], [2], [38], [-12], [-54], [-4], [-30], [-33], [-60], [-34], [18], [56], [12], [19], [12], [58], [-33], [-53], [-54], [-7], [-2], [-19], [69], [46], [-29], [-14], [-49], [-28], [-11], [46], [-70], [10], [21], [-57], [-12], [20], [-42], [39], [9], [-3], [1], [14], [-66], [-28], [-4], [76], [-38], [54], [-31], [30], [-22], [-37], [58], [-30], [47], [0], [-56], [-42], [22], [-19], [-17], [-25], [5], [-8], [-12], [-12], [6], [-31], [60], [-51], [79], [3], [-38], [-44], [-62], [12], [57], [20], [-72], [-38], [-32], [40], [12], [-10], [-39], [-53], [-2], [56], [-18], [26], [19], [44], [40], [-24], [72], [86], [46], [65], [-18], [-27], [53], [-16], [76], [4], [36], [-58], [-10], [-76], [-68], [-36], [2], [60], [-60], [-57], [56], [-21], [-57], [35], [-70], [20], [-39], [-50], [86], [-11], [20], [-28], [22], [16], [-1], [-41], [-90], [71], [-2], [-60], [40], [-38], [-34], [-54], [-41], [86], [-42], [-18], [8], [-61], [-63], [-55], [-14], [30], [-61], [43], [16], [61], [-55], [32], [-66], [6], [-34], [30], [-6], [-77], [51], [50], [-96], [-2], [-46], [44], [11], [-36], [37], [17], [-62], [-72], [-56], [50], [87], [2], [7], [32], [-26], [74], [-6], [-52], [-18], [-52], [-66], [77], [-76], [-1], [81], [-46], [-58], [69], [-12], [18], [79], [-26], [16], [32], [-37], [-53], [-2], [77], [63], [53], [-21], [13], [-58], [100], [36], [-28], [-10], [12], [88], [-67], [-18], [64], [-8], [-47], [-44], [-6], [-82], [70], [12], [-40], [4], [36], [-51], [76], [-59]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4140_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4140_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_4140_2_a_h(:prec:=1)
    chi := MakeCharacter_4140_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_4140_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_4140_2_a_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4140_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![1, 1]>,<11,R![6, 1]>,<13,R![-6, 1]>,<17,R![7, 1]>],Snew);
    return Vf;
end function;