
// Make newform 1960.2.a.l in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1960_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_1960_2_a_l();".
// 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_1960_2_a_l();".
// 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_1960_a();"
function MakeCharacter_1960_a()
    N := 1960;
    order := 1;
    char_gens := [1471, 981, 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_1960_a_Hecke(Kf)
    return MakeCharacter_1960_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], [1], [1], [0], [-2], [-4], [0], [-6], [3], [-3], [0], [-12], [7], [-9], [0], [-6], [10], [-5], [11], [-10], [8], [6], [3], [-17], [2], [17], [15], [-1], [-5], [-18], [8], [0], [-4], [-18], [-17], [20], [14], [-12], [-3], [8], [-8], [5], [14], [-22], [-6], [-12], [-14], [4], [4], [2], [-4], [20], [-18], [4], [-24], [21], [-31], [2], [8], [26], [28], [4], [21], [-10], [16], [-12], [4], [10], [-25], [-17], [-18], [-24], [-13], [36], [34], [33], [-14], [2], [-33], [13], [0], [-31], [-22], [-18], [4], [-21], [39], [8], [30], [29], [33], [-36], [32], [-24], [-10], [37], [-21], [18], [-28], [39], [-13], [30], [11], [22], [-18], [-2], [-20], [10], [-12], [-2], [7], [8], [-20], [-26], [-6], [13], [4], [-45], [0], [-46], [7], [-16], [36], [7], [10], [45], [1], [-26], [13], [-14], [-10], [9], [28], [20], [22], [-2], [-4], [37], [-8], [-11], [-32], [-34], [-13], [-31], [14], [-46], [-34], [26], [-56], [-43], [-22], [-41], [20], [-37], [9], [-34], [34], [-27], [-4], [38], [-13], [-28], [13], [-40], [18], [-9], [20], [-14], [-35], [-54], [-24], [-7], [-36], [18], [56], [39], [-6], [-30], [9], [1], [-17], [36], [-6], [-54], [-4], [-41], [-16], [28], [-41], [-30], [36], [-24], [2], [-13], [-28], [-18], [-31], [34], [58], [-8], [9], [-44], [2], [-37], [-36], [6], [46], [-44], [1], [-36], [-2], [-41], [-15], [39], [42], [-18], [-29], [29], [32], [48], [13], [-46], [33], [-24], [-13], [-21], [-16], [18], [-31], [32], [-36], [18], [66], [-15], [37], [-5], [73], [-16], [22], [66], [-60], [-16], [-32], [-15], [24], [38], [-29], [-24], [-16], [-24], [16], [-7], [1], [37], [-24], [54], [-5], [23], [-6], [42], [45], [15], [6], [56], [-72], [-50], [-50], [50], [15], [-4], [-27], [80], [-46], [64], [-22], [-64], [24], [-15], [29], [-58], [-23], [-32], [-56], [1], [75], [12], [-46], [0], [72], [-54], [45], [-43], [12], [-50], [14], [-3], [14], [26], [30], [12], [-76], [26], [86], [79], [0], [76], [-4], [27], [36], [-56], [15], [-42], [-25], [-25], [63], [47], [-16], [2], [-14], [-33], [16], [-76], [19], [59], [56], [-65], [60], [-73], [-88], [10], [-14], [56], [22], [25], [-42], [69], [-38], [62], [-21], [-88], [4], [36], [65], [-54], [78], [54], [-65], [-49], [-40], [0], [-22], [-8], [13], [7], [25], [16], [48], [-18], [-28], [-49], [-50], [-6], [-9], [-20], [87], [-88], [-92], [56], [-3], [34], [40], [21], [-78], [32], [32], [-66], [-50], [-24], [42], [54], [-39], [36], [7], [-64], [68], [59], [2], [52], [-53], [37], [14], [-32], [12], [44], [-58], [26], [70], [-25], [20], [-31], [21], [-86], [-31], [-16], [45], [-70], [4], [-33], [-71], [-28], [26], [22], [12], [-48], [-26], [78], [-42], [48], [-62], [-93], [71], [-24], [13], [-108], [30], [62], [-76], [63], [-40], [78]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1960_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1960_2_a_l();".
// 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_1960_2_a_l(:prec:=1)
    chi := MakeCharacter_1960_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_1960_2_a_l();".
// 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_1960_2_a_l( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1960_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-1, 1]>,<11,R![2, 1]>,<13,R![4, 1]>],Snew);
    return Vf;
end function;