
// Make newform 4730.2.a.i in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4730_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_4730_2_a_i();".
// 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_4730_2_a_i();".
// 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_4730_a();"
function MakeCharacter_4730_a()
    N := 4730;
    order := 1;
    char_gens := [947, 431, 1981];
    v := [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_4730_a_Hecke(Kf)
    return MakeCharacter_4730_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 := [[1], [1], [1], [-3], [1], [-4], [-3], [3], [0], [-1], [-3], [1], [12], [1], [-10], [-9], [0], [-15], [-4], [-11], [-10], [0], [-4], [-5], [-8], [6], [0], [-12], [6], [12], [-12], [3], [-6], [16], [1], [-12], [9], [9], [-9], [6], [-12], [6], [-12], [5], [-12], [13], [19], [0], [10], [10], [-1], [18], [8], [12], [22], [19], [-18], [-2], [10], [32], [14], [12], [10], [-11], [-6], [-19], [12], [3], [-14], [10], [-12], [28], [10], [-8], [-8], [8], [-12], [-18], [-9], [24], [-30], [-20], [-22], [4], [14], [4], [2], [-3], [-5], [14], [-3], [8], [22], [-1], [14], [-16], [-28], [-10], [-2], [-23], [28], [-38], [-24], [-18], [-35], [10], [-9], [-6], [41], [-12], [7], [-4], [-30], [-34], [13], [-27], [-17], [8], [-41], [23], [-14], [-37], [-46], [41], [-32], [23], [4], [29], [2], [22], [-32], [-39], [-37], [10], [-8], [22], [21], [-16], [-42], [28], [-43], [30], [10], [-20], [2], [16], [44], [33], [-6], [26], [-22], [-42], [17], [-8], [-21], [-11], [4], [17], [26], [49], [37], [-35], [-23], [-12], [12], [28], [-16], [-8], [22], [59], [-9], [16], [24], [13], [0], [-41], [-12], [32], [29], [-51], [-2], [41], [-45], [-53], [32], [-42], [10], [60], [42], [60], [38], [-42], [4], [-46], [18], [38], [-32], [14], [18], [-49], [9], [-6], [-53], [-40], [58], [9], [-16], [48], [-20], [-56], [-19], [-48], [-24], [-15], [28], [19], [-65], [34], [-62], [-39], [-38], [-6], [63], [-60], [-6], [-1], [42], [37], [-32], [32], [-58], [51], [-38], [-18], [52], [37], [-27], [24], [-10], [-15], [-49], [52], [-44], [-20], [-34], [46], [12], [36], [-43], [-51], [10], [-24], [-46], [1], [-56], [75], [-56], [-2], [-78], [27], [56], [68], [-37], [-4], [0], [40], [56], [81], [-38], [-26], [44], [-5], [-58], [28], [-31], [52], [68], [22], [20], [65], [-5], [40], [-16], [21], [-50], [-61], [-2], [66], [25], [30], [-20], [-12], [-39], [-27], [62], [-6], [56], [-19], [-48], [-74], [35], [-16], [10], [4], [30], [58], [-36], [-58], [23], [-32], [36], [14], [-8], [-75], [51], [-34], [-2], [-50], [-52], [-30], [-53], [-6], [-42], [-20], [5], [-77], [-20], [-4], [-21], [54], [3], [-38], [-22], [-56], [37], [-65], [4], [-69], [51], [9], [-70], [0], [15], [61], [63], [59], [48], [-35], [-42], [-35], [13], [-56], [2], [23], [-1], [-51], [-25], [25], [14], [64], [-54], [-66], [-16], [81], [11], [38], [0], [-49], [20], [-18], [-38], [-8], [-11], [18], [-86], [22], [84], [-7], [50], [45], [66], [64], [-16], [-67], [-76], [-20], [40], [-51], [-62], [-62], [-31], [-5], [-60], [32], [76], [-58], [-22], [25], [-40], [-27], [-7], [34], [-32], [34], [-90], [-10], [-41], [-18], [19], [-24], [14], [66], [-36], [-60], [-6], [-94], [94], [-66], [16], [-67], [34], [-4], [36], [85], [52], [87], [-5], [-24], [60], [84]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4730_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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