
// Make newform 9408.2.a.cz 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_cz();".
// 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_cz();".
// 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], [2], [0], [0], [2], [-2], [-4], [0], [-6], [0], [-6], [6], [8], [8], [-6], [12], [10], [16], [8], [6], [-8], [12], [14], [6], [2], [16], [-8], [18], [2], [16], [12], [-6], [4], [10], [-16], [-14], [8], [-8], [-14], [24], [10], [-24], [2], [-6], [-16], [0], [-8], [-4], [10], [10], [16], [-10], [20], [6], [-24], [-14], [-8], [2], [-6], [-4], [18], [28], [-32], [-34], [2], [-8], [34], [-16], [2], [-26], [0], [-24], [2], [24], [8], [-14], [18], [34], [14], [20], [-14], [0], [-26], [24], [-8], [-30], [-38], [2], [8], [-20], [8], [16], [8], [-16], [0], [-30], [6], [-4], [-22], [-8], [-30], [-12], [-6], [-40], [46], [-28], [-10], [40], [6], [-8], [10], [-38], [-20], [8], [-46], [20], [-8], [-22], [48], [18], [34], [-6], [-24], [28], [-22], [42], [-24], [16], [42], [24], [48], [-32], [-22], [-26], [38], [-46], [28], [2], [-22], [-52], [-22], [-24], [24], [-22], [-24], [-6], [-42], [20], [24], [-14], [6], [16], [-56], [-32], [-16], [-40], [30], [-42], [42], [48], [42], [-16], [44], [18], [8], [56], [34], [50], [-6], [-40], [18], [24], [26], [64], [14], [48], [-30], [-24], [58], [16], [-28], [26], [-2], [64], [-54], [10], [-4], [26], [24], [-2], [8], [32], [-14], [-8], [-66], [2], [66], [6], [16], [18], [16], [-14], [46], [-36], [-70], [-16], [-64], [10], [4], [2], [-14], [-32], [-20], [-32], [-2], [40], [-66], [-56], [-38], [10], [8], [2], [64], [12], [42], [-18], [32], [-8], [0], [34], [20], [-40], [-38], [-36], [32], [38], [74], [-8], [-16], [32], [-20], [24], [-22], [22], [-40], [-40], [60], [-40], [-72], [10], [70], [-72], [38], [10], [40], [26], [36], [66], [62], [16], [-64], [-14], [42], [-34], [-60], [-14], [30], [-64], [66], [50], [8], [6], [-16], [-34], [-56], [48], [-54], [10], [-52], [48], [-72], [8], [2], [76], [-48], [34], [-70], [-16], [-10], [-46], [28], [42], [-52], [58], [66], [-56], [-22], [20], [52], [14], [-6], [-8], [-48], [40], [-30], [48], [-22], [-32], [-62], [0], [18], [34], [72], [-40], [-26], [20], [-72], [46], [-62], [36], [-22], [58], [8], [-54], [54], [56], [-68], [16], [-78], [-22], [58], [-16], [84], [-16], [12], [-46], [-10], [86], [-64], [-46], [-6], [58], [8], [42], [-48], [26], [8], [-16], [66], [-28], [42], [66], [48], [-46], [14], [8], [60], [18], [0], [18], [-66], [32], [-8], [28], [10], [-78], [-64], [-54], [-72], [-60], [-24], [-6], [-88], [10], [12], [48], [-2], [-50], [78], [-30], [-54], [56], [-62], [-60], [96], [8], [2], [-16], [24], [50], [34], [56], [-12], [-96], [74], [64], [-58], [0], [82], [-86], [2], [-88], [-42], [-54], [80], [42], [-30], [4], [20], [14], [-62], [-32], [32], [-6], [-62], [72], [16], [-50], [80], [-86], [2], [-24], [-44], [14], [90], [-96], [90], [-12], [-88], [78], [48], [-40], [-64], [-22], [30], [42], [-86], [24], [64], [-76], [50], [-22], [40], [-18], [50], [28], [8], [-74], [58], [24], [64], [32], [-42], [-30], [-70], [-46], [-28], [-62], [-102], [-8], [32], [48], [-46], [36], [-78], [-80], [4], [82], [-76], [-32], [8], [-48], [-78], [-48], [114], [42], [8], [-64], [74], [-50], [-66], [-74], [98], [64], [48], [-46], [100], [-4], [-16], [-70], [48], [-6], [34], [24], [26], [68], [-54], [96], [-8], [-6]];
    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_cz();".
// 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_cz(: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_cz();".
// 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_cz( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_9408_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-2, 1]>,<11,R![0, 1]>,<13,R![-2, 1]>,<17,R![2, 1]>,<19,R![4, 1]>,<31,R![0, 1]>],Snew);
    return Vf;
end function;