
// Make newform 9126.2.a.w in Magma, downloaded from the LMFDB on 12 September 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_9126_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_9126_2_a_w();".
// 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_9126_2_a_w();".
// 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_9126_a();"
function MakeCharacter_9126_a()
    N := 9126;
    order := 1;
    char_gens := [677, 3889];
    v := [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_9126_a_Hecke( : Kf := Kf)
    return MakeCharacter_9126_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]];
    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], [-2], [5], [1], [0], [8], [-4], [9], [3], [7], [-7], [-2], [1], [-8], [-7], [11], [4], [-8], [8], [-8], [-4], [-15], [7], [8], [7], [-14], [12], [2], [-6], [0], [10], [-5], [0], [-6], [0], [-4], [4], [-12], [-10], [0], [-4], [1], [-10], [0], [-14], [3], [4], [4], [29], [24], [18], [2], [16], [-18], [11], [7], [16], [10], [17], [-31], [16], [-4], [21], [-21], [18], [-32], [9], [-24], [-30], [13], [-24], [-2], [0], [18], [-30], [10], [-27], [-15], [4], [-2], [2], [8], [27], [-20], [-22], [-6], [12], [18], [17], [14], [-2], [23], [-24], [-18], [-35], [18], [28], [-20], [25], [-13], [12], [28], [24], [28], [-18], [5], [-21], [12], [-43], [32], [23], [18], [10], [-9], [44], [-32], [-3], [-10], [-42], [23], [-2], [-46], [7], [-28], [-26], [42], [-21], [38], [-7], [2], [-6], [38], [12], [-37], [4], [6], [-22], [-17], [-4], [42], [-30], [-28], [-25], [10], [10], [-49], [42], [-41], [14], [11], [-16], [7], [39], [4], [-41], [-52], [59], [-37], [-50], [-53], [12], [23], [32], [-27], [-30], [18], [-2], [50], [-31], [57], [27], [-32], [30], [34], [-12], [-62], [34], [-12], [-44], [32], [-46], [-64], [3], [-12], [-29], [52], [-46], [-40], [-66], [-29], [-33], [-19], [-38], [-44], [0], [-24], [-66], [-42], [23], [42], [-12], [14], [-13], [32], [-10], [37], [14], [15], [11], [22], [-57], [64], [-12], [-20], [-30], [42], [-12], [32], [-2], [-6], [-43], [53], [1], [-6], [-44], [-8], [12], [-44], [39], [-24], [-21], [61], [12], [1], [-12], [-4], [54], [-44], [-39], [37], [7], [24], [-13], [31], [-69], [-51], [60], [0], [-48], [19], [-71], [-58], [77], [-21], [-24], [-18], [46], [30], [-38], [-44], [-66], [-31], [-22], [42], [36], [60], [-63], [-2], [-75], [-74], [72], [-16], [-24], [-65], [3], [3], [-37], [16], [30], [-36], [-23], [28], [59], [-50], [57], [-33], [-52], [-23], [0], [-53], [58], [11], [24], [-58], [75], [22], [-27], [66], [-32], [-58], [-6], [56], [54], [-1], [-56], [24], [86], [36], [14], [-55], [-44], [48], [32], [-12], [7], [60], [-24], [-26], [28], [7], [-18], [45], [40], [-51], [70], [20], [10], [20], [69], [15], [-71], [52], [-55], [56], [25], [25], [-83], [2], [70], [-67], [6], [0], [8], [-54], [74], [-22], [20], [-34], [14], [42], [-40], [-12], [-10], [84], [-54], [24], [45], [24], [24], [-58], [-10], [-48], [-52], [13], [-59], [-90], [64], [-38], [76], [-9], [56], [73], [20], [-10], [-45], [-3], [-10], [6], [70], [66], [74], [44], [23], [60], [-4], [27], [-54], [-18], [16], [-10], [6], [-19], [-20], [-4], [-61], [-83], [44], [-55], [82], [-42], [-38], [48], [-70], [-36], [18], [-3], [18], [64], [92], [-3], [42], [8], [84], [39], [-23], [-70], [78], [-19], [-36], [-14], [58], [-50], [2], [-60], [-92], [74], [63], [-20], [3], [-28], [-92], [-40], [-84], [79], [-42], [76], [-58], [75], [-49], [-44], [-21], [-42], [-8], [-98], [90], [-72], [8], [-11], [-88], [88], [-18], [24], [-17], [8], [-13], [19], [64], [66], [27], [-101], [20]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_9126_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_9126_2_a_w();".
// 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_9126_2_a_w(:prec:=1)
    chi := MakeCharacter_9126_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(3271) - 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_9126_2_a_w();".
// 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_9126_2_a_w( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_9126_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![2, 1]>,<7,R![-5, 1]>,<11,R![-1, 1]>,<17,R![-8, 1]>],Snew);
    return Vf;
end function;