
// Make newform 9408.2.a.br in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_9408_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_9408_2_a_br();".
// 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_9408_2_a_br();".
// 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_9408_a();"
function MakeCharacter_9408_a()
    N := 9408;
    order := 1;
    char_gens := [1471, 6469, 3137, 4609];
    v := [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_9408_a_Hecke(Kf)
    return MakeCharacter_9408_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], [1], [-4], [0], [4], [-4], [0], [-4], [0], [-2], [8], [6], [0], [-4], [-8], [10], [-4], [4], [-4], [8], [-16], [-8], [12], [8], [8], [-4], [-8], [-4], [14], [-14], [-16], [12], [-10], [-12], [10], [-8], [-4], [-12], [8], [4], [-12], [-20], [0], [2], [-6], [8], [28], [-16], [-20], [-4], [-10], [-24], [8], [20], [8], [8], [-28], [-32], [-22], [6], [28], [12], [-20], [32], [24], [-30], [-20], [14], [-12], [12], [-24], [16], [-16], [-22], [-36], [24], [6], [-4], [-18], [-24], [-28], [-6], [40], [-8], [0], [12], [-30], [-22], [-12], [8], [-20], [-24], [-8], [12], [12], [-16], [4], [32], [-4], [2], [20], [18], [4], [6], [-36], [-16], [-28], [-24], [24], [-32], [0], [-26], [22], [-20], [0], [-2], [36], [-24], [46], [-4], [-28], [-34], [-12], [44], [-20], [-34], [38], [-24], [-8], [4], [20], [0], [0], [-10], [16], [40], [-36], [-20], [-12], [42], [44], [10], [16], [-36], [52], [24], [-52], [0], [36], [48], [-18], [-8], [-4], [24], [20], [-8], [-48], [48], [0], [12], [28], [-6], [-8], [-4], [14], [-8], [8], [-28], [-14], [-12], [-36], [12], [24], [54], [8], [8], [-44], [6], [16], [-44], [0], [52], [6], [-24], [-40], [52], [34], [-36], [22], [-40], [-32], [44], [-20], [36], [68], [40], [46], [-34], [40], [0], [-14], [-8], [-28], [16], [-36], [12], [-40], [-36], [10], [68], [-18], [12], [-8], [-36], [64], [40], [40], [0], [-24], [-2], [-58], [0], [-30], [32], [-36], [22], [-56], [-8], [48], [36], [-18], [-44], [-56], [-22], [12], [-32], [56], [-22], [-4], [16], [44], [28], [16], [-14], [72], [-24], [32], [60], [-4], [-32], [-30], [40], [72], [-16], [-36], [-44], [22], [4], [60], [-8], [-16], [44], [20], [-12], [48], [-28], [18], [-72], [4], [38], [12], [20], [64], [-32], [16], [16], [52], [2], [10], [-36], [-16], [-16], [-40], [20], [-20], [-24], [-46], [-54], [56], [-72], [-78], [-4], [6], [-4], [18], [-44], [32], [-28], [36], [52], [-40], [-18], [-56], [-84], [12], [-34], [52], [68], [48], [-70], [-48], [54], [-66], [52], [-8], [-80], [-28], [-64], [80], [46], [20], [38], [12], [40], [-22], [72], [36], [-36], [-24], [-26], [82], [-66], [24], [4], [52], [-68], [-34], [-40], [64], [-72], [-86], [6], [4], [0], [-2], [12], [60], [-52], [48], [-36], [-60], [74], [-14], [-8], [10], [-56], [72], [-52], [82], [-64], [-38], [-24], [-72], [44], [28], [-42], [84], [56], [-70], [-12], [20], [32], [-86], [32], [2], [-4], [40], [-32], [-80], [8], [36], [-54], [96], [98], [-76], [0], [-24], [-60], [4], [-72], [-34], [-20], [-68], [-44], [64], [-26], [72], [0], [4], [54], [-76], [34], [-88], [-64], [84], [-8], [50], [-78], [68], [-60], [64], [-42], [-44], [44], [-42], [-12], [0], [56], [-16], [48], [-2], [-92], [48], [-12], [56], [-20], [-36], [6], [52], [40], [40], [60], [-20], [-32], [4], [48], [86], [22], [-68], [88], [100], [-18], [-26], [80], [-32], [-62], [76], [40], [24], [36], [-44], [0], [-4], [-48], [-14], [-86], [-98], [-76], [-92], [58], [28], [88], [60], [58], [20], [-50], [-40], [-44], [-66], [-28], [-96], [68], [0], [66], [-28], [12], [30], [64], [-16], [10], [24], [-72], [56], [-84], [-8], [20], [-50], [68], [28], [24], [-92], [96], [10], [4], [-12], [-68], [84], [58], [-24], [-12], [-98]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_9408_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_9408_2_a_br();".
// 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_9408_2_a_br(:prec:=1)
    chi := MakeCharacter_9408_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(3581) - 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_9408_2_a_br();".
// 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_9408_2_a_br( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_9408_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![4, 1]>,<11,R![-4, 1]>,<13,R![4, 1]>,<17,R![0, 1]>,<19,R![4, 1]>,<31,R![-8, 1]>],Snew);
    return Vf;
end function;