
// Make newform 9450.2.a.d in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_9450_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_9450_2_a_d();".
// 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_9450_2_a_d();".
// 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_9450_a();"
function MakeCharacter_9450_a()
    N := 9450;
    order := 1;
    char_gens := [9101, 6427, 6751];
    v := [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_9450_a_Hecke(Kf)
    return MakeCharacter_9450_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 := [[-1], [0], [0], [-1], [-5], [1], [7], [-2], [-9], [3], [3], [-2], [6], [13], [-7], [-4], [4], [4], [12], [10], [-14], [-11], [-16], [10], [2], [-7], [-6], [-18], [12], [-9], [12], [1], [2], [16], [15], [17], [7], [-15], [-4], [-8], [-20], [-14], [-10], [0], [18], [-5], [-8], [-26], [-22], [2], [-6], [-4], [-19], [-21], [-13], [0], [-7], [8], [-10], [-14], [-4], [-14], [29], [-2], [2], [20], [-30], [-8], [2], [0], [-3], [-22], [-10], [27], [8], [-9], [-9], [25], [12], [7], [-3], [22], [12], [-26], [24], [-18], [12], [-20], [-30], [18], [-22], [38], [-24], [24], [-16], [27], [11], [-18], [39], [22], [-35], [30], [0], [24], [28], [0], [-40], [-37], [-34], [9], [-2], [-47], [-15], [-6], [-12], [12], [-19], [8], [-10], [-12], [-20], [-36], [-12], [8], [-26], [-45], [-32], [-8], [44], [-54], [-34], [41], [13], [-29], [36], [-9], [-22], [29], [-30], [-36], [2], [-30], [-20], [-30], [-16], [26], [-55], [-2], [-54], [33], [15], [42], [20], [-15], [-7], [12], [-43], [54], [-4], [39], [-12], [-15], [18], [-21], [-21], [-9], [-45], [11], [-3], [-42], [-11], [12], [-18], [-20], [-17], [-4], [-18], [22], [26], [16], [-20], [-44], [35], [-9], [56], [-34], [-19], [-43], [-10], [18], [54], [8], [0], [-3], [-28], [54], [18], [-35], [46], [32], [45], [16], [-34], [-10], [-43], [42], [-25], [14], [36], [-36], [-8], [1], [22], [46], [54], [-17], [-38], [38], [53], [36], [-40], [71], [-50], [-2], [-16], [32], [63], [54], [-2], [3], [50], [52], [-7], [30], [-44], [0], [-57], [-66], [-20], [-66], [18], [8], [10], [14], [46], [-10], [-2], [77], [-46], [-65], [23], [56], [32], [26], [-30], [3], [-60], [51], [24], [-80], [-12], [-34], [24], [13], [21], [-12], [-30], [28], [49], [-34], [-40], [51], [-44], [-41], [-52], [-52], [12], [0], [-7], [69], [-47], [51], [-33], [72], [-43], [-36], [26], [-68], [-21], [-6], [-3], [6], [9], [-32], [14], [-6], [-8], [-8], [-37], [8], [-56], [-40], [35], [14], [16], [38], [24], [70], [-32], [-25], [-25], [-42], [-28], [-43], [75], [67], [-87], [36], [82], [-38], [38], [36], [47], [-16], [-9], [-7], [80], [-52], [59], [34], [60], [30], [5], [66], [22], [-42], [-50], [14], [-53], [38], [35], [23], [30], [47], [84], [1], [10], [28], [-24], [60], [-78], [-82], [-65], [66], [-18], [69], [48], [-39], [13], [-69], [-62], [40], [24], [-19], [-55], [-82], [30], [74], [-5], [65], [-34], [-27], [6], [-76], [59], [9], [-18], [-2], [40], [52], [87], [-3], [96], [-35], [-94], [37], [-16], [6], [45], [73], [-43], [-48], [36], [13], [-66], [-58], [0], [10], [-72], [93], [20], [45], [68], [17], [-70], [92], [-89], [6], [4], [59], [-50], [22], [-10], [28], [-22], [38], [20], [-6], [-54], [-53], [3], [2], [-17], [-1], [-98], [-18], [98], [66], [18], [-36], [103], [27], [-32], [60], [13], [-28], [47], [2], [77], [56], [-92], [8], [-20], [-88], [1], [-54], [-47], [-85], [111], [70], [92], [-78], [66], [-16], [-106], [17], [10], [-72], [41], [39], [4], [112], [11], [36], [-8], [-14], [1], [70], [0], [-32], [-82], [48], [30], [-53], [7], [-1], [-30], [-92], [-72], [-78], [80], [-4], [-54], [22], [92], [-90], [82], [88], [20], [-11], [118], [48], [41], [-2], [105], [-68], [55], [12], [-35], [100], [110], [96], [47], [40], [-21], [-114], [105], [-1], [58], [-19], [-19], [-38], [20], [-30], [-70], [38], [-42], [-108], [-18], [68], [67], [86], [-90], [-52], [-91], [27], [32], [-12], [-64], [-51], [-34], [98], [-46], [-105], [82], [-99], [38], [-84], [-86], [28], [84], [-106], [-31], [98], [72], [0], [70], [2], [-42], [-86], [-86], [93], [88], [-22], [-57], [-94], [-64], [120], [-106], [16], [-27], [22], [-48], [9], [-14], [-32], [15], [17], [-100], [-63], [6], [44], [-47], [-94], [21], [25], [-1], [-90], [67], [75], [30], [56], [76], [-52], [-28], [58], [56], [-54], [-70], [42]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_9450_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_9450_2_a_d();".
// 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_9450_2_a_d(:prec:=1)
    chi := MakeCharacter_9450_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(4297) - 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_9450_2_a_d();".
// 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_9450_2_a_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_9450_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<11,R![5, 1]>,<13,R![-1, 1]>,<17,R![-7, 1]>,<19,R![2, 1]>],Snew);
    return Vf;
end function;