
// Make newform 4840.2.a.p in Magma, downloaded from the LMFDB on 30 December 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_4840_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_4840_2_a_p();".
// 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_4840_2_a_p();".
// 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 := [-2, 0, 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_4840_a();"
function MakeCharacter_4840_a()
    N := 4840;
    order := 1;
    char_gens := [3631, 2421, 1937, 4721];
    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_4840_a_Hecke( : Kf := Kf)
    return MakeCharacter_4840_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]];
    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, 0], [1, 1], [1, 0], [1, 1], [0, 0], [2, 0], [0, 4], [2, 2], [-2, 4], [2, 0], [0, -4], [-4, 0], [-1, -6], [5, -1], [9, -1], [-2, -4], [0, -8], [1, 0], [-5, -1], [-4, 0], [4, 0], [-6, -6], [-2, -8], [-3, 4], [-6, -8], [13, -2], [2, 8], [11, 3], [11, 4], [6, 8], [-3, -1], [6, -6], [2, 8], [12, 0], [-17, 0], [0, 4], [12, 4], [9, -1], [5, 1], [2, -4], [2, -14], [-23, -2], [14, 2], [-10, 8], [10, -12], [-10, 2], [-8, 0], [15, -5], [19, 5], [3, 6], [10, -4], [-10, -10], [19, -2], [12, -8], [-4, 12], [2, 16], [-5, -8], [0, -8], [6, 16], [6, -8], [1, -15], [-8, -8], [-14, -8], [10, 2], [-10, -8], [-14, 8], [2, 2], [-2, 0], [-17, 13], [6, -8], [-12, -16], [-6, 2], [1, 3], [-4, -12], [-6, -10], [14, 12], [15, 8], [-24, 0], [-5, 8], [-3, -26], [-26, 6], [-19, 8], [26, -6], [-12, -12], [4, -20], [5, 3], [-15, -18], [-8, 8], [5, -10], [11, 3], [27, 11], [18, -14], [30, -8], [26, -6], [-18, -14], [-9, 13], [3, 6], [23, -12], [-2, -8], [9, -10], [26, 0], [0, 16], [-7, 7], [9, -18], [-2, 18], [0, -24], [7, 21], [-18, -4], [30, -10], [6, 24], [-6, 28], [-2, 12], [-40, 0], [-4, 8], [40, -4], [-26, -16], [13, -9], [-3, -19], [-2, -4], [-26, 2], [41, -4], [0, 12], [-20, 8], [-7, 19], [-6, -6], [6, 24], [-21, -10], [-34, 2], [45, 5], [12, 20], [-42, 6], [31, 1], [2, 2], [-14, -12], [6, 0], [-18, 0], [-12, -12], [-21, 15], [40, -4], [2, 16], [0, -4], [13, -12], [-9, 5], [3, -25], [-1, 24], [22, 14], [-24, -4], [6, -12], [-22, 10], [19, -19], [-36, -8], [11, -6], [-38, 4], [-19, 13], [11, 21], [-30, 2], [-10, 30], [34, 0], [-30, 0], [49, 2], [10, 0], [-4, 20], [2, 28], [-32, 20], [-20, 16], [-13, 5], [50, 6], [-18, -8], [9, 20], [6, -8], [12, -24], [-15, -26], [-10, -2], [-38, 12], [12, 4], [9, 0], [32, -20], [-7, -34], [-34, -20], [10, 32], [13, -1], [28, 0], [-6, 0], [-6, 20], [-1, 7], [-22, -8], [0, -28], [6, -24], [1, -18], [-4, 36], [28, -16], [22, -16], [-24, 12], [30, -16], [-45, 1], [-2, -4], [6, -40], [-10, 12], [-32, -16], [47, 5], [19, 26], [14, -14], [28, 16], [-27, 4], [40, 8], [-46, 16], [6, -38], [9, -13], [17, 24], [20, 28], [-30, -4], [-3, 32], [30, 20], [-38, 8], [-2, 2], [-1, 2], [-27, -1], [-2, -8], [17, -21], [-20, -12], [14, 0], [0, -8], [10, -8], [-13, 33], [35, -23], [-21, 12], [38, -8], [-32, 24], [-54, 4], [-10, -6], [-2, -24], [4, 44], [-20, 8], [15, 2], [-43, 15], [21, -7], [5, -2], [46, -20], [-54, 6], [12, 12], [10, -4], [-10, 22], [-29, -11], [51, -2], [42, 24], [-10, 46], [9, -43], [-6, 6], [2, -14], [-13, -21], [-30, 20], [6, -32], [29, 3], [26, -24], [-8, -44], [24, 12], [9, -42], [3, -9], [12, 4], [-18, -12], [7, -25], [-65, 9], [23, -14], [-42, 0], [36, 8], [-26, 2], [31, 10], [31, 8], [-39, 1], [-4, -4], [-2, -40], [15, 23], [28, 28], [72, -4], [22, 20], [-10, -20], [-30, -24], [43, -22], [-49, 2], [66, 6], [-37, -3], [24, 32], [-11, 9], [25, 10], [42, -16], [2, -6], [-4, 32], [58, -4], [14, 6], [66, -8], [-10, -40], [-30, -24], [56, -12], [-30, 22], [-48, 0], [18, -8], [-4, -56], [16, 12], [14, -30], [-13, -21], [8, 32], [52, -12], [-24, 24], [-27, 17], [-12, 12], [4, -40], [-21, -27], [3, 10], [-24, 4], [-52, 0], [-1, 19], [34, 0], [-1, -28], [-7, -55], [25, 13], [-22, -24], [-16, 4], [58, -2], [34, 36], [21, 34], [-2, -22], [32, 40], [14, 24], [27, 13], [6, 56], [-9, 50], [-54, -10], [5, 3], [33, 15], [26, -32], [-2, -24], [-20, -28], [-26, -22], [37, -15], [-20, 8], [18, 8], [-17, -12], [38, -28], [-69, -14], [22, 0], [-34, -20], [-12, -32], [-30, 8], [60, 0], [-50, 24], [28, 24], [26, -16], [18, -24], [30, 14], [28, -28], [14, 54], [-26, 48], [-3, 12], [23, -3], [-13, -10], [-30, 12], [-52, -12], [72, -4], [34, 28], [22, -20], [36, 24], [7, 44], [1, 35], [-52, 12], [-14, -16], [10, -8], [2, 0], [-17, -9], [-9, 54], [-36, 36], [6, -58], [75, -13], [3, -44], [66, -14], [6, 12], [4, 0], [36, 28], [44, 4], [61, 14], [12, -44], [25, -6], [38, 12], [-30, 20], [48, -32], [60, -12], [-57, 19], [22, -6], [-18, 20], [46, 0], [21, -3], [-62, -8], [6, -12], [4, 8], [11, -13], [-18, -26], [20, -16], [20, 32], [-31, -4], [-10, 6], [-27, 0], [39, -8], [18, 12], [-19, 39], [70, -4], [-29, -40], [-36, -8], [-22, -12], [51, 32], [-22, 44], [-52, 36], [-16, 44], [42, 24], [6, 48], [16, 20], [-46, 40], [-58, -24], [-8, 4], [29, -17], [10, 44], [-26, 4], [-18, -48], [42, -40], [21, -3], [-20, -28], [-6, 28], [-28, -8], [29, -33], [10, -48], [-30, 38], [-4, -16]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4840_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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