
// Make newform 9360.2.a.o in Magma, downloaded from the LMFDB on 20 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_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_9360_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_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], [0], [4], [1], [-2], [4], [8], [2], [8], [6], [6], [4], [-8], [-6], [-12], [-2], [4], [0], [-6], [-16], [-4], [-10], [18], [-6], [8], [12], [-2], [-2], [16], [-12], [6], [20], [10], [0], [-2], [20], [16], [2], [-12], [22], [16], [-14], [-6], [8], [-20], [24], [12], [22], [-26], [-24], [-14], [-4], [-18], [-24], [-14], [8], [-10], [22], [20], [-6], [4], [24], [26], [-30], [-20], [-14], [-4], [14], [14], [16], [16], [22], [-36], [24], [-6], [14], [30], [-38], [20], [-10], [8], [18], [24], [-4], [-18], [26], [18], [-8], [4], [8], [-16], [-20], [-28], [24], [-30], [22], [20], [14], [-36], [18], [4], [38], [-44], [18], [-12], [30], [40], [-38], [-32], [6], [38], [-4], [0], [30], [-28], [24], [-30], [36], [38], [34], [10], [4], [36], [34], [-10], [-48], [24], [30], [4], [0], [0], [-42], [-10], [-30], [-6], [4], [-46], [-42], [-20], [-22], [24], [-12], [-34], [16], [-42], [-10], [4], [-40], [-50], [14], [-36], [24], [4], [32], [40], [-18], [-22], [-46], [12], [-10], [16], [-36], [30], [0], [0], [38], [50], [-6], [-28], [14], [-24], [-38], [32], [6], [-20], [-38], [8], [46], [8], [36], [-26], [6], [24], [10], [30], [-28], [-22], [8], [2], [20], [12], [66], [-12], [-58], [18], [62], [-18], [40], [-14], [48], [-42], [-30], [-28], [18], [-56], [-28], [6], [4], [-14], [42], [-56], [-12], [0], [-22], [48], [-18], [48], [-30], [38], [32], [46], [-24], [68], [-10], [-42], [-48], [-56], [4], [46], [-20], [40], [-42], [20], [40], [-14], [-54], [-4], [-40], [60], [20], [24], [-34], [-34], [-72], [8], [60], [-20], [-64], [-2], [-50], [-16], [74], [34], [-52], [-42], [-20], [42], [-22], [80], [-60], [-42], [-34], [14], [-4], [-78], [-10], [-4], [74], [-18], [20], [-22], [16], [50], [0], [-60], [-18], [74], [-44], [64], [16], [24], [-42], [-20], [16], [-14], [-22], [-64], [62], [18], [-12], [22], [-60], [46], [-30], [64], [-6], [-20], [-76], [42], [-14], [-64], [52], [-28], [50], [-4], [-18], [0], [-26], [48], [-22], [62], [-36], [-48], [42], [60], [-8], [-46], [-18], [-4], [10], [-30], [8], [38], [50], [36], [76], [-48], [42], [-2], [50], [32], [60], [-84], [36], [46], [14], [10], [32], [22], [-42], [-70], [40], [66], [-60], [-90], [-20], [-72], [-6], [-76], [58], [50], [32], [-10], [-10], [-56], [20], [-2], [48], [-26], [54], [-16], [36], [44], [-54], [82], [-32], [-38], [-12], [4], [56], [42], [40], [-34], [-36], [64], [50], [-50], [-38], [-30], [-58], [-16], [-50], [-60], [-80], [-40], [22], [12], [-96], [-30], [-6], [-28], [-52], [32], [-70], [8], [-74], [52], [-86], [46], [46], [-8], [-10], [-70], [-40], [-82], [62], [4], [-36], [-14], [-6], [60], [44], [-22], [50], [24], [56], [30], [-24], [2], [-26], [40], [108], [-70], [18], [4], [-10], [-52], [40], [-70], [-36], [52], [88], [-2], [-34], [-54], [-90], [84], [-16], [-44], [30], [-10], [80], [50], [78], [-12], [-24], [34], [-82], [20], [48], [-84], [38], [-94], [-54], [46], [4], [-10], [-106], [100], [16], [4], [6], [-4], [66], [-72], [20], [-34], [-100], [-88], [-4], [24], [-30], [-20], [-82], [2], [56], [-112], [-54], [74], [38], [66], [-70], [48], [108], [94], [-4], [44], [-88], [30], [-8], [10], [34], [-60], [70], [28], [58], [-88], [-52], [-30], [8], [-58], [-96], [-2], [-82], [-24], [32], [22], [4], [-12], [-80], [-86], [-94], [-60], [-30], [10], [30], [88], [48], [-10], [-20], [46], [40], [-6], [-12], [50], [-38], [52], [18], [48], [86], [40], [28], [-50], [-16], [-58], [6], [50], [52], [48], [-30], [-40], [36], [6], [-36], [72], [4], [88], [-22], [-98], [-84], [-24], [34], [-36], [22], [-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_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_9360_2_a_o(: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_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_9360_2_a_o( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_9360_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![0, 1]>,<11,R![-4, 1]>,<17,R![2, 1]>,<19,R![-4, 1]>,<31,R![-8, 1]>],Snew);
    return Vf;
end function;