
// Make newform 4704.2.a.bf in Magma, downloaded from the LMFDB on 08 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4704_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_4704_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_4704_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_4704_a();"
function MakeCharacter_4704_a()
    N := 4704;
    order := 1;
    char_gens := [1471, 1765, 3137, 4609];
    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_4704_a_Hecke(Kf)
    return MakeCharacter_4704_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], [3], [0], [-1], [-4], [4], [0], [8], [-7], [11], [4], [-4], [-2], [-2], [-11], [7], [10], [10], [6], [-6], [11], [11], [6], [7], [6], [-16], [-7], [10], [12], [17], [3], [-2], [-22], [-6], [11], [-12], [-8], [-22], [-6], [-12], [12], [24], [-19], [6], [-20], [2], [9], [-7], [-8], [12], [0], [-25], [-25], [14], [26], [21], [-1], [32], [-30], [-2], [-7], [-8], [12], [1], [17], [12], [-15], [12], [-34], [-24], [2], [17], [-12], [28], [2], [-6], [-4], [16], [7], [-24], [30], [8], [-2], [15], [-21], [-12], [-17], [-2], [0], [12], [6], [-3], [-37], [10], [32], [-43], [14], [-20], [-30], [-20], [3], [19], [0], [-22], [-17], [-15], [0], [18], [-37], [-27], [-16], [22], [-22], [-43], [-10], [2], [-30], [43], [0], [-42], [45], [15], [21], [20], [33], [-34], [-18], [7], [14], [34], [-50], [-3], [-14], [-12], [-3], [30], [-34], [-25], [40], [-2], [37], [-16], [-21], [24], [20], [-30], [42], [34], [-18], [-32], [-2], [-52], [-8], [32], [58], [8], [18], [-13], [-49], [-40], [2], [-5], [-27], [26], [-48], [23], [2], [-41], [23], [9], [30], [0], [21], [13], [-28], [26], [-9], [-32], [22], [-53], [9], [-38], [58], [-54], [51], [-50], [-42], [-29], [-24], [-7], [-15], [-8], [-38], [-37], [-6], [30], [-22], [-18], [22], [-21], [-40], [-38], [-46], [24], [-23], [40], [9], [-36], [-2], [10], [-25], [-61], [33], [40], [10], [49], [0], [30], [-2], [56], [40], [-18], [39], [5], [64], [-10], [52], [15], [20], [6], [-36], [11], [-34], [-10], [28], [27], [15], [52], [-8], [12], [12], [33], [-10], [-52], [-40], [19], [-60], [-48], [-36], [-2], [46], [50], [-22], [37], [-65], [10], [8], [13], [-3], [-48], [-37], [-14], [-32], [-2], [-10], [-1], [-12], [24], [-81], [0], [22], [-59], [29], [13], [8], [81], [-8], [-2], [-40], [14], [55], [-44], [16], [-58], [-60], [-49], [-49], [5], [60], [10], [-63], [12], [-4], [-44], [-65], [8], [-54], [-28], [-14], [-74], [-9], [-83], [19], [38], [34], [67], [22], [-50], [-30], [-54], [30], [-44], [-40], [44], [86], [55], [60], [29], [64], [-48], [18], [-55], [32], [36], [19], [-2], [10], [38], [11], [-14], [-9], [-24], [16], [62], [28], [32], [-30], [-47], [-23], [2], [12], [41], [-63], [89], [21], [-26], [78], [-42], [-79], [40], [-61], [78], [69], [-34], [66], [-30], [39], [36], [18], [10], [0], [-42], [12], [40], [19], [62], [40], [-23], [97], [-56], [46], [41], [0], [16], [32], [28], [91], [-14], [54], [26], [50], [-8], [52], [10], [-6], [-16], [-4], [-20], [40], [17], [-62], [3], [58], [90], [7], [53], [-30], [64], [-25], [48], [-96], [-12], [38], [-59], [65], [50], [90], [38], [83], [-33], [11], [-67], [38], [-22], [45], [72], [72], [60], [-78], [-37], [-58], [-54], [75], [18], [-82], [-23], [-20], [102], [-12]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4704_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4704_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_4704_2_a_bf(:prec:=1)
    chi := MakeCharacter_4704_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_4704_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_4704_2_a_bf( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4704_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-3, 1]>,<11,R![1, 1]>,<13,R![4, 1]>,<19,R![0, 1]>,<31,R![-11, 1]>],Snew);
    return Vf;
end function;