
// Make newform 4730.2.a.l in Magma, downloaded from the LMFDB on 06 May 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_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_4730_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
        poly := [-4, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
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, 0], [0, -1], [1, 0], [-3, 0], [-1, 0], [1, 1], [4, -1], [-1, 0], [-4, -2], [1, 2], [3, -2], [0, -1], [-3, 1], [1, 0], [-2, 0], [-2, 1], [4, 2], [-5, 6], [-3, -1], [10, 1], [-1, -1], [1, 1], [-8, 6], [-4, -3], [10, -2], [0, -2], [-14, -2], [-3, 3], [-8, 2], [1, 3], [-8, 0], [-4, 3], [-3, 7], [-8, 4], [-1, 2], [10, 4], [-4, 3], [-8, 11], [-8, -3], [-11, 1], [-19, 3], [8, -2], [10, -6], [12, -5], [1, 3], [10, 1], [8, -1], [0, 6], [-16, 2], [6, 0], [-2, -7], [-3, 5], [2, -12], [-6, 0], [9, -11], [3, -4], [20, 4], [5, -5], [-16, -4], [-11, -3], [-17, 7], [-10, -4], [-25, -1], [7, -14], [18, -8], [-11, 0], [4, -8], [-22, 3], [14, 0], [-10, -4], [10, 8], [3, -7], [-8, -8], [-4, -10], [-6, 8], [5, 5], [2, -8], [2, -4], [19, -8], [0, -8], [19, 1], [-15, 7], [-16, 4], [11, -3], [-24, -4], [3, 5], [-28, -2], [-10, -1], [8, -11], [19, 7], [0, -1], [20, 8], [-30, 6], [4, -9], [-1, 9], [-16, 0], [-12, 4], [18, -12], [-2, -6], [-6, -11], [-36, 0], [19, 3], [-35, 1], [25, 3], [23, -6], [-15, -11], [16, -3], [-11, 9], [36, 1], [-4, -2], [-8, -5], [9, -3], [16, -8], [-20, 0], [-16, 3], [10, -11], [-41, 4], [19, -9], [22, 7], [4, 1], [-30, 0], [7, 8], [-14, 18], [4, 1], [-36, 2], [-4, -19], [-14, -8], [0, 9], [32, -2], [10, 2], [-13, -7], [15, -2], [-32, 5], [6, 0], [22, 8], [-5, 3], [-16, -1], [-7, 3], [17, -3], [1, -15], [5, 0], [10, 0], [34, -10], [-5, -11], [3, 17], [10, -10], [2, 16], [8, 3], [13, -15], [0, 2], [-2, -4], [-9, 15], [1, 16], [-19, 15], [3, 4], [-10, -7], [-7, -13], [-22, 1], [-18, 12], [6, 1], [-5, -6], [-13, 10], [-22, -7], [4, 16], [34, 6], [-25, 3], [32, -8], [20, 4], [-34, 4], [23, 0], [32, -13], [1, 1], [44, -6], [20, 3], [4, -4], [17, -6], [-17, -7], [7, -11], [4, 3], [41, -4], [-45, -5], [16, 3], [28, -11], [-22, 1], [3, 19], [5, -11], [26, 6], [18, 2], [5, -3], [42, 2], [2, -14], [-34, 18], [0, -18], [-7, -13], [-8, -4], [27, 11], [11, 1], [-37, 11], [-12, 26], [11, -4], [36, -9], [-20, 14], [-60, 5], [12, 0], [-27, 11], [16, -9], [-10, -6], [7, -11], [-18, -16], [34, -6], [21, -12], [40, -4], [18, -26], [-29, -6], [20, -8], [-5, 8], [-33, 16], [4, 10], [-40, 20], [-3, 4], [18, -24], [17, 1], [22, -17], [-38, 10], [0, 4], [-4, -19], [38, 12], [20, 11], [1, -13], [-9, 1], [2, 16], [22, -9], [43, 3], [-38, 4], [-32, 4], [-16, -17], [-8, -13], [-7, -11], [-13, 5], [29, -24], [10, -5], [-12, -8], [-28, 20], [8, -14], [-13, 23], [-31, -3], [-32, 8], [-44, 18], [30, -3], [20, -21], [22, -24], [-15, -21], [-24, 22], [-30, 25], [-8, -20], [-64, -1], [-14, -2], [-1, -5], [18, -28], [-10, 31], [3, 17], [14, -2], [-14, 3], [-15, 25], [-49, 1], [-15, 15], [17, 7], [-30, 19], [22, -6], [26, -8], [42, 12], [8, 9], [3, -15], [18, -24], [22, 13], [8, 2], [25, -21], [14, 16], [-22, -8], [15, -22], [-12, -15], [-29, -11], [0, -16], [13, -12], [36, -28], [22, -5], [-58, 16], [-14, 28], [-48, 23], [30, -8], [-6, -2], [-41, -7], [-30, 21], [3, 10], [-17, 3], [8, 22], [16, 0], [61, 4], [58, -6], [5, -7], [64, -15], [52, 4], [3, 5], [28, 0], [-15, 21], [9, 7], [7, -15], [27, -3], [-4, 33], [2, -18], [-56, 12], [23, 3], [-14, 26], [46, -3], [-34, 7], [-9, -25], [-30, 4], [0, -16], [56, 6], [12, -4], [-62, 11], [26, -20], [29, 13], [-8, 30], [43, -22], [-14, -3], [-33, -9], [17, 15], [-32, 1], [-22, -14], [2, -23], [49, 13], [-50, 2], [-56, 2], [-60, 19], [-8, 11], [-7, -31], [-30, -19], [6, -17], [3, 30], [-82, -4], [71, -9], [25, -42], [-52, -1], [41, -14], [16, -15], [-9, -3], [8, -31], [64, 0], [-82, 3], [-20, 15], [-25, 3], [10, -10], [7, -32], [-4, 29], [26, -9], [-18, 31], [41, -4], [62, 4], [-16, 12], [-82, -4], [38, 2], [24, 4], [21, -24], [-32, -9], [9, -1], [-32, -14], [-30, -25], [-16, 12], [15, -23], [7, 25], [22, -22], [-26, -1], [-37, 19], [4, 18], [-4, 4], [-1, -1], [8, -21], [-42, 18], [25, 24], [26, 22], [38, -36], [28, -40], [14, -21], [14, 26], [-38, 6], [6, -6], [-39, 0], [-9, 1], [-8, -6], [-15, 4], [10, 19], [-13, 17], [-39, 3], [-32, 24], [34, 20], [-39, 29], [-12, -5], [-52, -6], [-31, 0], [22, 25], [32, 14], [18, -30], [16, -24], [-5, 19], [52, 6], [2, 19], [-35, 13], [-24, -7], [-22, 28], [-38, -20], [-34, -10], [6, -26], [34, -14], [89, 5], [-24, -24], [-17, 29], [-8, 20], [24, -38], [-44, 1], [-55, 11], [46, -18], [-1, 17], [-66, 25], [62, -8], [-8, -35], [18, 3], [-56, -4], [32, -8], [10, 8]];
    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_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_4730_2_a_l(:prec:=2)
    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_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_4730_2_a_l( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4730_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-4, 1, 1]>,<7,R![3, 1]>,<13,R![-2, -3, 1]>],Snew);
    return Vf;
end function;