
// Make newform 3920.2.a.bf in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3920_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_3920_2_a_bf();".
// 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_3920_2_a_bf();".
// 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_3920_a();"
function MakeCharacter_3920_a()
    N := 3920;
    order := 1;
    char_gens := [1471, 981, 3137, 3041];
    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_3920_a_Hecke(Kf)
    return MakeCharacter_3920_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], [0], [1], [-3], [-2], [5], [-7], [-6], [-4], [-5], [-5], [-6], [9], [11], [-8], [-12], [4], [4], [12], [-14], [4], [6], [6], [12], [-20], [-12], [-4], [20], [-17], [-7], [-12], [-4], [10], [10], [5], [-4], [-5], [19], [-9], [2], [-12], [-20], [-27], [4], [13], [16], [8], [-28], [2], [-6], [23], [-29], [12], [-8], [-12], [8], [2], [-3], [22], [21], [6], [4], [16], [2], [27], [-26], [-8], [14], [20], [-30], [-19], [14], [21], [21], [-16], [18], [13], [-6], [5], [30], [16], [24], [10], [32], [13], [-22], [14], [-9], [10], [16], [8], [20], [-28], [-16], [-18], [-15], [-28], [-40], [20], [21], [-6], [-11], [-28], [-4], [-12], [-12], [-34], [-14], [-33], [-41], [42], [-35], [-40], [35], [34], [-23], [45], [16], [-36], [-42], [21], [36], [-8], [42], [-18], [36], [-41], [31], [41], [3], [-18], [-6], [-3], [-41], [15], [18], [2], [55], [5], [30], [-4], [14], [38], [14], [23], [-42], [-40], [13], [9], [-7], [-8], [0], [-22], [-42], [18], [-31], [-38], [24], [-14], [-20], [8], [-7], [18], [3], [-44], [46], [7], [-41], [-36], [16], [-20], [14], [12], [-6], [-27], [42], [35], [0], [-7], [25], [27], [-6], [-27], [22], [-11], [14], [49], [0], [-56], [-6], [51], [32], [-30], [-6], [-39], [22], [44], [-8], [-24], [22], [-2], [25], [-40], [21], [24], [4], [69], [-25], [24], [0], [-68], [-22], [-70], [-37], [11], [-19], [31], [33], [-16], [-20], [-33], [-25], [40], [4], [-6], [58], [-43], [-60], [41], [9], [6], [-31], [2], [12], [66], [-33], [-29], [-42], [-54], [11], [56], [-26], [-50], [6], [-41], [-39], [-44], [37], [-2], [-45], [-27], [-33], [-14], [3], [-22], [28], [3], [-38], [-61], [64], [58], [-14], [-78], [-1], [0], [-13], [6], [29], [20], [34], [-10], [-54], [-26], [33], [12], [-66], [-59], [67], [-21], [16], [-79], [-36], [-62], [-30], [12], [9], [62], [-65], [50], [62], [42], [-75], [11], [-60], [-14], [54], [-41], [-44], [-40], [-63], [20], [-36], [-52], [-40], [70], [2], [36], [9], [5], [-80], [-70], [-22], [-57], [45], [-47], [-6], [-52], [50], [28], [2], [-72], [85], [-24], [-25], [-83], [20], [48], [-46], [60], [14], [10], [-36], [-33], [52], [16], [36], [-27], [-41], [26], [42], [6], [-26], [46], [-76], [40], [-44], [-18], [-7], [-69], [-52], [0], [-19], [-32], [42], [-44], [-84], [-12], [16], [-27], [54], [64], [61], [-50], [-66], [3], [5], [59], [17], [-85], [16], [30], [42], [34], [15], [-30], [76], [-62], [-22], [56], [18], [-43], [-76], [-65], [-21], [-68], [-17], [54], [72], [-10], [-42], [20], [-48], [90], [14], [34], [-101], [-71], [-54], [40], [44], [-17], [102], [0], [8], [63], [82], [-90], [-75], [-58], [18], [-70], [67], [20], [0], [6], [67], [-30], [63], [10], [-15], [-48], [60], [88], [61], [-52], [55], [91], [12]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3920_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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