
// Make newform 2960.2.a.b in Magma, downloaded from the LMFDB on 23 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_b();".
// 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_b();".
// 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], [-2], [1], [-3], [-3], [0], [5], [2], [6], [1], [1], [1], [-5], [-1], [-4], [-9], [-8], [5], [0], [2], [-4], [-8], [4], [10], [-9], [-18], [8], [-6], [1], [15], [4], [-16], [4], [-19], [-18], [12], [7], [19], [-16], [-13], [-4], [-10], [-1], [-14], [2], [8], [-13], [-5], [-3], [-10], [24], [-21], [-12], [6], [-2], [19], [-22], [18], [-12], [20], [-4], [25], [-26], [3], [6], [-9], [10], [-12], [-20], [-14], [-3], [12], [-7], [10], [16], [20], [-37], [-10], [-14], [-32], [36], [2], [-21], [18], [7], [-34], [-6], [-17], [-5], [4], [-21], [32], [-2], [-36], [6], [36], [24], [1], [-36], [30], [-9], [-12], [21], [-6], [15], [2], [-45], [44], [16], [5], [-10], [-23], [16], [-19], [13], [39], [-29], [4], [10], [-20], [39], [-50], [38], [-39], [19], [34], [-15], [-34], [-40], [37], [-27], [-1], [-30], [-2], [11], [-16], [45], [-18], [46], [14], [48], [12], [-24], [-19], [13], [4], [-38], [-33], [-44], [21], [23], [-33], [-35], [-21], [28], [-56], [40], [5], [-46], [10], [-55], [0], [52], [23], [13], [-39], [19], [60], [-7], [-6], [-10], [5], [7], [-56], [-62], [-54], [-2], [6], [17], [-36], [-38], [-8], [-6], [-33], [45], [58], [37], [11], [-8], [44], [58], [-28], [-6], [48], [30], [6], [2], [-20], [10], [54], [46], [-20], [-49], [-39], [51], [28], [-26], [28], [50], [-13], [-5], [-27], [-52], [58], [-27], [31], [20], [0], [48], [57], [-26], [40], [15], [-48], [-72], [-10], [-48], [-38], [-7], [-14], [11], [8], [-44], [-10], [34], [59], [62], [-44], [48], [-48], [31], [-44], [-64], [-25], [-14], [-21], [-22], [16], [-21], [-40], [-58], [58], [17], [40], [-50], [27], [-32], [46], [-21], [-47], [30], [-8], [46], [23], [66], [-5], [8], [-60], [-44], [-74], [-42], [-75], [10], [29], [-14], [-5], [18], [-50], [9], [4], [64], [-69], [-21], [-44], [20], [18], [11], [23], [40], [6], [47], [-12], [-60], [-27], [79], [-10], [-26], [-34], [42], [-18], [63], [-62], [0], [36], [38], [53], [17], [-26], [-30], [36], [40], [34], [-25], [-56], [32], [-56], [39], [24], [64], [48], [48], [-22], [-9], [11], [-60], [-2], [-37], [1], [68], [69], [18], [-6], [29], [4], [-82], [-60], [-20], [56], [74], [-48], [74], [78], [66], [29], [36], [-64], [2], [2], [-93], [-69], [-29], [-4], [-71], [4], [-44], [-16], [53], [38], [-42], [-84], [40], [-62], [-22], [-28], [-58], [81], [-72], [44], [-62], [-28], [46], [-21], [-20], [23], [7], [60], [-24], [64], [-54], [-24], [45], [10], [-90], [26], [94], [-37], [-4], [16], [21], [-28], [-25], [84], [-10], [61], [-50], [94], [20], [-79], [91], [-48], [0], [-9], [-38], [102], [78], [8], [-8], [69], [24], [57], [36], [5], [-81], [90], [-19], [78], [40], [99], [-30], [46], [-106], [-6], [72], [1], [38], [67], [-16], [-39], [80], [63]];
    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_b();".
// 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_b(: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_b();".
// 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_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2960_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![2, 1]>,<7,R![3, 1]>,<13,R![0, 1]>],Snew);
    return Vf;
end function;