
// Make newform 3960.2.a.ba in Magma, downloaded from the LMFDB on 27 September 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3960_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_3960_2_a_ba();".
// 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_3960_2_a_ba();".
// 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
        poly := [-6, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_3960_a();"
function MakeCharacter_3960_a()
    N := 3960;
    order := 1;
    char_gens := [991, 1981, 3521, 2377, 2521];
    v := [1, 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_3960_a_Hecke(Kf)
    return MakeCharacter_3960_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, 0], [0, 0], [1, 0], [-2, 1], [-1, 0], [0, 1], [0, 3], [4, 0], [4, 2], [-2, -2], [2, -2], [-10, 0], [-2, -2], [6, -1], [4, 2], [2, -2], [2, -2], [-2, 2], [4, -2], [6, -2], [-8, 1], [8, 0], [10, -1], [6, 4], [2, 6], [14, 0], [-4, 0], [2, -5], [-2, -4], [2, 0], [-2, 5], [4, 6], [18, 0], [8, 6], [-6, 4], [-12, 0], [-2, -6], [-4, -8], [-14, 1], [12, -5], [4, 0], [-8, 0], [0, 4], [-8, -5], [12, -1], [8, 0], [-4, -4], [20, 2], [18, 3], [18, 0], [-4, 3], [4, 6], [-6, -6], [2, -2], [2, -10], [2, -3], [-4, 4], [-12, 8], [-8, 3], [6, -2], [-14, 5], [0, 7], [2, 9], [-18, 2], [6, -4], [-6, 4], [10, 2], [8, -5], [-2, -11], [-14, 2], [6, -10], [0, -6], [0, -6], [-28, -3], [28, 0], [28, 4], [16, 4], [-2, -14], [-6, -8], [-2, 8], [-4, -4], [-24, -4], [-8, -12], [-14, 6], [4, -14], [-12, -8], [6, 8], [12, 7], [26, -4], [12, 2], [12, 2], [-28, -2], [12, -10], [-16, -4], [4, 4], [6, 7], [8, -8], [10, 0], [-10, -13], [-6, 4], [6, 3], [-12, -3], [6, -11], [6, -4], [-28, -2], [22, 6], [12, -2], [8, -15], [0, -4], [-10, -2], [6, 5], [-4, -1], [6, -8], [-6, 10], [-24, -4], [18, 12], [4, 12], [12, 8], [-34, 2], [-8, -12], [2, 4], [8, -7], [24, -5], [8, 6], [4, 12], [-22, -10], [4, 0], [-32, 8], [16, -12], [-20, 9], [16, -6], [-6, -7], [8, -16], [-26, 2], [-6, 8], [10, 10], [-30, 2], [30, 3], [-14, -4], [-30, 6], [-32, 6], [14, -2], [-16, 16], [-34, -3], [4, -8], [6, 14], [-20, 1], [-24, 13], [26, 10], [20, 8], [-4, -9], [32, 8], [20, -4], [-34, 7], [16, 2], [0, 4], [16, 6], [44, 0], [28, -7], [-14, 4], [20, -14], [-4, -3], [-50, -3], [-28, 0], [-14, 4], [-20, -4], [-26, 6], [20, -1], [-6, -14], [-54, -2], [24, 0], [-4, -8], [16, -14], [-20, 9], [30, -10], [-32, 8], [-28, -10], [-20, 4], [-2, -15], [38, -4], [16, 8], [-12, 4], [-54, 2], [-4, -9], [20, -14], [-54, -4], [-4, 17], [-20, 10], [42, -8], [0, -16], [46, 2], [38, -3], [20, 0], [20, -4], [-42, 5], [-14, -6], [18, -2], [-22, 12], [-36, 1], [14, -5], [-14, -6], [-16, -2], [14, 8], [50, -2], [12, -8], [18, 0], [16, 12], [42, 7], [42, -2], [-14, -6], [-20, 7], [-6, -4], [16, -12], [32, -2], [0, -6], [30, -8], [14, -11], [2, 4], [12, -12], [-26, -6], [-10, -18], [-4, 0], [24, -16], [-16, -20], [22, -15], [-42, -4], [-42, 0], [-18, -18], [-6, -1], [-48, 2], [-6, -18], [-16, -22], [44, -10], [38, 2], [-4, -14], [-14, 21], [16, -4], [-48, 11], [6, 18], [-8, 24], [36, -8], [24, 4], [-28, -4], [16, 4], [-20, 11], [28, -2], [-36, -6], [6, -2], [12, -2], [14, -9], [-40, 11], [42, 8], [0, 16], [22, -8], [-4, -3], [24, -20], [-60, 0], [-42, -5], [-6, 8], [4, 5], [6, -15], [-10, -7], [-14, 18], [-16, -17], [-42, 14], [68, 0], [14, 20], [28, -16], [38, -1], [24, -21], [18, 8], [-40, -6], [-22, -14], [-16, 18], [4, -5], [4, 18], [0, 2], [6, -6], [50, 2], [24, 2], [-26, -5], [22, 2], [30, -5], [14, -24], [-22, -11], [-24, -20], [-10, -14], [8, 9], [-26, -2], [26, -4], [36, -4], [48, -8], [4, -3], [-60, -2], [0, -9], [10, -16], [-16, -8], [-30, -2], [-32, -8], [-50, 7], [-4, 7], [76, -1], [-20, -10], [-48, 2], [-34, -18], [22, 24], [44, 2], [-26, 4], [56, 8], [-20, -13], [70, -1], [12, 20], [-6, -16], [24, -10], [6, 19], [-50, -14], [20, 16], [12, 6], [50, -6], [-14, -24], [-28, 0], [50, 2], [22, 6], [-16, -6], [36, 3], [40, 12], [-18, -18], [-4, -8], [30, -17], [36, -3], [-62, -10], [18, -24], [4, 12], [2, 19], [24, 8], [-24, -8], [22, 24], [12, -21], [32, 12], [-22, -9], [42, 2], [-66, -6], [-38, -12], [-74, -6], [34, 10], [-24, 14], [14, 8], [-4, 0], [-36, -8], [-2, 2], [20, 18], [26, 4], [2, 4], [78, 5], [-58, 10], [48, 11], [-18, -6], [-24, 18], [-60, 5], [-12, 32], [16, -19], [42, -16], [36, 2], [-48, 18], [-8, 0], [42, -14], [4, -19], [2, 27], [26, 18], [-6, 14], [14, -2], [2, -3], [6, 12], [36, 6], [-22, -18], [36, 12], [-48, -14], [-28, -21], [70, 12], [-4, -31], [48, -20], [-22, 22], [10, 31], [16, -5], [4, -12], [24, -24], [24, 4], [-38, -6], [-66, -5], [-36, -20], [32, -4], [-22, 26], [78, -6], [16, -10], [26, -18], [-52, -9], [-44, 6], [36, 20], [52, -16], [86, 2], [-10, 0], [-34, 20], [-2, -39], [-6, 10], [-42, 8], [-24, 24], [2, 14], [-26, 4], [72, -2], [34, -14], [84, 3], [48, -9], [-36, -8], [-4, -2], [-24, -9], [-30, 0], [24, 16], [52, 4], [-34, 22], [-30, -11], [-14, -8], [8, 1], [-32, 20], [-44, 10], [22, 8], [-22, -10], [-28, 16], [22, -16], [-20, 0], [-56, -10]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3960_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3960_2_a_ba();".
// 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_3960_2_a_ba(:prec:=2)
    chi := MakeCharacter_3960_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_3960_2_a_ba();".
// 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_3960_2_a_ba( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3960_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![-2, 4, 1]>,<13,R![-6, 0, 1]>,<17,R![-54, 0, 1]>,<19,R![-4, 1]>,<23,R![-8, -8, 1]>,<29,R![-20, 4, 1]>],Snew);
    return Vf;
end function;