
// Make newform 4900.2.a.f in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4900_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_4900_2_a_f();".
// 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_4900_2_a_f();".
// 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_4900_a();"
function MakeCharacter_4900_a()
    N := 4900;
    order := 1;
    char_gens := [2451, 1177, 101];
    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_4900_a_Hecke(Kf)
    return MakeCharacter_4900_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 := [[0], [-2], [0], [0], [3], [-4], [0], [-2], [3], [9], [-8], [-5], [6], [-11], [6], [-6], [0], [10], [-5], [15], [-10], [-7], [12], [12], [8], [-18], [-10], [12], [-7], [-15], [7], [-12], [6], [10], [-21], [-1], [2], [4], [-12], [6], [24], [10], [12], [-23], [-15], [22], [-28], [14], [-18], [-14], [-21], [0], [-2], [-12], [-24], [9], [-12], [-20], [-26], [-15], [-22], [0], [-4], [-12], [-22], [-3], [5], [-2], [-3], [-26], [-24], [3], [14], [13], [-7], [0], [9], [-22], [33], [-14], [-12], [-13], [-12], [32], [4], [-12], [-9], [37], [12], [16], [42], [-12], [1], [3], [-4], [6], [-36], [18], [20], [-25], [-17], [45], [-6], [-27], [5], [2], [-42], [12], [9], [-8], [20], [7], [39], [-8], [5], [9], [-40], [6], [-6], [-36], [-38], [-14], [-6], [45], [10], [30], [-10], [18], [8], [-4], [-25], [0], [-52], [43], [48], [34], [48], [-22], [-12], [-21], [-26], [-30], [-17], [-3], [-44], [-54], [-4], [18], [-14], [9], [-50], [48], [-29], [42], [52], [33], [5], [18], [2], [0], [-36], [-3], [-8], [-18], [-45], [48], [-7], [62], [11], [-36], [9], [-2], [33], [46], [-50], [-12], [29], [30], [32], [-20], [49], [0], [55], [42], [51], [18], [-23], [-22], [41], [54], [-34], [27], [-25], [18], [33], [-54], [29], [19], [-54], [48], [18], [-38], [-4], [10], [6], [-42], [70], [-48], [-33], [-26], [43], [60], [25], [0], [-42], [16], [-47], [54], [-57], [-9], [-31], [-68], [-45], [-29], [-72], [-34], [-6], [-15], [-40], [3], [31], [52], [-28], [-45], [56], [36], [-74], [-15], [51], [0], [3], [28], [-4], [-49], [48], [24], [2], [42], [-16], [72], [22], [42], [72], [-44], [-42], [12], [-7], [-28], [-66], [-70], [-44], [-51], [28], [-16], [-18], [-80], [-33], [42], [13], [3], [-68], [52], [-28], [-31], [20], [-52], [-3], [2], [47], [24], [-66], [32], [-12], [28], [-46], [15], [-47], [-27], [-20], [-66], [-9], [60], [63], [18], [-35], [24], [46], [-12], [-6], [2], [-22], [18], [-19], [-51], [-19], [34], [3], [4], [21], [-26], [18], [54], [45], [76], [27], [40], [-66], [0], [-46], [21], [-32], [-74], [30], [7], [-3], [76], [41], [26], [3], [-69], [-46], [15], [58], [-60], [-4], [-78], [-7], [-24], [-44], [-34], [-26], [-21], [-78], [-19], [75], [-33], [-38], [-23], [66], [-42], [-14], [25], [42], [-22], [23], [-66], [36], [6], [18], [-12], [73], [-48], [-24], [-45], [-22], [46], [-42], [-95], [59], [39], [88], [-9], [54], [-20], [-11], [72], [63], [-40], [6], [26], [-78], [-51], [79], [93], [-14], [84], [-73], [-4], [1], [42], [17], [72], [-45], [26], [75], [-41], [76], [-24], [-76], [-39], [4], [9], [-29], [36], [18], [-20], [37], [6], [14], [-96], [68], [-45], [12], [65], [-77], [6], [72], [26], [12], [24], [6], [14], [0], [84], [20], [-66], [-39], [54], [28], [102]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4900_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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