
// Make newform 2940.2.a.h in Magma, downloaded from the LMFDB on 21 September 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_2940_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_2940_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_2940_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_2940_a();"
function MakeCharacter_2940_a()
    N := 2940;
    order := 1;
    char_gens := [1471, 1961, 1177, 1081];
    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_2940_a_Hecke( : Kf := Kf)
    return MakeCharacter_2940_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]];
    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], [1], [-1], [0], [-2], [-1], [4], [1], [4], [0], [5], [-5], [-2], [-9], [2], [12], [8], [14], [9], [2], [-1], [-3], [18], [4], [-10], [-6], [1], [4], [13], [-14], [-9], [18], [16], [13], [12], [-16], [22], [12], [-2], [16], [-26], [-17], [-10], [-17], [12], [16], [-28], [0], [-18], [-1], [-2], [26], [18], [-4], [-14], [12], [18], [0], [5], [-20], [25], [0], [19], [-26], [-19], [-4], [1], [-7], [24], [-2], [-30], [16], [-1], [-25], [-11], [36], [30], [-31], [12], [-13], [6], [-35], [-30], [-27], [-8], [-4], [22], [1], [16], [41], [-34], [-12], [33], [12], [-5], [-10], [-26], [-36], [-29], [-31], [28], [-2], [30], [-6], [-5], [13], [36], [-6], [-28], [-23], [-9], [2], [34], [11], [0], [-36], [-7], [14], [14], [4], [-35], [11], [-24], [-16], [11], [36], [-26], [-42], [3], [-9], [15], [-6], [31], [-22], [-20], [-31], [14], [16], [-12], [46], [-48], [-50], [16], [6], [-5], [48], [29], [-44], [-8], [30], [-22], [-24], [1], [-22], [9], [-12], [-11], [14], [21], [48], [-18], [-36], [37], [36], [18], [16], [-53], [-11], [-19], [12], [-8], [34], [8], [-7], [1], [-40], [5], [-30], [16], [-3], [43], [-52], [-47], [28], [46], [-20], [22], [19], [65], [-2], [-29], [-46], [-8], [-52], [-48], [36], [-37], [-2], [30], [48], [54], [27], [34], [54], [34], [38], [-48], [66], [-20], [36], [67], [-12], [28], [-12], [-24], [41], [-16], [4], [44], [-14], [17], [9], [30], [-8], [-62], [34], [-8], [54], [23], [-54], [-35], [-44], [-51], [-6], [-11], [22], [35], [-46], [-44], [22], [-18], [65], [-51], [62], [-8], [20], [67], [-42], [40], [-48], [13], [-72], [30], [19], [72], [-36], [53], [3], [66], [34], [61], [-32], [71], [29], [-22], [33], [-66], [54], [20], [-30], [17], [28], [-33], [-75], [-18], [52], [-14], [11], [-29], [-40], [48], [77], [-62], [53], [28], [6], [-67], [-70], [-31], [-78], [-68], [-52], [74], [-24], [75], [74], [-83], [-24], [-22], [-37], [34], [-86], [-52], [20], [59], [-31], [-66], [-45], [0], [-13], [24], [-10], [30], [24], [-48], [-81], [54], [28], [-61], [78], [-19], [3], [18], [49], [-10], [23], [25], [-39], [-36], [30], [-3], [-30], [61], [38], [53], [-54], [-14], [48], [67], [-43], [-89], [28], [40], [31], [-86], [-38], [75], [76], [-14], [38], [-28], [10], [50], [-40], [-45], [70], [6], [-68], [72], [-26], [-54], [-74], [42], [-82], [-51], [22], [28], [15], [70], [4], [-51], [-22], [74], [43], [-31], [-4], [88], [-38], [32], [-93], [-40], [-96], [-64], [-100], [28], [-44], [53], [2], [97], [-78], [25], [-2], [-88], [-37], [-62], [10], [-84], [-50], [-31], [54], [2], [-40], [-32], [-6], [-6], [-19], [-42], [52], [9], [-28], [-18], [-92], [-84], [28], [69], [2], [-6], [-7], [36], [56], [16], [-63], [-12], [-6], [34], [18], [62], [48], [68], [60]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2940_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_2940_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_2940_2_a_h(:prec:=1)
    chi := MakeCharacter_2940_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_2940_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_2940_2_a_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2940_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<11,R![2, 1]>,<13,R![1, 1]>,<17,R![-4, 1]>,<31,R![-5, 1]>],Snew);
    return Vf;
end function;