
// Make newform 5082.2.a.s in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5082_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_5082_2_a_s();".
// 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_5082_2_a_s();".
// 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_5082_a();"
function MakeCharacter_5082_a()
    N := 5082;
    order := 1;
    char_gens := [3389, 4357, 2059];
    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_5082_a_Hecke(Kf)
    return MakeCharacter_5082_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], [1], [0], [2], [4], [-6], [-4], [10], [6], [-6], [12], [8], [2], [6], [-8], [-6], [-4], [0], [12], [0], [-14], [10], [10], [6], [10], [-8], [-18], [-2], [16], [-10], [-10], [14], [-10], [16], [12], [12], [-12], [18], [20], [-8], [-12], [22], [-6], [10], [8], [-2], [14], [24], [26], [8], [28], [8], [-6], [-16], [24], [4], [22], [-30], [-2], [-6], [14], [-10], [6], [-18], [-20], [-22], [-16], [-2], [10], [-8], [-10], [-22], [-4], [-10], [2], [-4], [-34], [-24], [28], [6], [-8], [-22], [-28], [12], [-2], [6], [6], [16], [4], [8], [-28], [-12], [4], [4], [-32], [-30], [22], [34], [-28], [-30], [6], [42], [44], [14], [-32], [4], [-16], [8], [-24], [-22], [2], [12], [-44], [-38], [-16], [-10], [-6], [16], [-32], [10], [-6], [-12], [-40], [-50], [-6], [38], [-34], [-14], [-28], [0], [44], [50], [48], [40], [-28], [-22], [12], [30], [-2], [-10], [44], [12], [56], [14], [18], [0], [8], [12], [-50], [-18], [-28], [-8], [-44], [-12], [-24], [42], [4], [-6], [12], [-50], [56], [-36], [18], [-14], [-48], [-10], [-42], [0], [0], [-52], [-32], [-54], [26], [-18], [20], [-30], [-20], [-10], [32], [-58], [-14], [64], [-24], [24], [18], [-28], [2], [-60], [-26], [8], [-52], [60], [-60], [-6], [58], [2], [-36], [-36], [14], [28], [-52], [44], [-44], [48], [-22], [4], [46], [-60], [18], [-28], [24], [-24], [44], [6], [48], [-56], [-24], [-14], [18], [-20], [-26], [56], [-66], [54], [22], [-72], [36], [-64], [-54], [42], [-16], [22], [28], [4], [-54], [-42], [60], [-70], [-4], [-62], [74], [-74], [48], [-12], [74], [-24], [20], [64], [-30], [20], [-72], [-22], [18], [-24], [42], [46], [48], [20], [32], [20], [58], [30], [-2], [56], [-50], [46], [44], [58], [-60], [28], [-34], [-24], [-12], [62], [52], [-62], [-2], [-30], [-24], [-56], [-72], [6], [-62], [68], [38], [-2], [10], [32], [30], [12], [-30], [42], [14], [54], [18], [-48], [-42], [78], [-56], [78], [24], [-60], [-20], [-2], [-36], [16], [0], [38], [-16], [-78], [18], [68], [48], [52], [-56], [-32], [46], [-46], [18], [26], [-86], [44], [-66], [22], [-4], [36], [48], [54], [-14], [-22], [-68], [22], [-68], [-68], [-38], [64], [-14], [-72], [38], [2], [26], [92], [66], [28], [-32], [-28], [64], [-4], [-50], [22], [-10], [-72], [-34], [72], [-30], [-58], [14], [56], [46], [-32], [-8], [36], [4], [50], [58], [16], [14], [-84], [-52], [0], [10], [-40], [-2], [84], [-8], [8], [-36], [-68], [44], [26], [16], [-58], [70], [-90], [-4], [-40], [8], [66], [26], [68], [-44], [80], [-40], [50], [-40], [54], [12], [70], [-86], [18], [40], [-42], [-62], [12], [70], [6], [12], [44], [-44], [-62], [84], [-20], [-94], [-36], [8], [-26], [-18], [88], [-102], [50], [-32], [-30], [30], [52], [60], [-38], [20], [40]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5082_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_5082_2_a_s();".
// 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_5082_2_a_s(:prec:=1)
    chi := MakeCharacter_5082_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_5082_2_a_s();".
// 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_5082_2_a_s( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5082_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![0, 1]>,<13,R![-2, 1]>,<17,R![-4, 1]>,<19,R![6, 1]>],Snew);
    return Vf;
end function;