
// Make newform 9360.2.a.v in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_9360_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_9360_2_a_v();".
// 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_9360_2_a_v();".
// 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_9360_a();"
function MakeCharacter_9360_a()
    N := 9360;
    order := 1;
    char_gens := [8191, 2341, 2081, 5617, 5761];
    v := [1, 1, 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_9360_a_Hecke(Kf)
    return MakeCharacter_9360_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], [0], [-1], [2], [4], [-1], [-4], [2], [2], [-8], [-4], [6], [-10], [-4], [0], [-6], [-12], [-2], [8], [0], [0], [8], [-12], [10], [-8], [-20], [-4], [4], [4], [16], [-20], [14], [2], [16], [22], [4], [-10], [-12], [-12], [-14], [2], [-22], [-8], [12], [-10], [-24], [4], [22], [12], [28], [-28], [-8], [-22], [10], [8], [-18], [4], [28], [22], [10], [-28], [6], [0], [-16], [-10], [2], [10], [-26], [16], [-28], [-6], [24], [4], [14], [14], [-20], [-20], [6], [30], [-18], [14], [-16], [16], [-26], [-8], [-16], [18], [-16], [-30], [-2], [12], [24], [-38], [38], [14], [-42], [-38], [-6], [4], [0], [12], [10], [-24], [10], [16], [-4], [12], [-34], [-24], [-42], [12], [-2], [-38], [-26], [-40], [-18], [40], [-18], [38], [30], [-16], [-46], [22], [36], [18], [20], [4], [36], [-8], [6], [-50], [36], [-40], [-38], [46], [10], [-26], [28], [42], [6], [34], [-42], [20], [-44], [-10], [0], [-10], [16], [44], [-36], [-22], [18], [-44], [-6], [44], [-36], [-32], [34], [10], [38], [-4], [36], [-14], [42], [-42], [-12], [-40], [-26], [42], [-46], [16], [-28], [12], [60], [16], [26], [-22], [22], [56], [58], [-22], [30], [42], [62], [-36], [8], [34], [-64], [38], [-24], [10], [-52], [-28], [-42], [-40], [24], [30], [-26], [-42], [6], [42], [-48], [-6], [46], [-20], [22], [68], [16], [38], [-52], [-2], [8], [-8], [-36], [0], [34], [32], [18], [4], [-6], [26], [-40], [-18], [34], [24], [18], [-60], [20], [-48], [-20], [-58], [44], [36], [66], [44], [-60], [-42], [-22], [2], [-12], [-28], [60], [40], [32], [-10], [24], [46], [0], [-30], [30], [-58], [18], [48], [26], [-62], [48], [-10], [4], [42], [32], [-24], [52], [36], [-70], [-66], [32], [-30], [-6], [64], [-10], [-46], [-28], [44], [32], [-38], [-46], [-36], [32], [-18], [42], [-42], [-8], [38], [28], [48], [-68], [-2], [-74], [-24], [-66], [0], [52], [-2], [-48], [18], [12], [0], [-26], [54], [68], [54], [-18], [0], [40], [-4], [52], [36], [2], [-32], [10], [26], [18], [-30], [4], [48], [26], [12], [24], [-72], [78], [16], [56], [-12], [-74], [38], [-34], [-26], [56], [-10], [-30], [72], [62], [48], [-20], [74], [68], [-8], [-6], [-50], [44], [82], [-8], [38], [24], [54], [54], [94], [-48], [-72], [86], [-66], [28], [-50], [-96], [90], [44], [-8], [12], [-36], [48], [-30], [6], [-74], [-80], [52], [-34], [38], [14], [26], [-58], [88], [36], [-24], [-80], [-78], [96], [48], [-52], [34], [78], [-10], [-62], [6], [-82], [-66], [-56], [64], [50], [-56], [22], [-94], [18], [84], [-68], [0], [-10], [-76], [-18], [4], [26], [-32], [72], [30], [-74], [50], [-56], [14], [-14], [4], [-64], [78], [10], [-96], [-24], [-54], [-96], [0], [44], [14], [30], [-64], [-94], [64], [58], [-56], [42], [-60], [-30], [6], [24], [14], [88], [32], [84], [-10], [30], [54], [60], [52], [-44], [-60], [14], [8], [48], [-26], [-24], [-92], [-32], [70], [6], [52], [-8], [60], [-70], [-32], [-80], [-92], [18], [22], [30], [16], [72], [-102], [98], [-52], [80], [-24], [20], [-98], [20], [50], [76], [-32], [-50], [-30], [30], [-40], [-60], [18], [-6], [78], [-42], [18], [84], [-34], [-68], [-28], [0], [-50], [72], [38], [-18], [-2], [-34], [-78], [-20], [-48], [-10], [-80], [112], [98], [-74], [86], [46], [62], [-68], [-38], [0], [2], [-84], [72], [48], [-96], [-66], [-120], [60], [36], [38], [60], [-88], [-26], [98], [82], [78], [-14], [110], [6], [-38], [92], [-48], [-84], [78], [-4], [-78], [-74], [-44], [-70], [-66], [-26], [-76], [96], [-18], [-4], [88], [38], [18], [8], [12], [-54], [-90], [-90], [-44], [62], [-42], [40], [-74], [-76]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_9360_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_9360_2_a_v();".
// 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_9360_2_a_v(:prec:=1)
    chi := MakeCharacter_9360_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(4027) - 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_9360_2_a_v();".
// 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_9360_2_a_v( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_9360_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![-2, 1]>,<11,R![-4, 1]>,<17,R![4, 1]>,<19,R![-2, 1]>,<31,R![4, 1]>],Snew);
    return Vf;
end function;