
// Make newform 4730.2.a.r in Magma, downloaded from the LMFDB on 25 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_r();".
// 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_r();".
// 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 := [-5, -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], [-2, -1], [-1, 0], [-4, 0], [1, -1], [3, -1], [4, 0], [-2, -2], [-4, 0], [-4, 0], [2, -2], [1, 0], [-2, 1], [1, -3], [-8, 3], [6, -2], [-8, 0], [-14, 1], [-2, -2], [-6, -1], [4, 1], [-6, 6], [-2, 4], [2, -1], [5, -5], [-5, 3], [13, 1], [-3, 3], [8, 0], [-20, 0], [-2, 4], [-4, -4], [-8, -2], [-4, 2], [-6, 4], [1, 1], [6, 2], [6, 0], [4, -4], [-4, 2], [-1, 5], [12, 3], [-20, 2], [11, 5], [-12, 7], [0, -4], [20, -4], [-16, 4], [-6, -2], [-5, 1], [-8, -5], [19, -5], [15, 1], [1, -5], [8, 2], [8, -12], [-1, 11], [-28, 2], [19, 3], [0, -10], [16, -5], [-28, 0], [-13, 9], [-10, 5], [10, 0], [-13, 1], [10, 4], [-6, 12], [-2, -2], [-4, -8], [-13, 9], [-14, 11], [0, 7], [-14, 6], [14, 5], [6, 3], [13, -5], [-27, -1], [-6, 6], [-1, -1], [10, 9], [-26, 0], [-1, 9], [26, -8], [-10, 0], [-6, 6], [-19, -3], [-2, 0], [-1, -1], [0, -4], [-18, -3], [28, -3], [6, -16], [5, 7], [4, 6], [32, -4], [38, 0], [10, -1], [12, -15], [10, -16], [-8, 0], [-10, -6], [1, -7], [4, -3], [4, -13], [-16, 2], [-26, 10], [-12, 17], [21, 3], [0, 6], [-6, -2], [-9, 11], [-38, 1], [8, -16], [-4, -8], [30, -2], [26, -4], [6, -14], [12, -2], [4, -18], [14, -19], [4, -6], [-2, 10], [-1, 3], [-30, -2], [-14, -4], [20, -16], [-11, -7], [-40, -1], [14, 11], [14, -21], [-14, -2], [18, -4], [0, 2], [10, -20], [15, 7], [-22, 17], [-12, -4], [-12, -5], [-14, 15], [-24, 16], [-16, 16], [14, -4], [-28, -8], [24, -10], [8, -13], [-6, 6], [-4, -10], [-36, 6], [-4, -3], [-26, -4], [-7, -13], [-20, 0], [39, 1], [-18, -7], [-8, 14], [6, -6], [-5, -17], [10, 10], [18, 0], [-10, -8], [-5, 7], [-30, 8], [22, -10], [4, 0], [-2, -3], [-14, -4], [10, 13], [1, 13], [-18, -9], [20, 14], [-26, 0], [-41, 9], [44, -9], [24, -12], [-46, -5], [-34, -4], [0, 16], [-18, 18], [40, 4], [26, -8], [24, -20], [12, 4], [18, -7], [-37, 7], [-41, 3], [-8, -12], [20, 12], [36, 8], [36, 0], [4, 5], [19, -1], [34, -2], [24, 1], [-10, 10], [-27, -7], [0, 21], [-61, 1], [18, 16], [-22, 20], [6, -10], [14, -20], [-62, 4], [-8, 0], [10, 3], [8, -16], [8, 17], [17, -15], [20, -24], [-32, 20], [26, 7], [-32, -6], [34, -4], [-2, -8], [-1, 13], [40, -15], [-28, 10], [-17, -1], [-20, 0], [-40, -12], [-2, 10], [-10, 3], [-8, 20], [10, -7], [46, 4], [15, -3], [7, 9], [-13, 15], [12, 0], [44, -6], [-50, -5], [7, -15], [-16, 4], [-34, 20], [-36, 18], [-24, 12], [18, -20], [18, -8], [-3, -3], [13, 1], [30, -1], [-20, -14], [46, -2], [22, 3], [8, -16], [-27, 9], [4, 12], [-14, 16], [-6, -12], [-16, 26], [53, -1], [16, 10], [-14, 11], [28, -11], [21, 5], [68, 1], [-28, 2], [-13, -11], [50, 1], [6, 22], [-26, 0], [18, -4], [24, -3], [14, 20], [-55, -7], [-19, -1], [52, 4], [-10, 8], [-10, 12], [-8, 20], [-24, 3], [30, 4], [4, 5], [16, -16], [-40, 2], [56, 4], [4, -16], [-48, -7], [4, -8], [-28, -14], [-26, 10], [-12, -16], [-2, -6], [-40, 8], [-26, 18], [-35, 17], [29, -13], [-12, 0], [20, 8], [54, -10], [-66, 4], [37, -7], [-18, 0], [-22, -7], [6, 18], [54, 0], [-12, -18], [16, 17], [-44, -1], [-2, -8], [-11, 11], [72, -6], [-42, -8], [-9, 17], [24, 0], [-11, -3], [-31, -17], [-49, -9], [42, -7], [14, 17], [18, -18], [16, -5], [-34, 10], [26, 6], [21, -15], [0, -12], [-32, -6], [-51, 3], [12, 0], [4, 9], [2, -8], [-18, 11], [38, -20], [13, 7], [-20, 0], [-12, -24], [50, 8], [-2, -11], [-16, 2], [-27, -15], [10, 8], [34, -10], [20, -34], [36, -24], [62, 3], [2, 12], [-16, -10], [-24, 32], [-26, -3], [-12, -3], [4, -32], [10, 9], [-78, -5], [40, 4], [-22, -13], [-68, 8], [26, -16], [-50, -11], [6, -8], [-16, -23], [8, 2], [-24, -12], [-14, 11], [-2, -4], [-14, 13], [-56, 1], [-12, 25], [-23, 15], [58, -12], [37, -17], [-8, -8], [-20, 39], [66, 2], [40, -19], [-26, -21], [-52, -12], [5, -29], [-12, 10], [82, -6], [20, -24], [-20, 34], [-56, -12], [60, 6], [58, -18], [-18, -27], [9, 23], [-22, 20], [6, -22], [60, 12], [14, -10], [-12, 2], [2, 14], [46, -8], [34, 21], [-50, -7], [-26, -3], [16, -13], [15, 17], [36, -25], [-20, -26], [14, -12], [-2, 10], [46, -11], [-8, -1], [-54, -5], [24, 4], [-8, 32], [-10, -4], [-45, -5], [-14, -20], [74, -6], [22, 26], [-30, 26], [48, 8], [32, -30], [26, 2], [-49, 15], [45, 9], [-29, -11], [-38, 25], [-64, 0], [-16, 26], [-24, 6], [32, 4], [58, 5], [-7, 27], [10, 10], [-25, 41], [37, -27], [-4, 16], [-30, 30], [-22, -8], [30, -4], [38, 6], [0, 0], [-29, 5], [-68, -11], [-2, -2]];
    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_r();".
// 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_r(: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_r();".
// 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_r( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4730_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-5, -1, 1]>,<7,R![1, 5, 1]>,<13,R![4, 1]>],Snew);
    return Vf;
end function;