
// Make newform 4761.2.a.l in Magma, downloaded from the LMFDB on 16 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4761_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_4761_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_4761_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 := [-3, 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_4761_a();"
function MakeCharacter_4761_a()
    N := 4761;
    order := 1;
    char_gens := [2117, 1063];
    v := [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_4761_a_Hecke(Kf)
    return MakeCharacter_4761_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], [0, 0], [-1, 1], [-2, -1], [3, 1], [1, 0], [5, -1], [-2, -2], [0, 0], [1, 5], [-2, 0], [-2, 3], [-6, -2], [-8, -1], [3, -3], [-2, 4], [1, -5], [2, 3], [12, 1], [1, 3], [0, -4], [-4, 0], [6, 0], [-3, -3], [8, -4], [-9, -5], [2, -9], [-2, -8], [4, -2], [-2, 8], [-1, -6], [-12, -4], [-9, -1], [-19, -2], [14, -4], [-9, 8], [4, 2], [-1, 0], [-4, -2], [-9, -7], [11, 3], [-4, -2], [2, 0], [-3, 0], [-2, -6], [-16, 1], [-5, -2], [-13, 4], [3, -1], [-10, -3], [-4, 4], [-19, 3], [14, 3], [18, -4], [-16, 6], [-15, 7], [0, 2], [-13, -6], [23, 0], [2, 14], [16, -7], [-15, -1], [-15, -6], [20, 6], [-8, 7], [-2, 8], [1, 8], [22, -1], [-15, 1], [-11, 0], [6, 0], [4, -4], [-30, 2], [-14, 3], [26, -3], [8, 4], [9, 13], [17, -2], [6, 2], [5, -8], [-3, 1], [-10, -5], [-14, 6], [2, 3], [-11, 12], [-15, 5], [-15, 5], [-8, 3], [-3, 11], [13, 4], [17, -5], [0, -6], [1, 10], [9, 11], [9, -10], [-11, 1], [-4, 8], [-6, 8], [18, -2], [10, -2], [0, 22], [-12, 2], [-12, -2], [-19, -5], [20, -1], [-28, 0], [27, -5], [-1, -7], [-7, 9], [-5, 0], [31, 6], [-24, -11], [8, 12], [6, -11], [-32, 9], [-8, -2], [12, -11], [15, 7], [-20, -4], [-41, -1], [-24, 0], [23, -12], [13, 1], [-39, 3], [5, -2], [-22, -10], [-28, -7], [-21, -3], [24, -4], [-16, 1], [-19, -10], [-30, -6], [26, -2], [16, 18], [21, -5], [10, -7], [-8, -26], [-18, 18], [-3, 11], [-23, -13], [-23, -2], [10, -2], [-16, 0], [12, -16], [10, -12], [-42, -2], [27, -4], [-21, 13], [-23, 4], [42, -4], [2, 32], [-6, -18], [14, -24], [-20, -4], [-10, -13], [-32, 10], [6, 23], [45, 3], [20, 15], [-28, 0], [-17, 9], [-7, 11], [3, -14], [-22, -8], [24, 4], [47, 3], [-36, -6], [39, 0], [12, 5], [-13, -21], [7, 1], [9, 2], [10, -16], [-14, 16], [13, 0], [9, -7], [-5, 14], [27, -15], [34, 5], [-32, 3], [-7, -8], [-20, 2], [6, -18], [-4, 20], [35, 9], [37, -7], [-6, 10], [20, -4], [-43, -10], [35, -5], [5, -22], [9, 17], [-14, 7], [-20, 22], [31, 9], [3, -1], [-10, -19], [10, 23], [6, 32], [-7, -11], [-22, -10], [-34, 0], [31, 18], [6, 31], [-30, 18], [-58, 0], [-20, -19], [0, 6], [33, 11], [-17, 20], [-44, 4], [40, -6], [-30, 18], [-6, 0], [11, 11], [12, 14], [-37, 10], [-10, 30], [-17, -1], [10, -34], [10, 28], [36, 9], [24, 0], [-68, -4], [-30, 18], [8, -4], [15, -15], [-6, -6], [-4, -3], [14, -8], [-17, -10], [-10, -29], [-16, 14], [42, -8], [-14, 6], [15, 31], [-2, 5], [-17, 21], [-3, -15], [-19, -7], [22, 24], [-45, 2], [-3, 6], [45, 14], [20, 24], [9, 23], [-13, -28], [-33, 5], [16, 13], [-21, -17], [0, 2], [29, -13], [-21, -1], [-18, -11], [12, 28], [-23, -11], [-30, -1], [-58, 3], [-14, -8], [17, -6], [38, -1], [15, 15], [-39, -10], [-22, 23], [14, -10], [22, 30], [-33, -3], [5, 31], [-10, 7], [4, -38], [23, -12], [50, 3], [64, 10], [-24, 16], [20, 16], [0, 26], [-21, -19], [-11, -20], [10, 32], [0, 12], [-18, 14], [-14, -5], [7, -25], [-28, 3], [24, 10], [-14, 10], [10, -17], [-6, -24], [-9, -40], [-4, -40], [-27, -29], [31, -29], [6, 10], [15, -5], [27, -24], [-7, -39], [40, 9], [-4, 8], [-35, -1], [2, 8], [-2, 24], [-36, 8], [14, 5], [-4, 18], [32, -26], [5, -2], [8, 20], [42, 15], [-23, -15], [-7, -10], [-18, -28], [-11, -17], [73, -3], [-1, 28], [-61, 13], [-74, -8], [16, 14], [-42, -16], [-4, 7], [-31, 21], [12, -13], [2, 3], [-51, 9], [20, 10], [28, 0], [10, -32], [-26, 27], [69, -8], [-81, 1], [-26, -20], [-9, 26], [-35, -5], [-1, -32], [3, 13], [28, -9], [11, -25], [-18, -27], [47, -3], [-19, -22], [0, 11], [-11, 30], [9, -25], [8, -10], [-4, 3], [3, -23], [-45, 3], [3, 36], [-44, 16], [15, -33], [32, 18], [22, 0], [25, 24], [26, 2], [-30, 33], [24, -39], [5, -29], [67, -7], [67, 7], [45, -3], [40, -26], [-50, -13], [-23, 5], [15, -29], [-60, -12], [31, 30], [44, -20], [65, 5], [-28, 26], [38, 5], [0, -26], [28, -24], [-53, -1], [49, 17], [16, 35], [28, -12], [-30, 8], [-45, 23], [-10, -49], [60, 16], [36, -8], [30, -6], [-27, -11], [37, -14], [11, 1], [68, 15], [-27, -19], [25, 18], [29, -10], [-38, 7], [9, -29], [-2, -39], [-29, -21], [-9, -23], [-31, -28], [43, 27], [72, -3], [2, 7], [0, -52], [-26, -13], [-75, -11], [5, 46], [13, 33], [16, 22], [16, -14], [45, 17], [34, 8], [-20, -32], [-22, 28], [-6, 51], [-90, 6], [-49, 26], [-12, 20], [62, -18], [0, 5], [-56, 14], [44, -24], [60, -8], [-7, -32], [-60, 16], [-16, -6], [-10, 16], [-10, -4], [-76, -12], [-36, 8], [25, 4], [54, 24], [-6, -46], [18, -20], [-48, 22], [54, -12]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4761_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4761_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_4761_2_a_l(:prec:=2)
    chi := MakeCharacter_4761_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_4761_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_4761_2_a_l( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4761_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![-2, 2, 1]>,<5,R![-2, 2, 1]>,<7,R![1, 4, 1]>],Snew);
    return Vf;
end function;