
// Make newform 5390.2.a.z 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_z();".
// 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_z();".
// 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], [1], [0], [-2], [-6], [-9], [4], [-4], [-12], [-10], [12], [6], [-3], [7], [-13], [0], [-14], [17], [-6], [6], [-5], [9], [4], [-6], [-10], [-9], [-13], [-18], [9], [-20], [-6], [-19], [10], [5], [3], [21], [-21], [16], [12], [-4], [9], [10], [-16], [-20], [12], [10], [-12], [3], [10], [-12], [-9], [9], [-6], [-29], [-19], [24], [22], [-6], [-2], [-24], [1], [0], [-7], [32], [18], [-26], [6], [-27], [28], [29], [5], [-36], [12], [-2], [15], [-20], [0], [32], [-15], [-2], [13], [-24], [-30], [-4], [-15], [20], [-12], [21], [2], [-24], [20], [3], [6], [-6], [-2], [5], [8], [18], [-6], [42], [20], [-11], [15], [-42], [24], [28], [16], [2], [15], [-8], [2], [9], [19], [42], [18], [-6], [-26], [-34], [42], [39], [-17], [21], [-10], [-24], [34], [-11], [-16], [0], [-46], [-46], [-18], [4], [54], [-14], [-24], [12], [28], [3], [-16], [-6], [-20], [-42], [-26], [42], [-5], [36], [17], [51], [-19], [15], [20], [48], [-16], [21], [52], [3], [-48], [18], [8], [15], [30], [-30], [20], [10], [-34], [42], [-42], [10], [45], [38], [28], [51], [-28], [12], [16], [46], [-46], [-6], [50], [36], [-36], [24], [53], [-17], [-22], [-27], [10], [-24], [-31], [-42], [-12], [15], [-52], [62], [-36], [63], [-9], [-11], [22], [-26], [-15], [-6], [58], [-60], [12], [-17], [26], [-18], [32], [36], [33], [-41], [-37], [-18], [6], [12], [59], [-53], [63], [-52], [24], [-1], [-39], [42], [1], [-60], [-46], [34], [-13], [-60], [-17], [51], [58], [33], [-69], [-48], [36], [-2], [4], [-28], [-48], [-24], [-38], [21], [50], [-57], [-67], [-60], [54], [-26], [-39], [18], [-70], [4], [12], [-38], [-16], [-66], [-14], [79], [9], [-53], [-18], [15], [-58], [-6], [64], [-16], [-35], [-64], [70], [-44], [3], [17], [8], [-36], [51], [-4], [45], [-47], [-44], [48], [-19], [-30], [-26], [54], [66], [-33], [24], [-84], [-43], [-51], [-62], [-36], [-24], [82], [-14], [21], [-13], [-15], [11], [-73], [33], [-68], [48], [-70], [9], [0], [-18], [68], [-69], [34], [-45], [48], [46], [-36], [-38], [-67], [-45], [-46], [60], [43], [-52], [-8], [-21], [3], [-46], [-78], [37], [48], [2], [27], [-64], [60], [13], [16], [-64], [30], [15], [-4], [-24], [-84], [10], [-31], [0], [60], [40], [38], [30], [-17], [-22], [60], [-12], [54], [30], [30], [47], [-54], [42], [66], [31], [35], [18], [-64], [-64], [-3], [-20], [9], [-30], [-29], [-34], [-21], [0], [-14], [0], [-62], [-48], [30], [-43], [-6], [-14], [78], [-88], [-32], [8], [-12], [5], [-96], [-21], [-41], [90], [-94], [73], [-69], [-61], [45], [31], [-27], [-49], [-99], [-33], [28], [-70], [96], [100], [27], [82], [-18], [-21], [-88], [-4], [6], [3], [64], [45], [-45], [-60], [-11], [18], [66], [-47], [-66], [-27], [24], [73], [-45]];
    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_z();".
// 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_z(: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_z();".
// 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_z( : 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![-1, 1]>,<17,R![0, 1]>,<19,R![2, 1]>,<31,R![-4, 1]>],Snew);
    return Vf;
end function;