
// Make newform 5390.2.a.a in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5390_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_5390_2_a_a();".
// 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_5390_2_a_a();".
// 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_5390_a();"
function MakeCharacter_5390_a()
    N := 5390;
    order := 1;
    char_gens := [2157, 4511, 981];
    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_5390_a_Hecke(Kf)
    return MakeCharacter_5390_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], [-3], [-1], [0], [-1], [1], [6], [-4], [4], [1], [-8], [8], [12], [12], [6], [-6], [-15], [-3], [-7], [10], [4], [15], [-10], [-6], [5], [3], [-6], [-2], [-6], [-15], [-1], [-8], [7], [-16], [-6], [-5], [12], [15], [7], [1], [7], [-2], [-18], [4], [25], [-4], [8], [12], [-18], [4], [-16], [-11], [16], [0], [-15], [-15], [0], [21], [25], [-12], [16], [-26], [26], [-6], [3], [-24], [-7], [-32], [30], [34], [18], [3], [24], [-3], [13], [14], [-22], [-14], [23], [14], [24], [32], [7], [10], [19], [-28], [26], [34], [15], [34], [36], [15], [-36], [-14], [36], [-9], [42], [-10], [-22], [-17], [18], [6], [28], [-4], [30], [31], [-11], [6], [-12], [-26], [16], [-26], [-3], [-28], [-10], [37], [-47], [22], [-24], [-4], [-2], [-14], [-26], [-3], [-37], [15], [44], [-50], [26], [13], [30], [-20], [-20], [-2], [22], [2], [36], [-28], [34], [-24], [20], [-15], [26], [-4], [6], [-30], [-26], [-12], [-17], [-48], [-3], [-53], [3], [-37], [-20], [30], [-52], [33], [-10], [17], [36], [26], [32], [-45], [18], [42], [-6], [42], [24], [-10], [54], [-36], [31], [22], [-8], [11], [-10], [-6], [-44], [46], [10], [-22], [12], [56], [14], [44], [-55], [41], [18], [-45], [2], [30], [-7], [-50], [-48], [61], [-40], [4], [36], [39], [9], [19], [22], [40], [33], [-68], [20], [60], [6], [27], [36], [18], [-56], [-8], [51], [55], [-33], [-10], [-60], [-6], [-59], [-23], [-17], [32], [6], [37], [27], [14], [57], [-50], [-4], [14], [5], [-30], [1], [-17], [-6], [-31], [-61], [26], [60], [72], [56], [-68], [-12], [-72], [48], [-59], [56], [-49], [-3], [-4], [40], [-62], [33], [-30], [-30], [-56], [-12], [-26], [-8], [36], [-18], [-25], [43], [3], [48], [35], [-8], [-30], [-38], [28], [71], [64], [50], [14], [13], [55], [12], [-72], [51], [54], [33], [7], [-42], [18], [-21], [2], [0], [-36], [-42], [77], [6], [-34], [-55], [-13], [32], [40], [-34], [-60], [-10], [33], [45], [-37], [23], [-27], [-41], [28], [14], [-18], [-27], [16], [-52], [16], [-33], [-10], [51], [-32], [38], [30], [2], [79], [5], [-66], [-46], [43], [-12], [68], [-57], [-17], [62], [48], [-29], [-4], [72], [49], [74], [-36], [49], [84], [12], [38], [-23], [-48], [36], [-24], [-18], [11], [32], [94], [76], [58], [16], [-41], [-10], [-60], [56], [46], [-36], [64], [-33], [0], [-12], [60], [5], [13], [-14], [32], [-34], [37], [-52], [-7], [46], [-67], [-28], [-45], [76], [66], [-98], [40], [34], [-14], [61], [-74], [-28], [-58], [4], [-40], [-32], [-14], [37], [-70], [-57], [21], [-58], [-56], [67], [39], [-33], [47], [9], [-13], [71], [-57], [-39], [-68], [96], [-84], [20], [-33], [-34], [-82], [-35], [-10], [-58], [-74], [-43], [28], [-25], [-21], [-80], [73], [82], [2], [31], [56], [75], [62], [65], [81]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5390_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_5390_2_a_a();".
// 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_5390_2_a_a(:prec:=1)
    chi := MakeCharacter_5390_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_5390_2_a_a();".
// 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_5390_2_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5390_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![3, 1]>,<13,R![-1, 1]>,<17,R![-6, 1]>,<19,R![4, 1]>,<31,R![8, 1]>],Snew);
    return Vf;
end function;