
// Make newform 9408.2.a.dd 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_dd();".
// 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_dd();".
// 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], [-6], [-5], [2], [-1], [-6], [0], [-3], [-3], [-6], [-5], [-4], [6], [6], [2], [-7], [16], [-3], [11], [-12], [4], [-6], [-2], [-11], [-10], [15], [-16], [-7], [-6], [-12], [-5], [4], [-8], [-10], [-20], [-8], [-22], [0], [-25], [0], [-1], [-18], [20], [-12], [16], [-22], [11], [-8], [2], [10], [14], [-12], [0], [2], [16], [-1], [6], [11], [12], [-11], [-2], [31], [20], [5], [1], [22], [-26], [-4], [-24], [27], [29], [-27], [26], [4], [21], [-2], [13], [26], [-17], [0], [-25], [24], [26], [36], [25], [4], [-5], [-8], [2], [-1], [-8], [-29], [18], [24], [24], [17], [-37], [-28], [6], [-24], [18], [-29], [23], [-40], [-26], [20], [-21], [-5], [46], [-14], [25], [28], [38], [23], [10], [-34], [-6], [-3], [-29], [12], [-18], [-23], [-22], [6], [30], [-41], [-21], [51], [-18], [-7], [26], [50], [31], [22], [-32], [46], [8], [-28], [-4], [-24], [-12], [-5], [12], [33], [24], [-52], [54], [-2], [-42], [-7], [34], [-43], [-26], [43], [-36], [51], [-54], [30], [-6], [-53], [26], [30], [26], [25], [5], [-31], [-12], [-8], [-42], [36], [5], [-23], [52], [45], [36], [0], [-7], [-31], [20], [35], [2], [-46], [-24], [-14], [29], [41], [6], [-19], [52], [52], [38], [-46], [0], [-49], [-54], [-6], [-14], [30], [-5], [-22], [-70], [54], [-26], [-16], [-26], [52], [28], [47], [-16], [-16], [-14], [-50], [23], [8], [-12], [38], [-32], [-5], [29], [-52], [-16], [66], [-42], [-12], [34], [-53], [-24], [-17], [-4], [67], [-38], [51], [-64], [1], [-58], [8], [44], [76], [-65], [9], [-10], [-16], [-10], [-37], [0], [52], [-72], [-29], [50], [-6], [-23], [-4], [44], [-49], [-51], [72], [-2], [-13], [-44], [23], [9], [10], [39], [22], [12], [-80], [-30], [-59], [-32], [-3], [-41], [-62], [-8], [-12], [-51], [-41], [-70], [72], [-81], [74], [-59], [52], [42], [-35], [-28], [17], [-20], [-16], [28], [12], [-28], [-71], [18], [-23], [-50], [-64], [37], [-50], [30], [-40], [-24], [-13], [-51], [36], [-5], [68], [25], [-42], [-12], [46], [36], [-8], [-67], [80], [30], [-43], [-74], [-9], [-37], [2], [-5], [78], [-27], [-35], [79], [8], [-36], [35], [-56], [-55], [-78], [-47], [36], [38], [-68], [-35], [5], [65], [48], [-10], [37], [-60], [-30], [79], [52], [2], [54], [-16], [-6], [-18], [36], [29], [28], [-64], [54], [-30], [6], [78], [48], [-26], [-46], [31], [-26], [-36], [-23], [-6], [18], [27], [-24], [-30], [11], [55], [18], [54], [38], [-16], [-75], [-30], [-96], [52], [-38], [-48], [0], [55], [-14], [73], [36], [-91], [2], [-56], [-27], [90], [-22], [-32], [-86], [-15], [-80], [-34], [74], [16], [42], [-54], [-7], [78], [24], [-41], [44], [-50], [-20], [-16], [-80], [21], [38], [-42], [-35], [20], [-82], [-74], [73], [-24], [52], [-26], [60], [48], [-36], [60], [-52], [45], [30], [13], [-94], [-71], [66], [33], [49], [51], [64], [32], [-16], [5], [-58], [90], [-10], [76], [26], [2], [-25], [-93], [-80], [76], [108], [33], [-22], [-37], [86], [26], [6], [44], [79], [96], [41], [-108], [42], [-1], [88], [-68], [-61], [29], [-20], [96], [-63], [-60], [-30], [-66], [23], [116], [38], [-7], [24], [-43], [-98], [-80], [-36], [-49], [-44], [-52], [52], [-26], [102], [-57], [-10], [-36], [1], [77], [90], [-33], [71], [90]];
    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_dd();".
// 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_dd(: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_dd();".
// 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_dd( : 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![6, 1]>,<13,R![5, 1]>,<17,R![-2, 1]>,<19,R![1, 1]>,<31,R![3, 1]>],Snew);
    return Vf;
end function;