
// Make newform 5390.2.a.o in Magma, downloaded from the LMFDB on 25 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_o();".
// 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_o();".
// 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], [1], [1], [0], [1], [-3], [0], [6], [2], [-7], [-8], [0], [-4], [-6], [0], [6], [-1], [-1], [-5], [8], [14], [-13], [-6], [-6], [-7], [-15], [4], [6], [18], [-13], [-19], [-14], [-3], [-16], [-18], [-17], [6], [-11], [19], [9], [15], [12], [4], [8], [11], [-6], [-12], [-12], [-8], [26], [-12], [9], [-10], [4], [13], [-9], [-14], [-1], [-17], [-8], [10], [26], [-26], [12], [-13], [-20], [13], [-4], [-26], [-26], [-6], [31], [8], [-1], [-7], [16], [-32], [26], [-33], [-8], [-8], [-12], [-37], [26], [25], [40], [26], [-40], [-23], [4], [-36], [41], [2], [-36], [-4], [3], [30], [-10], [34], [-13], [-20], [-18], [38], [18], [-28], [31], [17], [30], [32], [4], [-24], [6], [27], [32], [-14], [1], [37], [-6], [22], [-14], [34], [-34], [-6], [-1], [-27], [-5], [-18], [-4], [-22], [-47], [-12], [40], [-50], [-2], [-30], [0], [18], [18], [-24], [-48], [-32], [53], [-4], [42], [-4], [2], [-26], [30], [-31], [16], [-29], [-35], [37], [47], [12], [4], [16], [-21], [-36], [3], [48], [30], [24], [-43], [30], [14], [56], [-6], [2], [42], [38], [-58], [39], [-46], [0], [13], [-56], [12], [56], [14], [26], [-42], [-30], [0], [-32], [24], [-33], [-23], [38], [1], [62], [20], [61], [62], [48], [-3], [24], [30], [52], [-49], [-23], [-15], [46], [30], [-45], [-30], [-62], [8], [-36], [-67], [-50], [50], [0], [-28], [21], [41], [-27], [34], [-38], [-24], [-51], [23], [15], [-44], [32], [65], [-13], [18], [49], [-12], [62], [58], [65], [-52], [-23], [11], [46], [-53], [23], [-8], [60], [-34], [-36], [12], [12], [0], [-74], [31], [-38], [33], [15], [-48], [18], [10], [29], [34], [-26], [20], [44], [34], [-40], [54], [-70], [-61], [27], [57], [6], [41], [58], [-10], [-56], [16], [39], [24], [38], [-4], [-53], [39], [56], [24], [43], [72], [-67], [-15], [52], [64], [49], [70], [34], [-6], [54], [1], [12], [-8], [47], [21], [-54], [-28], [-52], [-62], [-38], [-65], [33], [9], [-65], [-69], [9], [0], [-56], [-26], [-23], [-16], [66], [-76], [5], [-46], [9], [24], [2], [0], [-38], [-87], [27], [54], [-36], [85], [20], [56], [13], [-71], [-34], [66], [-15], [20], [-30], [-3], [-80], [-52], [-29], [56], [-32], [-2], [55], [-28], [72], [-12], [42], [25], [72], [-24], [40], [14], [-14], [31], [22], [60], [84], [2], [-34], [-78], [13], [-14], [-66], [-30], [57], [15], [-14], [-56], [-36], [1], [76], [95], [-74], [15], [-66], [-39], [-56], [-2], [-76], [66], [-36], [-90], [-69], [-74], [38], [14], [100], [-64], [60], [36], [45], [-40], [23], [1], [-30], [54], [-3], [-91], [-57], [75], [-9], [1], [73], [-9], [-73], [-28], [-70], [-72], [-36], [-87], [-38], [-54], [-29], [-40], [-88], [78], [-31], [-24], [87], [99], [96], [-7], [-54], [-30], [19], [-38], [-19], [44], [-77], [15]];
    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_o();".
// 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_o(: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_o();".
// 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_o( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5390_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-1, 1]>,<13,R![3, 1]>,<17,R![0, 1]>,<19,R![-6, 1]>,<31,R![8, 1]>],Snew);
    return Vf;
end function;