
// Make newform 2960.2.a.a in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2960_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_2960_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_2960_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_2960_a();"
function MakeCharacter_2960_a()
    N := 2960;
    order := 1;
    char_gens := [2591, 741, 1777, 2481];
    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_2960_a_Hecke(Kf)
    return MakeCharacter_2960_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], [-3], [-1], [3], [-5], [2], [4], [4], [-6], [6], [4], [-1], [-9], [-10], [11], [-11], [8], [-8], [8], [-3], [7], [-8], [9], [-16], [12], [-9], [6], [-4], [12], [14], [13], [0], [18], [4], [-1], [0], [3], [10], [-8], [-11], [2], [25], [12], [26], [23], [2], [11], [-5], [16], [-21], [14], [-6], [14], [10], [-16], [23], [-22], [5], [-4], [-4], [-4], [30], [23], [-12], [-22], [14], [22], [7], [-10], [30], [-12], [21], [-8], [1], [-17], [-36], [4], [7], [-10], [-4], [-1], [36], [14], [-23], [-28], [9], [-24], [6], [10], [-14], [-10], [14], [4], [-20], [44], [-12], [21], [-41], [6], [-36], [-16], [-10], [-42], [0], [-33], [28], [36], [43], [45], [10], [12], [27], [9], [-25], [48], [47], [-34], [28], [-8], [49], [8], [19], [-7], [-18], [-28], [20], [36], [19], [-4], [43], [15], [9], [-11], [-2], [-3], [42], [35], [-21], [12], [-46], [-49], [21], [-8], [30], [52], [-12], [10], [-12], [-56], [24], [-54], [42], [16], [37], [-4], [-42], [-14], [18], [13], [-18], [-8], [-51], [2], [24], [28], [-27], [34], [46], [-55], [8], [34], [14], [-12], [-53], [1], [12], [52], [-6], [35], [-46], [-28], [-58], [-52], [-20], [-48], [-15], [-11], [-54], [22], [43], [38], [-44], [-30], [45], [-9], [-51], [64], [-24], [15], [30], [-44], [-7], [-42], [19], [40], [24], [8], [-20], [-36], [19], [-34], [42], [36], [36], [-32], [-5], [-30], [-42], [-18], [-42], [-29], [3], [-17], [34], [49], [28], [10], [-25], [-13], [-6], [59], [-13], [-13], [-51], [33], [-15], [42], [22], [-30], [-10], [16], [-36], [35], [-10], [30], [-34], [70], [72], [-41], [12], [40], [-62], [16], [24], [14], [-57], [37], [36], [-63], [-10], [72], [8], [-66], [50], [-8], [41], [1], [12], [-37], [52], [-48], [-32], [26], [-14], [-62], [-52], [-68], [-20], [43], [-22], [-13], [-6], [-12], [77], [-54], [-69], [-54], [-10], [-50], [-42], [-12], [18], [-27], [-23], [-19], [-57], [-23], [42], [-50], [-49], [-10], [41], [83], [8], [-52], [-38], [54], [-10], [31], [22], [21], [45], [25], [7], [20], [64], [41], [-32], [-19], [-22], [-28], [47], [-30], [44], [45], [36], [12], [60], [-66], [26], [65], [12], [-30], [12], [-24], [-4], [-35], [-65], [-44], [-72], [-43], [67], [66], [-40], [-22], [-2], [-55], [-19], [16], [3], [-40], [19], [12], [-82], [66], [22], [48], [52], [87], [46], [14], [1], [34], [66], [35], [-46], [-50], [34], [50], [-58], [-98], [-48], [-30], [-54], [25], [-12], [-37], [-66], [6], [57], [12], [-28], [76], [-2], [24], [9], [43], [-50], [22], [-42], [-85], [-18], [86], [-70], [-31], [34], [-48], [-31], [-57], [6], [-75], [-90], [72], [78], [-30], [-25], [-30], [-82], [-68], [-45], [59], [-17], [58], [28], [-76], [-66], [64], [-87], [-30], [24], [-60], [62], [16], [5], [-6], [-57], [28], [-31], [68], [14]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2960_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_2960_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_2960_2_a_a(:prec:=1)
    chi := MakeCharacter_2960_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_2960_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_2960_2_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2960_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![3, 1]>,<7,R![-3, 1]>,<13,R![-2, 1]>],Snew);
    return Vf;
end function;