
// Make newform 4704.2.a.h in Magma, downloaded from the LMFDB on 20 April 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_h();".
// 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_h();".
// 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], [0], [0], [-2], [5], [-2], [-3], [-2], [8], [1], [-5], [2], [-7], [8], [-2], [-10], [-2], [11], [-12], [-3], [-17], [16], [12], [-14], [18], [1], [-2], [1], [-12], [-11], [6], [4], [1], [12], [-8], [-6], [-4], [-20], [-6], [-12], [-15], [-12], [23], [-18], [-4], [-20], [0], [-26], [13], [-12], [-30], [-22], [22], [-16], [-8], [-18], [16], [17], [18], [-31], [-4], [23], [30], [-17], [20], [15], [9], [30], [2], [24], [-32], [-17], [-21], [-1], [22], [-24], [-13], [10], [-35], [-18], [9], [-20], [-17], [24], [-6], [-24], [-23], [-20], [-9], [-12], [-18], [-21], [-32], [41], [-14], [-4], [32], [11], [-27], [44], [18], [20], [42], [-23], [7], [24], [30], [36], [-37], [-33], [26], [-14], [-5], [4], [26], [-11], [18], [-14], [42], [3], [-21], [-12], [30], [-5], [-6], [-22], [6], [-13], [-19], [17], [14], [-27], [22], [30], [-9], [18], [40], [-34], [4], [20], [16], [40], [-24], [-27], [28], [39], [24], [-4], [6], [-38], [58], [43], [2], [7], [50], [55], [0], [-13], [2], [34], [-34], [47], [-6], [-34], [6], [13], [-37], [49], [-28], [-24], [-54], [36], [45], [29], [44], [-17], [-24], [-40], [55], [-19], [-12], [13], [22], [-6], [0], [22], [39], [1], [-42], [-43], [24], [-28], [-50], [-14], [48], [-33], [38], [42], [-34], [-6], [31], [22], [2], [-6], [10], [56], [42], [-60], [-28], [7], [-64], [40], [18], [50], [-65], [32], [-60], [42], [4], [-19], [17], [-24], [24], [-2], [-14], [-4], [26], [-57], [-68], [33], [36], [-17], [2], [-23], [-28], [-15], [18], [-16], [8], [24], [-27], [-27], [26], [-64], [-38], [47], [36], [-60], [-24], [-59], [-2], [-14], [-31], [-20], [-52], [1], [7], [28], [30], [-9], [4], [25], [55], [-14], [13], [26], [24], [-24], [-66], [3], [-40], [-11], [19], [58], [64], [-24], [-53], [7], [-62], [-8], [-61], [2], [-29], [52], [-6], [-19], [-16], [-59], [24], [-56], [36], [48], [28], [79], [-50], [-11], [54], [28], [23], [22], [-78], [-16], [32], [-23], [37], [-40], [85], [-4], [39], [90], [72], [22], [4], [-56], [-19], [20], [-6], [5], [46], [-75], [3], [14], [-41], [-66], [37], [-1], [77], [-32], [56], [37], [-12], [69], [14], [-5], [8], [26], [12], [13], [-7], [-1], [0], [-58], [-39], [-64], [-42], [25], [-84], [-30], [26], [8], [2], [18], [-84], [11], [72], [-84], [-42], [-18], [30], [-62], [12], [-18], [30], [53], [94], [-52], [5], [10], [-46], [41], [44], [-82], [63], [49], [-38], [-46], [94], [-8], [21], [-94], [8], [-52], [-2], [-40], [-72], [19], [-10], [-61], [8], [-43], [94], [-48], [-73], [66], [-14], [-56], [42], [-61], [-4], [66], [-66], [-96], [-70], [94], [-83], [2], [0], [-11], [52], [-66], [68], [16], [-56], [5], [42], [-42], [57], [36], [90], [-10], [-73], [-72], [72], [78], [-64], [-76], [-44], [36], [-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_h();".
// 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_h(: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_h();".
// 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_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4704_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![0, 1]>,<11,R![2, 1]>,<13,R![-5, 1]>,<19,R![3, 1]>,<31,R![-1, 1]>],Snew);
    return Vf;
end function;